From 4277c0061137860d1145f88547d85e7f9bf2fe43 Mon Sep 17 00:00:00 2001 From: Guillaume Pujol Date: Wed, 29 Nov 2023 11:58:30 +0100 Subject: [PATCH] add `ApiClient.auth` to access the currently configured Auth Scheme for an `ApiClient` --- requests_oauth2client/api_client.py | 4 ++-- tests/unit_tests/test_flask.py | 5 ++--- tests/unit_tests/vendor_specific/test_auth0.py | 14 +++++--------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/requests_oauth2client/api_client.py b/requests_oauth2client/api_client.py index f5d250f..e2df261 100644 --- a/requests_oauth2client/api_client.py +++ b/requests_oauth2client/api_client.py @@ -76,7 +76,7 @@ def __init__( self.timeout = timeout self.session = session or requests.Session() - self.session.auth = auth + self.auth = auth for key, val in kwargs.items(): setattr(self.session, key, val) @@ -194,7 +194,7 @@ def request( # noqa: C901 headers=headers, cookies=cookies, files=files, - auth=auth, + auth=auth or self.auth, timeout=timeout, allow_redirects=allow_redirects, proxies=proxies, diff --git a/tests/unit_tests/test_flask.py b/tests/unit_tests/test_flask.py index 5dc66f0..214c568 100644 --- a/tests/unit_tests/test_flask.py +++ b/tests/unit_tests/test_flask.py @@ -24,7 +24,6 @@ def test_flask( from requests_oauth2client.flask import FlaskOAuth2ClientCredentialsAuth except ImportError: pytest.skip("Flask is not available") - return oauth_client = OAuth2Client(token_endpoint, ClientSecretPost(client_id, client_secret)) auth = FlaskOAuth2ClientCredentialsAuth( @@ -34,7 +33,7 @@ def test_flask( ) api_client = ApiClient(target_api, auth=auth) - assert isinstance(api_client.session.auth, FlaskOAuth2ClientCredentialsAuth) + assert isinstance(api_client.auth, FlaskOAuth2ClientCredentialsAuth) app = Flask("testapp") app.config["TESTING"] = True @@ -57,7 +56,7 @@ def get() -> Any: assert resp.json == json_resp resp = client.get("/api?call=2") assert resp.json == json_resp - api_client.session.auth.forget_token() + api_client.auth.forget_token() # assert api_client.session.auth.token is None with client.session_transaction() as sess: sess.pop(auth.session_key) diff --git a/tests/unit_tests/vendor_specific/test_auth0.py b/tests/unit_tests/vendor_specific/test_auth0.py index 7723a11..665521c 100644 --- a/tests/unit_tests/vendor_specific/test_auth0.py +++ b/tests/unit_tests/vendor_specific/test_auth0.py @@ -6,15 +6,11 @@ def test_auth0_management() -> None: auth0api = Auth0ManagementApiClient("test.eu.auth0.com", ("client_id", "client_secret")) - assert auth0api.session.auth is not None - assert isinstance(auth0api.session.auth, OAuth2ClientCredentialsAuth) - assert auth0api.session.auth.client is not None - assert ( - auth0api.session.auth.client.token_endpoint == "https://test.eu.auth0.com/oauth/token" - ) - assert auth0api.session.auth.token_kwargs == { - "audience": "https://test.eu.auth0.com/api/v2/" - } + assert auth0api.auth is not None + assert isinstance(auth0api.auth, OAuth2ClientCredentialsAuth) + assert auth0api.auth.client is not None + assert auth0api.auth.client.token_endpoint == "https://test.eu.auth0.com/oauth/token" + assert auth0api.auth.token_kwargs == {"audience": "https://test.eu.auth0.com/api/v2/"} def test_auth0_client() -> None: