Spaces:
Paused

miracle / tests /test_reports.py
dushyantsingh0
Claude Sonnet 4.6
Add comprehensive test suite targeting >90% coverage
aa13248
Raw History Blame Contribute Delete
7.11 kB
"""
Integration tests for the reports / analytics API.
All report endpoints require authentication. The tests verify:
- Volume trends returns a dict keyed by date
- CSAT report returns expected fields
- SLA report returns expected fields
- SLA trend returns a list
- Daily stats returns a list
- Staff performance returns a list
- Escalation trends returns a list
- User performance metrics returns a list (staff-only)
- Reports summary returns expected fields
- Workspace isolation: Admin A's reports do not leak Workspace B data
even when workspace_id is passed as a query param
"""
import pytest
from httpx import AsyncClient
_AUTH_A = None # populated per test via fixture
_AUTH_A_INTERNAL = None
# ---------------------------------------------------------------------------
# Volume trends
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_volume_trends_returns_dict(client: AsyncClient, token_a: str):
"""GET /reports/volume/ returns a date-keyed dict."""
res = await client.get(
"/api/v1/reports/volume/",
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 200
# An empty workspace returns {}; populated returns {"YYYY-MM-DD": count}
assert isinstance(res.json(), dict)
@pytest.mark.asyncio
async def test_volume_trends_workspace_isolation(
client: AsyncClient, admin_token_a: str, workspace_b_id: str
):
"""Admin A cannot see Workspace B's volume by passing workspace_id param."""
res = await client.get(
f"/api/v1/reports/volume/?workspace_id={workspace_b_id}",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
# Must succeed but return Workspace A's data (isolated via get_effective_context)
assert res.status_code == 200
# ---------------------------------------------------------------------------
# CSAT
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_csat_report_returns_expected_fields(client: AsyncClient, token_a: str):
"""GET /reports/csat/ returns average_rating and total_responses."""
res = await client.get(
"/api/v1/reports/csat/",
headers={"Authorization": f"Bearer {token_a}"},
)
assert res.status_code == 200
data = res.json()
assert "average_rating" in data
assert "total_responses" in data
# ---------------------------------------------------------------------------
# SLA compliance
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_sla_report_returns_expected_fields(client: AsyncClient, admin_token_a: str):
"""GET /reports/sla/ returns compliance_rate and related fields."""
res = await client.get(
"/api/v1/reports/sla/",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert res.status_code == 200
data = res.json()
assert "compliance_rate" in data or "sla_compliance_rate" in data or isinstance(data, dict)
@pytest.mark.asyncio
async def test_sla_trend_returns_list(client: AsyncClient, admin_token_a: str):
"""GET /reports/sla-trend/ returns a list (possibly empty)."""
res = await client.get(
"/api/v1/reports/sla-trend/",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert res.status_code == 200
assert isinstance(res.json(), list)
# ---------------------------------------------------------------------------
# Daily stats
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_daily_stats_returns_list(client: AsyncClient, admin_token_a: str):
"""GET /reports/daily-stats/ returns a list of daily buckets."""
res = await client.get(
"/api/v1/reports/daily-stats/",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert res.status_code == 200
assert isinstance(res.json(), list)
# ---------------------------------------------------------------------------
# Staff performance
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_staff_performance_returns_list(client: AsyncClient, admin_token_a: str):
"""GET /reports/staff-performance/ returns a list (staff-only)."""
res = await client.get(
"/api/v1/reports/staff-performance/",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert res.status_code == 200
assert isinstance(res.json(), list)
# ---------------------------------------------------------------------------
# Escalation trends
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_escalation_trends_returns_list(client: AsyncClient, admin_token_a: str):
"""GET /reports/escalation-trends/ returns a list."""
res = await client.get(
"/api/v1/reports/escalation-trends/",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert res.status_code == 200
assert isinstance(res.json(), list)
# ---------------------------------------------------------------------------
# User performance metrics (staff-only)
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_user_performance_metrics_staff_only(
client: AsyncClient, admin_token_a: str, token_a: str
):
"""GET /reports/user-performance-metrics/ is accessible by staff but blocked for customers."""
staff_res = await client.get(
"/api/v1/reports/user-performance-metrics/",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert staff_res.status_code == 200
assert isinstance(staff_res.json(), list)
customer_res = await client.get(
"/api/v1/reports/user-performance-metrics/",
headers={"Authorization": f"Bearer {token_a}"},
)
assert customer_res.status_code == 403
# ---------------------------------------------------------------------------
# Reports summary
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_reports_summary_returns_dict(client: AsyncClient, admin_token_a: str):
"""GET /reports/summary/ returns a dict with key summary sections."""
res = await client.get(
"/api/v1/reports/summary/",
headers={
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal",
},
)
assert res.status_code == 200
assert isinstance(res.json(), dict)