Skip to content

Commit 623f578

Browse files
committed
Handle no-op case
1 parent f88546d commit 623f578

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

dashboard/app/controllers/omniauth_callbacks_controller.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ def connect_provider
2929

3030
# Check if credential is already in use
3131
existing_credential_holder = User.find_by_credential type: provider, id: auth_hash.uid
32-
if existing_credential_holder && existing_credential_holder != current_user
33-
if existing_credential_holder.has_activity?
32+
if existing_credential_holder
33+
if existing_credential_holder == current_user
34+
flash.notice = I18n.t('auth.already_linked', provider: I18n.t("auth.#{provider}"))
35+
return redirect_to edit_user_registration_path
36+
elsif existing_credential_holder.has_activity?
3437
# Linking is not possible and takeover is not possible
3538
# Display a custom error message explaining the credential is already
3639
# tied to an account, and what we can do about it.

dashboard/test/controllers/omniauth_callbacks_controller_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,27 @@ class OmniauthCallbacksControllerTest < ActionController::TestCase
901901
assert_equal expected_error, flash.alert
902902
end
903903

904+
test "connect_provider: Presents no-op message if the provided credentials are already linked to user's account" do
905+
# Given the current user already has credential X
906+
user = create :user, :multi_auth_migrated
907+
credential = create :google_authentication_option, user: user
908+
assert_equal 1, user.authentication_options.count
909+
910+
# When I attempt to add credential X
911+
link_credential user,
912+
type: credential.credential_type,
913+
id: credential.authentication_id
914+
915+
# Then I should have the same authentication options
916+
user.reload
917+
assert_equal 1, user.authentication_options.count
918+
919+
# And receive a friendly notice about already having the credential
920+
assert_redirected_to 'http://test.host/users/edit'
921+
expected_notice = I18n.t('auth.already_linked', provider: I18n.t("auth.google_oauth2"))
922+
assert_equal expected_notice, flash.notice
923+
end
924+
904925
private
905926

906927
def set_oauth_takeover_session_variables(provider, user)

0 commit comments

Comments
 (0)