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

feat: update get_me to retrieve from kc #108

Merged
merged 2 commits into from
Mar 1, 2024
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
5 changes: 1 addition & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/routers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
from regtech_api_commons.api import Router
from entities.models import UserProfile

from regtech_api_commons.models.auth import AuthenticatedUser
from regtech_api_commons.oauth2.oauth2_admin import OAuth2Admin
from regtech_api_commons.models import RegTechUser
from regtech_api_commons.oauth2 import OAuth2Admin
from config import kc_settings

router = Router()

oauth2_admin = OAuth2Admin(kc_settings)


@router.get("/me/", response_model=AuthenticatedUser)
@router.get("/me/", response_model=RegTechUser)
@requires("authenticated")
def get_me(request: Request):
return request.user
return oauth2_admin.get_user(request.user.id)


@router.put("/me/", status_code=HTTPStatus.ACCEPTED, dependencies=[Depends(check_domain)])
Expand Down
3 changes: 1 addition & 2 deletions tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fastapi import FastAPI
from pytest_mock import MockerFixture
from starlette.authentication import AuthCredentials, UnauthenticatedUser
from regtech_api_commons.models.auth import AuthenticatedUser
from regtech_api_commons.models import AuthenticatedUser
from entities.models import (
FinancialInstitutionDao,
FinancialInstitutionDomainDao,
Expand Down Expand Up @@ -37,7 +37,6 @@ def auth_mock(mocker: MockerFixture) -> Mock:
@pytest.fixture
def authed_user_mock(auth_mock: Mock) -> Mock:
claims = {
"id": "test_user_id",
"name": "test",
"preferred_username": "test_user",
"email": "[email protected]",
Expand Down
9 changes: 7 additions & 2 deletions tests/api/routers/test_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pytest_mock import MockerFixture
from starlette.authentication import AuthCredentials

from regtech_api_commons.models.auth import AuthenticatedUser
from regtech_api_commons.models import RegTechUser, AuthenticatedUser


class TestAdminApi:
Expand All @@ -15,13 +15,16 @@ def test_get_me_unauthed(self, app_fixture: FastAPI, unauthed_user_mock: Mock):
assert res.status_code == 403

def test_get_me_authed(self, mocker: MockerFixture, app_fixture: FastAPI, authed_user_mock: Mock):
get_user_mock = mocker.patch("regtech_api_commons.oauth2.OAuth2Admin.get_user")
get_user_mock.return_value = authed_user_mock.return_value[1]
client = TestClient(app_fixture)
res = client.get("/v1/admin/me")
get_user_mock.assert_called_once_with("testuser123")
assert res.status_code == 200
assert res.json().get("name") == "test"
assert res.json().get("institutions") == []

def test_get_me_authed_with_institutions(self, app_fixture: FastAPI, auth_mock: Mock):
def test_get_me_authed_with_institutions(self, mocker: MockerFixture, app_fixture: FastAPI, auth_mock: Mock):
claims = {
"name": "test",
"preferred_username": "test_user",
Expand All @@ -33,6 +36,8 @@ def test_get_me_authed_with_institutions(self, app_fixture: FastAPI, auth_mock:
AuthCredentials(["authenticated"]),
AuthenticatedUser.from_claim(claims),
)
get_user_mock = mocker.patch("regtech_api_commons.oauth2.OAuth2Admin.get_user")
get_user_mock.return_value = RegTechUser.from_claim(claims)
client = TestClient(app_fixture)
res = client.get("/v1/admin/me")
assert res.status_code == 200
Expand Down
Loading