Spaces:
Paused

miracle / tests /test_projects.py
dushyantsingh0
Claude Sonnet 4.6
Add comprehensive test suite targeting >90% coverage
aa13248
Raw History Blame Contribute Delete
6.97 kB
"""
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