From 0221a29bcb7cd8042a52d1cc86c052833343fd03 Mon Sep 17 00:00:00 2001 From: jmcguffee Date: Wed, 15 May 2024 16:40:59 -0400 Subject: [PATCH] Added get user to update and associate apis and updated tests --- .../routers/admin.py | 7 ++- tests/api/routers/test_admin_api.py | 56 +++++++++++++++++-- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/regtech_user_fi_management/routers/admin.py b/src/regtech_user_fi_management/routers/admin.py index bc663c5..bdb5787 100644 --- a/src/regtech_user_fi_management/routers/admin.py +++ b/src/regtech_user_fi_management/routers/admin.py @@ -1,4 +1,3 @@ -from http import HTTPStatus from typing import Set from fastapi import Depends, Request from starlette.authentication import requires @@ -22,15 +21,17 @@ def get_me(request: Request): return oauth2_admin.get_user(request.user.id) -@router.put("/me/", status_code=HTTPStatus.ACCEPTED, dependencies=[Depends(check_domain)]) +@router.put("/me/", response_model=RegTechUser, dependencies=[Depends(check_domain)]) @requires("manage-account") def update_me(request: Request, user: UserProfile): oauth2_admin.update_user(request.user.id, user.to_keycloak_user()) if user.leis: oauth2_admin.associate_to_leis(request.user.id, user.leis) + return oauth2_admin.get_user(request.user.id) -@router.put("/me/institutions/", status_code=HTTPStatus.ACCEPTED, dependencies=[Depends(check_domain)]) +@router.put("/me/institutions/", response_model=RegTechUser, dependencies=[Depends(check_domain)]) @requires("manage-account") def associate_lei(request: Request, leis: Set[str]): oauth2_admin.associate_to_leis(request.user.id, leis) + return oauth2_admin.get_user(request.user.id) diff --git a/tests/api/routers/test_admin_api.py b/tests/api/routers/test_admin_api.py index 8cdbe54..9b01800 100644 --- a/tests/api/routers/test_admin_api.py +++ b/tests/api/routers/test_admin_api.py @@ -63,32 +63,76 @@ def test_update_me_no_permission(self, app_fixture: FastAPI, auth_mock: Mock): res = client.put("/v1/admin/me", json={"first_name": "testFirst", "last_name": "testLast", "leis": ["testLei"]}) assert res.status_code == 403 - def test_update_me(self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock): + def test_update_me(self, mocker: MockerFixture, app_fixture: FastAPI, auth_mock: Mock): update_user_mock = mocker.patch("regtech_api_commons.oauth2.oauth2_admin.OAuth2Admin.update_user") associate_lei_mock = mocker.patch("regtech_api_commons.oauth2.oauth2_admin.OAuth2Admin.associate_to_leis") + get_user_mock = mocker.patch("regtech_api_commons.oauth2.oauth2_admin.OAuth2Admin.get_user") + claims = { + "name": "testFirst testLast", + "preferred_username": "test_user", + "email": "test@local.host", + "sub": "testuser123", + "institutions": ["testlei1", "testlei2"], + } + auth_mock.return_value = ( + AuthCredentials(["manage-account"]), + AuthenticatedUser.from_claim(claims), + ) update_user_mock.return_value = None associate_lei_mock.return_value = None + get_user_mock.return_value = auth_mock.return_value[1] client = TestClient(app_fixture) data = {"first_name": "testFirst", "last_name": "testLast", "leis": ["testLei1", "testLei2"]} res = client.put("/v1/admin/me", json=data) update_user_mock.assert_called_once_with("testuser123", {"firstName": "testFirst", "lastName": "testLast"}) associate_lei_mock.assert_called_once_with("testuser123", {"testLei1", "testLei2"}) - assert res.status_code == 202 + assert res.status_code == 200 + assert res.json().get("name") == "testFirst testLast" + assert res.json().get("institutions") == ["testlei1", "testlei2"] - def test_update_me_no_lei(self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock): + def test_update_me_no_lei(self, mocker: MockerFixture, app_fixture: FastAPI, auth_mock: Mock): update_user_mock = mocker.patch("regtech_api_commons.oauth2.oauth2_admin.OAuth2Admin.update_user") associate_lei_mock = mocker.patch("regtech_api_commons.oauth2.oauth2_admin.OAuth2Admin.associate_to_leis") + get_user_mock = mocker.patch("regtech_api_commons.oauth2.oauth2_admin.OAuth2Admin.get_user") + claims = { + "name": "testFirst testLast", + "preferred_username": "test_user", + "email": "test@local.host", + "sub": "testuser123", + } + auth_mock.return_value = ( + AuthCredentials(["manage-account"]), + AuthenticatedUser.from_claim(claims), + ) update_user_mock.return_value = None + get_user_mock.return_value = auth_mock.return_value[1] client = TestClient(app_fixture) res = client.put("/v1/admin/me", json={"first_name": "testFirst", "last_name": "testLast"}) update_user_mock.assert_called_once_with("testuser123", {"firstName": "testFirst", "lastName": "testLast"}) associate_lei_mock.assert_not_called() - assert res.status_code == 202 + assert res.status_code == 200 + assert res.json().get("name") == "testFirst testLast" + assert res.json().get("institutions") == [] - def test_associate_institutions(self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock): + def test_associate_institutions(self, mocker: MockerFixture, app_fixture: FastAPI, auth_mock: Mock): associate_lei_mock = mocker.patch("regtech_api_commons.oauth2.oauth2_admin.OAuth2Admin.associate_to_leis") + get_user_mock = mocker.patch("regtech_api_commons.oauth2.oauth2_admin.OAuth2Admin.get_user") + claims = { + "name": "test", + "preferred_username": "test_user", + "email": "test@local.host", + "sub": "testuser123", + "institutions": ["testlei1", "testlei2"], + } + auth_mock.return_value = ( + AuthCredentials(["manage-account"]), + AuthenticatedUser.from_claim(claims), + ) associate_lei_mock.return_value = None + get_user_mock.return_value = auth_mock.return_value[1] client = TestClient(app_fixture) res = client.put("/v1/admin/me/institutions", json=["testlei1", "testlei2"]) associate_lei_mock.assert_called_once_with("testuser123", {"testlei1", "testlei2"}) - assert res.status_code == 202 + assert res.status_code == 200 + assert res.json().get("name") == "test" + assert res.json().get("institutions") == ["testlei1", "testlei2"]