From 5d764ecf2f8db574aa9932565c01d19f2635cf44 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Tue, 1 Dec 2026 07:06:32 -0500 Subject: [PATCH] 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 --- tests/test_routes_design_systems.py | 15 ------- tests/test_routes_rulebooks.py | 18 -------- tests/test_routes_snippets.py | 13 ------ tests/test_routes_systems.py | 11 ----- tests/test_routes_trash.py | 40 +++++------------- tests/test_service_user_id_contract.py | 58 ++++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 86 deletions(-) create mode 100644 tests/test_service_user_id_contract.py diff --git a/tests/test_routes_design_systems.py b/tests/test_routes_design_systems.py index 1ed0b322..e5afa5cd 100644 --- a/tests/test_routes_design_systems.py +++ b/tests/test_routes_design_systems.py @@ -60,21 +60,6 @@ def test_every_endpoint_is_reachable_on_the_app(): } -def test_service_functions_take_user_id(): - """Routes must call the services with user_id — verify the contract (#33).""" - from grimoire.services import design_systems as svc - for fn_name in ( - "create_design_system", "list_design_systems", "get_design_system", - "update_design_system", "delete_design_system", "resolve_design_system", - "create_token", "list_tokens", "update_token", "delete_token", - "set_project_design_system", - "stylesheet_for_system", "check_snippets_against_system", - ): - fn = getattr(svc, fn_name) - assert callable(fn) - assert "user_id" in inspect.signature(fn).parameters - - def test_agent_and_web_surfaces_stay_at_parity(): """The MCP tools and the REST routes are two callers of one service; a capability on one has to exist on the other. diff --git a/tests/test_routes_rulebooks.py b/tests/test_routes_rulebooks.py index e0597c59..b228c278 100644 --- a/tests/test_routes_rulebooks.py +++ b/tests/test_routes_rulebooks.py @@ -37,23 +37,6 @@ def test_topic_handlers_callable(): assert callable(getattr(rb_routes, name)) -def test_service_signatures_require_user_id(): - """Routes must call services with user_id — verify the contract.""" - from grimoire.services import rulebooks as svc - for fn_name in ( - "create_rulebook", "list_rulebooks", "get_rulebook", - "update_rulebook", "delete_rulebook", "find_rulebook_by_title", - "create_topic", "list_topics", "get_topic", "update_topic", "delete_topic", - "create_rule", "create_project_rule", "rule_detail", - "set_rule_systems", "add_rule_relation", "remove_rule_relation", - "list_rules", - "get_rule", "update_rule", "delete_rule", - "get_applicable_rules", - ): - sig = inspect.signature(getattr(svc, fn_name)) - assert "user_id" in sig.parameters, f"{fn_name} missing user_id param" - - def test_rule_model_carries_project_id_and_topic_id_nullable(): """Migration 0059 made topic_id nullable and added project_id.""" from grimoire.models.rulebook import Rule @@ -112,7 +95,6 @@ def test_the_rule_list_zero_fills_usage_on_every_row(): reading `undefined` on almost every row — and the difference between "no events" and "no field" is exactly the distinction a readout must keep. """ - import inspect from grimoire.routes import rulebooks as rb_routes diff --git a/tests/test_routes_snippets.py b/tests/test_routes_snippets.py index ed8477b6..a9f2d1c8 100644 --- a/tests/test_routes_snippets.py +++ b/tests/test_routes_snippets.py @@ -26,19 +26,6 @@ def test_snippet_handlers_callable(): assert callable(getattr(routes, name)) -def test_service_functions_take_user_id(): - """Routes must call snippet services with user_id — verify the contract.""" - from grimoire.services import snippets as svc - for fn_name in ( - "create_snippet", "list_snippets", "get_snippet", "update_snippet", - "delete_snippet", "merge_snippets", "record_verification", - "unmerge_snippet", - ): - fn = getattr(svc, fn_name) - assert callable(fn) - assert "user_id" in inspect.signature(fn).parameters - - def test_agent_and_web_surfaces_stay_at_parity(): """The MCP tools and the REST routes are two callers of one service; a capability on one has to exist on the other. Two doors maintained diff --git a/tests/test_routes_systems.py b/tests/test_routes_systems.py index a526e0a1..1f5f08af 100644 --- a/tests/test_routes_systems.py +++ b/tests/test_routes_systems.py @@ -25,14 +25,3 @@ def test_system_handlers_callable(): assert callable(getattr(routes, name)) -def test_service_functions_take_user_id(): - """Routes must call systems services with user_id — verify the contract.""" - from grimoire.services import systems as svc - for fn_name in ( - "create_system", "list_systems", "get_system", "update_system", - "delete_system", "list_records_for_system", "list_issues", - "open_issue_counts_by_system", - ): - fn = getattr(svc, fn_name) - assert callable(fn) - assert "user_id" in inspect.signature(fn).parameters diff --git a/tests/test_routes_trash.py b/tests/test_routes_trash.py index 5968a803..5ef7affd 100644 --- a/tests/test_routes_trash.py +++ b/tests/test_routes_trash.py @@ -1,32 +1,14 @@ -"""Structural tests for the trash blueprint + delete-flip contracts.""" -import inspect +"""The trash API is routed: list, restore and purge reach handlers on the +running app. What they do is the trash service's to prove +(tests/test_services_trash.py, tests/test_mcp_tool_trash.py).""" -def test_trash_blueprint_registered(): - from grimoire.routes.trash import trash_bp - assert trash_bp.name == "trash" - assert trash_bp.url_prefix == "/api/trash" - - -def test_trash_blueprint_registered_in_app(): +def test_the_trash_routes_are_on_the_app(): from grimoire.app import create_app - app = create_app() - assert "trash" in app.blueprints - - -def test_trash_handlers_callable(): - from grimoire.routes import trash as trash_routes - for name in ("list_trash", "restore_batch", "purge_batch"): - assert callable(getattr(trash_routes, name)) - - -def test_trash_service_surface(): - from grimoire.services import trash as svc - for fn_name in ("delete", "restore", "purge", "list_trash", "purge_expired", "alive"): - assert hasattr(svc, fn_name), f"trash service missing {fn_name}" - - -def test_delete_service_signature(): - from grimoire.services.trash import delete - params = inspect.signature(delete).parameters - assert "user_id" in params and "entity_type" in params and "entity_id" in params + rules = { + (str(r.rule), m) for r in create_app().url_map.iter_rules() for m in r.methods + } + for route in (("/api/trash", "GET"), + ("/api/trash//restore", "POST"), + ("/api/trash/", "DELETE")): + assert route in rules, route diff --git a/tests/test_service_user_id_contract.py b/tests/test_service_user_id_contract.py new file mode 100644 index 00000000..fbf1c0cb --- /dev/null +++ b/tests/test_service_user_id_contract.py @@ -0,0 +1,58 @@ +"""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}"