Skip to content
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

分報チャンネル作成後、生成されたチャンネルのURLをユーザーのプロフィールに自動で設定 #7564

Merged
merged 7 commits into from
Apr 17, 2024
3 changes: 2 additions & 1 deletion app/models/times_channel_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def call(payload)

times_channel = Discord::TimesChannel.new(user.login_name)
if times_channel.save
user.discord_profile.update(times_id: times_channel.id)
times_url = "https://discord.com/channels/#{ENV['DISCORD_GUILD_ID']}/#{times_channel.id}"
user.discord_profile.update(times_id: times_channel.id, times_url:)
else
Rails.logger.warn "[Discord API] #{user.login_name}の分報チャンネルが作成できませんでした。"
end
Expand Down
1 change: 1 addition & 0 deletions app/models/times_channel_destroyer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def call(payload)

if Discord::Server.delete_text_channel(user.discord_profile.times_id)
user.discord_profile.times_id = nil
user.discord_profile.times_url = nil
user.discord_profile.save!(context: :retirement)
else
Rails.logger.warn "[Discord API] #{user.login_name}の分報チャンネルが削除できませんでした。"
Expand Down
97 changes: 52 additions & 45 deletions test/integration/discord/users_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
# frozen_string_literal: true

require 'test_helper'
require 'supports/mock_env_helper'

module Discord
class UsersControllerTest < ActionDispatch::IntegrationTest
include MockEnvHelper

test 'POST create by student' do
Card.stub(:new, -> { FakeCard.new }) do
Subscription.stub(:new, -> { FakeSubscription.new }) do
Discord::TimesChannel.stub(:new, ->(_) { ValidTimesChannel.new }) do
assert_difference 'User.students.count', 1 do
post users_path,
params: {
user: {
adviser: 'false',
trainee: 'false',
company_id: '',
login_name: 'Piyopiyo-student',
email: 'piyopiyo-student@example.com',
name: '現役生です',
name_kana: 'ゲンエキセイデス',
description: '現役生と言います。よろしくお願いします。',
job: 'part_time_worker',
os: 'linux',
experience: 'inexperienced',
password: 'passW0rd1234',
password_confirmation: 'passW0rd1234',
coc: 1,
tos: 2
mock_env('DISCORD_GUILD_ID' => '111') do
Card.stub(:new, -> { FakeCard.new }) do
Subscription.stub(:new, -> { FakeSubscription.new }) do
Discord::TimesChannel.stub(:new, ->(_) { ValidTimesChannel.new }) do
assert_difference 'User.students.count', 1 do
post users_path,
params: {
user: {
adviser: 'false',
trainee: 'false',
company_id: '',
login_name: 'Piyopiyo-student',
email: 'piyopiyo-student@example.com',
name: '現役生です',
name_kana: 'ゲンエキセイデス',
description: '現役生と言います。よろしくお願いします。',
job: 'part_time_worker',
os: 'linux',
experience: 'inexperienced',
password: 'passW0rd1234',
password_confirmation: 'passW0rd1234',
coc: 1,
tos: 2
}
}
}
end
end
end
end
Expand All @@ -40,28 +45,30 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
end

test 'POST create by trainee' do
Discord::TimesChannel.stub(:new, ->(_) { ValidTimesChannel.new }) do
assert_difference 'User.trainees.count', 1 do
post users_path,
params: {
user: {
adviser: 'false',
trainee: 'true',
company_id: '123456789',
login_name: 'Piyopiyo-trainee',
email: 'piyopiyo-trainee@example.com',
name: '研修生です',
name_kana: 'ケンシュウセイデス',
description: '研修生と言います。よろしくお願いします。',
job: 'office_worker',
os: 'windows_wsl2',
experience: 'ruby',
password: 'passW0rd1234',
password_confirmation: 'passW0rd1234',
coc: 1,
tos: 2
mock_env('DISCORD_GUILD_ID' => '222') do
Discord::TimesChannel.stub(:new, ->(_) { ValidTimesChannel.new }) do
assert_difference 'User.trainees.count', 1 do
post users_path,
params: {
user: {
adviser: 'false',
trainee: 'true',
company_id: '123456789',
login_name: 'Piyopiyo-trainee',
email: 'piyopiyo-trainee@example.com',
name: '研修生です',
name_kana: 'ケンシュウセイデス',
description: '研修生と言います。よろしくお願いします。',
job: 'office_worker',
os: 'windows_wsl2',
experience: 'ruby',
password: 'passW0rd1234',
password_confirmation: 'passW0rd1234',
coc: 1,
tos: 2
}
}
}
end
end
end
assert_redirected_to root_url
Expand Down Expand Up @@ -124,7 +131,7 @@ def save
end

def id
'fake_times_snowflake_0123456789'
'1234567890123456789'
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions test/models/times_channel_creator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ class TimesChannelCreatorTest < ActiveSupport::TestCase
TimesChannelCreator.new.call({ user: })
end
assert_nil user.discord_profile.times_id
assert_nil user.discord_profile.times_url
assert_equal "[Discord API] #{user.login_name}の分報チャンネルが作成できませんでした。", logs.pop

Discord::TimesChannel.stub(:new, ->(_) { ValidTimesChannel.new }) do
TimesChannelCreator.new.call({ user: })
end
assert_equal '1234567890123456789', user.discord_profile.times_id
expected_url = "https://discord.com/channels/#{ENV['DISCORD_GUILD_ID']}/1234567890123456789"
assert_equal expected_url, user.discord_profile.times_url
assert_nil logs.last
end
end
Expand Down
1 change: 1 addition & 0 deletions test/models/times_channel_destroyer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TimesChannelDestroyerTest < ActiveSupport::TestCase
TimesChannelDestroyer.new.call({ user: })
end
assert_nil user.discord_profile.times_id
assert_nil user.discord_profile.times_url
assert_nil logs.last
end
end
Expand Down
1 change: 1 addition & 0 deletions test/system/retirement_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class RetirementTest < ApplicationSystemTestCase
end
assert_equal Date.current, user.reload.retired_on
assert_nil user.discord_profile.times_id
assert_nil user.discord_profile.times_url
end

test 'retire user with postmark error' do
Expand Down