1
1
import json
2
2
import re
3
3
import time
4
+ from datetime import datetime
4
5
from unittest .mock import Mock
5
6
from urllib .parse import parse_qsl
6
7
from urllib .parse import urlparse
@@ -88,17 +89,40 @@ def userinfo(self):
88
89
"sub" : "username"
89
90
}
90
91
92
+ @pytest .fixture
93
+ def id_token (self , userinfo ):
94
+ issuer_keys = build_keyjar (DEFAULT_KEY_DEFS )
95
+ signing_key = issuer_keys .get_signing_key (key_type = 'RSA' )[0 ]
96
+ signing_key .alg = "RS256"
97
+ auth_time = int (datetime .utcnow ().timestamp ())
98
+ id_token_claims = {
99
+ "auth_time" : auth_time ,
100
+ "iss" : ISSUER ,
101
+ "sub" : userinfo ["sub" ],
102
+ "aud" : CLIENT_ID ,
103
+ "nonce" : NONCE ,
104
+ "exp" : auth_time + 3600 ,
105
+ "iat" : auth_time ,
106
+ }
107
+ id_token = IdToken (** id_token_claims )
108
+ return id_token
109
+
110
+ @pytest .fixture
111
+ def all_user_claims (self , userinfo , id_token ):
112
+ all_user_claims = {** userinfo , ** id_token }
113
+ return all_user_claims
114
+
91
115
def test_client (self , backend_config ):
92
116
assert isinstance (self .oidc_backend .client , StandAloneClient )
93
117
# 3 signing keys. One RSA, one EC and one symmetric
94
118
assert len (self .oidc_backend .client .context .keyjar .get_signing_key ()) == 3
95
119
assert self .oidc_backend .client .context .jwks_uri == backend_config ['client' ]['jwks_uri' ]
96
120
97
121
def assert_expected_attributes (self , attr_map , user_claims , actual_attributes ):
98
- expected_attributes = {}
99
- for out_attr , in_mapping in attr_map [ "attributes" ]. items ():
100
- expected_attributes [ out_attr ] = [ user_claims [ in_mapping [ "openid" ][ 0 ]]]
101
-
122
+ expected_attributes = {
123
+ out_attr : [ user_claims [ in_mapping [ "openid" ][ 0 ]]]
124
+ for out_attr , in_mapping in attr_map [ "attributes" ]. items ()
125
+ }
102
126
assert actual_attributes == expected_attributes
103
127
104
128
def setup_token_endpoint (self , userinfo ):
@@ -166,16 +190,19 @@ def test_register_endpoints(self):
166
190
assert re .search (regex , redirect_uri_path )
167
191
assert callback == self .oidc_backend .response_endpoint
168
192
169
- def test_translate_response_to_internal_response (self , userinfo ):
170
- internal_response = self .oidc_backend ._translate_response (userinfo , ISSUER )
171
- assert internal_response .subject_id == userinfo ["sub" ]
172
- self .assert_expected_attributes (self .oidc_backend .internal_attributes , userinfo ,
173
- internal_response .attributes )
193
+ def test_translate_response_to_internal_response (self , all_user_claims ):
194
+ internal_response = self .oidc_backend ._translate_response (all_user_claims , ISSUER )
195
+ assert internal_response .subject_id == all_user_claims ["sub" ]
196
+ self .assert_expected_attributes (
197
+ self .oidc_backend .internal_attributes ,
198
+ all_user_claims ,
199
+ internal_response .attributes ,
200
+ )
174
201
175
202
@responses .activate
176
- def test_response_endpoint (self , context , userinfo , incoming_authn_response ):
177
- self .setup_token_endpoint (userinfo )
178
- self .setup_userinfo_endpoint (userinfo )
203
+ def test_response_endpoint (self , context , all_user_claims , incoming_authn_response ):
204
+ self .setup_token_endpoint (all_user_claims )
205
+ self .setup_userinfo_endpoint (all_user_claims )
179
206
180
207
response_context = Context ()
181
208
response_context .request = incoming_authn_response
@@ -186,8 +213,9 @@ def test_response_endpoint(self, context, userinfo, incoming_authn_response):
186
213
args = self .oidc_backend .auth_callback_func .call_args [0 ]
187
214
assert isinstance (args [0 ], Context )
188
215
assert isinstance (args [1 ], InternalData )
189
- self .assert_expected_attributes (self .oidc_backend .internal_attributes , userinfo ,
190
- args [1 ].attributes )
216
+ self .assert_expected_attributes (
217
+ self .oidc_backend .internal_attributes , all_user_claims , args [1 ].attributes
218
+ )
191
219
192
220
def test_start_auth_redirects_to_provider_authorization_endpoint (self , context ):
193
221
_client = self .oidc_backend .client
0 commit comments