test_trusted_delivery_subscription_api.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from __future__ import annotations
  2. USER_UID = "01900000-0000-7000-8000-000000069801"
  3. class FakeSubscriptionService:
  4. def create_subscription(self, payload, *, actor_uid):
  5. return {"uid": "01900000-0000-7000-8000-000000069901", "status": "draft"}
  6. def pause_subscription(self, uid, *, actor_uid):
  7. return {"uid": uid, "status": "paused"}
  8. def activate_subscription(self, uid, *, actor_uid):
  9. return {"uid": uid, "status": "active"}
  10. def resume_subscription(self, uid, *, actor_uid):
  11. return {"uid": uid, "status": "active"}
  12. def terminate_subscription(self, uid, *, actor_uid):
  13. return {"uid": uid, "status": "terminated"}
  14. def compensate_dead_letter(self, uid, *, reason_code, receipt_code, actor_uid):
  15. return {"uid": uid, "status": "compensated"}
  16. def report_anomaly(self, payload, *, actor_uid):
  17. return {"kind": "purpose_breach"}
  18. def _client(monkeypatch):
  19. from app import create_app
  20. from app.api.system import trusted_delivery
  21. monkeypatch.setattr(trusted_delivery, "_subscription_service", lambda: FakeSubscriptionService())
  22. 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)
  23. app = create_app()
  24. app.config.update(TESTING=True)
  25. return app.test_client()
  26. def test_subscription_api_enforces_rbac_closed_bodies_and_no_store(monkeypatch):
  27. client = _client(monkeypatch)
  28. editor = {"Authorization": "Bearer editor"}
  29. admin = {"Authorization": "Bearer admin"}
  30. assert client.post("/api/system/trusted-delivery/subscriptions", json={}, headers={"Authorization": "Bearer viewer"}).status_code == 403
  31. response = client.post("/api/system/trusted-delivery/subscriptions", json={"unexpected": True}, headers=editor)
  32. assert response.status_code == 400
  33. assert response.headers["Cache-Control"] == "no-store"
  34. assert client.post("/api/system/trusted-delivery/subscriptions/01900000-0000-7000-8000-000000069901/pause", json={}, headers=editor).status_code == 200
  35. assert client.post("/api/system/trusted-delivery/subscriptions/deliveries/01900000-0000-7000-8000-000000069902/compensate", json={}, headers=editor).status_code == 403
  36. response = client.post("/api/system/trusted-delivery/subscriptions/deliveries/01900000-0000-7000-8000-000000069902/compensate", json={"unexpected": True}, headers=admin)
  37. assert response.status_code == 400
  38. assert response.headers["Cache-Control"] == "no-store"
  39. def test_subscription_activate_is_operate_protected_and_no_store(monkeypatch):
  40. client = _client(monkeypatch)
  41. uid = "01900000-0000-7000-8000-000000069901"
  42. assert client.post(f"/api/system/trusted-delivery/subscriptions/{uid}/activate", json={}, headers={"Authorization": "Bearer viewer"}).status_code == 403
  43. response = client.post(f"/api/system/trusted-delivery/subscriptions/{uid}/activate", json={}, headers={"Authorization": "Bearer editor"})
  44. assert response.status_code == 200
  45. assert response.headers["Cache-Control"] == "no-store"