diff --git a/Gemfile b/Gemfile index eadf2bf5c56..3abdfd6aa82 100644 --- a/Gemfile +++ b/Gemfile @@ -104,6 +104,7 @@ group :test do gem 'capybara' gem 'minitest-ci' gem 'minitest-retry' + gem 'minitest-stub_any_instance' gem 'selenium-webdriver' gem 'vcr' gem 'webmock' diff --git a/Gemfile.lock b/Gemfile.lock index 115d9eb6756..a50f22d75f1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -292,6 +292,7 @@ GEM minitest-retry (0.2.2) minitest (>= 5.0) msgpack (1.7.2) + minitest-stub_any_instance (1.0.3) multi_json (1.15.0) multi_xml (0.6.0) multipart-post (2.3.0) @@ -632,6 +633,7 @@ DEPENDENCIES mini_magick minitest-ci minitest-retry + minitest-stub_any_instance net-imap net-pop net-smtp diff --git a/test/integration/scheduler/daily/auto_retire_test.rb b/test/integration/scheduler/daily/auto_retire_test.rb new file mode 100644 index 00000000000..53307ff159b --- /dev/null +++ b/test/integration/scheduler/daily/auto_retire_test.rb @@ -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が未設定 + 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 diff --git a/test/integration/scheduler/daily/fetch_external_entry_test.rb b/test/integration/scheduler/daily/fetch_external_entry_test.rb new file mode 100644 index 00000000000..4865287655b --- /dev/null +++ b/test/integration/scheduler/daily/fetch_external_entry_test.rb @@ -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 + get scheduler_daily_fetch_external_entry_path(token: 'token') + assert_response 200 + end + end + end +end diff --git a/test/integration/scheduler/daily/notify_certain_period_passed_after_last_answer_test.rb b/test/integration/scheduler/daily/notify_certain_period_passed_after_last_answer_test.rb new file mode 100644 index 00000000000..c7d6b7fb12d --- /dev/null +++ b/test/integration/scheduler/daily/notify_certain_period_passed_after_last_answer_test.rb @@ -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 diff --git a/test/integration/scheduler/daily/notify_coming_soon_regular_events_test.rb b/test/integration/scheduler/daily/notify_coming_soon_regular_events_test.rb new file mode 100644 index 00000000000..580db617971 --- /dev/null +++ b/test/integration/scheduler/daily/notify_coming_soon_regular_events_test.rb @@ -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 diff --git a/test/system/auto_retire_test.rb b/test/system/auto_retire_test.rb index 242a91f3b4f..a92d55df59d 100644 --- a/test/system/auto_retire_test.rb +++ b/test/system/auto_retire_test.rb @@ -161,26 +161,4 @@ class AutoRetireTest < ApplicationSystemTestCase assert_equal Date.current, user.reload.retired_on end end - - test 'not retire when token is invalid' do - user = users(:kyuukai) - travel_to Time.zone.local(2020, 7, 2, 0, 0, 0) do - VCR.use_cassette 'subscription/update' do - mock_env('TOKEN' => 'token') do - visit scheduler_daily_auto_retire_path(token: 'invalid') - end - end - assert_nil user.reload.retired_on - end - end - - test 'not retire when token is not set' do - user = users(:kyuukai) - travel_to Time.zone.local(2020, 7, 2, 0, 0, 0) do - VCR.use_cassette 'subscription/update' do - visit scheduler_daily_auto_retire_path - end - assert_nil user.reload.retired_on - end - end end diff --git a/test/system/external_entries_test.rb b/test/system/external_entries_test.rb index 01340711c33..fad1bc4406b 100644 --- a/test/system/external_entries_test.rb +++ b/test/system/external_entries_test.rb @@ -20,26 +20,4 @@ class ExternalEntriesTest < ApplicationSystemTestCase end end end - - test 'not fetch and save rss feeds when token is invalid' do - assert_no_difference 'ExternalEntry.count' do - VCR.use_cassette 'external_entry/fetch2', vcr_options do - VCR.use_cassette 'external_entry/fetch' do - mock_env('TOKEN' => 'token') do - visit scheduler_daily_fetch_external_entry_path(token: 'invalid') - end - end - end - end - end - - test 'not fetch and save rss feeds when token is not set' do - assert_no_difference 'ExternalEntry.count' do - VCR.use_cassette 'external_entry/fetch2', vcr_options do - VCR.use_cassette 'external_entry/fetch' do - visit scheduler_daily_fetch_external_entry_path - end - end - end - end end diff --git a/test/system/notification/questions_test.rb b/test/system/notification/questions_test.rb index 69f28931346..a45e63a2b73 100644 --- a/test/system/notification/questions_test.rb +++ b/test/system/notification/questions_test.rb @@ -324,66 +324,4 @@ class Notification::QuestionsTest < ApplicationSystemTestCase assert_text 'Q&A「テストの質問」のベストアンサーがまだ選ばれていません。' end end - - test 'not notify to questioner when a week has passed since last answer when token is invalid' do - questioner = users(:kimura) - answerer = users(:komagata) - question = Question.create!( - title: 'テストの質問', - description: 'テスト', - user: questioner, - created_at: '2022-10-31', - updated_at: '2022-10-31', - published_at: '2022-10-31' - ) - Answer.create!( - description: '最後の回答', - user: answerer, - question: question, - created_at: '2022-10-31', - updated_at: '2022-10-31' - ) - - travel_to Time.zone.local(2022, 11, 7, 0, 0, 0) do - assert_no_difference 'questioner.notifications.count' do - mock_env('TOKEN' => 'token') do - visit scheduler_daily_notify_certain_period_passed_after_last_answer_path(token: 'invalid') - end - visit_with_auth '/notifications', 'kimura' - - assert_no_text 'Q&A「テストの質問」のベストアンサーがまだ選ばれていません。' - assert_current_path(notifications_path(_login_name: 'kimura')) - end - end - end - - test 'not notify to questioner when a week has passed since last answer when token is not set' do - questioner = users(:kimura) - answerer = users(:komagata) - question = Question.create!( - title: 'テストの質問', - description: 'テスト', - user: questioner, - created_at: '2022-10-31', - updated_at: '2022-10-31', - published_at: '2022-10-31' - ) - Answer.create!( - description: '最後の回答', - user: answerer, - question: question, - created_at: '2022-10-31', - updated_at: '2022-10-31' - ) - - travel_to Time.zone.local(2022, 11, 7, 0, 0, 0) do - assert_no_difference 'questioner.notifications.count' do - visit scheduler_daily_notify_certain_period_passed_after_last_answer_path - visit_with_auth '/notifications', 'kimura' - - assert_no_text 'Q&A「テストの質問」のベストアンサーがまだ選ばれていません。' - assert_current_path(notifications_path(_login_name: 'kimura')) - end - end - end end diff --git a/test/system/notification/regular_events_test.rb b/test/system/notification/regular_events_test.rb index 172b69c5151..3276488444c 100644 --- a/test/system/notification/regular_events_test.rb +++ b/test/system/notification/regular_events_test.rb @@ -74,98 +74,4 @@ class Notification::RegularEventsTest < ApplicationSystemTestCase assert_requested(stub_message) end - - test 'not notify_coming_soon_regular_events when token is invalid' do - event_info = <<~TEXT.chomp - ⚡️⚡️⚡️イベントのお知らせ⚡️⚡️⚡️ - - < 今日 (05/05 金) 開催 > - - ⚠️ Discord通知確認用、祝日非開催イベント(金曜日開催) - ⚠️ Discord通知確認用、祝日非開催イベント(金曜日 + 土曜日開催) - はお休みです。 - - ------------------------------ - - < 明日 (05/06 土) 開催 > - - Discord通知確認用イベント(土曜日開催) - 時間: 21:00〜22:00 - 詳細: http://localhost:3000/regular_events/284302086 - - Discord通知確認用イベント(土曜日 + 日曜日開催) - 時間: 21:00〜22:00 - 詳細: http://localhost:3000/regular_events/670378901 - - Discord通知確認用、祝日非開催イベント(金曜日 + 土曜日開催) - 時間: 21:00〜22:00 - 詳細: http://localhost:3000/regular_events/808817380 - - ⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️ - TEXT - params = { - url: 'https://discord.com/api/webhooks/0123456789/all', - username: 'ピヨルド', - avatar_url: 'https://i.gyazo.com/7099977680d8d8c2d72a3f14ddf14cc6.png', - content: event_info - } - - stub_message = stub_request(:post, 'https://discord.com/api/webhooks/0123456789/all') - .with(body: params, - headers: { 'Content-Type' => 'application/json' }) - - travel_to Time.zone.local(2023, 5, 5, 6, 0, 0) do - mock_env('TOKEN' => 'token') do - visit scheduler_daily_notify_coming_soon_regular_events_path(token: 'invalid') - end - end - - assert_not_requested(stub_message) - end - - test 'not notify_coming_soon_regular_events when token is not set' do - event_info = <<~TEXT.chomp - ⚡️⚡️⚡️イベントのお知らせ⚡️⚡️⚡️ - - < 今日 (05/05 金) 開催 > - - ⚠️ Discord通知確認用、祝日非開催イベント(金曜日開催) - ⚠️ Discord通知確認用、祝日非開催イベント(金曜日 + 土曜日開催) - はお休みです。 - - ------------------------------ - - < 明日 (05/06 土) 開催 > - - Discord通知確認用イベント(土曜日開催) - 時間: 21:00〜22:00 - 詳細: http://localhost:3000/regular_events/284302086 - - Discord通知確認用イベント(土曜日 + 日曜日開催) - 時間: 21:00〜22:00 - 詳細: http://localhost:3000/regular_events/670378901 - - Discord通知確認用、祝日非開催イベント(金曜日 + 土曜日開催) - 時間: 21:00〜22:00 - 詳細: http://localhost:3000/regular_events/808817380 - - ⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️ - TEXT - params = { - url: 'https://discord.com/api/webhooks/0123456789/all', - username: 'ピヨルド', - avatar_url: 'https://i.gyazo.com/7099977680d8d8c2d72a3f14ddf14cc6.png', - content: event_info - } - - stub_message = stub_request(:post, 'https://discord.com/api/webhooks/0123456789/all') - .with(body: params, - headers: { 'Content-Type' => 'application/json' }) - - travel_to Time.zone.local(2023, 5, 5, 6, 0, 0) do - visit scheduler_daily_notify_coming_soon_regular_events_path - end - - assert_not_requested(stub_message) - end end