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>
This commit is contained in:
@@ -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():
|
def test_agent_and_web_surfaces_stay_at_parity():
|
||||||
"""The MCP tools and the REST routes are two callers of one service; a
|
"""The MCP tools and the REST routes are two callers of one service; a
|
||||||
capability on one has to exist on the other.
|
capability on one has to exist on the other.
|
||||||
|
|||||||
@@ -37,23 +37,6 @@ def test_topic_handlers_callable():
|
|||||||
assert callable(getattr(rb_routes, name))
|
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():
|
def test_rule_model_carries_project_id_and_topic_id_nullable():
|
||||||
"""Migration 0059 made topic_id nullable and added project_id."""
|
"""Migration 0059 made topic_id nullable and added project_id."""
|
||||||
from grimoire.models.rulebook import Rule
|
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
|
reading `undefined` on almost every row — and the difference between "no
|
||||||
events" and "no field" is exactly the distinction a readout must keep.
|
events" and "no field" is exactly the distinction a readout must keep.
|
||||||
"""
|
"""
|
||||||
import inspect
|
|
||||||
|
|
||||||
from grimoire.routes import rulebooks as rb_routes
|
from grimoire.routes import rulebooks as rb_routes
|
||||||
|
|
||||||
|
|||||||
@@ -26,19 +26,6 @@ def test_snippet_handlers_callable():
|
|||||||
assert callable(getattr(routes, name))
|
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():
|
def test_agent_and_web_surfaces_stay_at_parity():
|
||||||
"""The MCP tools and the REST routes are two callers of one service; a
|
"""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
|
capability on one has to exist on the other. Two doors maintained
|
||||||
|
|||||||
@@ -25,14 +25,3 @@ def test_system_handlers_callable():
|
|||||||
assert callable(getattr(routes, name))
|
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
|
|
||||||
|
|||||||
+11
-29
@@ -1,32 +1,14 @@
|
|||||||
"""Structural tests for the trash blueprint + delete-flip contracts."""
|
"""The trash API is routed: list, restore and purge reach handlers on the
|
||||||
import inspect
|
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():
|
def test_the_trash_routes_are_on_the_app():
|
||||||
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():
|
|
||||||
from grimoire.app import create_app
|
from grimoire.app import create_app
|
||||||
app = create_app()
|
rules = {
|
||||||
assert "trash" in app.blueprints
|
(str(r.rule), m) for r in create_app().url_map.iter_rules() for m in r.methods
|
||||||
|
}
|
||||||
|
for route in (("/api/trash", "GET"),
|
||||||
def test_trash_handlers_callable():
|
("/api/trash/<batch_id>/restore", "POST"),
|
||||||
from grimoire.routes import trash as trash_routes
|
("/api/trash/<batch_id>", "DELETE")):
|
||||||
for name in ("list_trash", "restore_batch", "purge_batch"):
|
assert route in rules, route
|
||||||
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
|
|
||||||
|
|||||||
@@ -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}"
|
||||||
Reference in New Issue
Block a user