Skip to content

Commit 83f14b5

Browse files
committed
Handle missing ORCID from metadata
1 parent 90f765c commit 83f14b5

4 files changed

Lines changed: 83 additions & 7 deletions

File tree

app/controllers/orcid_controller.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ def callback
1717
orcid = token.access_token&.raw_attributes['orcid']
1818
respond_to do |format|
1919
profile = current_user.profile
20-
if profile.authenticate_orcid(orcid)
21-
flash[:notice] = t('orcid.authentication_success')
20+
if orcid.present?
21+
if profile.authenticate_orcid(orcid)
22+
flash[:notice] = t('orcid.authentication_success')
23+
else
24+
flash[:error] = profile.errors.full_messages.join(', ')
25+
end
2226
else
23-
flash[:error] = profile.errors.full_messages.join(', ')
27+
flash[:error] = t('orcid.authentication_failure')
2428
end
2529
format.html { redirect_to current_user }
2630
end

config/locales/en.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,4 +1119,5 @@ en:
11191119
orcid:
11201120
error: 'An error occurred whilst trying to authenticate your ORCID.'
11211121
authenticate: 'Authenticate your ORCID'
1122-
authentication_success: 'You have successfully authenticated your ORCID.'
1122+
authentication_success: 'You have successfully authenticated your ORCID.'
1123+
authentication_failure: 'Failed to authenticate your ORCID.'

test/controllers/orcid_controller_test.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class OrcidControllerTest < ActionController::TestCase
8181
end
8282

8383
assert_response :unprocessable_entity
84-
assert_select '#error-message', text: /ORCID/
84+
assert_select '#error-message', text: /error occurred.+ORCID/
8585
profile = user.profile.reload
8686
assert profile.orcid.blank?
8787
refute profile.orcid_authenticated?
@@ -92,12 +92,28 @@ class OrcidControllerTest < ActionController::TestCase
9292
user = users(:regular_user)
9393
sign_in user
9494

95-
VCR.use_cassette('orcid/error_500') do
95+
VCR.use_cassette('orcid/get_token_orcid_missing') do
9696
get :callback, params: { code: '123xyz' }
9797
end
9898

9999
assert_response :unprocessable_entity
100-
assert_select '#error-message', text: /ORCID/
100+
assert_select '#error-message', text: /error occurred.+ORCID/
101+
profile = user.profile.reload
102+
assert profile.orcid.blank?
103+
refute profile.orcid_authenticated?
104+
end
105+
106+
test 'handle missing orcid during callback' do
107+
mock_images
108+
user = users(:regular_user)
109+
sign_in user
110+
111+
VCR.use_cassette('orcid/get_token_orcid_missing') do
112+
get :callback, params: { code: '123xyz' }
113+
end
114+
115+
assert_redirected_to user
116+
assert_includes flash[:error], 'Failed to authenticat'
101117
profile = user.profile.reload
102118
assert profile.orcid.blank?
103119
refute profile.orcid_authenticated?

test/vcr_cassettes/orcid/get_token_orcid_missing.yml

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)