Skip to content

Commit fdbcf98

Browse files
committed
Authenticating ORCID should de-authenticate existing ORCID users
instead of throwing error
1 parent 359b780 commit fdbcf98

3 files changed

Lines changed: 28 additions & 19 deletions

File tree

app/models/profile.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,18 @@ def merge(*others)
5050

5151
def authenticate_orcid(orcid)
5252
existing = Profile.where(orcid: orcid, orcid_authenticated: true)
53-
if existing.any?
54-
errors.add(:orcid, :orcid_taken)
55-
false
56-
else
57-
self.orcid = orcid
58-
self.orcid_authenticated = true
59-
self.save
53+
self.orcid = orcid
54+
self.orcid_authenticated = true
55+
out = self.save
56+
57+
if out
58+
existing.each do |profile|
59+
next if profile == self
60+
profile.update_column(:orcid_authenticated, false)
61+
end
6062
end
63+
64+
out
6165
end
6266

6367
private

test/controllers/orcid_controller_test.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,23 @@ class OrcidControllerTest < ActionController::TestCase
4747
assert_redirected_to user
4848
end
4949

50-
test 'handle callback but do not assign orcid if already used' do
50+
test 'handle callback and assign orcid even if already used' do
5151
mock_images
5252
user = users(:regular_user)
53+
existing_orcid_user = users(:trainer_user)
54+
assert existing_orcid_user.profile.orcid_authenticated?
5355
sign_in user
5456

5557
VCR.use_cassette('orcid/get_token_existing_orcid') do
5658
get :callback, params: { code: '123xyz' }
5759
end
5860

5961
profile = user.profile.reload
60-
assert profile.orcid.blank?
61-
refute profile.orcid_authenticated?
62+
assert_equal '0000-0002-1825-0097', profile.orcid
63+
assert profile.orcid_authenticated?
64+
refute existing_orcid_user.reload.profile.orcid_authenticated?
6265
assert_redirected_to user
63-
assert_includes flash[:error], 'ORCID has already been'
66+
assert flash[:error].blank?
6467
end
6568

6669
test 'do not handle callback if not logged-in' do
@@ -92,7 +95,7 @@ class OrcidControllerTest < ActionController::TestCase
9295
user = users(:regular_user)
9396
sign_in user
9497

95-
VCR.use_cassette('orcid/get_token_orcid_missing') do
98+
VCR.use_cassette('orcid/error_500') do
9699
get :callback, params: { code: '123xyz' }
97100
end
98101

test/models/profile_test.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,23 @@ class ProfileTest < ActiveSupport::TestCase
101101
assert profile.orcid_authenticated
102102
end
103103

104-
test 'does not assign authenticated orcid if already taken' do
104+
test 'assigns authenticated orcid and de-authenticates other profiles that use it' do
105105
existing_profile = profiles(:trainer_one_profile)
106106
profile = users(:regular_user).profile
107107
assert existing_profile.orcid.present?
108108
assert existing_profile.orcid_authenticated
109109
refute profile.orcid.present?
110110
refute profile.orcid_authenticated
111+
existing_orcid = existing_profile.orcid
111112

112-
profile.authenticate_orcid(existing_profile.orcid)
113+
profile.authenticate_orcid(existing_orcid)
113114

114-
assert profile.errors.added?(:orcid, :orcid_taken)
115-
assert existing_profile.orcid.present?
116-
assert existing_profile.orcid_authenticated
117-
refute profile.orcid.present?
118-
refute profile.orcid_authenticated
115+
assert_empty profile.errors
116+
assert_equal existing_orcid, profile.orcid
117+
assert profile.orcid_authenticated
118+
existing_profile.reload
119+
assert_equal existing_orcid, existing_profile.orcid
120+
refute existing_profile.orcid_authenticated
119121
end
120122

121123
test 'assigns authenticated orcid if existing orcid is not authenticated' do

0 commit comments

Comments
 (0)