feat(dashboard): add governed DSAR coverage
This commit is contained in:
@@ -0,0 +1,356 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import unittest
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from govoplan_core.core.dsar import (
|
||||
DsarErasureActionRef,
|
||||
DsarProvider,
|
||||
DsarRecordRef,
|
||||
DsarSubjectRef,
|
||||
)
|
||||
from govoplan_core.db.base import Base
|
||||
from govoplan_core.privacy.dsar_workflow import (
|
||||
create_data_subject_request,
|
||||
search_data_subject_request,
|
||||
)
|
||||
from govoplan_dashboard.backend.db.models import DashboardLayout
|
||||
from govoplan_dashboard.backend.dsar_provider import (
|
||||
DASHBOARD_DSAR_CAPABILITY,
|
||||
DashboardDsarProvider,
|
||||
)
|
||||
from govoplan_dashboard.backend.manifest import manifest
|
||||
|
||||
|
||||
class _Registry:
|
||||
def __init__(
|
||||
self,
|
||||
provider: DashboardDsarProvider,
|
||||
*,
|
||||
active: bool = True,
|
||||
) -> None:
|
||||
self.provider = provider
|
||||
self.active = active
|
||||
|
||||
def capability_names(self):
|
||||
return (DASHBOARD_DSAR_CAPABILITY,)
|
||||
|
||||
def capability_owner(self, name):
|
||||
self._assert_capability(name)
|
||||
return "dashboard"
|
||||
|
||||
def tenant_entitlement_resolver(self):
|
||||
active = self.active
|
||||
|
||||
class _Resolver:
|
||||
@staticmethod
|
||||
def resolve(session, tenant_id):
|
||||
del session, tenant_id
|
||||
return type(
|
||||
"State",
|
||||
(),
|
||||
{"effective_modules": ("dashboard",) if active else ()},
|
||||
)()
|
||||
|
||||
return _Resolver()
|
||||
|
||||
def require_tenant_capability(self, name, session, **kwargs):
|
||||
del session, kwargs
|
||||
self._assert_capability(name)
|
||||
return self.provider
|
||||
|
||||
def manifests(self):
|
||||
return (type("Manifest", (), {"id": "dashboard"})(),)
|
||||
|
||||
@staticmethod
|
||||
def _assert_capability(name: str) -> None:
|
||||
if name != DASHBOARD_DSAR_CAPABILITY:
|
||||
raise KeyError(name)
|
||||
|
||||
|
||||
class DashboardDsarProviderTests(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.engine = create_engine("sqlite+pysqlite:///:memory:")
|
||||
Base.metadata.create_all(self.engine)
|
||||
self.session = Session(self.engine)
|
||||
self.provider = DashboardDsarProvider()
|
||||
self.assertIsInstance(self.provider, DsarProvider)
|
||||
self._seed()
|
||||
self.session.commit()
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self.session.close()
|
||||
self.engine.dispose()
|
||||
|
||||
def _seed(self) -> None:
|
||||
def layout(
|
||||
layout_id: str,
|
||||
*,
|
||||
tenant_id: str = "tenant-1",
|
||||
account_id: str = "account-1",
|
||||
view_id: str | None = None,
|
||||
) -> DashboardLayout:
|
||||
return DashboardLayout(
|
||||
id=layout_id,
|
||||
tenant_id=tenant_id,
|
||||
account_id=account_id,
|
||||
context_key=f"view:{view_id}" if view_id else "full",
|
||||
view_id=view_id,
|
||||
layout_version=1,
|
||||
revision=3,
|
||||
placements=[
|
||||
{
|
||||
"instance_id": f"instance-{layout_id}",
|
||||
"widget_id": "reporting.metric",
|
||||
"size": "medium",
|
||||
"column_start": 2,
|
||||
"configuration": {
|
||||
"subjectFilter": f"private-{layout_id}-do-not-export"
|
||||
},
|
||||
}
|
||||
],
|
||||
known_widget_ids=["reporting.metric"],
|
||||
)
|
||||
|
||||
self.session.add_all(
|
||||
(
|
||||
layout("layout-full"),
|
||||
layout("layout-view", view_id="view-1"),
|
||||
layout("layout-other-account", account_id="account-other"),
|
||||
layout("layout-other-tenant", tenant_id="tenant-2"),
|
||||
)
|
||||
)
|
||||
|
||||
def test_search_is_tenant_and_account_scoped_and_minimized(self) -> None:
|
||||
records = self.provider.search_subject(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=DsarSubjectRef(account_id="account-1"),
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
["layout-full", "layout-view"],
|
||||
[record.resource_id for record in records],
|
||||
)
|
||||
exported = json.dumps([record.to_dict() for record in records])
|
||||
self.assertNotIn("private-layout-full-do-not-export", exported)
|
||||
self.assertNotIn("private-layout-view-do-not-export", exported)
|
||||
self.assertNotIn("layout-other-account", exported)
|
||||
self.assertNotIn("layout-other-tenant", exported)
|
||||
self.assertIn("reporting.metric", exported)
|
||||
|
||||
def test_references_narrow_and_conflicts_fail_closed(self) -> None:
|
||||
by_layout = self.provider.search_subject(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=DsarSubjectRef(
|
||||
account_id="account-1",
|
||||
external_references={"dashboard.layout": "layout-view"},
|
||||
),
|
||||
)
|
||||
by_view = self.provider.search_subject(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=DsarSubjectRef(
|
||||
account_id="account-1",
|
||||
external_references={"dashboard.view": "view-1"},
|
||||
),
|
||||
)
|
||||
conflict = self.provider.search_subject(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=DsarSubjectRef(
|
||||
account_id="account-1",
|
||||
external_references={"dashboard.account": "account-other"},
|
||||
),
|
||||
)
|
||||
without_account = self.provider.search_subject(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=DsarSubjectRef(
|
||||
external_references={"dashboard.layout": "layout-view"}
|
||||
),
|
||||
)
|
||||
|
||||
self.assertEqual(["layout-view"], [item.resource_id for item in by_layout])
|
||||
self.assertEqual(["layout-view"], [item.resource_id for item in by_view])
|
||||
self.assertEqual((), conflict)
|
||||
self.assertEqual((), without_account)
|
||||
|
||||
def test_erasure_is_owner_scoped_revision_safe_and_idempotent(self) -> None:
|
||||
subject = DsarSubjectRef(
|
||||
account_id="account-1",
|
||||
external_references={"dashboard.layout": "layout-view"},
|
||||
)
|
||||
records = self.provider.search_subject(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=subject,
|
||||
)
|
||||
actions = self.provider.plan_erasure(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=subject,
|
||||
records=records,
|
||||
)
|
||||
|
||||
self.assertEqual(1, len(actions))
|
||||
self.assertEqual("delete", actions[0].kind)
|
||||
first = self.provider.execute_erasure(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=subject,
|
||||
actions=actions,
|
||||
request_id="dsar-1",
|
||||
)
|
||||
second = self.provider.execute_erasure(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=subject,
|
||||
actions=actions,
|
||||
request_id="dsar-1",
|
||||
)
|
||||
|
||||
self.assertEqual("executed", first[0].status)
|
||||
self.assertEqual("unchanged", second[0].status)
|
||||
self.assertIsNone(self.session.get(DashboardLayout, "layout-view"))
|
||||
self.assertIsNotNone(self.session.get(DashboardLayout, "layout-full"))
|
||||
self.assertIsNotNone(
|
||||
self.session.get(DashboardLayout, "layout-other-account")
|
||||
)
|
||||
self.assertIsNotNone(
|
||||
self.session.get(DashboardLayout, "layout-other-tenant")
|
||||
)
|
||||
|
||||
def test_changed_or_foreign_resources_are_blocked(self) -> None:
|
||||
subject = DsarSubjectRef(account_id="account-1")
|
||||
record = self.provider.search_subject(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=DsarSubjectRef(
|
||||
account_id="account-1",
|
||||
external_references={"dashboard.layout": "layout-full"},
|
||||
),
|
||||
)[0]
|
||||
action = self.provider.plan_erasure(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=subject,
|
||||
records=(record,),
|
||||
)[0]
|
||||
self.session.get(DashboardLayout, "layout-full").revision += 1
|
||||
changed = self.provider.execute_erasure(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=subject,
|
||||
actions=(action,),
|
||||
request_id="dsar-1",
|
||||
)
|
||||
self.assertEqual("blocked", changed[0].status)
|
||||
|
||||
with self.assertRaisesRegex(ValueError, "foreign provider record"):
|
||||
self.provider.plan_erasure(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=subject,
|
||||
records=(
|
||||
DsarRecordRef(
|
||||
provider_id="views",
|
||||
module_id="views",
|
||||
resource_type="dashboard_layout",
|
||||
resource_id="layout-full",
|
||||
category="preference",
|
||||
title="Foreign layout",
|
||||
),
|
||||
),
|
||||
)
|
||||
with self.assertRaisesRegex(ValueError, "foreign provider action"):
|
||||
self.provider.execute_erasure(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
subject=subject,
|
||||
actions=(
|
||||
DsarErasureActionRef(
|
||||
action_id="views:delete:dashboard_layout:layout-full",
|
||||
provider_id="views",
|
||||
module_id="views",
|
||||
kind="delete",
|
||||
resource_type="dashboard_layout",
|
||||
resource_id="layout-full",
|
||||
title="Delete layout",
|
||||
rationale="Foreign action",
|
||||
executable=True,
|
||||
),
|
||||
),
|
||||
request_id="dsar-1",
|
||||
)
|
||||
|
||||
def test_core_workflow_reports_active_and_inactive_provider(self) -> None:
|
||||
row = create_data_subject_request(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
reference="DSAR-DASHBOARD-1",
|
||||
request_kind="access",
|
||||
subject=DsarSubjectRef(account_id="account-1"),
|
||||
purpose="Respond to a verified request.",
|
||||
legal_basis="Article 15 GDPR",
|
||||
due_at=None,
|
||||
requested_by_account_id="privacy-officer",
|
||||
)
|
||||
self.session.commit()
|
||||
search_data_subject_request(
|
||||
self.session,
|
||||
registry=_Registry(self.provider),
|
||||
row=row,
|
||||
expected_revision=1,
|
||||
)
|
||||
self.assertEqual(
|
||||
[DASHBOARD_DSAR_CAPABILITY], row.coverage["provider_capabilities"]
|
||||
)
|
||||
self.assertEqual(2, row.search_result["record_count"])
|
||||
|
||||
inactive = create_data_subject_request(
|
||||
self.session,
|
||||
tenant_id="tenant-1",
|
||||
reference="DSAR-DASHBOARD-2",
|
||||
request_kind="access",
|
||||
subject=DsarSubjectRef(account_id="account-1"),
|
||||
purpose="Respond to a verified request.",
|
||||
legal_basis="Article 15 GDPR",
|
||||
due_at=None,
|
||||
requested_by_account_id="privacy-officer",
|
||||
)
|
||||
self.session.commit()
|
||||
search_data_subject_request(
|
||||
self.session,
|
||||
registry=_Registry(self.provider, active=False),
|
||||
row=inactive,
|
||||
expected_revision=1,
|
||||
)
|
||||
self.assertEqual([], inactive.coverage["provider_capabilities"])
|
||||
self.assertEqual(
|
||||
[DASHBOARD_DSAR_CAPABILITY],
|
||||
inactive.coverage["inactive_provider_capabilities"],
|
||||
)
|
||||
|
||||
def test_manifest_registers_and_documents_the_capability(self) -> None:
|
||||
self.assertIn(DASHBOARD_DSAR_CAPABILITY, manifest.capability_factories)
|
||||
self.assertIn(DASHBOARD_DSAR_CAPABILITY, manifest.capability_documentation)
|
||||
self.assertIn(
|
||||
DASHBOARD_DSAR_CAPABILITY,
|
||||
{item.name for item in manifest.provides_interfaces},
|
||||
)
|
||||
self.assertTrue(
|
||||
any(
|
||||
topic.id == "dashboard.data-subject-requests"
|
||||
and {"admin", "user"}.issubset(topic.documentation_types)
|
||||
for topic in manifest.documentation
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user