test_trusted_delivery_controls_api.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from __future__ import annotations
  2. USER_UID = "01900000-0000-7000-8000-000000068801"
  3. class FakeControlsService:
  4. def active_profile_summary(self, profile_id):
  5. return {"profile_id": profile_id, "status": "active"}
  6. def evaluate_release_gate(self, payload, *, actor_uid):
  7. return {"decision_code": "RELEASE_GATE_APPROVED"}
  8. def activate_profile(self, payload, *, actor_uid, operation="activate"):
  9. return {"profile_id": payload.get("profile_id"), "status": "active", "operation": operation}
  10. def approve_capability(self, payload, *, actor_uid):
  11. return {"status": "approved"}
  12. def evidence_summary(self):
  13. return [{"evidence_ref": "trusted-evidence:api", "decision_code": "SAFE"}]
  14. def approve_destruction(self, payload, *, actor_uid):
  15. return {"status": "approved"}
  16. def _client(monkeypatch):
  17. from app import create_app
  18. from app.api.system import trusted_delivery_controls
  19. monkeypatch.setattr(trusted_delivery_controls, "_service", lambda: FakeControlsService())
  20. monkeypatch.setattr("app.core.system.auth.load_identity_from_token", lambda token, secret: {"id": USER_UID, "roles": [token]} if token in {"viewer", "editor", "admin"} else None)
  21. app = create_app()
  22. app.config.update(TESTING=True)
  23. return app.test_client()
  24. def test_controls_api_rbac_closed_schema_no_store_and_safe_viewer_summary(monkeypatch):
  25. client = _client(monkeypatch)
  26. viewer = {"Authorization": "Bearer viewer"}
  27. editor = {"Authorization": "Bearer editor"}
  28. admin = {"Authorization": "Bearer admin"}
  29. summary = client.get("/api/system/trusted-delivery/controls/profiles/finance/active", headers=viewer)
  30. assert summary.status_code == 200
  31. assert summary.headers["Cache-Control"] == "no-store"
  32. assert client.post("/api/system/trusted-delivery/controls/release-gates/evaluate", json={}, headers=viewer).status_code == 403
  33. response = client.post("/api/system/trusted-delivery/controls/release-gates/evaluate", json={"raw_scan": "secret"}, headers=editor)
  34. assert response.status_code == 400
  35. assert response.headers["Cache-Control"] == "no-store"
  36. assert "raw_scan" not in response.get_data(as_text=True)
  37. assert client.post("/api/system/trusted-delivery/controls/profiles/activate", json={}, headers=editor).status_code == 403
  38. for path in ("/api/system/trusted-delivery/controls/profiles/activate", "/api/system/trusted-delivery/controls/profiles/rollback", "/api/system/trusted-delivery/controls/destruction-approvals"):
  39. response = client.post(path, json={"unexpected": True}, headers=admin)
  40. assert response.status_code == 400
  41. assert response.headers["Cache-Control"] == "no-store"