Files
FabledGrimoire/tests/test_service_user_id_contract.py
T
bvandeusenandClaude Opus 5.5 5d764ecf2f test(8249): the caller-scoping contract is a property of the services, not four lists of names
Review of the signature-flagged tests:
- Four route files each listed service names by hand and checked each takes
  user_id. A new service without the caller passed them, and a renamed one
  failed them. They become one test over services/{design_systems,systems,
  rulebooks,snippets}.py: every public coroutine takes user_id, except eight
  named with the reason each needs none, and an exception for a function that
  is gone or now takes the caller fails too.
- test_routes_trash was four existence checks: blueprint name, registered,
  handlers callable, the service surface, delete's parameter names. It
  becomes one check that the three trash routes are on the app; what they do
  is proven by test_services_trash and test_mcp_tool_trash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 07:06:32 -05:00

59 lines
2.3 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": {},
}
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}"