Skip to content

Commit 8f27a88

Browse files
committed
Refactor FAPI advanced test to use application.api_client()
Replace custom FapiClient with FapiMtlsAuth — a standard request auth object that retrieves a certificate-bound token per request. Use application.api_client(cert=...) for mTLS calls, consistent with the rest of the testsuite. Add fapi_client_invalid fixture to encapsulate the negative test case.
1 parent 4ff4f84 commit 8f27a88

1 file changed

Lines changed: 26 additions & 30 deletions

File tree

testsuite/tests/apicast/policy/fapi/test_advanced_profile.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
"""Test Financial-Grade API policy with oauth2 certificate bound access token (advanced profile)"""
22

33
import pytest
4-
import requests
54

65
from testsuite import rawobj
76
from testsuite.utils import blame, warn_and_skip
87
from testsuite.rhsso import OIDCClientAuth, OIDCClientAuthHook
98
from testsuite.certificates import Certificate
109

11-
# pylint: disable=reimported, unused-import
10+
# pylint: disable=reimported,unused-import
1211
# flake8: noqa
1312
from testsuite.tests.apicast.policy.tls.conftest import (
1413
certificate,
@@ -109,29 +108,32 @@ def fapi_sso_client(rhsso_service_info, fapi_sso_client_id, mtls_client_cert, ap
109108

110109

111110
# pylint: disable=too-few-public-methods
112-
class FapiClient:
113-
"""Temporary class for sending requests to apicast. This should be replaced by using fixture api_client"""
111+
class FapiMtlsAuth:
112+
"""Auth class for FAPI mTLS client credentials token retrieval"""
114113

115-
def __init__(self, base_url, verify):
116-
self.base_url = base_url
117-
self.http_session = requests.Session()
118-
self.http_session.verify = verify
114+
def __init__(self, fapi_sso_client):
115+
self._fapi_sso_client = fapi_sso_client
119116

120-
def get(self, path="/", headers=None, token=None, cert=None):
121-
"""Sends http get request to url which was set up during initialization"""
122-
if headers is None:
123-
headers = {}
124-
if token:
125-
headers.update({"authorization": "Bearer " + token})
126-
return self.http_session.get(self.base_url + path, headers=headers, cert=cert, verify=False)
117+
def __call__(self, request):
118+
token = self._fapi_sso_client.oidc_client.token(grant_type="client_credentials")["access_token"]
119+
request.headers.update({"Authorization": "Bearer " + token})
120+
return request
127121

128122

129123
@pytest.fixture()
130-
def fapi_client(application):
131-
"""Create client for api_calls on apicast"""
132-
# pylint: disable=protected-access
133-
api_base_url = application.api_client()._base_url
134-
return FapiClient(api_base_url, verify=False)
124+
def fapi_client(application, fapi_sso_client, mtls_client_cert):
125+
"""Create client for api_calls on apicast using valid mTLS certificate"""
126+
client = application.api_client(cert=mtls_client_cert)
127+
client.auth = FapiMtlsAuth(fapi_sso_client)
128+
return client
129+
130+
131+
@pytest.fixture()
132+
def fapi_client_invalid(application, fapi_sso_client, unknown_cert):
133+
"""Create client for api_calls on apicast using unknown certificate"""
134+
client = application.api_client(cert=unknown_cert)
135+
client.auth = FapiMtlsAuth(fapi_sso_client)
136+
return client
135137

136138

137139
@pytest.fixture(scope="module", autouse=True)
@@ -140,29 +142,23 @@ def rhsso_setup(lifecycle_hooks, rhsso_service_info):
140142
lifecycle_hooks.append(OIDCClientAuthHook(rhsso_service_info))
141143

142144

143-
def test_valid_cert_returns_200(fapi_client, fapi_sso_client, mtls_client_cert):
145+
def test_valid_cert_returns_200(fapi_client):
144146
"""
145147
Test client authentication using certificate bound access token (https://datatracker.ietf.org/doc/html/rfc8705).
146148
147149
Obtain client certificate bound access token.
148150
Using the same certificate and obtained token send request to staging apicast.
149151
Assert, that response contains http return code 200
150152
"""
151-
mtls_client = fapi_sso_client.oidc_client
152-
api_access_token = mtls_client.token(grant_type="client_credentials")["access_token"]
153-
response = fapi_client.get(token=api_access_token, cert=mtls_client_cert)
154-
assert response.status_code == 200
153+
assert fapi_client.get("/").status_code == 200
155154

156155

157-
def test_invalid_cert_returns_401(fapi_client, fapi_sso_client, unknown_cert):
156+
def test_invalid_cert_returns_401(fapi_client_invalid):
158157
"""
159158
Test client authentication using certificate bound access token (https://datatracker.ietf.org/doc/html/rfc8705).
160159
161160
Obtain client certificate bound access token.
162161
Using the different certificate and obtained token send request to staging apicast.
163162
Assert, that response contains http return code 401
164163
"""
165-
mtls_client = fapi_sso_client.oidc_client
166-
api_access_token = mtls_client.token(grant_type="client_credentials")["access_token"]
167-
response = fapi_client.get(token=api_access_token, cert=unknown_cert)
168-
assert response.status_code == 401
164+
assert fapi_client_invalid.get("/").status_code == 401

0 commit comments

Comments
 (0)