Skip to content

Commit 956f4fa

Browse files
committed
環境変数TOKENを設定したときに正しいクエリでアクセスすると定期処理ができるテスト
1 parent 248b2e9 commit 956f4fa

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

app/controllers/scheduler_controller.rb

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class SchedulerController < ApplicationController
88
protected
99

1010
def require_token
11-
return if Rails.env.test?
1211
return if ENV['TOKEN'].present? && ENV['TOKEN'] == params[:token]
1312

1413
head :unauthorized

test/application_system_test_case.rb

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
require 'supports/report_helper'
99
require 'supports/comment_helper'
1010
require 'supports/tag_helper'
11+
require 'supports/query_helper'
12+
require 'supports/mock_env_helper'
1113

1214
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
1315
include LoginHelper
@@ -17,6 +19,8 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
1719
include ReportHelper
1820
include CommentHelper
1921
include TagHelper
22+
include QueryHelper
23+
include MockEnvHelper
2024

2125
if ENV['HEADED']
2226
driven_by :selenium, using: :chrome

test/supports/mock_env_helper.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module MockEnvHelper
4+
def mock_env(partial_env_hash)
5+
old = ENV.to_hash
6+
ENV.update partial_env_hash
7+
begin
8+
yield
9+
ensure
10+
ENV.replace old
11+
end
12+
end
13+
end

test/supports/query_helper.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module QueryHelper
4+
def visit_with_query(url, query_hash)
5+
uri = URI.parse(url)
6+
uri.query = query_hash.to_query
7+
visit uri.to_s
8+
end
9+
end

test/system/auto_retire_test.rb

+24-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class AutoRetireTest < ApplicationSystemTestCase
1616
user = users(:kyuukai)
1717
travel_to Time.zone.local(2020, 7, 2, 0, 0, 0) do
1818
VCR.use_cassette 'subscription/update' do
19-
visit scheduler_daily_auto_retire_path
19+
mock_env({ 'TOKEN' => 'token' }) do
20+
visit_with_query scheduler_daily_auto_retire_path, { token: 'token' }
21+
end
2022
end
2123
assert_equal Date.current, user.reload.retired_on
2224
end
@@ -49,7 +51,9 @@ class AutoRetireTest < ApplicationSystemTestCase
4951
user = users(:kyuukai)
5052
travel_to Time.zone.local(2020, 7, 1, 0, 0, 0) do
5153
VCR.use_cassette 'subscription/update' do
52-
visit scheduler_daily_auto_retire_path
54+
mock_env({ 'TOKEN' => 'token' }) do
55+
visit_with_query scheduler_daily_auto_retire_path, { token: 'token' }
56+
end
5357
end
5458
assert_nil user.reload.retired_on
5559
end
@@ -64,7 +68,9 @@ class AutoRetireTest < ApplicationSystemTestCase
6468
logout
6569

6670
travel_to Time.zone.local(2020, 7, 2, 0, 0, 0) do
67-
visit scheduler_daily_auto_retire_path
71+
mock_env({ 'TOKEN' => 'token' }) do
72+
visit_with_query scheduler_daily_auto_retire_path, { token: 'token' }
73+
end
6874
assert_nil user.reload.retired_on
6975
end
7076
end
@@ -76,7 +82,9 @@ class AutoRetireTest < ApplicationSystemTestCase
7682
user.update!(retired_on: retired_date)
7783

7884
travel_to Time.zone.local(2020, 7, 2, 0, 0, 0) do
79-
visit scheduler_daily_auto_retire_path
85+
mock_env({ 'TOKEN' => 'token' }) do
86+
visit_with_query scheduler_daily_auto_retire_path, { token: 'token' }
87+
end
8088
assert_equal retired_date, user.reload.retired_on
8189
end
8290
end
@@ -89,7 +97,9 @@ class AutoRetireTest < ApplicationSystemTestCase
8997

9098
travel_to Time.zone.local(2020, 7, 2, 0, 0, 0) do
9199
VCR.use_cassette 'subscription/update' do
92-
visit scheduler_daily_auto_retire_path
100+
mock_env({ 'TOKEN' => 'token' }) do
101+
visit_with_query scheduler_daily_auto_retire_path, { token: 'token' }
102+
end
93103
end
94104
assert_equal Date.current, user.reload.retired_on
95105
end
@@ -106,7 +116,9 @@ class AutoRetireTest < ApplicationSystemTestCase
106116
travel_to Time.zone.local(2020, 7, 2, 0, 0, 0) do
107117
Discord::Server.stub(:delete_text_channel, true) do
108118
VCR.use_cassette 'subscription/update' do
109-
visit scheduler_daily_auto_retire_path
119+
mock_env({ 'TOKEN' => 'token' }) do
120+
visit_with_query scheduler_daily_auto_retire_path, { token: 'token' }
121+
end
110122
end
111123
end
112124
assert_equal Date.current, user.reload.retired_on
@@ -123,7 +135,9 @@ class AutoRetireTest < ApplicationSystemTestCase
123135
UserMailer.stub(:auto_retire, stub_postmark_error) do
124136
travel_to Time.zone.local(2020, 7, 2, 0, 0, 0) do
125137
VCR.use_cassette 'subscription/update' do
126-
visit scheduler_daily_auto_retire_path
138+
mock_env({ 'TOKEN' => 'token' }) do
139+
visit_with_query scheduler_daily_auto_retire_path, { token: 'token' }
140+
end
127141
end
128142
assert_equal Date.current, user.reload.retired_on
129143
end
@@ -140,7 +154,9 @@ class AutoRetireTest < ApplicationSystemTestCase
140154

141155
travel_to Time.zone.local(2020, 7, 2, 0, 0, 0) do
142156
VCR.use_cassette 'subscription/update' do
143-
visit scheduler_daily_auto_retire_path
157+
mock_env({ 'TOKEN' => 'token' }) do
158+
visit_with_query scheduler_daily_auto_retire_path, { token: 'token' }
159+
end
144160
end
145161
assert_equal Date.current, user.reload.retired_on
146162
end

test/system/notification/questions_test.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,19 @@ class Notification::QuestionsTest < ApplicationSystemTestCase
303303
)
304304

305305
travel_to Time.zone.local(2022, 11, 6, 0, 0, 0) do
306-
visit '/scheduler/daily/notify_certain_period_passed_after_last_answer'
306+
mock_env({ 'TOKEN' => 'token' }) do
307+
visit_with_query scheduler_daily_notify_certain_period_passed_after_last_answer_path, { token: 'token' }
308+
end
307309
visit_with_auth '/notifications', 'kimura'
308310

309311
assert_no_text 'Q&A「テストの質問」のベストアンサーがまだ選ばれていません。'
310312
end
311313
logout
312314

313315
travel_to Time.zone.local(2022, 11, 7, 0, 0, 0) do
314-
visit '/scheduler/daily/notify_certain_period_passed_after_last_answer'
316+
mock_env({ 'TOKEN' => 'token' }) do
317+
visit_with_query scheduler_daily_notify_certain_period_passed_after_last_answer_path, { token: 'token' }
318+
end
315319
visit_with_auth '/notifications', 'kimura'
316320

317321
assert_text 'Q&A「テストの質問」のベストアンサーがまだ選ばれていません。'

0 commit comments

Comments
 (0)