|
| 1 | +import uuid |
| 2 | +from types import SimpleNamespace |
| 3 | + |
| 4 | +import pytest |
| 5 | +from fastapi import HTTPException |
| 6 | + |
| 7 | +from app.api import organization |
| 8 | +from app.schemas.schemas import UserUpdate |
| 9 | + |
| 10 | + |
| 11 | +class DummyResult: |
| 12 | + def __init__(self, value=None): |
| 13 | + self.value = value |
| 14 | + |
| 15 | + def scalar_one_or_none(self): |
| 16 | + return self.value |
| 17 | + |
| 18 | + def scalars(self): |
| 19 | + return self |
| 20 | + |
| 21 | + def all(self): |
| 22 | + return [] |
| 23 | + |
| 24 | + |
| 25 | +class RecordingDB: |
| 26 | + def __init__(self, responses): |
| 27 | + self.responses = list(responses) |
| 28 | + self.statements = [] |
| 29 | + |
| 30 | + async def execute(self, statement): |
| 31 | + self.statements.append(statement) |
| 32 | + return self.responses.pop(0) |
| 33 | + |
| 34 | + |
| 35 | +def _org_admin(*, tenant_id: uuid.UUID, identity_id: uuid.UUID) -> SimpleNamespace: |
| 36 | + return SimpleNamespace( |
| 37 | + role="org_admin", |
| 38 | + tenant_id=tenant_id, |
| 39 | + identity_id=identity_id, |
| 40 | + identity=SimpleNamespace(is_platform_admin=False), |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.asyncio |
| 45 | +async def test_org_admin_cannot_load_user_from_another_tenant_for_update() -> None: |
| 46 | + tenant_id = uuid.uuid4() |
| 47 | + db = RecordingDB([DummyResult()]) |
| 48 | + |
| 49 | + with pytest.raises(HTTPException) as raised: |
| 50 | + await organization.admin_update_user( |
| 51 | + user_id=uuid.uuid4(), |
| 52 | + data=UserUpdate(display_name="Changed"), |
| 53 | + current_user=_org_admin(tenant_id=tenant_id, identity_id=uuid.uuid4()), |
| 54 | + db=db, |
| 55 | + ) |
| 56 | + |
| 57 | + assert raised.value.status_code == 404 |
| 58 | + assert "users.tenant_id" in str(db.statements[0]) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_org_admin_cannot_list_users_from_another_tenant() -> None: |
| 63 | + tenant_id = uuid.uuid4() |
| 64 | + requested_tenant_id = uuid.uuid4() |
| 65 | + db = RecordingDB([DummyResult()]) |
| 66 | + |
| 67 | + users = await organization.list_users( |
| 68 | + tenant_id=requested_tenant_id, |
| 69 | + current_user=_org_admin(tenant_id=tenant_id, identity_id=uuid.uuid4()), |
| 70 | + db=db, |
| 71 | + ) |
| 72 | + |
| 73 | + assert users == [] |
| 74 | + assert db.statements[0].compile().params["tenant_id_1"] == tenant_id |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.asyncio |
| 78 | +async def test_org_admin_cannot_change_another_members_global_login_email() -> None: |
| 79 | + tenant_id = uuid.uuid4() |
| 80 | + current_identity_id = uuid.uuid4() |
| 81 | + target = SimpleNamespace(id=uuid.uuid4(), tenant_id=tenant_id, identity_id=uuid.uuid4()) |
| 82 | + db = RecordingDB([DummyResult(target)]) |
| 83 | + |
| 84 | + with pytest.raises(HTTPException) as raised: |
| 85 | + await organization.admin_update_user( |
| 86 | + user_id=target.id, |
| 87 | + data=UserUpdate(email="new-address@example.com"), |
| 88 | + current_user=_org_admin(tenant_id=tenant_id, identity_id=current_identity_id), |
| 89 | + db=db, |
| 90 | + ) |
| 91 | + |
| 92 | + assert raised.value.status_code == 403 |
| 93 | + assert raised.value.detail == "Cannot modify another user's login email" |
0 commit comments