test_trusted_delivery_api.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from __future__ import annotations
  2. USER_UID = "01900000-0000-7000-8000-000000068801"
  3. class FakeTrustedService:
  4. preview_calls = 0
  5. mutation_calls = 0
  6. def create_policy_version(self, payload, *, actor_uid):
  7. return {"uid": "policy-1", "status": "active"}
  8. def evaluate_gateway(self, payload):
  9. return {"decision": "deny", "reason_code": "default_deny"}
  10. def create_legal_hold(self, payload, *, actor_uid):
  11. return {"uid": "hold-1", "status": "active"}
  12. def activate_policy_version(self, payload, *, actor_uid):
  13. return {"uid": "policy-1", "status": "active"}
  14. def rollback_policy_version(self, payload, *, actor_uid):
  15. return {"uid": "policy-1", "status": "active"}
  16. def provision(self, payload, *, actor_uid):
  17. return {"grant_uid": "grant-1", "status": "applied"}
  18. def release_legal_hold(self, payload, *, actor_uid):
  19. return {"uid": "hold-1", "status": "released"}
  20. def execute_reclaim(self, payload, *, actor_uid):
  21. return {"grant_uid": "grant-1", "status": "reclaimed"}
  22. def revoke_expired(self, *, as_of, actor_uid):
  23. type(self).mutation_calls += 1
  24. return []
  25. def preview_reclaim(self, *, as_of, actor_uid):
  26. type(self).preview_calls += 1
  27. return [{"grant_uid": "grant-1", "status": "eligible"}]
  28. def _client(monkeypatch):
  29. from app import create_app
  30. from app.api.system import trusted_delivery
  31. monkeypatch.setattr(trusted_delivery, "_service", lambda: FakeTrustedService())
  32. 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)
  33. app = create_app()
  34. app.config.update(TESTING=True)
  35. return app.test_client()
  36. def test_trusted_delivery_api_permissions_closed_schema_and_no_store(monkeypatch):
  37. client = _client(monkeypatch)
  38. headers = {"Authorization": "Bearer editor"}
  39. assert client.post("/api/system/trusted-delivery/policy-versions", json={}, headers={"Authorization": "Bearer viewer"}).status_code == 403
  40. response = client.post("/api/system/trusted-delivery/policy-versions", json={"unexpected": True}, headers=headers)
  41. assert response.status_code == 400
  42. assert response.headers["Cache-Control"] == "no-store"
  43. assert "unexpected" not in response.get_data(as_text=True)
  44. decision = client.post("/api/system/trusted-delivery/decisions", json={}, headers=headers)
  45. assert decision.status_code == 200
  46. assert decision.headers["Cache-Control"] == "no-store"
  47. assert client.post("/api/system/trusted-delivery/legal-holds", json={}, headers=headers).status_code == 403
  48. assert client.post("/api/system/trusted-delivery/legal-holds", json={}, headers={"Authorization": "Bearer admin"}).status_code == 201
  49. def test_trusted_delivery_lifecycle_api_has_separate_operate_manage_boundaries(monkeypatch):
  50. client = _client(monkeypatch)
  51. editor = {"Authorization": "Bearer editor"}
  52. admin = {"Authorization": "Bearer admin"}
  53. for path in ("/api/system/trusted-delivery/policy-versions/activate", "/api/system/trusted-delivery/policy-versions/rollback", "/api/system/trusted-delivery/provisions"):
  54. response = client.post(path, json={"unexpected": True}, headers=editor)
  55. assert response.status_code == 400
  56. assert response.headers["Cache-Control"] == "no-store"
  57. for path in ("/api/system/trusted-delivery/legal-holds/release", "/api/system/trusted-delivery/reclaim/execute"):
  58. assert client.post(path, json={}, headers=editor).status_code == 403
  59. response = client.post(path, json={"unexpected": True}, headers=admin)
  60. assert response.status_code == 400
  61. assert response.headers["Cache-Control"] == "no-store"
  62. def test_reclaim_preview_is_read_only(monkeypatch):
  63. client = _client(monkeypatch)
  64. response = client.post("/api/system/trusted-delivery/reclaim/preview", json={}, headers={"Authorization": "Bearer admin"})
  65. assert response.status_code == 200
  66. assert FakeTrustedService.preview_calls == 1
  67. assert FakeTrustedService.mutation_calls == 0
  68. assert response.headers["Cache-Control"] == "no-store"