diff --git a/src/entities/models/dto.py b/src/entities/models/dto.py index f06fce3..9c9eff3 100644 --- a/src/entities/models/dto.py +++ b/src/entities/models/dto.py @@ -1,5 +1,6 @@ -from typing import List, Dict, Any, Set -from pydantic import BaseModel +from typing import List, Dict, Any, Set, Optional +from pydantic import BaseModel, Field +from pydantic.utils import to_camel from starlette.authentication import BaseUser @@ -41,12 +42,13 @@ class Config: class UserProfile(BaseModel): - firstName: str - lastName: str - leis: Set[str] = None + first_name: str = Field(alias='firstName') + last_name: str = Field(alias='lastName') + leis: Optional[Set[str]] = Field(alias='leis') - def get_user(self) -> Dict[str, Any]: - return {"firstName": self.firstName, "lastName": self.lastName} + class Config: + alias_generator = to_camel + # populate_by_name = True class AuthenticatedUser(BaseUser, BaseModel): diff --git a/src/oauth2/oauth2_admin.py b/src/oauth2/oauth2_admin.py index e02faf7..1f53072 100644 --- a/src/oauth2/oauth2_admin.py +++ b/src/oauth2/oauth2_admin.py @@ -88,7 +88,7 @@ def associate_to_lei(self, user_id: str, lei: str) -> None: detail="No institution found for given LEI", ) - def associate_to_lei_set(self, user_id, leis: Set[str]): + def associate_to_leis(self, user_id, leis: Set[str]): for lei in leis: self.associate_to_lei(user_id, lei) diff --git a/src/routers/admin.py b/src/routers/admin.py index 9d2163c..e64a246 100644 --- a/src/routers/admin.py +++ b/src/routers/admin.py @@ -21,12 +21,12 @@ async def get_me(request: Request): @router.put("/me/", status_code=HTTPStatus.ACCEPTED, dependencies=[Depends(check_domain)]) @requires("manage-account") async def update_me(request: Request, user: UserProfile): - oauth2_admin.update_user(request.user.id, user.get_user()) + oauth2_admin.update_user(request.user.id, user.dict(by_alias=True, exclude={'leis'})) if user.leis: - oauth2_admin.associate_to_lei_set(request.user.id, user.leis) + oauth2_admin.associate_to_leis(request.user.id, user.leis) @router.put("/me/institutions/", status_code=HTTPStatus.ACCEPTED, dependencies=[Depends(check_domain)]) @requires("manage-account") async def associate_lei(request: Request, leis: Set[str]): - oauth2_admin.associate_to_lei_set(request.user.id, leis) + oauth2_admin.associate_to_leis(request.user.id, leis) diff --git a/tests/api/routers/test_admin_api.py b/tests/api/routers/test_admin_api.py index 675c498..9c066c8 100644 --- a/tests/api/routers/test_admin_api.py +++ b/tests/api/routers/test_admin_api.py @@ -60,7 +60,7 @@ def test_update_me_no_permission(self, app_fixture: FastAPI, auth_mock: Mock): def test_update_me(self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock): update_user_mock = mocker.patch("oauth2.oauth2_admin.OAuth2Admin.update_user") - associate_lei_mock = mocker.patch("oauth2.oauth2_admin.OAuth2Admin.associate_to_lei_set") + associate_lei_mock = mocker.patch("oauth2.oauth2_admin.OAuth2Admin.associate_to_leis") update_user_mock.return_value = None associate_lei_mock.return_value = None client = TestClient(app_fixture) @@ -80,10 +80,9 @@ def test_update_me_no_lei(self, mocker: MockerFixture, app_fixture: FastAPI, aut assert res.status_code == 202 def test_associate_institutions(self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock): - associate_lei_mock = mocker.patch("oauth2.oauth2_admin.OAuth2Admin.associate_to_lei_set") + associate_lei_mock = mocker.patch("oauth2.oauth2_admin.OAuth2Admin.associate_to_leis") associate_lei_mock.return_value = None client = TestClient(app_fixture) - data = ["testlei1", "testlei2"] - res = client.put("/v1/admin/me/institutions", json=data) + 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