Spaces:
Paused

miracle / tests /test_workspace.py
dushyantsingh0
Claude Sonnet 4.6
Add comprehensive test suite targeting >90% coverage
aa13248
Raw History Blame Contribute Delete
4.92 kB
"""
Integration tests for the workspace management API.
Coverage:
- GET /workspace/ returns the caller's current workspace
- GET /workspace/list/ returns only workspaces the caller belongs to
- PATCH /workspace/ (admin-only) updates workspace name
- Customer cannot update workspace (403)
- GET /workspace/branding/ returns branding config (dict)
"""
import pytest
from httpx import AsyncClient
_INTERNAL = {"X-Nexus-Portal": "internal"}
# ---------------------------------------------------------------------------
# Get current workspace
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_get_my_workspace_customer(client: AsyncClient, token_a: str):
"""GET /workspace/ returns the workspace the customer belongs to."""
res = await client.get(
"/api/v1/workspace/",
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 200
data = res.json()
assert "id" in data
assert "name" in data
assert data["name"] == "Test Workspace A"
@pytest.mark.asyncio
async def test_get_my_workspace_admin(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN can also retrieve their current workspace."""
res = await client.get(
"/api/v1/workspace/",
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 200
assert res.json()["name"] == "Test Workspace A"
# ---------------------------------------------------------------------------
# List available workspaces
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_list_workspaces_customer_only_sees_own(
client: AsyncClient, token_a: str
):
"""A customer only sees the workspaces they are a member of."""
res = await client.get(
"/api/v1/workspace/list/",
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 200
names = [w["name"] for w in res.json()]
assert "Test Workspace A" in names
assert "Test Workspace B" not in names
@pytest.mark.asyncio
async def test_list_workspaces_admin_sees_all(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN (platform admin) sees all workspaces in the system."""
res = await client.get(
"/api/v1/workspace/list/",
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 200
names = [w["name"] for w in res.json()]
# Both seeded workspaces must be visible to the platform admin
assert "Test Workspace A" in names
assert "Test Workspace B" in names
# ---------------------------------------------------------------------------
# Update workspace
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_admin_can_update_workspace_name(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN can rename their workspace."""
res = await client.patch(
"/api/v1/workspace/",
json={"name": "Renamed Workspace A"},
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 200
assert res.json()["name"] == "Renamed Workspace A"
@pytest.mark.asyncio
async def test_customer_cannot_update_workspace(client: AsyncClient, token_a: str):
"""A CUSTOMER_USER must receive 403 when trying to rename the workspace."""
res = await client.patch(
"/api/v1/workspace/",
json={"name": "Unauthorized Rename"},
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 403
# ---------------------------------------------------------------------------
# Branding
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_get_branding_returns_dict(client: AsyncClient, token_a: str):
"""GET /workspace/branding/ returns a JSON object (empty dict if not set)."""
res = await client.get(
"/api/v1/workspace/branding/",
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 200
assert isinstance(res.json(), dict)
@pytest.mark.asyncio
async def test_admin_can_set_branding(client: AsyncClient, admin_token_a: str):
"""SC_ADMIN can write branding config; it is then returned by the branding endpoint."""
await client.patch(
"/api/v1/workspace/",
json={"branding": {"primary_color": "#FF5733", "logo_url": "https://example.com/logo.png"}},
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
res = await client.get(
"/api/v1/workspace/branding/",
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
assert res.status_code == 200
branding = res.json()
assert branding.get("primary_color") == "#FF5733"