Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Throws a 404 when a user lookup fails. (#61)
Browse files Browse the repository at this point in the history
Relates to #53
  • Loading branch information
stratosgear authored Jul 23, 2022
1 parent 35a3f60 commit ed9b39f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fastapi_keycloak/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
UpdatePasswordException,
UpdateProfileException,
UpdateUserLocaleException,
UserNotFound,
VerifyEmailException,
)
from fastapi_keycloak.model import (
Expand Down Expand Up @@ -844,6 +845,11 @@ def get_user(self, user_id: str = None, query: str = "") -> KeycloakUser:
response = self._admin_request(
url=f"{self.users_uri}/{user_id}", method=HTTPMethod.GET
)
if response.status_code == status.HTTP_404_NOT_FOUND:
raise UserNotFound(
status_code = status.HTTP_404_NOT_FOUND,
reason=f"User with user_id[{user_id}] was not found"
)
return KeycloakUser(**response.json())

@result_or_error(response_model=KeycloakUser)
Expand Down
11 changes: 11 additions & 0 deletions fastapi_keycloak/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ def __init__(self, status_code: int, reason: str):
self.reason = reason
super().__init__(f"HTTP {status_code}: {reason}")

class UserNotFound(Exception):
"""Thrown when a user lookup fails.
Attributes:
status_code (int): The status code of the response received
reason (str): The reason why the requests did fail
"""
def __init__(self, status_code: int, reason: str):
self.status_code = status_code
self.reason = reason
super().__init__(f"HTTP {status_code}: {reason}")

class MandatoryActionException(HTTPException):
"""Throw if the exchange of username and password for an access token fails"""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
UpdatePasswordException,
UpdateProfileException,
UpdateUserLocaleException,
UserNotFound,
VerifyEmailException,
)
from fastapi_keycloak.model import (
Expand Down Expand Up @@ -445,3 +446,10 @@ def test_login_exceptions(self, idp, action, exception, user):

# Clean up
idp.delete_user(user_id=user.id)

def test_user_not_found_exception(self, idp):

with pytest.raises(
UserNotFound
): # Expect the get to fail due to anon existant user
u = idp.get_user(user_id="some_non_existant_user_id")

0 comments on commit ed9b39f

Please sign in to comment.