Skip to content

Commit 41c5503

Browse files
authored
Merge pull request #1237 from ElixirTeSS/orcid-auth-fixes
Check ORCID authentication enabled
2 parents 1604149 + 8c9d095 commit 41c5503

File tree

8 files changed

+74
-11
lines changed

8 files changed

+74
-11
lines changed

app/controllers/orcid_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class OrcidController < ApplicationController
2+
before_action :orcid_auth_enabled
23
before_action :authenticate_user!
34
before_action :set_oauth_client, only: [:authenticate, :callback]
45

@@ -43,4 +44,10 @@ def set_oauth_client
4344
host: config[:host].presence || (Rails.env.production? ? 'orcid.org' : 'sandbox.orcid.org')
4445
)
4546
end
47+
48+
def orcid_auth_enabled
49+
unless TeSS::Config.orcid_authentication_enabled?
50+
raise ActionController::RoutingError.new('Feature not enabled')
51+
end
52+
end
4653
end

app/views/users/show.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
<span class="empty">None specified</span>
5151
<% else %>
5252
<%= orcid_link(@user.profile) %>
53-
<% if current_user == @user && !@user.profile.orcid_authenticated? %>
54-
<%= button_to t('orcid.authenticate'), authenticate_orcid_path, class: 'btn btn-default' %>
55-
<% end %>
53+
<% end %>
54+
<% if TeSS::Config.orcid_authentication_enabled? && current_user == @user && !@user.profile.orcid_authenticated? %>
55+
<%= button_to t(@user.profile.orcid.blank? ? 'orcid.link' : 'orcid.authenticate'), authenticate_orcid_path, class: 'btn btn-default' %>
5656
<% end %>
5757
</p>
5858

config/application.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ def _sentry_dsn
170170
def sentry_enabled?
171171
_sentry_dsn.present? && Rails.env.production?
172172
end
173+
174+
def orcid_authentication_enabled?
175+
Rails.application.config.secrets.orcid.present? &&
176+
Rails.application.config.secrets.orcid[:client_id].present? &&
177+
Rails.application.config.secrets.orcid[:secret].present?
178+
end
173179
end
174180

175181
Config = TessConfig.new(tess_config)

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,7 @@ en:
11291129
Spaces are customizable, community-managed sub-portals within %{site_name}, each with their own catalogue of training content.
11301130
orcid:
11311131
error: 'An error occurred whilst trying to authenticate your ORCID.'
1132+
link: 'Link your ORCID'
11321133
authenticate: 'Authenticate your ORCID'
11331134
authentication_success: 'You have successfully authenticated your ORCID.'
11341135
authentication_failure: 'Failed to authenticate your ORCID.'

docker-compose-prod.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ services:
6969
- redis-data:/data
7070
sidekiq:
7171
container_name: ${PREFIX}-sidekiq
72-
build:
73-
context: .
74-
dockerfile: Dockerfile
75-
target: production
7672
image: ${PREFIX}-app
7773
restart: always
7874
depends_on:

docker-compose.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ services:
6161
- redis-data:/data
6262
sidekiq:
6363
container_name: ${PREFIX}-sidekiq
64-
build:
65-
context: .
66-
dockerfile: Dockerfile
67-
target: development
6864
image: ${PREFIX}-app
6965
depends_on:
7066
- app

test/controllers/orcid_controller_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class OrcidControllerTest < ActionController::TestCase
2020
test 'handle callback and assign orcid if free' do
2121
mock_images
2222
user = users(:regular_user)
23+
assert user.profile.orcid.blank?
2324
sign_in user
2425

2526
VCR.use_cassette('orcid/get_token_free_orcid') do
@@ -121,4 +122,30 @@ class OrcidControllerTest < ActionController::TestCase
121122
assert profile.orcid.blank?
122123
refute profile.orcid_authenticated?
123124
end
125+
126+
test 'do not authenticate orcid if feature not enabled' do
127+
Rails.application.config.secrets.stub(:orcid, nil) do
128+
sign_in users(:regular_user)
129+
130+
assert_raises(ActionController::RoutingError) do
131+
post :authenticate
132+
end
133+
end
134+
end
135+
136+
test 'do not handle orcid callback if feature not enabled' do
137+
Rails.application.config.secrets.stub(:orcid, nil) do
138+
mock_images
139+
user = users(:regular_user)
140+
sign_in user
141+
142+
VCR.use_cassette('orcid/get_token_unauth_orcid') do
143+
assert_raises(ActionController::RoutingError) do
144+
get :callback, params: { code: '123xyz' }
145+
end
146+
profile = user.profile.reload
147+
refute profile.orcid_authenticated?
148+
end
149+
end
150+
end
124151
end

test/controllers/users_controller_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,36 @@ class UsersControllerTest < ActionController::TestCase
504504
assert_select '#sidebar button', text: 'Authenticate your ORCID'
505505
end
506506

507+
test 'should show link orcid button if own profile and orcid currently blank' do
508+
user = users(:private_user)
509+
user.profile.update_column(:orcid, nil)
510+
511+
refute user.profile.orcid.present?
512+
refute user.profile.orcid_authenticated?
513+
514+
sign_in user
515+
516+
get :show, params: { id: user }
517+
518+
assert_response :success
519+
assert_select '#sidebar button', text: 'Link your ORCID'
520+
end
521+
522+
test 'should not show authenticate orcid button if feature disabled' do
523+
Rails.application.config.secrets.stub(:orcid, nil) do
524+
user = users(:private_user)
525+
assert user.profile.orcid.present?
526+
refute user.profile.orcid_authenticated?
527+
528+
sign_in user
529+
530+
get :show, params: { id: user }
531+
532+
assert_response :success
533+
assert_select '#sidebar button', text: 'Authenticate your ORCID', count: 0
534+
end
535+
end
536+
507537
test 'should not show authenticate orcid button if not own profile' do
508538
user = users(:private_user)
509539
assert user.profile.orcid.present?

0 commit comments

Comments
 (0)