mirror of
https://github.com/rennf93/roboco.git
synced 2026-08-03 07:23:24 +02:00
Route layer now rejects 403 if the role doesn't match the router's allowed set. Choreographer still re-checks role per verb where needed, but defense in depth means a future verb that forgets the role check doesn't leak. Auditor router also gated.
82 lines
2.4 KiB
Python
82 lines
2.4 KiB
Python
"""Unit tests for /api/v2/flow/auditor/* endpoints.
|
|
|
|
Uses a minimal FastAPI test client built from the new router only.
|
|
No DB required — Choreographer is mocked.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
from roboco.api.deps import get_choreographer
|
|
from roboco.api.routes.v2.flow_auditor import router
|
|
|
|
_HTTP_200 = 200
|
|
|
|
_AGENT_ID = str(uuid4())
|
|
_TASK_ID = str(uuid4())
|
|
_HEADERS = {"X-Agent-ID": _AGENT_ID, "X-Agent-Role": "auditor"}
|
|
|
|
|
|
def _make_envelope(
|
|
status: str = "ok", task_id: str | None = None, **extra: object
|
|
) -> MagicMock:
|
|
"""Return a mock Envelope whose as_dict() returns a predictable payload."""
|
|
env = MagicMock()
|
|
payload: dict[str, object] = {"status": status, "task_id": task_id, "next": "..."}
|
|
payload.update(extra)
|
|
env.as_dict.return_value = payload
|
|
return env
|
|
|
|
|
|
def _build_app(mock_choreographer: MagicMock) -> FastAPI:
|
|
"""Build minimal FastAPI app with the flow_auditor router and a mocked dep."""
|
|
app = FastAPI()
|
|
app.include_router(router)
|
|
app.dependency_overrides[get_choreographer] = lambda: mock_choreographer
|
|
return app
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_triage_returns_envelope() -> None:
|
|
"""POST /api/v2/flow/auditor/triage returns 200 with envelope shape."""
|
|
mock_chore = MagicMock()
|
|
mock_chore.auditor_triage = AsyncMock(
|
|
return_value=_make_envelope(status="blocked", task_id=_TASK_ID)
|
|
)
|
|
client = TestClient(_build_app(mock_chore))
|
|
|
|
resp = client.post(
|
|
"/api/v2/flow/auditor/triage",
|
|
json={},
|
|
headers=_HEADERS,
|
|
)
|
|
|
|
assert resp.status_code == _HTTP_200
|
|
body = resp.json()
|
|
assert body["status"] == "blocked"
|
|
mock_chore.auditor_triage.assert_awaited_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_i_am_idle_returns_envelope() -> None:
|
|
"""POST /api/v2/flow/auditor/i_am_idle delegates to Choreographer.i_am_idle."""
|
|
mock_chore = MagicMock()
|
|
mock_chore.i_am_idle = AsyncMock(return_value=_make_envelope(status="idle"))
|
|
client = TestClient(_build_app(mock_chore))
|
|
|
|
resp = client.post(
|
|
"/api/v2/flow/auditor/i_am_idle",
|
|
json={},
|
|
headers=_HEADERS,
|
|
)
|
|
|
|
assert resp.status_code == _HTTP_200
|
|
body = resp.json()
|
|
assert body["status"] == "idle"
|
|
mock_chore.i_am_idle.assert_awaited_once()
|