""" [GET]/user/self/claims is not tested because it requires OIDC authentication. [DELETE/user/ is not tested because the testing client cannot attach a body to a delete request. """ import pytest from httpx import AsyncClient from .conftest import generate_query_and_status pytestmark = [ pytest.mark.user_module, ] @pytest.mark.anyio async def test_get_self_db_success(default_client: AsyncClient): resp = await default_client.get("/user/self/db") data = resp.json() assert resp.status_code == 200 assert data["first_name"] == "Admin" assert data["last_name"] == "Test" assert data["email"] == "admin@test.com" assert "organisations" in data assert isinstance(data["organisations"], list) assert "groups" in data assert isinstance(data["groups"], dict) @pytest.mark.anyio async def test_get_user_success(default_client: AsyncClient): resp = await default_client.get("/user/?user_id=1") data = resp.json() assert resp.status_code == 200 assert data["first_name"] == "Admin" assert data["last_name"] == "Test" assert data["email"] == "admin@test.com" assert "organisations" in data assert isinstance(data["organisations"], list) assert "groups" in data assert isinstance(data["groups"], dict) @pytest.mark.anyio @pytest.mark.parametrize( "query, expected_status", generate_query_and_status(["user_id"]) ) async def test_get_user_status_checks( default_client: AsyncClient, query: str, expected_status: int ): resp = await default_client.get(f"/user/?{query}") assert resp.status_code == expected_status @pytest.mark.anyio async def test_delete_user_success(default_client: AsyncClient): resp = await default_client.delete("/user/?user_id=1") assert resp.status_code == 204 @pytest.mark.anyio async def test_post_user_invitation_success(default_client: AsyncClient): body = {"user_email": "admin@test.com", "organisation_id": 1} resp = await default_client.post("/user/invitation", json=body) assert resp.status_code == 200 assert resp.json() == "Invitation sent" @pytest.mark.parametrize( "body, expected_status", [ ({"organisation_id": 42, "user_email": "admin@test.com"}, 404), ({"organisation_id": "Test Org", "user_email": "admin@test.com"}, 422), ({"organisation_id": "", "user_email": "admin@test.com"}, 422), ({}, 422), ({"user_email": 42}, 422), ({"organisation_id": 1, "user_email": "Test User"}, 422), ], ) @pytest.mark.anyio async def test_post_user_invitation_status_checks( default_client: AsyncClient, body, expected_status ): resp = await default_client.post("/user/invitation", json=body) assert resp.status_code == expected_status @pytest.mark.parametrize( "body, expected_status", [ ({"jwt": "invalid"}, 401), ({"jwt": ""}, 401), ({"jwt": None}, 422), ({"jwt": 42}, 422), ], ) @pytest.mark.anyio async def test_post_user_invitation_accept_status_checks( default_client: AsyncClient, body, expected_status ): resp = await default_client.post("/user/invitation/accept", json=body) assert resp.status_code == expected_status if resp.status_code == 401: assert resp.json()["detail"] == "Invalid JWS" @pytest.mark.anyio async def test_get_self_orgs_success(default_client: AsyncClient): resp = await default_client.get("/user/self/orgs") assert resp.status_code == 200 data = resp.json() assert "organisations" in data assert isinstance(data["organisations"], list) assert len(data["organisations"]) > 0 org = data["organisations"][0] assert org["organisation_id"] == 1 assert org["name"] == "Test Org" assert org["status"] == "approved" assert org["root_user_email"] == "admin@test.com" assert "intake_questionnaire" in org assert isinstance(org["intake_questionnaire"], dict) assert isinstance(org["billing_contact"], dict) assert org["billing_contact"]["email"] == "billing@test.org" assert org["billing_contact"]["id"] == 1 assert isinstance(org["owner_contact"], dict) assert org["owner_contact"]["email"] == "owner@test.org" assert org["owner_contact"]["id"] == 2 assert isinstance(org["security_contact"], dict) assert org["security_contact"]["email"] == "security@test.org" assert org["security_contact"]["id"] == 3