Skip to content

Commit

Permalink
Fix auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasarus committed Jan 29, 2025
1 parent 2027de2 commit 43023c5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
9 changes: 6 additions & 3 deletions fia_auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ def authenticate(credentials: UserCredentials) -> User:
logger.info("Session created with UOWS")
user_id = response.json()["userId"]
uows_api_key = os.environ.get("UOWS_API_KEY", "")
details_response = requests.post(
details_response = requests.get(
f"{UOWS_URL}/v1/basic-person-details?userNumbers={user_id}",
json=data,
headers={"Authorization": f"Api-key {uows_api_key}", "Content-Type": "application/json"},
timeout=30,
)
return User(user_number=user_id, username=details_response.json()["displayName"])
if (details_response.status_code != HTTPStatus.OK or len(details_response.json()) < 1
or "displayName" not in details_response.json()[0]):
logger.warning("Unexpected error occured when authentication with the UOWS: %s", response.text)
raise UOWSError("An unexpected error occurred when authenticating with the user office web service")
return User(user_number=user_id, username=details_response.json()[0]["displayName"])
if response.status_code == HTTPStatus.UNAUTHORIZED:
logger.info("Bad credentials given to UOWS")
raise BadCredentialsError("Invalid user credentials provided to authenticate with the user office web service.")
Expand Down
12 changes: 8 additions & 4 deletions test/e2e/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
@patch("fia_auth.model.is_instrument_scientist")
@patch("fia_auth.auth.requests")
def test_successful_login(mock_auth_requests, is_instrument_scientist):
mock_auth_response = Mock()
mock_auth_response.status_code = HTTPStatus.CREATED
mock_auth_response.json.return_value = {"userId": 1234, "displayName": "Mr Cool"}
mock_auth_requests.post.return_value = mock_auth_response
mock_post_auth_response = Mock()
mock_post_auth_response.status_code = HTTPStatus.CREATED
mock_post_auth_response.json.return_value = {"userId": 1234, "displayName": "Mr Cool"}
mock_get_auth_response = Mock()
mock_get_auth_response.status_code = HTTPStatus.OK
mock_get_auth_response.json.return_value = [{"displayName": "Mr Cool"}]
mock_auth_requests.post.return_value = mock_post_auth_response
mock_auth_requests.get.return_value = mock_get_auth_response
is_instrument_scientist.return_value = False

response = client.post("/api/jwt/authenticate", json={"username": "foo", "password": "foo"})
Expand Down
4 changes: 3 additions & 1 deletion test/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
from fia_auth.model import UserCredentials


@patch("requests.get")
@patch("requests.post")
def test_authenticate_success(mock_post):
def test_authenticate_success(mock_post, mock_get):
uows_api_key = str(mock.MagicMock())
os.environ["UOWS_API_KEY"] = uows_api_key
mock_response = Mock(status_code=HTTPStatus.CREATED, json=lambda: {"userId": "12345", "displayName": "Mr Cool"})
mock_post.return_value = mock_response
mock_get.return_value = mock_response

credentials = UserCredentials(username="valid_user", password="valid_password") # noqa: S106

Expand Down

0 comments on commit 43023c5

Please sign in to comment.