From 294baadcb71ef5c409df297b57df4af1f265abd4 Mon Sep 17 00:00:00 2001 From: luxferre Date: Wed, 10 Jun 2026 13:48:59 +0100 Subject: [PATCH] feat: more ids returned on endpoints Issue: #23 --- src/iam/router.py | 16 +++++++++++++--- src/iam/schemas.py | 12 ++++++++++-- src/organisation/router.py | 12 ++++++++++-- src/organisation/schemas.py | 23 +++++++++++++++-------- src/schemas.py | 15 +++++++++++++++ test/test_iam.py | 12 ++++++++++-- test/test_organisation.py | 19 +++++++++++++++++-- 7 files changed, 90 insertions(+), 19 deletions(-) diff --git a/src/iam/router.py b/src/iam/router.py index df4bc95..07bd960 100644 --- a/src/iam/router.py +++ b/src/iam/router.py @@ -21,6 +21,7 @@ from sqlalchemy.exc import IntegrityError from src.iam.exceptions import GroupNotFoundException from src.organisation.exceptions import OrgNotFoundException +from src.schemas import GroupSummary, OrgSummary from src.service.exceptions import ServiceNotFoundException from src.exceptions import ConflictException from src.database import db_dependency @@ -127,7 +128,11 @@ async def get_group_permissions( ): if group_model.org_id != org_model.id: raise UnauthorizedException("Group does not belong to this organization") - return {"permissions": group_model.permission_rel} + return { + "organisation": org_model, + "group": group_model, + "permissions": group_model.permission_rel, + } @router.get("/group/users", response_model=IAMGetGroupUsersResponse) @@ -137,7 +142,11 @@ async def get_group_users( ): if group_model.org_id != org_model.id: raise UnauthorizedException("Group does not belong to this organization") - return {"users": group_model.user_rel} + return { + "organisation": org_model, + "group": group_model, + "users": group_model.user_rel, + } @router.post("/group", response_model=IAMPostGroupResponse) @@ -180,7 +189,8 @@ async def add_group_permission( db.flush() response = IAMPutGroupPermissionResponse( - group=GroupSchema(**group_model.__dict__), + organisation=OrgSummary(**org_model.__dict__), + group=GroupSummary(**group_model.__dict__), permissions=group_model.permission_rel, ) db.commit() diff --git a/src/iam/schemas.py b/src/iam/schemas.py index 8ab00e4..66e3954 100644 --- a/src/iam/schemas.py +++ b/src/iam/schemas.py @@ -18,6 +18,9 @@ from src.schemas import ( UserIDMixin, PermIDMixin, GroupIDMixin, + GroupSummary, + OrgSummary, + UserSummary, ) @@ -50,11 +53,15 @@ class IAMCAoRRequest(CustomBaseModel): class IAMGetGroupPermissionsResponse(CustomBaseModel): + organisation: OrgSummary + group: GroupSummary permissions: list[PermissionSchema] class IAMGetGroupUsersResponse(CustomBaseModel): - users: list[UserSchema] + organisation: OrgSummary + group: GroupSummary + users: list[UserSummary] class IAMPostGroupRequest(OrgIDMixin): @@ -70,7 +77,8 @@ class IAMPutGroupPermissionRequest(GroupIDMixin, PermIDMixin, OrgIDMixin): class IAMPutGroupPermissionResponse(CustomBaseModel): - group: GroupSchema + organisation: OrgSummary + group: GroupSummary permissions: list[PermissionSchema] diff --git a/src/organisation/router.py b/src/organisation/router.py index 96c59e1..5c862ea 100644 --- a/src/organisation/router.py +++ b/src/organisation/router.py @@ -314,7 +314,10 @@ async def add_user_to_org( raise ConflictException(message="User already a part of this organisation") org_model.user_rel.append(user_model) db.flush() - response = {"users": [user.email for user in org_model.user_rel]} + response = { + "organisation": org_model, + "users": [{"id": user.id, "email": user.email} for user in org_model.user_rel], + } db.commit() return response @@ -437,7 +440,12 @@ async def get_org_groups(org_model: org_model_root_claim_query_dependency): """ Returns a list of the names of all IAM groups created by the organisation. """ - return {"groups": [group.name for group in org_model.group_rel]} + return { + "organisation": org_model, + "groups": [ + {"id": group.id, "name": group.name} for group in org_model.group_rel + ], + } @router.delete( diff --git a/src/organisation/schemas.py b/src/organisation/schemas.py index 01085c5..b4e9f23 100644 --- a/src/organisation/schemas.py +++ b/src/organisation/schemas.py @@ -10,7 +10,14 @@ from typing import Optional from pydantic import EmailStr, ConfigDict -from src.schemas import CustomBaseModel, OrgIDMixin, UserIDMixin +from src.schemas import ( + CustomBaseModel, + OrgIDMixin, + UserIDMixin, + GroupSummary, + OrgSummary, + UserSummary, +) from src.contact.schemas import ContactModel from src.organisation.constants import Status, ContactType @@ -22,11 +29,6 @@ class Questionnaire(CustomBaseModel): question_three: Optional[str] = None -class OrgSummary(CustomBaseModel): - id: int - name: str - - class ContactSummary(CustomBaseModel): id: int email: Optional[EmailStr] = None @@ -49,6 +51,7 @@ class OrgPostOrgRequest(CustomBaseModel): class OrgPostOrgResponse(CustomBaseModel): + id: int name: str status: Status @@ -59,6 +62,7 @@ class OrgPatchQuestionnaireRequest(OrgIDMixin): class OrgPatchQuestionnaireResponse(CustomBaseModel): + id: int name: str intake_questionnaire: Questionnaire status: Status @@ -69,6 +73,7 @@ class OrgPatchStatusRequest(OrgIDMixin): class OrgPatchStatusResponse(CustomBaseModel): + id: int name: str status: Status @@ -95,7 +100,8 @@ class OrgPostUserRequest(OrgIDMixin, UserIDMixin): class OrgPostUserResponse(CustomBaseModel): - users: list[str] + organisation: OrgSummary + users: list[UserSummary] class OrgPatchRootRequest(OrgIDMixin, UserIDMixin): @@ -113,7 +119,8 @@ class OrgGetUserResponse(CustomBaseModel): class OrgGetGroupResponse(CustomBaseModel): - groups: list[str] + organisation: OrgSummary + groups: list[GroupSummary] class OrgGetContactResponse(CustomBaseModel): diff --git a/src/schemas.py b/src/schemas.py index e281fd5..0244c11 100644 --- a/src/schemas.py +++ b/src/schemas.py @@ -40,3 +40,18 @@ class ServiceIDMixin(CustomBaseModel): class UserIDMixin(CustomBaseModel): user_id: int = Field(gt=0) + + +class OrgSummary(CustomBaseModel): + id: int + name: str + + +class GroupSummary(CustomBaseModel): + id: int + name: str + + +class UserSummary(CustomBaseModel): + id: int + email: str diff --git a/test/test_iam.py b/test/test_iam.py index 764c46c..0176f9d 100644 --- a/test/test_iam.py +++ b/test/test_iam.py @@ -221,10 +221,18 @@ async def test_get_group_users_success(default_client: AsyncClient): user = data["users"][0] assert user["id"] == 1 - assert user["first_name"] == "Admin" - assert user["last_name"] == "Test" assert user["email"] == "admin@test.com" + assert "group" in data + assert isinstance(data["group"], dict) + assert data["group"]["id"] == 1 + assert data["group"]["name"] == "Test Group" + + assert "organisation" in data + assert isinstance(data["organisation"], dict) + assert data["organisation"]["id"] == 1 + assert data["organisation"]["name"] == "Test Org" + @pytest.mark.parametrize( "query, expected_status", generate_query_and_status(["group_id", "org_id"]) diff --git a/test/test_organisation.py b/test/test_organisation.py index 69e3eac..364c753 100644 --- a/test/test_organisation.py +++ b/test/test_organisation.py @@ -265,9 +265,16 @@ async def test_post_org_user_success(default_client: AsyncClient, db_session): data = resp.json() + assert "organisation" in data + assert isinstance(data["organisation"], dict) + assert data["organisation"]["id"] == 1 + assert data["organisation"]["name"] == "Test Org" + assert "users" in data assert isinstance(data["users"], list) - assert "user@test.org" in data["users"] + assert ( + len([user for user in data["users"] if user["email"] == "user@test.org"]) == 1 + ) @pytest.mark.parametrize( @@ -386,9 +393,17 @@ async def test_get_org_groups_success(default_client: AsyncClient): data = resp.json() + assert "organisation" in data + assert isinstance(data["organisation"], dict) + assert data["organisation"]["id"] == 1 + assert data["organisation"]["name"] == "Test Org" + assert "groups" in data assert isinstance(data["groups"], list) - assert "Test Group" in data["groups"] + group = data["groups"][0] + assert isinstance(group, dict) + assert group["id"] == 1 + assert group["name"] == "Test Group" @pytest.mark.parametrize(