1
1
import json
2
- from unittest .mock import Mock
3
- from urllib .parse import urljoin , urlparse , parse_qsl
4
-
5
2
import pytest
6
3
import responses
7
4
8
- from saml2 .saml import NAMEID_FORMAT_TRANSIENT
9
-
10
5
from satosa .backends .orcid import OrcidBackend
11
6
from satosa .context import Context
12
7
from satosa .internal import InternalData
13
8
from satosa .response import Response
9
+ from unittest .mock import Mock
10
+ from urllib .parse import urljoin , urlparse , parse_qsl
14
11
15
12
ORCID_PERSON_ID = "0000-0000-0000-0000"
16
13
ORCID_PERSON_GIVEN_NAME = "orcid_given_name"
19
16
ORCID_PERSON_EMAIL = "orcid_email"
20
17
ORCID_PERSON_COUNTRY = "XX"
21
18
19
+
22
20
class TestOrcidBackend (object ):
23
21
@pytest .fixture (autouse = True )
24
22
def create_backend (self , internal_attributes , backend_config ):
25
- self .orcid_backend = OrcidBackend (Mock (), internal_attributes , backend_config , backend_config ["base_url" ], "orcid" )
23
+ self .orcid_backend = OrcidBackend (
24
+ Mock (),
25
+ internal_attributes ,
26
+ backend_config ,
27
+ backend_config ["base_url" ],
28
+ "orcid"
29
+ )
26
30
27
31
@pytest .fixture
28
32
def backend_config (self ):
@@ -44,12 +48,12 @@ def backend_config(self):
44
48
def internal_attributes (self ):
45
49
return {
46
50
"attributes" : {
47
- "address" : { "orcid" : ["address" ] },
48
- "displayname" : { "orcid" : ["name" ] },
51
+ "address" : {"orcid" : ["address" ]},
52
+ "displayname" : {"orcid" : ["name" ]},
49
53
"edupersontargetedid" : {"orcid" : ["orcid" ]},
50
54
"givenname" : {"orcid" : ["givenname" ]},
51
55
"mail" : {"orcid" : ["mail" ]},
52
- "name" : { "orcid" : ["name" ] },
56
+ "name" : {"orcid" : ["name" ]},
53
57
"surname" : {"orcid" : ["surname" ]},
54
58
}
55
59
}
@@ -58,17 +62,21 @@ def internal_attributes(self):
58
62
def userinfo (self ):
59
63
return {
60
64
"name" : {
61
- "given-names" : { "value" : ORCID_PERSON_GIVEN_NAME },
62
- "family-name" : { "value" : ORCID_PERSON_FAMILY_NAME },
65
+ "given-names" : {"value" : ORCID_PERSON_GIVEN_NAME },
66
+ "family-name" : {"value" : ORCID_PERSON_FAMILY_NAME },
63
67
},
64
68
"emails" : {
65
69
"email" : [
66
- { "email" : ORCID_PERSON_EMAIL , "verified" : True , "primary" : True }
70
+ {
71
+ "email" : ORCID_PERSON_EMAIL ,
72
+ "verified" : True ,
73
+ "primary" : True
74
+ }
67
75
]
68
76
},
69
77
"addresses" : {
70
78
"address" : [
71
- { "country" : { "value" : ORCID_PERSON_COUNTRY } }
79
+ {"country" : {"value" : ORCID_PERSON_COUNTRY } }
72
80
]
73
81
}
74
82
}
@@ -77,8 +85,8 @@ def userinfo(self):
77
85
def userinfo_private (self ):
78
86
return {
79
87
"name" : {
80
- "given-names" : { "value" : ORCID_PERSON_GIVEN_NAME },
81
- "family-name" : { "value" : ORCID_PERSON_FAMILY_NAME },
88
+ "given-names" : {"value" : ORCID_PERSON_GIVEN_NAME },
89
+ "family-name" : {"value" : ORCID_PERSON_FAMILY_NAME },
82
90
},
83
91
"emails" : {
84
92
"email" : [
@@ -90,7 +98,6 @@ def userinfo_private(self):
90
98
}
91
99
}
92
100
93
-
94
101
def assert_expected_attributes (self , user_claims , actual_attributes ):
95
102
print (user_claims )
96
103
print (actual_attributes )
@@ -116,18 +123,22 @@ def setup_token_endpoint(self, token_endpoint_url):
116
123
"orcid" : ORCID_PERSON_ID
117
124
}
118
125
119
- responses .add (responses .POST ,
120
- token_endpoint_url ,
121
- body = json .dumps (token_response ),
122
- status = 200 ,
123
- content_type = "application/json" )
126
+ responses .add (
127
+ responses .POST ,
128
+ token_endpoint_url ,
129
+ body = json .dumps (token_response ),
130
+ status = 200 ,
131
+ content_type = "application/json"
132
+ )
124
133
125
134
def setup_userinfo_endpoint (self , userinfo_endpoint_url , userinfo ):
126
- responses .add (responses .GET ,
127
- urljoin (userinfo_endpoint_url , '{}/person' .format (ORCID_PERSON_ID )),
128
- body = json .dumps (userinfo ),
129
- status = 200 ,
130
- content_type = "application/json" )
135
+ responses .add (
136
+ responses .GET ,
137
+ urljoin (userinfo_endpoint_url , '{}/person' .format (ORCID_PERSON_ID )),
138
+ body = json .dumps (userinfo ),
139
+ status = 200 ,
140
+ content_type = "application/json"
141
+ )
131
142
132
143
@pytest .fixture
133
144
def incoming_authn_response (self , context , backend_config ):
@@ -149,7 +160,10 @@ def test_start_auth(self, context, backend_config):
149
160
assert auth_params ["scope" ] == " " .join (backend_config ["scope" ])
150
161
assert auth_params ["response_type" ] == backend_config ["response_type" ]
151
162
assert auth_params ["client_id" ] == backend_config ["client_config" ]["client_id" ]
152
- assert auth_params ["redirect_uri" ] == backend_config ["base_url" ] + "/" + backend_config ["authz_page" ]
163
+ assert auth_params ["redirect_uri" ] == "{}/{}" .format (
164
+ backend_config ["base_url" ],
165
+ backend_config ["authz_page" ]
166
+ )
153
167
154
168
@responses .activate
155
169
def test_authn_response (self , backend_config , userinfo , incoming_authn_response ):
@@ -166,9 +180,16 @@ def test_authn_response(self, backend_config, userinfo, incoming_authn_response)
166
180
167
181
@responses .activate
168
182
def test_user_information (self , context , backend_config , userinfo ):
169
- self .setup_userinfo_endpoint (backend_config ["server_info" ]["user_info" ], userinfo )
183
+ self .setup_userinfo_endpoint (
184
+ backend_config ["server_info" ]["user_info" ],
185
+ userinfo
186
+ )
170
187
171
- user_attributes = self .orcid_backend .user_information ("orcid_access_token" , ORCID_PERSON_ID , ORCID_PERSON_NAME )
188
+ user_attributes = self .orcid_backend .user_information (
189
+ "orcid_access_token" ,
190
+ ORCID_PERSON_ID ,
191
+ ORCID_PERSON_NAME
192
+ )
172
193
173
194
assert user_attributes ["address" ] == ORCID_PERSON_COUNTRY
174
195
assert user_attributes ["displayname" ] == ORCID_PERSON_NAME
@@ -180,9 +201,16 @@ def test_user_information(self, context, backend_config, userinfo):
180
201
181
202
@responses .activate
182
203
def test_user_information_private (self , context , backend_config , userinfo_private ):
183
- self .setup_userinfo_endpoint (backend_config ["server_info" ]["user_info" ], userinfo_private )
184
-
185
- user_attributes = self .orcid_backend .user_information ("orcid_access_token" , ORCID_PERSON_ID , ORCID_PERSON_NAME )
204
+ self .setup_userinfo_endpoint (
205
+ backend_config ["server_info" ]["user_info" ],
206
+ userinfo_private
207
+ )
208
+
209
+ user_attributes = self .orcid_backend .user_information (
210
+ "orcid_access_token" ,
211
+ ORCID_PERSON_ID ,
212
+ ORCID_PERSON_NAME
213
+ )
186
214
187
215
assert user_attributes ["address" ] == ""
188
216
assert user_attributes ["mail" ] == ""
0 commit comments