Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auth bug #96

Merged
merged 4 commits into from
Jan 30, 2025
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
12 changes: 9 additions & 3 deletions fia_auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ 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
39 changes: 18 additions & 21 deletions test/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from http import HTTPStatus
from unittest import mock
from unittest.mock import Mock, call, patch
from unittest.mock import Mock, patch

import pytest

Expand All @@ -11,12 +11,17 @@
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_post_response = Mock(
status_code=HTTPStatus.CREATED, json=lambda: {"userId": "12345", "displayName": "Mr Cool"}
)
mock_get_response = Mock(status_code=HTTPStatus.OK, json=lambda: [{"displayName": "Mr Cool"}])
mock_post.return_value = mock_post_response
mock_get.return_value = mock_get_response

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

Expand All @@ -25,25 +30,17 @@ def test_authenticate_success(mock_post):
assert user.user_number == "12345"
assert user.username == "Mr Cool"

assert (
call(
"https://devapi.facilities.rl.ac.uk/users-service/v1/sessions",
json={"username": "valid_user", "password": "valid_password"},
headers={"Content-Type": "application/json"},
timeout=30,
)
in mock_post.mock_calls
mock_post.assert_called_once_with(
"https://devapi.facilities.rl.ac.uk/users-service/v1/sessions",
json={"username": "valid_user", "password": "valid_password"},
headers={"Content-Type": "application/json"},
timeout=30,
)
assert (
call(
"https://devapi.facilities.rl.ac.uk/users-service/v1/basic-person-details?userNumbers=12345",
json={"username": "valid_user", "password": "valid_password"},
headers={"Authorization": f"Api-key {uows_api_key}", "Content-Type": "application/json"},
timeout=30,
)
in mock_post.mock_calls
mock_get.assert_called_once_with(
"https://devapi.facilities.rl.ac.uk/users-service/v1/basic-person-details?userNumbers=12345",
headers={"Authorization": f"Api-key {uows_api_key}", "Content-Type": "application/json"},
timeout=30,
)
assert mock_post.call_count == 2 # noqa: PLR2004


@patch("requests.post")
Expand Down