-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ベストアンサーなし通知が本番で動作しないバグを修正 #6970
Changes from 15 commits
281534a
670ca83
042f9e0
878fa49
5e75b03
63298c5
8e49357
4ce1914
1023269
132848a
d87ce58
32973ea
fa5565b
d45e5e5
03d531f
da12474
5ba8640
f546eb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ class SchedulerController < ApplicationController | |
protected | ||
|
||
def require_token | ||
return if ENV['TOKEN'] == params[:token] | ||
return if ENV['TOKEN'].present? && ENV['TOKEN'] == params[:token] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TOKENが未設定だとエラー、paramsのtokenが不一致だったらエラー、というパターンもテストしたい。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. それぞれのテストについて、TOKENが未設定のケースと不一致のケースのテストを加えました。 |
||
|
||
head :unauthorized | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
require 'supports/mock_env_helper' | ||
|
||
class Scheduler::Daily::AutoRetireTest < ActionDispatch::IntegrationTest | ||
include MockEnvHelper | ||
|
||
test 'token evaluation' do | ||
# サーバー側のTOKENが未設定 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントいらないんじゃないかなー。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 削除しました! |
||
mock_env('TOKEN' => '') do | ||
get scheduler_daily_auto_retire_path(token: '') | ||
assert_response 401 | ||
end | ||
|
||
mock_env('TOKEN' => 'token') do | ||
# リクエストで指定したtokenが不正 | ||
get scheduler_daily_auto_retire_path(token: 'invalid') | ||
assert_response 401 | ||
|
||
# tokenが正しい | ||
Scheduler::Daily::AutoRetireController.stub_any_instance(:auto_retire) do | ||
get scheduler_daily_auto_retire_path(token: 'token') | ||
assert_response 200 | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
require 'supports/mock_env_helper' | ||
|
||
class Scheduler::Daily::FetchExternalEntryTest < ActionDispatch::IntegrationTest | ||
include MockEnvHelper | ||
|
||
test 'token evaluation' do | ||
# サーバー側のTOKENが未設定 | ||
mock_env('TOKEN' => '') do | ||
get scheduler_daily_fetch_external_entry_path(token: '') | ||
assert_response 401 | ||
end | ||
|
||
mock_env('TOKEN' => 'token') do | ||
# リクエストで指定したtokenが不正 | ||
get scheduler_daily_fetch_external_entry_path(token: 'invalid') | ||
assert_response 401 | ||
|
||
# tokenが正しい | ||
ExternalEntry.stub(:fetch_and_save_rss_feeds, nil) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 質問)nilって付けないと問題が起きるんでしょうか? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stubメソッドでは第2引数で戻り値を指定する必要があるようです。省略するとエラーになりました。ここでは戻り値はとくに問題ではないので、nilを指定することにしました。 |
||
get scheduler_daily_fetch_external_entry_path(token: 'token') | ||
assert_response 200 | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
require 'supports/mock_env_helper' | ||
|
||
class Scheduler::Daily::NotifyCertainPeriodPassedAfterLastAnswerTest < ActionDispatch::IntegrationTest | ||
include MockEnvHelper | ||
|
||
test 'token evaluation' do | ||
# サーバー側のTOKENが未設定 | ||
mock_env('TOKEN' => '') do | ||
get scheduler_daily_notify_certain_period_passed_after_last_answer_path(token: '') | ||
assert_response 401 | ||
end | ||
|
||
mock_env('TOKEN' => 'token') do | ||
# リクエストで指定したtokenが不正 | ||
get scheduler_daily_notify_certain_period_passed_after_last_answer_path(token: 'invalid') | ||
assert_response 401 | ||
|
||
# tokenが正しい | ||
Question.stub(:notify_certain_period_passed_after_last_answer, nil) do | ||
get scheduler_daily_notify_certain_period_passed_after_last_answer_path(token: 'token') | ||
assert_response 200 | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
require 'supports/mock_env_helper' | ||
|
||
class Scheduler::Daily::NotifyComingSoonRegularEventsTest < ActionDispatch::IntegrationTest | ||
include MockEnvHelper | ||
|
||
test 'token evaluation' do | ||
# サーバー側のTOKENが未設定 | ||
mock_env('TOKEN' => '') do | ||
get scheduler_daily_notify_coming_soon_regular_events_path(token: '') | ||
assert_response 401 | ||
end | ||
|
||
mock_env('TOKEN' => 'token') do | ||
# リクエストで指定したtokenが不正 | ||
get scheduler_daily_notify_coming_soon_regular_events_path(token: 'invalid') | ||
assert_response 401 | ||
|
||
# tokenが正しい | ||
Scheduler::Daily::NotifyComingSoonRegularEventsController.stub_any_instance(:notify_coming_soon_regular_events) do | ||
get scheduler_daily_notify_coming_soon_regular_events_path(token: 'token') | ||
assert_response 200 | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||||||||||||||||||||||||||||||
# frozen_string_literal: true | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
module MockEnvHelper | ||||||||||||||||||||||||||||||||
def mock_env(partial_env_hash) | ||||||||||||||||||||||||||||||||
old = ENV.to_hash | ||||||||||||||||||||||||||||||||
ENV.update partial_env_hash | ||||||||||||||||||||||||||||||||
begin | ||||||||||||||||||||||||||||||||
yield | ||||||||||||||||||||||||||||||||
ensure | ||||||||||||||||||||||||||||||||
ENV.replace old | ||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
ENVを書き替えるのは事故が怖いのでstubメソッドのした方が安心かも There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらで修正しました。 |
||||||||||||||||||||||||||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
とした上で
とした方がコードの意図が明確になりそうな気がしました
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらで修正しました。
5ba8640