From d9f9b6da0202cabd24ee26964842a974a0e29537 Mon Sep 17 00:00:00 2001 From: Denini Gabriel Date: Wed, 22 Apr 2026 13:42:58 +0000 Subject: [PATCH] test: improve oauth2 session coverage and make state generation deterministic --- test/oauth2_session_test.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/test/oauth2_session_test.py b/test/oauth2_session_test.py index c1d98b9..3078cdc 100644 --- a/test/oauth2_session_test.py +++ b/test/oauth2_session_test.py @@ -31,10 +31,14 @@ def test_api_endpoint_url(self): f'https://{TEST_DOMAIN}/api/v4/users/' ) + @mock.patch('bynder_sdk.oauth2.random.choice', return_value='a') @mock.patch('requests_oauthlib.OAuth2Session.authorization_url') - def test_authorization_url(self, mocked_func): + def test_authorization_url(self, mocked_auth_url, mocked_choice): self.session.authorization_url() - assert mocked_func.call_count == 1 + mocked_auth_url.assert_called_once_with( + oauth2_url(TEST_DOMAIN, 'auth'), + state='aaaaaaaa' + ) @mock.patch('requests_oauthlib.OAuth2Session.fetch_token') def test_fetch_token(self, mocked_func): @@ -49,3 +53,28 @@ def test_fetch_token(self, mocked_func): def test_user_agent_header(self): # The UA header is contained within the session headers assert UA_HEADER.items() <= self.session.headers.items() + + def test_oauth2_url_auth_endpoint(self): + self.assertEqual( + oauth2_url(TEST_DOMAIN, 'auth'), + f'https://{TEST_DOMAIN}/v6/authentication/oauth2/auth', + ) + + def test_auto_refresh_url_set_on_init(self): + self.assertEqual( + self.session.auto_refresh_url, + oauth2_url(TEST_DOMAIN, 'token'), + ) + + @mock.patch('bynder_sdk.util.SessionMixin.post') + def test_post_https_url_withholds_token(self, mocked_post): + self.session.post('https://example.com/api') + mocked_post.assert_called_once_with( + 'https://example.com/api', + withhold_token=True, + ) + + @mock.patch('bynder_sdk.util.SessionMixin.post') + def test_post_http_url_does_not_withhold_token(self, mocked_post): + self.session.post('http://example.com/api') + mocked_post.assert_called_once_with('http://example.com/api')