Files
FabledGrimoire/tests/test_service_user_id_contract.py
T
bvandeusenandClaude Opus 5.5 198bf110c2
CI & Build / TypeScript typecheck (push) Successful in 11s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / Python tests (push) Successful in 2m19s
CI & Build / integration (push) Successful in 2m46s
CI & Build / Build & push image (push) Successful in 38s
test(8565): idea_key is a named exception to the caller contract, and the topic test finds the idea under its family record
idea_key translates an id the caller passed; access is checked on its result
by every caller. The topic test listed ideas by the project record's id, which
promotion now moves onto a family record.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 15:22:08 -05:00

74 lines
3.1 KiB
Python

"""Every public service the routes and MCP tools call is scoped by its caller.
The access helpers (services/access.py) need the caller's id, so a public
coroutine in these modules takes `user_id`. A new one without it fails here,
before a route passes it nothing. The exceptions are named with the reason
each needs none: they work on records the caller has already been allowed to
read, on ids it already holds, or on the whole instance.
"""
import ast
import pathlib
import pytest
SERVICES = pathlib.Path(__file__).resolve().parents[1] / "src" / "grimoire" / "services"
NEEDS_NO_CALLER = {
"systems": {
"standard_systems": "the shared vocabulary, the same for everyone",
"system_names_for": "names for records the caller has already read",
},
"rulebooks": {
"list_rule_moments": "batch detail for rule ids the caller already holds",
"list_rule_systems": "batch detail for rule ids the caller already holds",
"list_rule_relations": "batch detail for rule ids the caller already holds",
},
"snippets": {
"backfill_snippet_data": "a startup backfill over every snippet",
"attach_live_body": "decorates a snippet the caller has already pulled",
"invalidate_for_push": "a forge push touches every snippet at that location",
},
"design_systems": {},
"family": {
"next_version": "numbers a canon version inside a write that has checked the caller",
"idea_key": "translates an id the caller passed to the id its idea is kept under; "
"every caller checks access on the result next",
},
"family_adoption": {
"family_reference_ids": "the coverage refresh's mechanical read for a project",
"family_reach_ids": "retrieval's reach for a project it has already scoped",
},
"platforms": {
"list_platforms": "the catalog is global and the same for everyone",
"resolve_slugs": "a lookup in the global catalog",
"record_detected": "the coverage refresh records what it detected",
"detect_for_project": "the coverage refresh's own call",
},
}
def _public_coroutines(module: str) -> dict[str, list[str]]:
tree = ast.parse((SERVICES / f"{module}.py").read_text())
return {
n.name: [a.arg for a in n.args.args + n.args.kwonlyargs]
for n in tree.body
if isinstance(n, ast.AsyncFunctionDef) and not n.name.startswith("_")
}
@pytest.mark.parametrize("module", sorted(NEEDS_NO_CALLER))
def test_every_public_service_takes_the_caller(module):
found = _public_coroutines(module)
missing = sorted(
name for name, args in found.items()
if "user_id" not in args and name not in NEEDS_NO_CALLER[module]
)
assert not missing, f"services/{module}.py: {missing} take no user_id"
# An exception for a function that is gone, or now takes the caller,
# would excuse whatever takes the name next.
stale = sorted(
name for name in NEEDS_NO_CALLER[module]
if name not in found or "user_id" in found[name]
)
assert not stale, f"services/{module}.py: stale exceptions {stale}"