mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
* chore(coherence): adopt Coherence with specs, invariants and a PR report Install @posthog/coherence as a frontend dev dependency and wire its Claude Code hooks into the committed .claude/settings.json, so every agent session in the repo gets the vocabulary, specs, journal and checks. The hooks call the binary under frontend/ through $CLAUDE_PROJECT_DIR. Declare 23 components and the entrances that carry outside trust, and six invariants, each refuted with a deliberate break before it counted: - every /api route requires auth and its scopes, except a named public list (new route sweep in tests/endpoints/test_route_guards.py) - a hidden rom, platform or firmware answers every id route exactly as a missing id would (new sweep over every method of every id route) - the unscoped save, state and screenshot fetches by id are reached only from their own routers (chokepoints) - validate_path never resolves outside its base folder The PR workflow runs in comment mode: one report comment, failing only when Coherence itself breaks. Trunk skips Prettier and markdownlint on *.spec.md and *.practice.md, since Prettier escapes their globs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(routes): satisfy mypy in the route guard sweep Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(coherence): record the worktree test database enactment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(roms): answer a hidden rom like a missing one on file routes; tighten route sweeps Addresses Greptile's review on #5584. The rom file delete and reading-progress routes answered "File not found" for a hidden rom but "Rom with id N not found" for a missing one, so a caller could tell them apart. They now raise the same not-found. The route sweeps now reach the checks they claim to test: a protected route with no scopes counts as public (its guard admits anyone), write requests carry a body and upload name so validation can't answer first, RomFile-keyed routes get a real file id, error bodies are compared with the id normalized, and the rom routes are swept with the rom hidden directly, through its platform, and by the unrated filter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(coherence): tighten route guard sweeps and isolate oracle test databases (code-review) - Count a route as protected only when its endpoint is the scope-checking wrapper with non-empty scopes, not just when it declares the bearer scopes. - Treat 401 and 403 like 422 as never reaching the visibility check, and sweep /api/streaming/states/{rom_id}. - Run pytest oracles with a per-run ROMM_TEST_DB_TAG unless one is set. - Use the shared setup-frontend action and ubuntu-24.04 in the workflow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(tests): simplify route guard sweeps and path tests (simplify) - Mark scope-guard wrappers with required_scopes so the auth sweep reads a public attribute instead of the private closure. - Derive the entity regex from one segment list, ask each missing id once, and run the three hide scenarios through one sweep helper. - Fold the new validate_path inputs into the existing valid, traversal and absolute path tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(coherence): trim workflow comments (review-polish) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(filesystem): restore the validate_path property test the paths invariant cites Filesystem.spec.md names test_validate_path_never_escapes_its_base as the invariant's evidence, and Coherence selects it by name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(coherence): run hooks from the local binary instead of npx Each hook calls frontend/node_modules/.bin/coherence directly and exits 0 when it isn't installed, so sessions without frontend deps (fresh worktrees, backend-only setups, cloud setup still installing) don't report a hook error on every tool call, and no hook pays npx's startup. Also commits the journal and run records left by earlier sessions, which .coherence/.gitignore keeps as durable records. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(tests): share the bearer, user token and hide helpers Tests built the same Authorization headers and HiddenEntity rows in about 40 local copies, and two modules imported private helpers from other test files. They now use bearer, user_headers and hide from tests/auth_helpers.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(endpoints): one 404 for a missing or hidden rom, platform or firmware Each route wrote the missing-row 404 and the hidden-row 404 separately and had to keep them identical by hand. visible_rom_or_404, visible_platform_or_404 and visible_firmware_or_404 take the lookup result and raise one answer for both, either the standard not-found or a bespoke detail. Re-reads after a write keep a plain not-found check, so an edit that hides the row doesn't turn into a 404 after it succeeded. The RomFile-keyed routes (/roms/{file_id}/files, .../files/content/{name}, /roms/{file_id}/patch) name their path parameter file_id instead of id, so the route sweep reads which id a route takes from its path and drops its hand-kept list. URLs are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(setup): develop inside WSL on Windows @posthog/coherence installs only on Linux and macOS, so npm install in frontend/ fails on native Windows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(coherence): record the whole-repo chokepoint run Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(types): regenerate API types for the file_id patch route Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
342 lines
12 KiB
Python
342 lines
12 KiB
Python
"""Admin CRUD for groups, memberships, overrides and hidden entities, and the
|
|
end-to-end effect on a member's /permissions/me."""
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
from tests.auth_helpers import bearer, user_headers
|
|
from tests.factories import make_device_token
|
|
|
|
from endpoints import permissions as permissions_endpoints
|
|
from handler.database import db_user_handler
|
|
from handler.database.base_handler import sync_session
|
|
from handler.database.permissions_handler import DBPermissionsHandler
|
|
from models.permission import PermissionGroup
|
|
|
|
|
|
def _fresh_user_auth(user_id):
|
|
# Reload the user so the token carries the post-change projected scopes.
|
|
user = db_user_handler.get_user(user_id)
|
|
assert user is not None
|
|
return user_headers(user)
|
|
|
|
|
|
def _me_actions(client, user_id):
|
|
body = client.get("/api/permissions/me", headers=_fresh_user_auth(user_id)).json()
|
|
return {g["action"] for g in body["grants"]}, body
|
|
|
|
|
|
def _cleanup():
|
|
with sync_session.begin() as s:
|
|
s.query(PermissionGroup).filter(PermissionGroup.system_key.is_(None)).delete(
|
|
synchronize_session="evaluate"
|
|
)
|
|
|
|
|
|
def test_non_admin_forbidden(client, viewer_access_token):
|
|
h = bearer(viewer_access_token)
|
|
assert client.get("/api/permissions/groups", headers=h).status_code == 403
|
|
assert client.get("/api/permissions/catalog", headers=h).status_code == 403
|
|
assert (
|
|
client.post(
|
|
"/api/permissions/groups", headers=h, json={"name": "x"}
|
|
).status_code
|
|
== 403
|
|
)
|
|
|
|
|
|
def test_catalog_lists_vocabulary(client, access_token):
|
|
body = client.get("/api/permissions/catalog", headers=bearer(access_token)).json()
|
|
assert "roms" in body["entities"] and "platforms" in body["entities"]
|
|
assert set(body["actions"]) == {"read", "write", "delete"}
|
|
|
|
|
|
def test_catalog_lists_the_ownable_entities(client, access_token):
|
|
body = client.get("/api/permissions/catalog", headers=bearer(access_token)).json()
|
|
assert "collections" in body["ownable_entities"]
|
|
assert not {"users", "tasks", "logs", "roms"} & set(body["ownable_entities"])
|
|
|
|
|
|
def test_own_only_is_rejected_on_an_entity_without_owners(client, access_token):
|
|
h = bearer(access_token)
|
|
own_users = {"entity": "users", "action": "write", "own_only": True}
|
|
try:
|
|
created = client.post(
|
|
"/api/permissions/groups",
|
|
headers=h,
|
|
json={"name": "x", "grants": [own_users]},
|
|
)
|
|
assert created.status_code == 422
|
|
|
|
gid = client.post(
|
|
"/api/permissions/groups",
|
|
headers=h,
|
|
json={
|
|
"name": "Own collections",
|
|
"grants": [
|
|
{"entity": "collections", "action": "write", "own_only": True}
|
|
],
|
|
},
|
|
).json()["id"]
|
|
updated = client.put(
|
|
f"/api/permissions/groups/{gid}", headers=h, json={"grants": [own_users]}
|
|
)
|
|
assert updated.status_code == 422
|
|
finally:
|
|
_cleanup()
|
|
|
|
|
|
def test_own_only_override_is_rejected_on_an_entity_without_owners(
|
|
client, access_token, viewer_user
|
|
):
|
|
response = client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=bearer(access_token),
|
|
json={
|
|
"overrides": [
|
|
{
|
|
"entity": "tasks",
|
|
"action": "write",
|
|
"granted": True,
|
|
"own_only": True,
|
|
}
|
|
]
|
|
},
|
|
)
|
|
assert response.status_code == 422
|
|
|
|
|
|
def test_own_only_revoke_override_is_accepted_on_an_entity_without_owners(
|
|
client, access_token, viewer_user
|
|
):
|
|
response = client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=bearer(access_token),
|
|
json={
|
|
"overrides": [
|
|
{
|
|
"entity": "tasks",
|
|
"action": "write",
|
|
"granted": False,
|
|
"own_only": True,
|
|
}
|
|
]
|
|
},
|
|
)
|
|
assert response.status_code == 200
|
|
|
|
|
|
def test_groups_expose_the_system_key(client, access_token):
|
|
groups = client.get("/api/permissions/groups", headers=bearer(access_token)).json()
|
|
keys = {g["name"]: g["system_key"] for g in groups}
|
|
assert keys["Viewer"] == "viewer"
|
|
assert keys["Editor"] == "editor"
|
|
assert "is_system" not in groups[0]
|
|
|
|
|
|
def test_group_create_assign_and_effective_permissions(
|
|
client, access_token, viewer_user
|
|
):
|
|
try:
|
|
created = client.post(
|
|
"/api/permissions/groups",
|
|
headers=bearer(access_token),
|
|
json={
|
|
"name": "Curators",
|
|
"description": "roms + platform read",
|
|
"grants": [
|
|
{"entity": "roms", "action": "read"},
|
|
{"entity": "roms", "action": "write"},
|
|
{"entity": "platforms", "action": "read"},
|
|
],
|
|
},
|
|
)
|
|
assert created.status_code == 201
|
|
group = created.json()
|
|
assert group["member_count"] == 0
|
|
assert {(g["entity"], g["action"]) for g in group["grants"]} == {
|
|
("roms", "read"),
|
|
("roms", "write"),
|
|
("platforms", "read"),
|
|
}
|
|
gid = group["id"]
|
|
|
|
assigned = client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=bearer(access_token),
|
|
json={"set_group": True, "permission_group_id": gid},
|
|
)
|
|
assert assigned.status_code == 200
|
|
assert assigned.json()["permission_group_id"] == gid
|
|
|
|
actions, _ = _me_actions(client, viewer_user.id)
|
|
# Group grants -> these actions; nothing from the default viewer matrix.
|
|
assert {"rom.view", "rom.upload", "platform.view"} <= actions
|
|
assert "collection.create" not in actions # group grants no collections
|
|
finally:
|
|
_cleanup()
|
|
|
|
|
|
def test_user_override_adds_capability(client, access_token, viewer_user):
|
|
try:
|
|
gid = client.post(
|
|
"/api/permissions/groups",
|
|
headers=bearer(access_token),
|
|
json={"name": "RO", "grants": [{"entity": "roms", "action": "read"}]},
|
|
).json()["id"]
|
|
client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=bearer(access_token),
|
|
json={
|
|
"set_group": True,
|
|
"permission_group_id": gid,
|
|
"overrides": [{"entity": "roms", "action": "delete", "granted": True}],
|
|
},
|
|
)
|
|
actions, _ = _me_actions(client, viewer_user.id)
|
|
assert "rom.delete" in actions # granted by override on top of read-only group
|
|
finally:
|
|
_cleanup()
|
|
|
|
|
|
def test_a_permission_change_closes_the_users_device_sockets(
|
|
mocker, client, access_token, viewer_user
|
|
):
|
|
token, _ = make_device_token(viewer_user, None, scopes="devices.read")
|
|
close = mocker.patch.object(
|
|
permissions_endpoints, "close_client_token_sockets", AsyncMock()
|
|
)
|
|
|
|
response = client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=bearer(access_token),
|
|
json={"overrides": [{"entity": "roms", "action": "delete", "granted": True}]},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
close.assert_awaited_once_with([token.id])
|
|
|
|
|
|
def test_a_group_edit_closes_every_members_device_sockets_at_once(
|
|
mocker, client, access_token, viewer_user, editor_user
|
|
):
|
|
try:
|
|
gid = client.post(
|
|
"/api/permissions/groups",
|
|
headers=bearer(access_token),
|
|
json={"name": "Handhelds"},
|
|
).json()["id"]
|
|
for user in (viewer_user, editor_user):
|
|
client.put(
|
|
f"/api/permissions/users/{user.id}",
|
|
headers=bearer(access_token),
|
|
json={"set_group": True, "permission_group_id": gid},
|
|
)
|
|
viewer_token, _ = make_device_token(viewer_user, None, scopes="devices.read")
|
|
editor_token, _ = make_device_token(editor_user, None, scopes="devices.read")
|
|
close = mocker.patch.object(
|
|
permissions_endpoints, "close_client_token_sockets", AsyncMock()
|
|
)
|
|
|
|
response = client.put(
|
|
f"/api/permissions/groups/{gid}",
|
|
headers=bearer(access_token),
|
|
json={"grants": [{"entity": "roms", "action": "read"}]},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
close.assert_awaited_once()
|
|
assert close.await_args is not None
|
|
assert sorted(close.await_args.args[0]) == sorted(
|
|
[viewer_token.id, editor_token.id]
|
|
)
|
|
finally:
|
|
_cleanup()
|
|
|
|
|
|
def test_hide_entity_for_user(client, access_token, viewer_user):
|
|
resp = client.post(
|
|
"/api/permissions/hidden",
|
|
headers=bearer(access_token),
|
|
json={"entity": "platforms", "entity_id": 11, "user_id": viewer_user.id},
|
|
)
|
|
assert resp.status_code == 200
|
|
|
|
_, body = _me_actions(client, viewer_user.id)
|
|
assert body["hidden"]["platforms"] == [11]
|
|
|
|
# Idempotent + removable.
|
|
client.post(
|
|
"/api/permissions/hidden",
|
|
headers=bearer(access_token),
|
|
json={"entity": "platforms", "entity_id": 11, "user_id": viewer_user.id},
|
|
)
|
|
removed = client.request(
|
|
"DELETE",
|
|
"/api/permissions/hidden",
|
|
headers=bearer(access_token),
|
|
json={"entity": "platforms", "entity_id": 11, "user_id": viewer_user.id},
|
|
)
|
|
assert removed.status_code == 204
|
|
_, body = _me_actions(client, viewer_user.id)
|
|
assert body["hidden"]["platforms"] == []
|
|
|
|
|
|
def test_hidden_requires_exactly_one_principal(client, access_token):
|
|
resp = client.post(
|
|
"/api/permissions/hidden",
|
|
headers=bearer(access_token),
|
|
json={"entity": "roms", "entity_id": 1, "user_id": 1, "group_id": 1},
|
|
)
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_cannot_delete_default_group(client, access_token):
|
|
groups = client.get("/api/permissions/groups", headers=bearer(access_token)).json()
|
|
default = next(g for g in groups if g["is_default"])
|
|
resp = client.delete(
|
|
f"/api/permissions/groups/{default['id']}", headers=bearer(access_token)
|
|
)
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_update_group_deleted_mid_update_returns_404(client, access_token, mocker):
|
|
try:
|
|
gid = client.post(
|
|
"/api/permissions/groups",
|
|
headers=bearer(access_token),
|
|
json={"name": "Vanishing"},
|
|
).json()["id"]
|
|
mocker.patch.object(DBPermissionsHandler, "update_group", return_value=None)
|
|
|
|
resp = client.put(
|
|
f"/api/permissions/groups/{gid}",
|
|
headers=bearer(access_token),
|
|
json={"description": "renamed"},
|
|
)
|
|
assert resp.status_code == 404
|
|
finally:
|
|
_cleanup()
|
|
|
|
|
|
def test_delete_group_falls_members_back(client, access_token, viewer_user):
|
|
gid = client.post(
|
|
"/api/permissions/groups",
|
|
headers=bearer(access_token),
|
|
json={"name": "Temp", "grants": [{"entity": "roms", "action": "read"}]},
|
|
).json()["id"]
|
|
client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=bearer(access_token),
|
|
json={"set_group": True, "permission_group_id": gid},
|
|
)
|
|
|
|
resp = client.delete(f"/api/permissions/groups/{gid}", headers=bearer(access_token))
|
|
assert resp.status_code == 200
|
|
|
|
# FK SET NULL -> user falls back to the default (viewer) matrix.
|
|
user = db_user_handler.get_user(viewer_user.id)
|
|
assert user is not None
|
|
assert user.permission_group_id is None
|
|
actions, _ = _me_actions(client, viewer_user.id)
|
|
assert "collection.create" in actions # default viewer behaviour restored
|