Skip to content

Commit

Permalink
Merge branch 'main' into feature/161_lei_check_from_commons
Browse files Browse the repository at this point in the history
  • Loading branch information
lchen-2101 committed May 17, 2024
2 parents e6c3aa4 + 280b56b commit 45fd0af
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/regtech_user_fi_management/routers/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from http import HTTPStatus
from typing import Set
from fastapi import Depends, Request
from starlette.authentication import requires
Expand All @@ -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)
56 changes: 50 additions & 6 deletions tests/api/routers/test_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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"]

0 comments on commit 45fd0af

Please sign in to comment.