Spaces:
Paused

miracle / tests /test_dashboard.py
dushyantsingh0
Claude Sonnet 4.6
Add comprehensive test suite targeting >90% coverage
aa13248
Raw History Blame Contribute Delete
5.89 kB
"""
Integration tests for the dashboard API.
Coverage:
- GET /dashboard/stats/ returns expected top-level fields
- Dashboard stats are workspace-scoped (isolation)
- GET /dashboard/my-tickets/ returns tickets for the caller
- GET /dashboard/agent-queue/ is staff-only (403 for customers)
- GET /dashboard/hydration/ returns combined stats+reports+queue
"""
import pytest
from httpx import AsyncClient
# ---------------------------------------------------------------------------
# Stats
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_dashboard_stats_has_required_fields(
client: AsyncClient, admin_token_a: str
):
"""GET /dashboard/stats/ returns the full telemetry payload."""
res = await client.get(
"/api/v1/dashboard/stats",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert res.status_code == 200
data = res.json()
# Core telemetry keys
assert "total_tickets" in data
assert "by_status" in data
assert "by_priority" in data
assert "performance" in data
@pytest.mark.asyncio
async def test_dashboard_stats_customer_view(client: AsyncClient, token_a: str):
"""Customers can access the dashboard stats endpoint for their workspace."""
res = await client.get(
"/api/v1/dashboard/stats",
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 200
data = res.json()
assert "total_tickets" in data
@pytest.mark.asyncio
async def test_dashboard_stats_workspace_isolation(
client: AsyncClient, admin_token_a: str, workspace_b_id: str
):
"""
Passing workspace_b_id to /dashboard/stats must NOT return B's data —
get_effective_context enforces that Admin A stays in their own workspace.
"""
res_own = await client.get(
"/api/v1/dashboard/stats",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
res_injected = await client.get(
f"/api/v1/dashboard/stats?workspace_id={workspace_b_id}",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert res_own.status_code == 200
assert res_injected.status_code == 200
# Both should refer to the same workspace
assert res_own.json()["workspace_id"] == res_injected.json()["workspace_id"]
# ---------------------------------------------------------------------------
# My tickets
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_my_tickets_returns_own_tickets(client: AsyncClient, token_a: str):
"""GET /dashboard/my-tickets/ returns tickets created by the caller."""
await client.post(
"/api/v1/tickets/",
json={"title": "My Tracked Ticket", "description": "Desc", "priority": "P2"},
headers={"Authorization": f"Bearer {token_a}"},
)
res = await client.get(
"/api/v1/dashboard/my-tickets/",
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 200
titles = [t["title"] for t in res.json()]
assert "My Tracked Ticket" in titles
@pytest.mark.asyncio
async def test_my_tickets_does_not_include_others(
client: AsyncClient, token_a: str, token_b: str
):
"""A customer's my-tickets list does not include tickets from another workspace."""
await client.post(
"/api/v1/tickets/",
json={"title": "B's Own Ticket", "description": "Desc", "priority": "P2"},
headers={"Authorization": f"Bearer {token_b}"},
)
res = await client.get(
"/api/v1/dashboard/my-tickets/",
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 200
titles = [t["title"] for t in res.json()]
assert "B's Own Ticket" not in titles
# ---------------------------------------------------------------------------
# Agent queue (staff-only)
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_agent_queue_accessible_by_staff(client: AsyncClient, admin_token_a: str):
"""GET /dashboard/agent-queue/ succeeds for SC_ADMIN."""
res = await client.get(
"/api/v1/dashboard/agent-queue/",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert res.status_code == 200
assert isinstance(res.json(), list)
@pytest.mark.asyncio
async def test_agent_queue_blocked_for_customers(client: AsyncClient, token_a: str):
"""GET /dashboard/agent-queue/ returns 403 for a customer."""
res = await client.get(
"/api/v1/dashboard/agent-queue/",
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 403
# ---------------------------------------------------------------------------
# Dashboard hydration (unified endpoint)
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_dashboard_hydration_returns_combined_payload(
client: AsyncClient, admin_token_a: str
):
"""
GET /dashboard/hydration/ returns a combined dict with stats, reports,
queue, and escalations sections.
"""
res = await client.get(
"/api/v1/dashboard/hydration/",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert res.status_code == 200
data = res.json()
# Must contain unified sections
assert "stats" in data or "total_tickets" in data # telemetry or nested
# Reports summary section
assert "reports" in data or isinstance(data, dict)