107 lines
4.3 KiB
Python
107 lines
4.3 KiB
Python
# #region Test.Api.Scenarios [C:3] [TYPE Module] [SEMANTICS testing,api,scenario,rbac]
|
|
# @defgroup Test.Api Scenario REST API tests — compile/validate/resolve/draft-pack + RBAC.
|
|
# @LAYER Test
|
|
# @RELATION BINDS_TO -> [Api.DashboardTesting.Scenario]
|
|
# @RATIONALE The REST surface must expose deterministic scenario operations with RBAC enforcement.
|
|
# @REJECTED Testing only the service layer — would leave authn/z and envelope drift unchecked.
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from src.app import app
|
|
|
|
_FIXTURES = Path(__file__).resolve().parents[1] / "fixtures" / "dashboard_scenarios"
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def _scenario_json() -> dict:
|
|
return json.loads((_FIXTURES / "scenario_valid.json").read_text(encoding="utf-8"))
|
|
|
|
|
|
def test_compile_requires_auth() -> None:
|
|
resp = client.post("/api/dashboard-testing/scenarios/compile", json={})
|
|
assert resp.status_code in (401, 403)
|
|
|
|
|
|
def test_compile_rejects_extra_fields() -> None:
|
|
body = {
|
|
"agent_run_id": "x", "objective": {"goal": "g", "selected_case_ids": []},
|
|
"query_model": {}, "checklist_catalog_version": 1, "baseline_version": "v",
|
|
"capabilities": {}, "parameters": {}, "has_dataset_fields": True,
|
|
"environment_id": "e", "dashboard_id": 1, "dashboard_name": "d",
|
|
"executable_code": "import os",
|
|
}
|
|
resp = client.post("/api/dashboard-testing/scenarios/compile", json=body)
|
|
# either auth gate (401/403) or 422 extra-forbid
|
|
assert resp.status_code in (401, 403, 422)
|
|
|
|
|
|
def test_validate_accepts_scenario_shape() -> None:
|
|
resp = client.post("/api/dashboard-testing/scenarios/validate", json=_scenario_json())
|
|
# auth gate may block first
|
|
assert resp.status_code in (401, 403, 200)
|
|
|
|
|
|
def test_resolve_rejects_stale_revision_shape() -> None:
|
|
body = {"base_revision_hash": "0" * 64, "changes": [{"kind": "parameter", "target": "test_date", "value": "2026-08-01"}]}
|
|
resp = client.post("/api/dashboard-testing/scenarios/fi-0080_verify/resolve", json=body)
|
|
assert resp.status_code in (401, 403, 409, 422)
|
|
|
|
|
|
def test_resolve_accepts_nested_req_scenario_shape() -> None:
|
|
"""The /resolve route declares two body models (`req`, `scenario`); FastAPI embeds
|
|
them by parameter name, so the agent tool must send {"req": {...}, "scenario": {...}}.
|
|
|
|
A bare FastAPI body 422 has list-typed `detail` (missing field). A 422/409 from
|
|
resolve_scenario has object-typed `detail` ({"code": ...}) — proving the body parsed.
|
|
"""
|
|
from types import SimpleNamespace
|
|
|
|
from src.app import app
|
|
from src.dependencies import get_current_user
|
|
|
|
admin = SimpleNamespace(
|
|
id="admin", username="admin",
|
|
roles=[SimpleNamespace(is_admin=True, permissions=[])],
|
|
)
|
|
app.dependency_overrides[get_current_user] = lambda: admin
|
|
try:
|
|
body = {
|
|
"req": {"base_revision_hash": "0" * 64, "changes": []},
|
|
"scenario": _scenario_json(),
|
|
}
|
|
resp = client.post("/api/dashboard-testing/scenarios/fi-0080_verify/resolve", json=body)
|
|
assert resp.status_code in (200, 409, 422), f"unexpected status {resp.status_code}: {resp.text[:300]}"
|
|
if resp.status_code == 422:
|
|
# Body parsed (nested shape accepted); the 422 came from resolve_scenario.
|
|
assert isinstance(resp.json().get("detail"), dict), "unexpected body-validation 422"
|
|
finally:
|
|
app.dependency_overrides.pop(get_current_user, None)
|
|
|
|
|
|
def test_draft_pack_requires_auth() -> None:
|
|
resp = client.post("/api/dashboard-testing/scenarios/scn-1/draft-pack", json={})
|
|
assert resp.status_code in (401, 403)
|
|
|
|
|
|
def test_capture_route_registered() -> None:
|
|
resp = client.post("/api/dashboard-testing/scenarios/scn-1/capture", json={"profile_id": "default", "step_id": "s1"})
|
|
assert resp.status_code in (401, 403, 422, 200)
|
|
|
|
|
|
def test_vlm_route_rejects_bad_analysis() -> None:
|
|
resp = client.post("/api/dashboard-testing/scenarios/scn-1/vlm", json={"analysis": {"provider_id": "p"}})
|
|
assert resp.status_code in (401, 403, 422)
|
|
|
|
|
|
def test_disposition_route_rejects_empty() -> None:
|
|
resp = client.post("/api/dashboard-testing/scenarios/scn-1/disposition", json={"findings": [], "dispositions": []})
|
|
assert resp.status_code in (401, 403, 200, 422)
|
|
|
|
# #endregion Test.Api.Scenarios
|