Files
govoplan-policy/tests/test_policy_module_contract.py
T

131 lines
4.7 KiB
Python

from __future__ import annotations
import pathlib
import tomllib
import unittest
from govoplan_core.core.policy import (
CAPABILITY_POLICY_DEFINITION_GOVERNANCE,
CAPABILITY_POLICY_FUNCTION_ASSIGNMENT_GOVERNANCE,
CAPABILITY_POLICY_PRIVACY_RETENTION,
CAPABILITY_POLICY_SCHEDULING_PARTICIPANT_PRIVACY,
CAPABILITY_POLICY_VIEW_GOVERNANCE,
)
from govoplan_core.core.access import CAPABILITY_POLICY_ACCESS_EXPLANATION_SUBJECTS
from govoplan_core.core.distribution_lists import (
CAPABILITY_POLICY_DISTRIBUTION_CHANNELS,
)
from govoplan_core.core.reporting import CAPABILITY_POLICY_REPORTING_GOVERNANCE
from govoplan_policy.backend.manifest import manifest
ROOT = pathlib.Path(__file__).resolve().parents[1]
class PolicyModuleContractTests(unittest.TestCase):
def test_policy_package_does_not_hard_require_access(self) -> None:
project = tomllib.loads((ROOT / "pyproject.toml").read_text(encoding="utf-8"))[
"project"
]
dependencies = tuple(project["dependencies"])
self.assertTrue(
any(item.startswith("govoplan-core>=") for item in dependencies)
)
self.assertFalse(
any(item.startswith("govoplan-access") for item in dependencies)
)
def test_policy_source_does_not_import_access_implementation(self) -> None:
offenders: list[str] = []
for path in (ROOT / "src" / "govoplan_policy").rglob("*.py"):
source = path.read_text(encoding="utf-8")
if "govoplan_access" in source:
offenders.append(str(path.relative_to(ROOT)))
self.assertEqual([], offenders)
def test_policy_manifest_exposes_policy_capabilities(self) -> None:
self.assertEqual(
{
CAPABILITY_POLICY_DEFINITION_GOVERNANCE,
CAPABILITY_POLICY_DISTRIBUTION_CHANNELS,
CAPABILITY_POLICY_FUNCTION_ASSIGNMENT_GOVERNANCE,
CAPABILITY_POLICY_PRIVACY_RETENTION,
CAPABILITY_POLICY_REPORTING_GOVERNANCE,
CAPABILITY_POLICY_SCHEDULING_PARTICIPANT_PRIVACY,
CAPABILITY_POLICY_VIEW_GOVERNANCE,
CAPABILITY_POLICY_ACCESS_EXPLANATION_SUBJECTS,
},
set(manifest.capability_factories),
)
def test_retention_documentation_exposes_stable_help_contexts(self) -> None:
topic = next(
item
for item in manifest.documentation
if item.id == "policy.hierarchy-overrides-and-retention"
)
self.assertTrue(
{
"policy.retention",
"privacy.retention",
"policy.retention.action.save",
"policy.retention.field.store-raw-campaign-json",
"policy.retention.field.generated-eml-retention-days",
"policy.retention.field.audit-detail-level",
"policy.retention.field.allow-lower-level-limits",
}.issubset(topic.metadata["help_contexts"])
)
self.assertEqual("workflow", topic.metadata["kind"])
self.assertIn("/admin", topic.metadata["route"])
self.assertEqual({"title", "summary", "body"}, set(topic.translations["de"]))
self.assertIn("Quellenpfad", topic.translations["de"]["body"])
execution_topic = next(
item
for item in manifest.documentation
if item.id == "policy.retention-execution-and-recovery"
)
self.assertEqual(
{
"policy.retention.execution",
"policy.retention.action.dry-run",
"policy.retention.action.apply",
"policy.retention.confirm-apply",
"policy.retention.outcome",
},
set(execution_topic.metadata["help_contexts"]),
)
self.assertIn(
"nicht wiederherstellen", execution_topic.translations["de"]["body"]
)
def test_view_policy_administration_contract_is_documented_and_exposed(
self,
) -> None:
topic = next(
item
for item in manifest.documentation
if item.id == "policy.view-governance-administration"
)
self.assertIn("policy.view-governance", topic.metadata["help_contexts"])
self.assertEqual(
{
"policy.admin.system-view-policy",
"policy.admin.tenant-view-policy",
"policy.admin.group-view-policy",
"policy.admin.user-view-policy",
},
{
surface.id
for surface in manifest.frontend.view_surfaces
if "view-policy" in surface.id
},
)
if __name__ == "__main__":
unittest.main()