-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathorcid_controller_test.rb
More file actions
124 lines (97 loc) · 3.27 KB
/
Copy pathorcid_controller_test.rb
File metadata and controls
124 lines (97 loc) · 3.27 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
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)
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
end