Per-site configurable cookie categories (#3)
* feat: per-site configurable cookie categories
Operators can now choose which cookie categories the banner displays
for a given site — useful for sites that genuinely don't use
e.g. marketing cookies and shouldn't be forced to show the toggle.
**Backend**
* New ``enabled_categories`` JSONB column on ``site_configs``,
``site_group_configs``, and ``org_configs`` (migration 0003).
NULL at a level means "inherit"; an explicit list overrides.
* ``config_resolver`` merges ``enabled_categories`` through the
existing cascade (system → org → group → site) and normalises
the result via ``_normalise_enabled_categories``:
- Unknown slugs stripped.
- ``necessary`` is forced in regardless of the operator's input
— it's never optional.
- Empty / invalid values fall back to the full five-category
default so a cleared field doesn't silently hide the banner.
- Output is returned in canonical display order so insertion
order from the cascade doesn't leak into the UI.
* ``build_public_config`` surfaces ``enabled_categories`` to the
banner-facing public config endpoint.
* Schemas for site/group/org config create + update + response all
include the new field.
**Banner**
* ``apps/banner/src/banner.ts`` replaces the hard-coded
``ALL_CATEGORIES`` / ``NON_ESSENTIAL`` constants with a runtime
``resolveEnabledCategories(config)`` helper. ``renderCategories``
takes the enabled list and only renders toggles for those
categories; ``nonEssentialFor(enabled)`` derives the user-toggleable
subset. Falls back to all five when the field is missing in the
config payload so older banner bundles against newer APIs (and
vice versa) don't break.
* ``SiteConfig`` type in ``apps/banner/src/types.ts`` has
``enabled_categories?: CategorySlug[]`` to match.
**Admin UI**
* New ``SiteCategoriesTab`` component — five checkboxes, ``necessary``
locked on, with "Reset to inherited" to clear the site override.
Wired in as a new core tab on ``SiteDetailPage`` between
Configuration and Cookies.
* ``SiteConfig`` type in ``types/api.ts`` declares ``enabled_categories``
and a new ``ALL_COOKIE_CATEGORIES`` constant exposing label/description
metadata shared between the tab component and any future display of
the list.
**Semantics of a disabled category**
When the operator unticks e.g. ``marketing`` for a site:
* The toggle is not rendered in the banner.
* A visitor can never grant consent for ``marketing``.
* Any cookie or script that classifies into ``marketing`` stays
blocked permanently by the auto-blocker.
That's the correct behaviour for sites that genuinely don't use a
category: declare it, hide it from the visitor, have the blocker
enforce it.
**Tests**
* ``test_config_resolver.py`` — 13 new cases covering the full
cascade, ``necessary`` forcing, unknown-slug stripping, empty /
non-list values, canonical display order, and the public-config
surface. 37 passed total.
* ``test_SiteCategoriesTab.test.tsx`` — renders all five, locks
``necessary``, pre-fills from an override, saves the explicit
list, and resets to inherited by sending NULL. 6 cases.
* Full API suite (610) and admin-ui suite (139) both green;
banner bundle builds cleanly with 363 tests passing.
* style: ruff format config_resolver.py
This commit is contained in:
@@ -5,7 +5,9 @@ import uuid
|
||||
import pytest
|
||||
|
||||
from src.services.config_resolver import (
|
||||
ALL_CATEGORIES,
|
||||
SYSTEM_DEFAULTS,
|
||||
_normalise_enabled_categories,
|
||||
build_public_config,
|
||||
resolve_config,
|
||||
)
|
||||
@@ -256,3 +258,72 @@ class TestConfigRoutes:
|
||||
site_id = uuid.uuid4()
|
||||
resp = await client.post(f"/api/v1/config/sites/{site_id}/publish")
|
||||
assert resp.status_code == 401
|
||||
|
||||
|
||||
class TestEnabledCategories:
|
||||
"""Cascade semantics for ``enabled_categories``."""
|
||||
|
||||
def test_system_default_is_all_five(self):
|
||||
result = resolve_config({})
|
||||
assert result["enabled_categories"] == ALL_CATEGORIES
|
||||
|
||||
def test_site_override_narrows_system_default(self):
|
||||
result = resolve_config({"enabled_categories": ["necessary", "analytics"]})
|
||||
assert result["enabled_categories"] == ["necessary", "analytics"]
|
||||
|
||||
def test_site_override_beats_org_override(self):
|
||||
result = resolve_config(
|
||||
site_config={"enabled_categories": ["necessary", "marketing"]},
|
||||
org_defaults={"enabled_categories": ["necessary", "analytics"]},
|
||||
)
|
||||
assert result["enabled_categories"] == ["necessary", "marketing"]
|
||||
|
||||
def test_group_override_beats_org_override_when_site_unset(self):
|
||||
result = resolve_config(
|
||||
site_config={},
|
||||
org_defaults={"enabled_categories": ["necessary", "analytics"]},
|
||||
group_defaults={"enabled_categories": ["necessary", "functional"]},
|
||||
)
|
||||
assert result["enabled_categories"] == ["necessary", "functional"]
|
||||
|
||||
def test_unset_site_inherits_org(self):
|
||||
result = resolve_config(
|
||||
site_config={},
|
||||
org_defaults={"enabled_categories": ["necessary", "marketing"]},
|
||||
)
|
||||
assert result["enabled_categories"] == ["necessary", "marketing"]
|
||||
|
||||
def test_necessary_forced_in_when_missing_from_override(self):
|
||||
"""Operators can't accidentally drop ``necessary``."""
|
||||
result = resolve_config({"enabled_categories": ["analytics", "marketing"]})
|
||||
assert "necessary" in result["enabled_categories"]
|
||||
assert result["enabled_categories"] == ["necessary", "analytics", "marketing"]
|
||||
|
||||
def test_unknown_slugs_are_stripped(self):
|
||||
result = resolve_config({"enabled_categories": ["necessary", "spam", "analytics"]})
|
||||
assert result["enabled_categories"] == ["necessary", "analytics"]
|
||||
|
||||
def test_empty_list_falls_back_to_default(self):
|
||||
"""An empty list is treated as 'no categories configured' → default."""
|
||||
result = resolve_config({"enabled_categories": []})
|
||||
assert result["enabled_categories"] == ALL_CATEGORIES
|
||||
|
||||
def test_non_list_value_falls_back_to_default(self):
|
||||
result = resolve_config({"enabled_categories": "not-a-list"}) # type: ignore[dict-item]
|
||||
assert result["enabled_categories"] == ALL_CATEGORIES
|
||||
|
||||
def test_result_is_in_canonical_display_order(self):
|
||||
"""Insertion order from the cascade must not leak into the output."""
|
||||
result = resolve_config({"enabled_categories": ["marketing", "necessary", "analytics"]})
|
||||
assert result["enabled_categories"] == ["necessary", "analytics", "marketing"]
|
||||
|
||||
def test_public_config_includes_enabled_categories(self):
|
||||
resolved = resolve_config({"enabled_categories": ["necessary", "analytics"]})
|
||||
public = build_public_config("site-xyz", resolved)
|
||||
assert public["enabled_categories"] == ["necessary", "analytics"]
|
||||
|
||||
def test_normalise_handles_none(self):
|
||||
assert _normalise_enabled_categories(None) == ALL_CATEGORIES
|
||||
|
||||
def test_normalise_preserves_explicit_full_list(self):
|
||||
assert _normalise_enabled_categories(list(ALL_CATEGORIES)) == ALL_CATEGORIES
|
||||
|
||||
Reference in New Issue
Block a user