diff --git a/app/controllers/hibernation_controller.rb b/app/controllers/hibernation_controller.rb index f32db28dae0..f03d99f0609 100644 --- a/app/controllers/hibernation_controller.rb +++ b/app/controllers/hibernation_controller.rb @@ -16,6 +16,8 @@ def create if @hibernation.save update_hibernated_at! destroy_subscription! + notify_to_chat + notify_to_mentors_and_admins logout redirect_to hibernation_path else @@ -42,7 +44,11 @@ def destroy_subscription! def notify_to_mentors_and_admins User.admins_and_mentors.each do |admin_or_mentor| - NotificationFacade.retired(current_user, admin_or_mentor) + NotificationFacade.hibernated(current_user, admin_or_mentor) end end + + def notify_to_chat + DiscordNotifier.with(sender: current_user).hibernated.notify_now + end end diff --git a/app/models/notification_facade.rb b/app/models/notification_facade.rb index e5cabbc1190..d3ae715db1d 100644 --- a/app/models/notification_facade.rb +++ b/app/models/notification_facade.rb @@ -198,7 +198,6 @@ def self.graduated(sender, receiver) def self.hibernated(sender, receiver) ActivityNotifier.with(sender: sender, receiver: receiver).hibernated.notify_now - DiscordNotifier.with(sender: sender, receiver: receiver).hibernated.notify_now return unless receiver.mail_notification? NotificationMailer.with( diff --git a/test/mailers/notification_mailer_test.rb b/test/mailers/notification_mailer_test.rb index b58dca0a7ec..6eb35fbd9fa 100644 --- a/test/mailers/notification_mailer_test.rb +++ b/test/mailers/notification_mailer_test.rb @@ -384,4 +384,32 @@ class NotificationMailerTest < ActionMailer::TestCase assert_equal '[FBC] hajimeさんが新しく入会しました!', email.subject assert_match(/入会/, email.body.to_s) end + + test 'hibernated' do + user = users(:kimura) + mentor = users(:komagata) + Notification.create!( + kind: 19, + sender: user, + user: mentor, + message: 'kimuraさんが休会しました。', + link: "/users/#{user.id}", + read: false + ) + mailer = NotificationMailer.with( + sender: user, + receiver: mentor + ).hibernated + + perform_enqueued_jobs do + mailer.deliver_later + end + + assert_not ActionMailer::Base.deliveries.empty? + email = ActionMailer::Base.deliveries.last + assert_equal ['noreply@bootcamp.fjord.jp'], email.from + assert_equal ['komagata@fjord.jp'], email.to + assert_equal '[FBC] kimuraさんが休会しました。', email.subject + assert_match(/休会/, email.body.to_s) + end end diff --git a/test/notifiers/activity_notifier_test.rb b/test/notifiers/activity_notifier_test.rb index 5ba6d5ebf77..8b8ef368b9f 100644 --- a/test/notifiers/activity_notifier_test.rb +++ b/test/notifiers/activity_notifier_test.rb @@ -74,6 +74,18 @@ class ActivityNotifierTest < ActiveSupport::TestCase end end + test '#hibernated' do + notification = ActivityNotifier.with(sender: users(:kimura), receiver: users(:komagata)).hibernated + + assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do + notification.notify_now + end + + assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do + notification.notify_later + end + end + test '#retired' do notification = ActivityNotifier.with(sender: users(:kimura), receiver: users(:komagata)).retired diff --git a/test/notifiers/discord_notifier_test.rb b/test/notifiers/discord_notifier_test.rb index e2beb3dc65d..b10853193ee 100644 --- a/test/notifiers/discord_notifier_test.rb +++ b/test/notifiers/discord_notifier_test.rb @@ -81,4 +81,29 @@ class DiscordNotifierTest < ActiveSupport::TestCase DiscordNotifier.with(params).invalid_user.notify_later end end + + test '.hibernated' do + params = { + body: 'test message', + sender: users(:kimura), + name: 'bob', + webhook_url: 'https://discord.com/api/webhooks/0123456789/xxxxxxxx' + } + + expected = { + body: 'kimuraさんが休会しました。', + name: 'ピヨルド', + webhook_url: 'https://discord.com/api/webhooks/0123456789/xxxxxxxx' + } + + assert_notifications_sent 2, **expected do + DiscordNotifier.hibernated(params).notify_now + DiscordNotifier.with(params).hibernated.notify_now + end + + assert_notifications_enqueued 2, **expected do + DiscordNotifier.hibernated(params).notify_later + DiscordNotifier.with(params).hibernated.notify_later + end + end end diff --git a/test/system/current_user_test.rb b/test/system/current_user_test.rb index 30c3a5e4d7f..180c4000fbc 100644 --- a/test/system/current_user_test.rb +++ b/test/system/current_user_test.rb @@ -53,7 +53,7 @@ class CurrentUserTest < ApplicationSystemTestCase visit_with_auth '/current_user/edit', 'komagata' fill_in 'user[times_url]', with: 'https://example.com/channels/1234/5678/' click_button '更新する' - assert_text '分報チャンネル URL は Discord のチャンネル URL を入力してください' + assert_text '分報チャンネル URLはDiscordのチャンネルURLを入力してください' end test 'Do not show after graduation hope when advisor' do diff --git a/test/system/notification/hibernation_test.rb b/test/system/notification/hibernation_test.rb new file mode 100644 index 00000000000..37d03a3eb9f --- /dev/null +++ b/test/system/notification/hibernation_test.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require 'application_system_test_case' + +class Notification::HibernationTest < ApplicationSystemTestCase + setup do + @delivery_mode = AbstractNotifier.delivery_mode + AbstractNotifier.delivery_mode = :normal + end + + teardown do + AbstractNotifier.delivery_mode = @delivery_mode + end + + test 'notify admins and mentors when a student hibernate' do + visit_with_auth notifications_path, 'komagata' + find('#notifications.loaded', wait: 10) + within first('.card-list-item') do + assert_no_selector '.card-list-item-title__link-label', text: 'kimuraさんが休会しました。' + end + + visit_with_auth new_hibernation_path, 'kimura' + fill_in 'hibernation[scheduled_return_on]', with: Time.current.next_month + fill_in 'hibernation[reason]', with: 'テストのため' + accept_confirm do + click_button '休会する' + end + assert_text '休会処理が完了しました' + + visit_with_auth notifications_path, 'komagata' + find('#notifications.loaded', wait: 10) + within first('.card-list-item.is-unread') do + assert_selector '.card-list-item-title__link-label', text: 'kimuraさんが休会しました。' + end + end + + test 'notify admins and mentors when a trainee hibernate' do + visit_with_auth notifications_path, 'komagata' + find('#notifications.loaded', wait: 10) + within first('.card-list-item') do + assert_no_selector '.card-list-item-title__link-label', text: 'kensyuさんが休会しました。' + end + + visit_with_auth new_hibernation_path, 'kensyu' + fill_in 'hibernation[scheduled_return_on]', with: Time.current.next_month + fill_in 'hibernation[reason]', with: 'テストのため' + accept_confirm do + click_button '休会する' + end + assert_text '休会処理が完了しました' + + visit_with_auth notifications_path, 'komagata' + find('#notifications.loaded', wait: 10) + within first('.card-list-item.is-unread') do + assert_selector '.card-list-item-title__link-label', text: 'kensyuさんが休会しました。' + end + end + + test 'notify admins and mentors when a adviser hibernate' do + visit_with_auth notifications_path, 'komagata' + find('#notifications.loaded', wait: 10) + within first('.card-list-item') do + assert_no_selector '.card-list-item-title__link-label', text: 'senpaiさんが休会しました。' + end + + visit_with_auth new_hibernation_path, 'senpai' + fill_in 'hibernation[scheduled_return_on]', with: Time.current.next_month + fill_in 'hibernation[reason]', with: 'テストのため' + accept_confirm do + click_button '休会する' + end + assert_text '休会処理が完了しました' + + visit_with_auth notifications_path, 'komagata' + find('#notifications.loaded', wait: 10) + within first('.card-list-item.is-unread') do + assert_selector '.card-list-item-title__link-label', text: 'senpaiさんが休会しました。' + end + end +end