mirror of
https://github.com/rennf93/roboco.git
synced 2026-08-03 07:23:24 +02:00
* [f8480831] refactor(api): extract route-layer helpers into services/schemas/utils (batch B) Moves 28 non-@router-decorated helper functions out of 15 route files (optimal, project, release, dashboard, pitch, x, docs, git, playbooks, product, provider, research, secretary, system, work_session) into their paired services module (DB/service-calling helpers), the route's schemas module as a converter (pure response/request shaping, mirroring the existing project_to_response/assignment_to_response pattern), or roboco/utils/converters.py (pure generic helpers). Adds two small shared role-check helpers to api/deps.py (require_auditor_or_ceo, require_role_in) for endpoint-specific role gates that had no existing home. Placement-only: no route paths, schemas, or observable behavior changed. Fixes the handful of tests that imported the old private helper names directly. * [f8480831] docs(map): document Batch B route-helper relocation in api-routes-schemas.md * [f8480831] docs(map): add Key Symbols rows for require_auditor_or_ceo/require_role_in --------- Co-authored-by: Backend Developer 2 <be-dev-2@roboco.tech> Co-authored-by: Backend Documenter <be-doc@roboco.tech>
196 lines
6.1 KiB
Python
196 lines
6.1 KiB
Python
"""Dashboard auditor flag/report mutating routes (``create_auditor_flag``,
|
|
``resolve_auditor_flag``, ``create_auditor_report``, ``send_auditor_report``)
|
|
are gated to AUDITOR or CEO via a ``CurrentAgentContext`` dependency plus
|
|
``roboco.api.deps.require_auditor_or_ceo`` — the same check playbooks.py uses.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from http import HTTPStatus
|
|
from typing import TYPE_CHECKING
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
import pytest_asyncio
|
|
from fastapi import FastAPI
|
|
from httpx import ASGITransport, AsyncClient
|
|
from roboco.api.deps import get_agent_context, get_db
|
|
from roboco.api.routes.dashboard import router as dashboard_router
|
|
from roboco.models import AgentRole
|
|
from roboco.models.permissions import AgentContext
|
|
from roboco.services.dashboard import reset_storage
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import AsyncGenerator, AsyncIterator
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
def _override_agent(role: AgentRole) -> AgentContext:
|
|
return AgentContext(agent_id=uuid4(), role=role, team=None)
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def auditor_client(
|
|
db_session: AsyncSession,
|
|
) -> AsyncIterator[AsyncClient]:
|
|
"""A client authenticated as the Auditor (the legitimate caller)."""
|
|
reset_storage()
|
|
app = FastAPI()
|
|
app.include_router(dashboard_router, prefix="/api/dashboard")
|
|
|
|
async def _override_db() -> AsyncGenerator[AsyncSession]:
|
|
yield db_session
|
|
|
|
app.dependency_overrides[get_db] = _override_db
|
|
app.dependency_overrides[get_agent_context] = lambda: _override_agent(
|
|
AgentRole.AUDITOR
|
|
)
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
yield client
|
|
app.dependency_overrides.clear()
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def dev_client(
|
|
db_session: AsyncSession,
|
|
) -> AsyncIterator[AsyncClient]:
|
|
"""A client authenticated as a Developer — must NOT be able to mutate
|
|
auditor flags/reports."""
|
|
reset_storage()
|
|
app = FastAPI()
|
|
app.include_router(dashboard_router, prefix="/api/dashboard")
|
|
|
|
async def _override_db() -> AsyncGenerator[AsyncSession]:
|
|
yield db_session
|
|
|
|
app.dependency_overrides[get_db] = _override_db
|
|
app.dependency_overrides[get_agent_context] = lambda: _override_agent(
|
|
AgentRole.DEVELOPER
|
|
)
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
yield client
|
|
app.dependency_overrides.clear()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Legitimate caller (Auditor) succeeds
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_auditor_can_create_flag(auditor_client: AsyncClient) -> None:
|
|
response = await auditor_client.post(
|
|
"/api/dashboard/auditor/flags",
|
|
json={
|
|
"severity": "warning",
|
|
"category": "quality",
|
|
"title": "Flag",
|
|
"description": "x",
|
|
},
|
|
)
|
|
assert response.status_code == HTTPStatus.CREATED
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_auditor_can_create_report(auditor_client: AsyncClient) -> None:
|
|
response = await auditor_client.post(
|
|
"/api/dashboard/auditor/reports",
|
|
json={
|
|
"report_type": "weekly",
|
|
"title": "T",
|
|
"summary": "s",
|
|
"sections": [],
|
|
},
|
|
)
|
|
assert response.status_code == HTTPStatus.CREATED
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_auditor_can_send_report(auditor_client: AsyncClient) -> None:
|
|
create = await auditor_client.post(
|
|
"/api/dashboard/auditor/reports",
|
|
json={
|
|
"report_type": "weekly",
|
|
"title": "T",
|
|
"summary": "s",
|
|
"sections": [],
|
|
},
|
|
)
|
|
rid = create.json()["id"]
|
|
response = await auditor_client.post(f"/api/dashboard/auditor/reports/{rid}/send")
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_auditor_can_resolve_flag(auditor_client: AsyncClient) -> None:
|
|
create = await auditor_client.post(
|
|
"/api/dashboard/auditor/flags",
|
|
json={
|
|
"severity": "warning",
|
|
"category": "quality",
|
|
"title": "F",
|
|
"description": "x",
|
|
},
|
|
)
|
|
flag_id = create.json()["id"]
|
|
response = await auditor_client.put(
|
|
f"/api/dashboard/auditor/flags/{flag_id}/resolve",
|
|
params={"notes": "fixed"},
|
|
)
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Forged caller (Developer) is rejected with 403
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_developer_cannot_create_flag(dev_client: AsyncClient) -> None:
|
|
response = await dev_client.post(
|
|
"/api/dashboard/auditor/flags",
|
|
json={
|
|
"severity": "warning",
|
|
"category": "quality",
|
|
"title": "F",
|
|
"description": "x",
|
|
},
|
|
)
|
|
assert response.status_code == HTTPStatus.FORBIDDEN
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_developer_cannot_resolve_flag(dev_client: AsyncClient) -> None:
|
|
# The role gate fires before the route checks flag existence, so a random
|
|
# UUID is enough to prove the dev is rejected at the gate.
|
|
response = await dev_client.put(
|
|
f"/api/dashboard/auditor/flags/{uuid4()}/resolve",
|
|
params={"notes": "fixed"},
|
|
)
|
|
assert response.status_code == HTTPStatus.FORBIDDEN
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_developer_cannot_create_report(dev_client: AsyncClient) -> None:
|
|
response = await dev_client.post(
|
|
"/api/dashboard/auditor/reports",
|
|
json={
|
|
"report_type": "weekly",
|
|
"title": "T",
|
|
"summary": "s",
|
|
"sections": [],
|
|
},
|
|
)
|
|
assert response.status_code == HTTPStatus.FORBIDDEN
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_developer_cannot_send_report(dev_client: AsyncClient) -> None:
|
|
response = await dev_client.post(
|
|
f"/api/dashboard/auditor/reports/{uuid4()}/send",
|
|
)
|
|
assert response.status_code == HTTPStatus.FORBIDDEN
|