""" Integration tests for user management and workspace membership. Coverage: - CUSTOMER_ADMIN can create a CUSTOMER_USER in their workspace - CUSTOMER_ADMIN cannot promote a new user to SC_ADMIN (role escalation) - CUSTOMER_USER cannot create workspace users at all (403) - Registering with a duplicate email returns 400 - Registering with a weak password is rejected with 422 - Registering without a workspace creates a new workspace for the user - Registering with an existing workspace_id joins that workspace - /me returns the calling user's profile without exposing hashed_password """ import pytest from httpx import AsyncClient # --------------------------------------------------------------------------- # /me endpoint # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_me_customer_returns_profile(client: AsyncClient, token_a: str): """/me returns the authenticated customer's profile.""" res = await client.get( "/api/v1/auth/me", headers={"Authorization": f"Bearer {token_a}"}, ) assert res.status_code == 200 data = res.json() assert data["email"] == "customer@a.com" assert data["full_name"] == "Customer A" # Password must never appear in the response assert "hashed_password" not in data assert "password" not in data @pytest.mark.asyncio async def test_me_admin_returns_profile(client: AsyncClient, admin_token_a: str): """/me works for internal staff as well.""" res = await client.get( "/api/v1/auth/me", headers={"Authorization": f"Bearer {admin_token_a}", "X-Nexus-Portal": "internal"}, ) assert res.status_code == 200 assert res.json()["email"] == "admin@a.com" # --------------------------------------------------------------------------- # Registration # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_register_new_user_creates_workspace(client: AsyncClient): """Registering without a workspace_id creates a new workspace automatically.""" res = await client.post( "/api/v1/auth/register/", json={ "email": "newowner@example.com", "password": "SecurePass1!", "full_name": "New Owner", }, ) assert res.status_code == 201 data = res.json() assert "access_token" in data assert "refresh_token" in data @pytest.mark.asyncio async def test_register_duplicate_email_rejected(client: AsyncClient): """Registering the same email a second time (same workspace) returns 400.""" payload = { "email": "duplicate@example.com", "password": "SecurePass1!", "full_name": "First User", } res1 = await client.post("/api/v1/auth/register/", json=payload) assert res1.status_code == 201 # Second registration — should fail because user already owns a workspace # and trying to create again with same email hits duplicate check res2 = await client.post("/api/v1/auth/register/", json=payload) # The exact status depends on whether the user is joining the SAME workspace # or creating a new one; either way, we should not get a 201 for a repeat attempt # with the same workspace context. At minimum it should not silently succeed # by leaking an existing account without password verification. assert res2.status_code in (400, 401) @pytest.mark.asyncio async def test_register_weak_password_rejected(client: AsyncClient): """Passwords that are too short must be rejected with 422.""" res = await client.post( "/api/v1/auth/register/", json={"email": "weak@example.com", "password": "abc", "full_name": "Weak"}, ) assert res.status_code == 422 assert "at least 8 characters" in str(res.json()) @pytest.mark.asyncio async def test_register_password_without_digit_or_symbol_rejected(client: AsyncClient): """Passwords that lack digits or special characters are rejected.""" res = await client.post( "/api/v1/auth/register/", json={ "email": "simple@example.com", "password": "onlyletters", "full_name": "Simple", }, ) assert res.status_code == 422 assert "digit or special character" in str(res.json()) @pytest.mark.asyncio async def test_register_join_existing_workspace(client: AsyncClient, workspace_a_id: str): """A new user can join an existing workspace by providing workspace_id.""" res = await client.post( "/api/v1/auth/register/", json={ "email": "joiner@example.com", "password": "JoinPass1!", "full_name": "Joiner", "workspace_id": workspace_a_id, }, ) assert res.status_code == 201 @pytest.mark.asyncio async def test_register_invalid_workspace_id_returns_404(client: AsyncClient): """Providing a non-existent workspace_id returns 404.""" res = await client.post( "/api/v1/auth/register/", json={ "email": "nobody@example.com", "password": "SecurePass1!", "full_name": "Nobody", "workspace_id": "00000000-0000-0000-0000-000000000000", }, ) assert res.status_code == 404 # --------------------------------------------------------------------------- # Admin user creation # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_customer_admin_can_create_customer_user( client: AsyncClient, workspace_a_id: str ): """ A CUSTOMER_ADMIN (registered owner) can invite another user into their workspace with the CUSTOMER_USER role. """ # Register as CUSTOMER_ADMIN reg = await client.post( "/api/v1/auth/register/", json={ "email": "ca_owner@example.com", "password": "OwnerPass1!", "full_name": "CA Owner", }, ) assert reg.status_code == 201 ca_token = reg.json()["access_token"] # Create a CUSTOMER_USER inside the owner's workspace res = await client.post( "/api/v1/users/", json={ "email": "new_member@example.com", "password": "MemberPass1!", "full_name": "New Member", "role": "CUSTOMER_USER", }, headers={"Authorization": f"Bearer {ca_token}"}, ) assert res.status_code == 201 assert res.json()["email"] == "new_member@example.com" @pytest.mark.asyncio async def test_customer_admin_cannot_create_sc_admin(client: AsyncClient): """ A CUSTOMER_ADMIN must not be able to escalate a new user to SC_ADMIN — that would be a privilege escalation vulnerability. """ reg = await client.post( "/api/v1/auth/register/", json={ "email": "ca_priv_esc@example.com", "password": "OwnerPass1!", "full_name": "CA Priv Esc", }, ) ca_token = reg.json()["access_token"] res = await client.post( "/api/v1/users/", json={ "email": "fake_admin@example.com", "password": "AdminPass1!", "full_name": "Fake Admin", "role": "SC_ADMIN", }, headers={"Authorization": f"Bearer {ca_token}"}, ) assert res.status_code == 403 @pytest.mark.asyncio async def test_customer_user_cannot_create_users(client: AsyncClient, token_a: str): """A CUSTOMER_USER does not have permission to create workspace users.""" res = await client.post( "/api/v1/users/", json={ "email": "unauthorized@example.com", "password": "SomePass1!", "full_name": "Unauthorized", "role": "CUSTOMER_USER", }, headers={"Authorization": f"Bearer {token_a}"}, ) assert res.status_code == 403