From 83f9089bc2f296ae754a882fb7afd292e27814f2 Mon Sep 17 00:00:00 2001 From: Daniel Perrefort Date: Mon, 5 Aug 2024 13:34:32 -0400 Subject: [PATCH] Pre-Release cleanup (#49) --- keystone_client/client.py | 12 +++++++--- .../test_authentication_manager.py | 23 +++++++++++++++---- tests/client/test_http_client.py | 9 ++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/keystone_client/client.py b/keystone_client/client.py index 8a02d82..ee60a25 100644 --- a/keystone_client/client.py +++ b/keystone_client/client.py @@ -55,14 +55,20 @@ def login(self, username: str, password: str, timeout: int = DEFAULT_TIMEOUT) -> self._auth.login(username, password, timeout) # pragma: nocover - def logout(self, timeout: int = DEFAULT_TIMEOUT) -> None: - """Log out and blacklist any active credentials + def logout(self, raise_blacklist: bool = False, timeout: int = DEFAULT_TIMEOUT) -> None: + """Clear current credentials and blacklist any active credentials Args: + raise_blacklist: Optionally raise an exception if the blacklist request fails timeout: Seconds before the blacklist request times out """ - self._auth.logout(timeout) # pragma: nocover + try: + self._auth.logout(timeout) # pragma: nocover + + except requests.HTTPError: + if raise_blacklist: + raise def is_authenticated(self) -> bool: """Return whether the client instance has active credentials""" diff --git a/tests/authentication/test_authentication_manager.py b/tests/authentication/test_authentication_manager.py index d06f639..41030c9 100644 --- a/tests/authentication/test_authentication_manager.py +++ b/tests/authentication/test_authentication_manager.py @@ -138,6 +138,15 @@ def test_jwt_credentials_are_cached(self, mock_post: Mock) -> None: self.assertEqual(manager.jwt.access, 'fake_access_token') self.assertEqual(manager.jwt.refresh, 'fake_refresh_token') + @patch('requests.post') + def test_login_network_error(self, mock_post: Mock) -> None: + """Test network errors during login""" + + mock_post.side_effect = requests.ConnectionError() + manager = AuthenticationManager(API_HOST) + with self.assertRaises(requests.ConnectionError): + manager.login(API_USER, API_PASSWORD) + class Logout(TestCase): """Test session invalidation via the `logout` method""" @@ -156,7 +165,7 @@ def test_user_is_logged_out(self) -> None: self.assertIsNone(self.manager.jwt) @patch('requests.post') - def test_blacklist_request_sent(self, mock_post): + def test_blacklist_request_sent(self, mock_post: Mock) -> None: """Test a blacklist request is sent to the API server""" refresh_token = self.manager.jwt.refresh @@ -205,10 +214,13 @@ def test_refresh_while_not_authenticated(self) -> None: mock_post.assert_not_called() def test_refresh_with_valid_access_token(self) -> None: - """Test the refresh call exits silently when not credentials are not expired""" + """Test the refresh call exits silently when credentials are not expired""" manager = AuthenticationManager(API_HOST) - manager.login(API_USER, API_PASSWORD) + manager.jwt = create_token( + access_expires=datetime.now() + timedelta(hours=1), + refresh_expires=datetime.now() + timedelta(days=1) + ) with patch('requests.post') as mock_post: manager.refresh() @@ -218,7 +230,10 @@ def test_refresh_with_valid_access_token_force(self) -> None: """Test the refresh call refreshes valid credentials `force=True`""" manager = AuthenticationManager(API_HOST) - manager.login(API_USER, API_PASSWORD) + manager.jwt = create_token( + access_expires=datetime.now() + timedelta(hours=1), + refresh_expires=datetime.now() + timedelta(days=1) + ) refresh_token = manager.jwt.refresh with patch('requests.post') as mock_post: diff --git a/tests/client/test_http_client.py b/tests/client/test_http_client.py index f486b09..3446455 100644 --- a/tests/client/test_http_client.py +++ b/tests/client/test_http_client.py @@ -86,6 +86,15 @@ def test_http_error(self, mock_request: Mock) -> None: self.assert_http_request_called() + def test_connection_error(self, mock_request: Mock) -> None: + """Test that a connection error is raised""" + + self.mock_request = mock_request + self.mock_request.side_effect = requests.ConnectionError("Connection error") + + with self.assertRaises(requests.ConnectionError): + self.method_to_test(self.endpoint_str, **self.request_params) + class HttpGet(BaseHttpMethodTests, TestCase): """Tests for the `http_get` method"""