Skip to content

Commit

Permalink
token関連のテストをintegration testに置き換える
Browse files Browse the repository at this point in the history
  • Loading branch information
ogawa-tomo committed Nov 5, 2023
1 parent dce7aa6 commit 432e576
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 200 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -632,6 +633,7 @@ DEPENDENCIES
mini_magick
minitest-ci
minitest-retry
minitest-stub_any_instance
net-imap
net-pop
net-smtp
Expand Down
28 changes: 28 additions & 0 deletions test/integration/scheduler/daily/auto_retire_test.rb
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が未設定
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
28 changes: 28 additions & 0 deletions test/integration/scheduler/daily/fetch_external_entry_test.rb
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
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
22 changes: 0 additions & 22 deletions test/system/auto_retire_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 0 additions & 22 deletions test/system/external_entries_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
62 changes: 0 additions & 62 deletions test/system/notification/questions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
94 changes: 0 additions & 94 deletions test/system/notification/regular_events_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 432e576

Please sign in to comment.