-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathorcid_controller_test.rb
More file actions
151 lines (120 loc) · 4.03 KB
/
Copy pathorcid_controller_test.rb
File metadata and controls
151 lines (120 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'test_helper'
class OrcidControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers
test 'authenticate orcid logged-in user' do
sign_in users(:regular_user)
post :authenticate
assert_redirected_to /https:\/\/sandbox\.orcid\.org\/oauth\/authorize\?.+/
end
test 'do not authenticate orcid if user not logged-in' do
post :authenticate
assert_redirected_to new_user_session_path
end
test 'handle callback and assign orcid if free' do
mock_images
user = users(:regular_user)
assert user.profile.orcid.blank?
sign_in user
VCR.use_cassette('orcid/get_token_free_orcid') do
get :callback, params: { code: '123xyz' }
end
profile = user.profile.reload
assert_equal '0009-0006-0987-5702', profile.orcid
assert profile.orcid_authenticated?
assert_redirected_to user
end
test 'handle callback and assign orcid if unauthenticated' do
mock_images
user = users(:regular_user)
sign_in user
VCR.use_cassette('orcid/get_token_unauth_orcid') do
get :callback, params: { code: '123xyz' }
end
profile = user.profile.reload
assert_equal '0000-0002-0048-3300', profile.orcid
assert profile.orcid_authenticated?
assert_redirected_to user
end
test 'handle callback and assign orcid even if already used' do
mock_images
user = users(:regular_user)
existing_orcid_user = users(:trainer_user)
assert existing_orcid_user.profile.orcid_authenticated?
sign_in user
VCR.use_cassette('orcid/get_token_existing_orcid') do
get :callback, params: { code: '123xyz' }
end
profile = user.profile.reload
assert_equal '0000-0002-1825-0097', profile.orcid
assert profile.orcid_authenticated?
refute existing_orcid_user.reload.profile.orcid_authenticated?
assert_redirected_to user
assert flash[:error].blank?
end
test 'do not handle callback if not logged-in' do
mock_images
get :callback, params: { code: '123xyz' }
assert_redirected_to new_user_session_path
end
test 'handle unauth error during callback' do
mock_images
user = users(:regular_user)
sign_in user
VCR.use_cassette('orcid/error_unauth') do
get :callback, params: { code: '123xyz' }
end
assert_response :unprocessable_entity
assert_select '#error-message', text: /error occurred.+ORCID/
profile = user.profile.reload
assert profile.orcid.blank?
refute profile.orcid_authenticated?
end
test 'handle unexpected error during callback' do
mock_images
user = users(:regular_user)
sign_in user
VCR.use_cassette('orcid/error_500') do
get :callback, params: { code: '123xyz' }
end
assert_response :unprocessable_entity
assert_select '#error-message', text: /error occurred.+ORCID/
profile = user.profile.reload
assert profile.orcid.blank?
refute profile.orcid_authenticated?
end
test 'handle missing orcid during callback' do
mock_images
user = users(:regular_user)
sign_in user
VCR.use_cassette('orcid/get_token_orcid_missing') do
get :callback, params: { code: '123xyz' }
end
assert_redirected_to user
assert_includes flash[:error], 'Failed to authenticat'
profile = user.profile.reload
assert profile.orcid.blank?
refute profile.orcid_authenticated?
end
test 'do not authenticate orcid if feature not enabled' do
Rails.application.config.secrets.stub(:orcid, nil) do
sign_in users(:regular_user)
assert_raises(ActionController::RoutingError) do
post :authenticate
end
end
end
test 'do not handle orcid callback if feature not enabled' do
Rails.application.config.secrets.stub(:orcid, nil) do
mock_images
user = users(:regular_user)
sign_in user
VCR.use_cassette('orcid/get_token_unauth_orcid') do
assert_raises(ActionController::RoutingError) do
get :callback, params: { code: '123xyz' }
end
profile = user.profile.reload
refute profile.orcid_authenticated?
end
end
end
end