Spaces:
Paused
Paused
File size: 6,970 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 | """
Integration tests for the projects API.
Coverage:
- Admin can create, list, get, update, and soft-delete a project
- Customer cannot create or modify projects (403)
- Project list is scoped to the caller's workspace
- Get by ID returns 404 for another workspace's project (BOLA)
- Deleted projects do not appear in the list
"""
import pytest
from httpx import AsyncClient
_INTERNAL = {"X-Nexus-Portal": "internal"}
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
async def _create_project(
client: AsyncClient, admin_token: str, name: str = "Test Project"
) -> dict:
res = await client.post(
"/api/v1/projects/",
json={"name": name, "description": "A test project"},
headers={"Authorization": f"Bearer {admin_token}", **_INTERNAL},
)
assert res.status_code == 201, f"Project creation failed: {res.text}"
return res.json()
# ---------------------------------------------------------------------------
# CRUD — happy path
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_admin_can_create_project(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN can create a new project."""
res = await client.post(
"/api/v1/projects/",
json={"name": "Alpha Project", "description": "First project"},
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 201
data = res.json()
assert data["name"] == "Alpha Project"
assert "id" in data
@pytest.mark.asyncio
async def test_admin_can_list_projects(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN can list all projects in their workspace."""
await _create_project(client, admin_token_a, "Listed Project")
res = await client.get(
"/api/v1/projects/",
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 200
names = [p["name"] for p in res.json()]
assert "Listed Project" in names
@pytest.mark.asyncio
async def test_admin_can_get_project_by_id(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN can fetch a specific project by ID."""
project = await _create_project(client, admin_token_a, "Fetchable Project")
res = await client.get(
f"/api/v1/projects/{project['id']}/",
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 200
assert res.json()["id"] == project["id"]
@pytest.mark.asyncio
async def test_admin_can_update_project(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN can update a project name."""
project = await _create_project(client, admin_token_a, "Before Update")
res = await client.patch(
f"/api/v1/projects/{project['id']}/",
json={"name": "After Update"},
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 200
assert res.json()["name"] == "After Update"
@pytest.mark.asyncio
async def test_admin_can_delete_project(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN can soft-delete a project; it disappears from subsequent list."""
project = await _create_project(client, admin_token_a, "To Be Deleted")
del_res = await client.delete(
f"/api/v1/projects/{project['id']}/",
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert del_res.status_code == 200
# Verify it no longer appears in list
list_res = await client.get(
"/api/v1/projects/",
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert list_res.status_code == 200
assert not any(p["id"] == project["id"] for p in list_res.json())
# ---------------------------------------------------------------------------
# RBAC
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_customer_cannot_create_project(client: AsyncClient, token_a: str):
"""CUSTOMER_USER must receive 403 when trying to create a project."""
res = await client.post(
"/api/v1/projects/",
json={"name": "Unauthorized Project"},
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 403
@pytest.mark.asyncio
async def test_customer_can_list_projects(client: AsyncClient, token_a: str, admin_token_a: str):
"""Customers can list projects (read-only access)."""
await _create_project(client, admin_token_a, "Customer Visible Project")
res = await client.get(
"/api/v1/projects/",
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 200
@pytest.mark.asyncio
async def test_customer_cannot_update_project(
client: AsyncClient, token_a: str, admin_token_a: str
):
"""CUSTOMER_USER must receive 403 when trying to update a project."""
project = await _create_project(client, admin_token_a, "No Customer Edits")
res = await client.patch(
f"/api/v1/projects/{project['id']}/",
json={"name": "Hijacked"},
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 403
# ---------------------------------------------------------------------------
# BOLA / workspace isolation
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_project_list_scoped_to_workspace(
client: AsyncClient, admin_token_a: str, admin_token_b: str
):
"""Workspace B's project must not appear in Workspace A's listing."""
await _create_project(client, admin_token_b, "Workspace B Only Project")
res = await client.get(
"/api/v1/projects/",
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 200
names = [p["name"] for p in res.json()]
assert "Workspace B Only Project" not in names
@pytest.mark.asyncio
async def test_get_project_cross_workspace_returns_404(
client: AsyncClient, admin_token_a: str, admin_token_b: str
):
"""Admin A cannot fetch a project that belongs to Workspace B (BOLA)."""
project_b = await _create_project(client, admin_token_b, "Secret B Project")
res = await client.get(
f"/api/v1/projects/{project_b['id']}/",
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 404
@pytest.mark.asyncio
async def test_get_nonexistent_project_returns_404(
client: AsyncClient, admin_token_a: str
):
"""GET /projects/{id} with an unknown UUID returns 404."""
res = await client.get(
"/api/v1/projects/00000000-0000-0000-0000-000000000000/",
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 404
|