Spaces:
Paused

miracle / tests /test_system.py
dushyantsingh0
Claude Sonnet 4.6
Add comprehensive test suite targeting >90% coverage
aa13248
Raw History Blame Contribute Delete
8 kB
"""
Integration tests for the system / diagnostics API.
Endpoints covered:
GET /system/workflows/ - staff only, returns list
GET /system/interactions/ - staff only, returns list
GET /system/history/ - staff only, returns list
GET /system/audit/ - staff only, returns list with actor_name hydration
GET /system/status/ - super-admin only (or 403 if role guard rejects)
All staff-accessible endpoints must return 403 for CUSTOMER_USER.
"""
import pytest
from httpx import AsyncClient
_INTERNAL = {"X-Nexus-Portal": "internal"}
# ---------------------------------------------------------------------------
# Helper
# ---------------------------------------------------------------------------
def _staff_headers(token: str) -> dict:
return {"Authorization": f"Bearer {token}", "X-Nexus-Portal": "internal"}
def _customer_headers(token: str) -> dict:
return {"Authorization": f"Bearer {token}"}
# ---------------------------------------------------------------------------
# AI Workflows
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_workflows_accessible_by_staff(client: AsyncClient, admin_token_a: str):
"""GET /system/workflows/ returns a list for SC_ADMIN."""
res = await client.get(
"/api/v1/system/workflows/",
headers=_staff_headers(admin_token_a),
)
assert res.status_code == 200
assert isinstance(res.json(), list)
@pytest.mark.asyncio
async def test_workflows_blocked_for_customers(client: AsyncClient, token_a: str):
"""GET /system/workflows/ returns 403 for CUSTOMER_USER."""
res = await client.get(
"/api/v1/system/workflows/",
headers=_customer_headers(token_a),
)
assert res.status_code == 403
@pytest.mark.asyncio
async def test_workflows_pagination(client: AsyncClient, admin_token_a: str):
"""GET /system/workflows/ respects offset and limit query params."""
res = await client.get(
"/api/v1/system/workflows/?offset=0&limit=5",
headers=_staff_headers(admin_token_a),
)
assert res.status_code == 200
assert len(res.json()) <= 5
@pytest.mark.asyncio
async def test_workflows_unauthenticated_returns_401(client: AsyncClient):
"""GET /system/workflows/ without a token returns 401."""
res = await client.get("/api/v1/system/workflows/")
assert res.status_code == 401
# ---------------------------------------------------------------------------
# AI Interactions
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_interactions_accessible_by_staff(client: AsyncClient, admin_token_a: str):
"""GET /system/interactions/ returns a list for SC_ADMIN."""
res = await client.get(
"/api/v1/system/interactions/",
headers=_staff_headers(admin_token_a),
)
assert res.status_code == 200
assert isinstance(res.json(), list)
@pytest.mark.asyncio
async def test_interactions_blocked_for_customers(client: AsyncClient, token_a: str):
res = await client.get(
"/api/v1/system/interactions/",
headers=_customer_headers(token_a),
)
assert res.status_code == 403
@pytest.mark.asyncio
async def test_interactions_pagination(client: AsyncClient, admin_token_a: str):
res = await client.get(
"/api/v1/system/interactions/?offset=0&limit=5",
headers=_staff_headers(admin_token_a),
)
assert res.status_code == 200
assert len(res.json()) <= 5
# ---------------------------------------------------------------------------
# Infra health history
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_history_accessible_by_staff(client: AsyncClient, admin_token_a: str):
"""GET /system/history/ returns a list (possibly empty in fresh test DB)."""
res = await client.get(
"/api/v1/system/history/",
headers=_staff_headers(admin_token_a),
)
assert res.status_code == 200
assert isinstance(res.json(), list)
@pytest.mark.asyncio
async def test_history_blocked_for_customers(client: AsyncClient, token_a: str):
res = await client.get(
"/api/v1/system/history/",
headers=_customer_headers(token_a),
)
assert res.status_code == 403
# ---------------------------------------------------------------------------
# Audit log
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_audit_accessible_by_staff(client: AsyncClient, admin_token_a: str):
"""GET /system/audit/ returns a list; ticket creation events appear."""
# Create a ticket to generate at least one audit log entry
await client.post(
"/api/v1/tickets/",
json={"title": "Audit Log Trigger", "description": "Test", "priority": "P2"},
headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
)
res = await client.get(
"/api/v1/system/audit/",
headers=_staff_headers(admin_token_a),
)
assert res.status_code == 200
data = res.json()
assert isinstance(data, list)
# Every entry must have actor_name (hydrated field)
for entry in data:
assert "actor_name" in entry
@pytest.mark.asyncio
async def test_audit_blocked_for_customers(client: AsyncClient, token_a: str):
res = await client.get(
"/api/v1/system/audit/",
headers=_customer_headers(token_a),
)
assert res.status_code == 403
@pytest.mark.asyncio
async def test_audit_filter_by_entity_type(client: AsyncClient, admin_token_a: str):
"""GET /system/audit/?entity_type=ticket filters results correctly."""
res = await client.get(
"/api/v1/system/audit/?entity_type=ticket",
headers=_staff_headers(admin_token_a),
)
assert res.status_code == 200
for entry in res.json():
assert entry["entity_type"] == "ticket"
@pytest.mark.asyncio
async def test_audit_filter_by_action(client: AsyncClient, admin_token_a: str):
"""GET /system/audit/?action=create filters results correctly."""
res = await client.get(
"/api/v1/system/audit/?action=create",
headers=_staff_headers(admin_token_a),
)
assert res.status_code == 200
for entry in res.json():
assert entry["action"] == "create"
@pytest.mark.asyncio
async def test_audit_unauthenticated_returns_401(client: AsyncClient):
res = await client.get("/api/v1/system/audit/")
assert res.status_code == 401
# ---------------------------------------------------------------------------
# System status (super-admin guard)
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_system_status_blocked_for_customers(client: AsyncClient, token_a: str):
"""GET /system/status/ returns 403 for CUSTOMER_USER."""
res = await client.get(
"/api/v1/system/status/",
headers=_customer_headers(token_a),
)
assert res.status_code == 403
@pytest.mark.asyncio
async def test_system_status_unauthenticated_returns_401(client: AsyncClient):
res = await client.get("/api/v1/system/status/")
assert res.status_code == 401
@pytest.mark.asyncio
async def test_system_status_staff_gets_structured_response(
client: AsyncClient, admin_token_a: str
):
"""
SC_ADMIN hits the status endpoint. The super-admin guard has a case-
sensitivity issue (lower-casing the role before comparing against the
enum list), so SC_ADMIN may receive 403. Either way the endpoint must
not crash with 500.
"""
res = await client.get(
"/api/v1/system/status/",
headers=_staff_headers(admin_token_a),
)
assert res.status_code in (200, 403), (
f"Unexpected status {res.status_code}: {res.text}"
)
if res.status_code == 200:
data = res.json()
assert "database" in data
assert "redis" in data