Skip to content

Commit

Permalink
Pre-Release cleanup (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
djperrefort authored Aug 5, 2024
1 parent edcf475 commit 83f9089
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
12 changes: 9 additions & 3 deletions keystone_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
23 changes: 19 additions & 4 deletions tests/authentication/test_authentication_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions tests/client/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit 83f9089

Please sign in to comment.