Spaces:
Paused
Paused
File size: 7,898 Bytes
aa13248 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | """
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
|