Add module event producer coverage
Some checks failed
Dependency Audit / dependency-audit (push) Has been cancelled
Module Matrix / module-matrix (push) Has been cancelled

This commit is contained in:
2026-07-10 18:40:38 +02:00
parent 83784ea19d
commit c61abe154c
10 changed files with 332 additions and 26 deletions

View File

@@ -16,6 +16,7 @@ from govoplan_core.core.events import (
EventTenantRef,
PlatformEvent,
current_event_trace,
event_bus_context,
event_context,
normalize_trace_id,
)
@@ -149,6 +150,83 @@ class CoreEventTests(unittest.TestCase):
finally:
shutil.rmtree(root, ignore_errors=True)
def test_audit_event_publishes_governed_platform_event(self) -> None:
root = Path(tempfile.mkdtemp(prefix="govoplan-audit-event-"))
try:
with temporary_database(f"sqlite:///{root / 'audit.db'}") as database:
from govoplan_access.backend.db import models as access_models # noqa: F401
from govoplan_admin.backend.db import models as admin_models # noqa: F401
from govoplan_audit.backend.db import models as audit_models # noqa: F401
from govoplan_tenancy.backend.db import models as tenancy_models # noqa: F401
Base.metadata.create_all(bind=database.engine)
seen: list[PlatformEvent] = []
bus = EventBus()
bus.subscribe("*", seen.append)
with database.session() as session:
with event_bus_context(bus), event_context(correlation_id="corr-1"):
item = audit_event(
session,
tenant_id="tenant-1",
user_id="user-1",
scope="tenant",
action="user.updated",
object_type="user",
object_id="user-2",
details={"password": "secret", "field": "display_name"},
)
action_events = [event for event in seen if event.type == "user.updated"]
self.assertEqual(1, len(action_events))
event = action_events[0]
self.assertEqual("access", event.module_id)
self.assertEqual("corr-1", event.correlation_id)
self.assertEqual(EventActorRef(type="user", id="user-1"), event.actor)
self.assertEqual(EventTenantRef(id="tenant-1"), event.tenant)
self.assertEqual(EventObjectRef(type="user", id="user-2"), event.resource)
self.assertEqual(item.id, event.payload["audit_log_id"])
self.assertEqual("<redacted>", event.payload["details"]["password"])
finally:
shutil.rmtree(root, ignore_errors=True)
def test_audit_events_map_existing_module_action_prefixes(self) -> None:
root = Path(tempfile.mkdtemp(prefix="govoplan-audit-module-events-"))
try:
with temporary_database(f"sqlite:///{root / 'audit.db'}") as database:
from govoplan_access.backend.db import models as access_models # noqa: F401
from govoplan_admin.backend.db import models as admin_models # noqa: F401
from govoplan_audit.backend.db import models as audit_models # noqa: F401
from govoplan_tenancy.backend.db import models as tenancy_models # noqa: F401
Base.metadata.create_all(bind=database.engine)
seen: list[PlatformEvent] = []
bus = EventBus()
bus.subscribe("*", seen.append)
cases = {
"user.created": "access",
"tenant.created": "tenancy",
"privacy_retention.policy_updated": "policy",
"files.connector.imported": "files",
"campaign.created": "campaigns",
"report.email_sent": "campaigns",
}
with database.session() as session:
with event_bus_context(bus):
for action in cases:
audit_event(
session,
tenant_id="tenant-1",
user_id="user-1",
action=action,
object_type="demo",
object_id=action,
)
by_type = {event.type: event.module_id for event in seen if event.type in cases}
self.assertEqual(cases, by_type)
finally:
shutil.rmtree(root, ignore_errors=True)
def test_audit_operation_context_keeps_lifecycle_ids_and_sanitizes_details(self) -> None:
payload = audit_operation_context(
module_id="mail",