From 0db90716e64acbebcf2b015829e1f51a00bc99b2 Mon Sep 17 00:00:00 2001 From: jmcguffee Date: Thu, 2 Nov 2023 16:19:02 -0400 Subject: [PATCH 1/2] Added institution check and test Added a check for when no institutions are present. Added corresponding test. --- src/routers/institutions.py | 2 ++ tests/api/routers/test_institutions_api.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/routers/institutions.py b/src/routers/institutions.py index 78b5d5b..2ef1297 100644 --- a/src/routers/institutions.py +++ b/src/routers/institutions.py @@ -53,6 +53,8 @@ async def create_institution( async def get_associated_institutions(request: Request): user: AuthenticatedUser = request.user email_domain = get_email_domain(user.email) + if not user.institutions: + return [] associated_institutions = await repo.get_institutions(request.state.db_session, user.institutions) return [ FinanicialInstitutionAssociationDto( diff --git a/tests/api/routers/test_institutions_api.py b/tests/api/routers/test_institutions_api.py index 3af621f..3fe1bcd 100644 --- a/tests/api/routers/test_institutions_api.py +++ b/tests/api/routers/test_institutions_api.py @@ -172,3 +172,24 @@ def test_get_associated_institutions( inst2 = next(filter(lambda inst: inst["lei"] == "TESTBANK234", data)) assert inst1["approved"] is False assert inst2["approved"] is True + + def test_get_associated_institutions_with_no_institutions( + self, app_fixture: FastAPI, auth_mock: Mock, get_institutions_mock: Mock + ): + get_institutions_mock.return_value = None + claims = { + "name": "test", + "preferred_username": "test_user", + "email": "test@test234.bank", + "sub": "testuser123", + "institutions": [], + } + auth_mock.return_value = ( + AuthCredentials(["authenticated"]), + AuthenticatedUser.from_claim(claims), + ) + client = TestClient(app_fixture) + res = client.get("/v1/institutions/associated") + assert res.status_code == 200 + get_institutions_mock.assert_not_called() + assert res.json() == [] From 3cfdeed1f7ed83ddd47e765a92464d28cc2d3839 Mon Sep 17 00:00:00 2001 From: jmcguffee Date: Mon, 6 Nov 2023 16:00:34 -0500 Subject: [PATCH 2/2] Added check to institutions repo --- src/entities/repos/institutions_repo.py | 2 +- src/routers/institutions.py | 2 -- tests/api/routers/test_institutions_api.py | 4 ++-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/entities/repos/institutions_repo.py b/src/entities/repos/institutions_repo.py index b4820e5..8605ce6 100644 --- a/src/entities/repos/institutions_repo.py +++ b/src/entities/repos/institutions_repo.py @@ -27,7 +27,7 @@ async def get_institutions( .limit(count) .offset(page * count) ) - if leis: + if leis is not None: stmt = stmt.filter(FinancialInstitutionDao.lei.in_(leis)) elif d := domain.strip(): search = "%{}%".format(d) diff --git a/src/routers/institutions.py b/src/routers/institutions.py index 2ef1297..78b5d5b 100644 --- a/src/routers/institutions.py +++ b/src/routers/institutions.py @@ -53,8 +53,6 @@ async def create_institution( async def get_associated_institutions(request: Request): user: AuthenticatedUser = request.user email_domain = get_email_domain(user.email) - if not user.institutions: - return [] associated_institutions = await repo.get_institutions(request.state.db_session, user.institutions) return [ FinanicialInstitutionAssociationDto( diff --git a/tests/api/routers/test_institutions_api.py b/tests/api/routers/test_institutions_api.py index 3fe1bcd..dd6d706 100644 --- a/tests/api/routers/test_institutions_api.py +++ b/tests/api/routers/test_institutions_api.py @@ -176,7 +176,7 @@ def test_get_associated_institutions( def test_get_associated_institutions_with_no_institutions( self, app_fixture: FastAPI, auth_mock: Mock, get_institutions_mock: Mock ): - get_institutions_mock.return_value = None + get_institutions_mock.return_value = [] claims = { "name": "test", "preferred_username": "test_user", @@ -191,5 +191,5 @@ def test_get_associated_institutions_with_no_institutions( client = TestClient(app_fixture) res = client.get("/v1/institutions/associated") assert res.status_code == 200 - get_institutions_mock.assert_not_called() + get_institutions_mock.assert_called_once_with(ANY, []) assert res.json() == []