11import json
22import re
33import time
4+ from datetime import datetime
45from unittest .mock import Mock
56from urllib .parse import parse_qsl
67from urllib .parse import urlparse
@@ -88,17 +89,40 @@ def userinfo(self):
8889 "sub" : "username"
8990 }
9091
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+
91115 def test_client (self , backend_config ):
92116 assert isinstance (self .oidc_backend .client , StandAloneClient )
93117 # 3 signing keys. One RSA, one EC and one symmetric
94118 assert len (self .oidc_backend .client .context .keyjar .get_signing_key ()) == 3
95119 assert self .oidc_backend .client .context .jwks_uri == backend_config ['client' ]['jwks_uri' ]
96120
97121 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+ }
102126 assert actual_attributes == expected_attributes
103127
104128 def setup_token_endpoint (self , userinfo ):
@@ -166,16 +190,19 @@ def test_register_endpoints(self):
166190 assert re .search (regex , redirect_uri_path )
167191 assert callback == self .oidc_backend .response_endpoint
168192
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+ )
174201
175202 @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 )
179206
180207 response_context = Context ()
181208 response_context .request = incoming_authn_response
@@ -186,8 +213,9 @@ def test_response_endpoint(self, context, userinfo, incoming_authn_response):
186213 args = self .oidc_backend .auth_callback_func .call_args [0 ]
187214 assert isinstance (args [0 ], Context )
188215 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+ )
191219
192220 def test_start_auth_redirects_to_provider_authorization_endpoint (self , context ):
193221 _client = self .oidc_backend .client
0 commit comments