Skip to content

Commit 4d73605

Browse files
committed
Fix direct pytest fixture invocations
Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent 5c4c146 commit 4d73605

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

tests/satosa/backends/test_openid_connect.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ def userinfo(self):
8080
def signing_key(self):
8181
return RSAKey(key=RSA.generate(2048), alg="RS256")
8282

83-
def assert_expected_attributes(self, actual_attributes):
84-
user_claims = self.userinfo()
85-
attr_map = self.internal_attributes()
83+
def assert_expected_attributes(self, attr_map, user_claims, actual_attributes):
8684
expected_attributes = {}
8785
for out_attr, in_mapping in attr_map["attributes"].items():
8886
expected_attributes[out_attr] = [user_claims[in_mapping["openid"][0]]]
@@ -97,10 +95,10 @@ def setup_jwks_uri(self, jwks_uri, key):
9795
status=200,
9896
content_type="application/json")
9997

100-
def setup_token_endpoint(self, token_endpoint_url, signing_key):
98+
def setup_token_endpoint(self, token_endpoint_url, userinfo, signing_key):
10199
id_token_claims = {
102100
"iss": ISSUER,
103-
"sub": self.userinfo()["sub"],
101+
"sub": userinfo["sub"],
104102
"aud": CLIENT_ID,
105103
"nonce": NONCE,
106104
"exp": time.time() + 3600,
@@ -120,10 +118,10 @@ def setup_token_endpoint(self, token_endpoint_url, signing_key):
120118
status=200,
121119
content_type="application/json")
122120

123-
def setup_userinfo_endpoint(self, userinfo_endpoint_url):
121+
def setup_userinfo_endpoint(self, userinfo_endpoint_url, userinfo):
124122
responses.add(responses.POST,
125123
userinfo_endpoint_url,
126-
body=json.dumps(self.userinfo()),
124+
body=json.dumps(userinfo),
127125
status=200,
128126
content_type="application/json")
129127

@@ -153,24 +151,24 @@ def test_register_endpoints(self, backend_config):
153151
assert re.search(regex, redirect_uri_path)
154152
assert callback == self.oidc_backend.response_endpoint
155153

156-
def test_translate_response_to_internal_response(self, userinfo):
154+
def test_translate_response_to_internal_response(self, internal_attributes, userinfo):
157155
internal_response = self.oidc_backend._translate_response(userinfo, ISSUER)
158156
assert internal_response.subject_id == userinfo["sub"]
159-
self.assert_expected_attributes(internal_response.attributes)
157+
self.assert_expected_attributes(internal_attributes, userinfo, internal_response.attributes)
160158

161159
@responses.activate
162-
def test_response_endpoint(self, backend_config, signing_key, incoming_authn_response):
160+
def test_response_endpoint(self, backend_config, internal_attributes, userinfo, signing_key, incoming_authn_response):
163161
self.setup_jwks_uri(backend_config["provider_metadata"]["jwks_uri"], signing_key)
164-
self.setup_token_endpoint(backend_config["provider_metadata"]["token_endpoint"], signing_key)
165-
self.setup_userinfo_endpoint(backend_config["provider_metadata"]["userinfo_endpoint"])
162+
self.setup_token_endpoint(backend_config["provider_metadata"]["token_endpoint"], userinfo, signing_key)
163+
self.setup_userinfo_endpoint(backend_config["provider_metadata"]["userinfo_endpoint"], userinfo)
166164

167165
self.oidc_backend.response_endpoint(incoming_authn_response)
168166
assert self.oidc_backend.name not in incoming_authn_response.state
169167

170168
args = self.oidc_backend.auth_callback_func.call_args[0]
171169
assert isinstance(args[0], Context)
172170
assert isinstance(args[1], InternalData)
173-
self.assert_expected_attributes(args[1].attributes)
171+
self.assert_expected_attributes(internal_attributes, userinfo, args[1].attributes)
174172

175173
def test_start_auth_redirects_to_provider_authorization_endpoint(self, context, backend_config):
176174
auth_response = self.oidc_backend.start_auth(context, None)
@@ -188,8 +186,8 @@ def test_start_auth_redirects_to_provider_authorization_endpoint(self, context,
188186
assert "nonce" in auth_params
189187

190188
@responses.activate
191-
def test_entire_flow(self, context, backend_config):
192-
self.setup_userinfo_endpoint(backend_config["provider_metadata"]["userinfo_endpoint"])
189+
def test_entire_flow(self, context, backend_config, internal_attributes, userinfo):
190+
self.setup_userinfo_endpoint(backend_config["provider_metadata"]["userinfo_endpoint"], userinfo)
193191
auth_response = self.oidc_backend.start_auth(context, None)
194192
auth_params = dict(parse_qsl(urlparse(auth_response.message).query))
195193

@@ -198,7 +196,7 @@ def test_entire_flow(self, context, backend_config):
198196
self.oidc_backend.response_endpoint(context)
199197
assert self.oidc_backend.name not in context.state
200198
args = self.oidc_backend.auth_callback_func.call_args[0]
201-
self.assert_expected_attributes(args[1].attributes)
199+
self.assert_expected_attributes(internal_attributes, userinfo, args[1].attributes)
202200

203201

204202
class TestCreateClient(object):

tests/satosa/frontends/test_openid_connect.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ def frontend_config(self, signing_key_path):
4343

4444
return config
4545

46-
@pytest.fixture
47-
def frontend(self, frontend_config):
46+
def create_frontend(self, frontend_config):
4847
# will use in-memory storage
4948
instance = OpenIDConnectFrontend(lambda ctx, req: None, INTERNAL_ATTRIBUTES,
5049
frontend_config, BASE_URL, "oidc_frontend")
5150
instance.register_endpoints(["foo_backend"])
5251
return instance
5352

53+
@pytest.fixture
54+
def frontend(self, frontend_config):
55+
return self.create_frontend(frontend_config)
56+
5457
@pytest.fixture
5558
def authn_req(self):
5659
state = "my_state"
@@ -210,7 +213,7 @@ def test_register_endpoints_token_and_userinfo_endpoint_is_published_if_necessar
210213
def test_register_endpoints_token_and_userinfo_endpoint_is_not_published_if_only_implicit_flow(
211214
self, frontend_config, context):
212215
frontend_config["provider"]["response_types_supported"] = ["id_token", "id_token token"]
213-
frontend = self.frontend(frontend_config)
216+
frontend = self.create_frontend(frontend_config)
214217

215218
urls = frontend.register_endpoints(["test"])
216219
assert ("^{}/{}".format("test", TokenEndpoint.url), frontend.token_endpoint) not in urls
@@ -227,7 +230,7 @@ def test_register_endpoints_token_and_userinfo_endpoint_is_not_published_if_only
227230
def test_register_endpoints_dynamic_client_registration_is_configurable(
228231
self, frontend_config, client_registration_enabled):
229232
frontend_config["provider"]["client_registration_supported"] = client_registration_enabled
230-
frontend = self.frontend(frontend_config)
233+
frontend = self.create_frontend(frontend_config)
231234

232235
urls = frontend.register_endpoints(["test"])
233236
assert (("^{}/{}".format(frontend.name, RegistrationEndpoint.url),
@@ -238,7 +241,7 @@ def test_register_endpoints_dynamic_client_registration_is_configurable(
238241
def test_token_endpoint(self, context, frontend_config, authn_req):
239242
token_lifetime = 60 * 60 * 24
240243
frontend_config["provider"]["access_token_lifetime"] = token_lifetime
241-
frontend = self.frontend(frontend_config)
244+
frontend = self.create_frontend(frontend_config)
242245

243246
user_id = "test_user"
244247
self.insert_client_in_client_db(frontend, authn_req["redirect_uri"])

0 commit comments

Comments
 (0)