test_trusted_delivery_database_boundary_contract.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """Regression contract for the WP06 database write boundary."""
  2. from __future__ import annotations
  3. import importlib
  4. from pathlib import Path
  5. ROOT = Path(__file__).resolve().parents[2]
  6. def test_wp06_database_boundary_uses_preprovisioned_owner_and_definer_gateway():
  7. migration = importlib.import_module(
  8. "migrations.versions.20260811_486_trusted_delivery_database_boundary"
  9. )
  10. assert migration.down_revision == "20260811_485"
  11. source = Path(migration.__file__).read_text()
  12. for required in (
  13. "dataops_trusted_delivery_owner",
  14. "dataops_trusted_delivery_writer",
  15. "SECURITY DEFINER",
  16. "SET search_path = pg_catalog, public",
  17. "REVOKE ALL ON FUNCTION",
  18. "GRANT EXECUTE",
  19. "REVOKE INSERT, UPDATE, DELETE, TRUNCATE",
  20. "trusted_delivery_policy_versions",
  21. "trusted_delivery_control_evidence",
  22. ):
  23. assert required in source
  24. assert "CREATE ROLE" not in source
  25. assert "ALTER ROLE" not in source
  26. def test_runtime_bootstrap_does_not_grant_schema_wide_table_dml_and_precreates_owner_roles():
  27. source = (ROOT / "app/core/edge_gateway/runtime_roles.py").read_text()
  28. assert "dataops_trusted_delivery_owner" in source
  29. assert "dataops_trusted_delivery_writer" in source
  30. assert "ON ALL TABLES IN SCHEMA public" not in source
  31. assert "tablename NOT LIKE 'trusted_delivery_%'" in source
  32. def test_production_runtime_identity_rejects_trusted_delivery_owner_membership():
  33. source = (ROOT / "app/config/config.py").read_text()
  34. assert "trusted_delivery_owner_member" in source
  35. assert "dataops_trusted_delivery_owner" in source
  36. assert "dataops_trusted_delivery_writer" in source
  37. def test_wp06_all_runtime_writes_are_routed_through_the_v2_closed_gateway():
  38. migration = importlib.import_module(
  39. "migrations.versions.20260811_487_trusted_delivery_runtime_write_gateway"
  40. )
  41. assert migration.down_revision == "20260811_486"
  42. source = Path(migration.__file__).read_text()
  43. for operation in (
  44. "complete_delivery", "release_hold", "claim_reclaim", "transition_subscription",
  45. "subscription_attempt", "activate_control_profile", "approve_capability",
  46. "record_release_gate", "approve_destruction",
  47. ):
  48. assert operation in source
  49. for repository in (
  50. "app/core/system/trusted_delivery_repository.py",
  51. "app/core/system/trusted_delivery_subscription_repository.py",
  52. "app/core/system/trusted_delivery_controls_repository.py",
  53. ):
  54. assert "trusted_delivery_runtime_write" in (ROOT / repository).read_text()
  55. def test_wp06_approval_facts_and_incident_impacts_have_a_second_database_gate():
  56. migration = importlib.import_module(
  57. "migrations.versions.20260811_488_trusted_delivery_approval_and_incident_gate"
  58. )
  59. assert migration.down_revision == "20260811_487"
  60. source = Path(migration.__file__).read_text()
  61. for required in (
  62. "trusted_delivery_approval_is_valid",
  63. "trusted_delivery_runtime_incident_impact",
  64. "activate_control_profile",
  65. "release_hold",
  66. "data_incident_impacts",
  67. "REVOKE ALL ON FUNCTION",
  68. ):
  69. assert required in source
  70. def test_wp06_runtime_reclaim_is_one_fenced_database_operation():
  71. """Runtime reclaim must not compose separate delivery/grant writes."""
  72. migration = importlib.import_module(
  73. "migrations.versions.20260811_489_trusted_delivery_runtime_reclaim_gateway"
  74. )
  75. assert migration.down_revision == "20260811_488"
  76. source = Path(migration.__file__).read_text()
  77. for required in (
  78. "claim_reclaim",
  79. "fail_reclaim",
  80. "complete_reclaim",
  81. "trusted_delivery_runtime_reclaim_write",
  82. "lease_fence",
  83. "active legal hold blocks reclaim",
  84. "trusted_delivery_receipts",
  85. "SECURITY DEFINER",
  86. "REVOKE ALL ON FUNCTION",
  87. ):
  88. assert required in source
  89. def test_wp06_control_fact_trigger_selects_the_actor_before_validation():
  90. migration = importlib.import_module(
  91. "migrations.versions.20260811_491_trusted_delivery_control_fact_actor_fix"
  92. )
  93. assert migration.down_revision == "20260811_490"
  94. source = Path(migration.__file__).read_text()
  95. assert "IF TG_TABLE_NAME='trusted_delivery_control_evidence' THEN v_actor:=NEW.actor_uid" in source
  96. assert "v_actor:=NEW.created_by" in source
  97. assert "CASE WHEN TG_TABLE_NAME='trusted_delivery_control_evidence' THEN NEW.actor_uid" not in source.split("def downgrade", 1)[0]