diff --git a/keystone_client/client.py b/keystone_client/client.py index 84802e3..c282d7e 100644 --- a/keystone_client/client.py +++ b/keystone_client/client.py @@ -29,9 +29,14 @@ def logout(self) -> None: """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. @@ -90,7 +95,7 @@ def logout(self, timeout: int = httpx.USE_CLIENT_DEFAULT) -> None: 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. @@ -145,7 +150,7 @@ async def logout(self, timeout: int = httpx.USE_CLIENT_DEFAULT) -> None: 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: """Return metadata for the currently authenticated user. Returns an empty dictionary if the current session is not authenticated. diff --git a/tests/function_tests/test_async.py b/tests/function_tests/test_async.py index 2efcb24..948cbd8 100644 --- a/tests/function_tests/test_async.py +++ b/tests/function_tests/test_async.py @@ -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.""" @@ -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.""" @@ -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() @@ -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']) diff --git a/tests/function_tests/test_sync.py b/tests/function_tests/test_sync.py index 9502566..1fdd223 100644 --- a/tests/function_tests/test_sync.py +++ b/tests/function_tests/test_sync.py @@ -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.""" @@ -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.""" @@ -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() @@ -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']) diff --git a/tests/unit_tests/test_client/test_AsyncKeystoneClient.py b/tests/unit_tests/test_client/test_AsyncKeystoneClient.py index a56a419..067ce33 100644 --- a/tests/unit_tests/test_client/test_AsyncKeystoneClient.py +++ b/tests/unit_tests/test_client/test_AsyncKeystoneClient.py @@ -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: @@ -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: @@ -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() diff --git a/tests/unit_tests/test_client/test_KeystoneClient.py b/tests/unit_tests/test_client/test_KeystoneClient.py index 649c312..323d07a 100644 --- a/tests/unit_tests/test_client/test_KeystoneClient.py +++ b/tests/unit_tests/test_client/test_KeystoneClient.py @@ -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: @@ -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: @@ -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()