Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions keystone_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@
"""Terminate the current user session."""

@abc.abstractmethod
def is_authenticated(self) -> dict:
def whoami(self) -> dict:
"""Return metadata for the currently authenticated user."""

def is_authenticated(self) -> bool:
"""Return a boolean indicating if the current session is authenticated."""

return bool(self.whoami())

@staticmethod
def _handle_identity_response(response: httpx.Response) -> dict:
"""Handle identity check responses, returning empty dict on 401.
Expand Down Expand Up @@ -90,7 +95,7 @@
if exception.response.status_code != 401:
raise

def is_authenticated(self, timeout: int = httpx.USE_CLIENT_DEFAULT) -> dict:
def whoami(self, timeout: int = httpx.USE_CLIENT_DEFAULT) -> dict:
"""Return metadata for the currently authenticated user.

Returns an empty dictionary if the current session is not authenticated.
Expand Down Expand Up @@ -145,7 +150,7 @@
if exception.response.status_code != 401:
raise

async def is_authenticated(self, timeout: int = httpx.USE_CLIENT_DEFAULT) -> dict:
async def whoami(self, timeout: int = httpx.USE_CLIENT_DEFAULT) -> dict:

Check warning on line 153 in keystone_client/client.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

keystone_client/client.py#L153

Method 'whoami' was expected to be 'non-async', found it instead as 'async'
"""Return metadata for the currently authenticated user.

Returns an empty dictionary if the current session is not authenticated.
Expand Down
18 changes: 9 additions & 9 deletions tests/function_tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ async def asyncTearDown(self) -> None:
async def test_login_logout(self) -> None:
"""Verify users are successfully logged in/out when providing valid credentials."""

self.assertFalse(await self.client.is_authenticated())
self.assertFalse(await self.client.whoami())

await self.client.login(API_USER, API_PASSWORD)
self.assertTrue(await self.client.is_authenticated())
self.assertTrue(await self.client.whoami())

await self.client.logout()
self.assertFalse(await self.client.is_authenticated())
self.assertFalse(await self.client.whoami())

async def test_incorrect_credentials(self) -> None:
"""Verify an error is raised when authenticating with incorrect credentials."""
Expand All @@ -41,9 +41,9 @@ async def test_incorrect_credentials(self) -> None:
async def test_logout_unauthenticated(self) -> None:
"""Verify the `logout` method exits silently when logging out an unauthenticated user."""

self.assertFalse(await self.client.is_authenticated())
self.assertFalse(await self.client.whoami())
await self.client.logout()
self.assertFalse(await self.client.is_authenticated())
self.assertFalse(await self.client.whoami())

async def test_authentication_is_not_shared(self) -> None:
"""Test user authentication is tied to specific instances."""
Expand All @@ -52,8 +52,8 @@ async def test_authentication_is_not_shared(self) -> None:
client2 = AsyncKeystoneClient(API_HOST)

await client1.login(API_USER, API_PASSWORD)
self.assertTrue(await client1.is_authenticated())
self.assertFalse(await client2.is_authenticated())
self.assertTrue(await client1.whoami())
self.assertFalse(await client2.whoami())

await client1.close()
await client2.close()
Expand All @@ -75,11 +75,11 @@ async def asyncTearDown(self) -> None:
async def test_unauthenticated_user(self) -> None:
"""Verify an empty dictionary is returned for an unauthenticated user."""

self.assertEqual(dict(), await self.client.is_authenticated())
self.assertEqual(dict(), await self.client.whoami())

async def test_authenticated_user(self) -> None:
"""Verify user metadata is returned for an authenticated user."""

await self.client.login(API_USER, API_PASSWORD)
user_meta = await self.client.is_authenticated()
user_meta = await self.client.whoami()
self.assertEqual(API_USER, user_meta['username'])
18 changes: 9 additions & 9 deletions tests/function_tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def tearDown(self) -> None:
def test_login_logout(self) -> None:
"""Verify users are successfully logged in/out when providing valid credentials."""

self.assertFalse(self.client.is_authenticated())
self.assertFalse(self.client.whoami())

self.client.login(API_USER, API_PASSWORD)
self.assertTrue(self.client.is_authenticated())
self.assertTrue(self.client.whoami())

self.client.logout()
self.assertFalse(self.client.is_authenticated())
self.assertFalse(self.client.whoami())

def test_incorrect_credentials(self) -> None:
"""Verify an error is raised when authenticating with incorrect credentials."""
Expand All @@ -41,9 +41,9 @@ def test_incorrect_credentials(self) -> None:
def test_logout_unauthenticated(self) -> None:
"""Verify the `logout` method exits silently when logging out an unauthenticated user."""

self.assertFalse(self.client.is_authenticated())
self.assertFalse(self.client.whoami())
self.client.logout()
self.assertFalse(self.client.is_authenticated())
self.assertFalse(self.client.whoami())

def test_authentication_is_not_shared(self) -> None:
"""Test user authentication is tied to specific instances."""
Expand All @@ -52,8 +52,8 @@ def test_authentication_is_not_shared(self) -> None:
client2 = KeystoneClient(API_HOST)

client1.login(API_USER, API_PASSWORD)
self.assertTrue(client1.is_authenticated())
self.assertFalse(client2.is_authenticated())
self.assertTrue(client1.whoami())
self.assertFalse(client2.whoami())

client1.close()
client2.close()
Expand All @@ -75,11 +75,11 @@ def tearDown(self) -> None:
def test_unauthenticated_user(self) -> None:
"""Verify an empty dictionary is returned for an unauthenticated user."""

self.assertEqual(dict(), self.client.is_authenticated())
self.assertEqual(dict(), self.client.whoami())

def test_authenticated_user(self) -> None:
"""Verify user metadata is returned for an authenticated user."""

self.client.login(API_USER, API_PASSWORD)
user_meta = self.client.is_authenticated()
user_meta = self.client.whoami()
self.assertEqual(API_USER, user_meta['username'])
6 changes: 3 additions & 3 deletions tests/unit_tests/test_client/test_AsyncKeystoneClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, json=expected_data)

client = AsyncKeystoneClient(base_url=self.api_url, transport=httpx.MockTransport(handler))
result = await client.is_authenticated()
result = await client.whoami()
self.assertEqual(result, expected_data)

async def test_unauthenticated_response(self) -> None:
Expand All @@ -122,7 +122,7 @@ def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(401, json={"detail": "Unauthorized"})

client = AsyncKeystoneClient(base_url=self.api_url, transport=httpx.MockTransport(handler))
result = await client.is_authenticated()
result = await client.whoami()
self.assertEqual(result, {})

async def test_http_error_is_raised(self) -> None:
Expand All @@ -136,4 +136,4 @@ def handler(request: httpx.Request) -> httpx.Response:
client = AsyncKeystoneClient(base_url=self.api_url, transport=httpx.MockTransport(handler))

with self.assertRaises(httpx.HTTPStatusError):
await client.is_authenticated()
await client.whoami()
6 changes: 3 additions & 3 deletions tests/unit_tests/test_client/test_KeystoneClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, json=expected_data)

client = KeystoneClient(base_url=self.api_url, transport=httpx.MockTransport(handler))
result = client.is_authenticated()
result = client.whoami()
self.assertEqual(result, expected_data)

def test_unauthenticated_response(self) -> None:
Expand All @@ -122,7 +122,7 @@ def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(401, json={"detail": "Unauthorized"})

client = KeystoneClient(base_url=self.api_url, transport=httpx.MockTransport(handler))
result = client.is_authenticated()
result = client.whoami()
self.assertEqual(result, {})

def test_http_error_is_raised(self) -> None:
Expand All @@ -136,4 +136,4 @@ def handler(request: httpx.Request) -> httpx.Response:
client = KeystoneClient(base_url=self.api_url, transport=httpx.MockTransport(handler))

with self.assertRaises(httpx.HTTPStatusError):
client.is_authenticated()
client.whoami()
Loading