-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7436 from fjordllc/feature/oauth-discord-to-fetch-id
Discord認証ができるようにする
- Loading branch information
Showing
14 changed files
with
214 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
class Authentication::Discord | ||
include Rails.application.routes.url_helpers | ||
|
||
def initialize(login_user, auth) | ||
@login_user = login_user | ||
@auth = auth | ||
end | ||
|
||
def authenticate | ||
if link | ||
{ path: root_path, notice: 'Discordと連携しました' } | ||
else | ||
{ path: root_path, alert: 'Discordの連携に失敗しました' } | ||
end | ||
end | ||
|
||
private | ||
|
||
def link | ||
discord_profile = DiscordProfile.find_or_initialize_by(user: @login_user) | ||
discord_profile.account_name = @auth[:info][:name] | ||
discord_profile.save | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
class Authentication::Github | ||
include Rails.application.routes.url_helpers | ||
|
||
def initialize(user, auth) | ||
@user = user | ||
@auth = auth | ||
end | ||
|
||
def authenticate | ||
if @user.blank? | ||
user = User.find_by(github_id: @auth[:uid]) | ||
if user.blank? | ||
{ path: root_path, alert: 'ログインに失敗しました。先にアカウントを作成後、GitHub連携を行ってください。' } | ||
elsif user.retired_on? | ||
{ path: retirement_path } | ||
else | ||
{ path: root_path, notice: 'サインインしました。', user_id: user.id, back: true } | ||
end | ||
else | ||
link if @user.github_id.blank? | ||
{ path: root_path, notice: 'GitHubと連携しました。' } | ||
end | ||
rescue StandardError => e | ||
Rails.logger.warn "[GitHub Login] ログインに失敗しました。:#{e.message}" | ||
{ path: root_path, alert: 'GitHubログインに失敗しました。数回試しても続く場合、管理者に連絡してください。' } | ||
end | ||
|
||
private | ||
|
||
def link | ||
@user.github_account = @auth[:info][:nickname] | ||
@user.github_id = @auth[:uid] | ||
@user.save! | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class Authentication::DiscordTest < ActiveSupport::TestCase | ||
include Rails.application.routes.url_helpers | ||
|
||
test 'authentication succeeds when arguments are valid' do | ||
user = users(:komagata) | ||
discord_authentication = Authentication::Discord.new(user, { info: { name: 'komagata_discord' } }) | ||
result = discord_authentication.authenticate | ||
|
||
assert_equal result[:notice], 'Discordと連携しました' | ||
assert_equal result[:path], root_path | ||
end | ||
|
||
test 'authentication fails when arguments are invalid' do | ||
discord_authentication = Authentication::Discord.new(nil, { info: { name: 'komagata_discord' } }) | ||
result = discord_authentication.authenticate | ||
|
||
assert_equal result[:alert], 'Discordの連携に失敗しました' | ||
assert_equal result[:path], root_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class Authentication::GithubTest < ActiveSupport::TestCase | ||
include Rails.application.routes.url_helpers | ||
|
||
test 'authentication fails when github id does not match' do | ||
github_authentication = Authentication::Github.new(nil, { info: { nickname: 'kimura_github' }, uid: 'uid_test_data' }) | ||
result = github_authentication.authenticate | ||
|
||
assert_equal result[:path], root_path | ||
assert_equal result[:alert], 'ログインに失敗しました。先にアカウントを作成後、GitHub連携を行ってください。' | ||
end | ||
|
||
test 'retirement path when github id matches a retired user' do | ||
user = users(:yameo) | ||
user.github_id = 'uid_test_data' | ||
user.save! | ||
|
||
github_authentication = Authentication::Github.new(nil, { info: { nickname: 'yameo_github' }, uid: 'uid_test_data' }) | ||
|
||
assert_equal github_authentication.authenticate[:path], retirement_path | ||
end | ||
|
||
test 'authentication succeeds when github id matches a regular user' do | ||
user = users(:kimura) | ||
user.github_id = 'uid_test_data' | ||
user.save! | ||
|
||
github_authentication = Authentication::Github.new(nil, { info: { name: 'komagata_discord' }, uid: 'uid_test_data' }) | ||
result = github_authentication.authenticate | ||
|
||
assert_equal result[:path], root_path | ||
assert_equal result[:notice], 'サインインしました。' | ||
assert_equal result[:user_id], user.id | ||
assert_equal result[:back], true | ||
end | ||
|
||
test 'github is linked when user is logged in and not linked with Github' do | ||
user = users(:kimura) | ||
github_authentication = Authentication::Github.new(user, { info: { nickname: 'kimura_github' }, uid: 'uid_test_data' }) | ||
result = github_authentication.authenticate | ||
|
||
assert_equal result[:path], root_path | ||
assert_equal user.reload.github_account, 'kimura_github' | ||
assert_equal user.reload.github_id, 'uid_test_data' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'application_system_test_case' | ||
|
||
class Authentication::DiscordSystemTest < ApplicationSystemTestCase | ||
setup do | ||
OmniAuth.config.test_mode = true | ||
OmniAuth.config.add_mock(:discord, { info: { name: 'discord_name' } }) | ||
end | ||
|
||
test 'cannot register discord account already setting user' do | ||
visit_with_auth '/current_user/edit', 'kimura' | ||
|
||
assert_text 'Discord アカウントは登録されています。' | ||
assert_no_text 'Discord アカウントを登録する' | ||
end | ||
|
||
test 'can register discord account not setting user' do | ||
visit_with_auth '/current_user/edit', 'hatsuno' | ||
|
||
click_link 'Discord アカウントを登録する' | ||
assert_text 'Discordと連携しました' | ||
|
||
visit '/current_user/edit' | ||
assert_text 'Discord アカウントは登録されています。' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters