mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 10:32:57 +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>
364 lines
10 KiB
Python
364 lines
10 KiB
Python
from io import BytesIO
|
|
from unittest import mock
|
|
|
|
from fastapi import status
|
|
from tests.auth_helpers import bearer, hide
|
|
from tests.factories import make_screenshot
|
|
|
|
from handler.database import db_screenshot_handler
|
|
from models.assets import Screenshot
|
|
from models.permission import PermEntity
|
|
from models.platform import Platform
|
|
from models.rom import Rom
|
|
from models.user import User
|
|
|
|
# ---------- POST /api/screenshots ----------
|
|
|
|
|
|
@mock.patch(
|
|
"endpoints.screenshots.fs_asset_handler.write_file", new_callable=mock.AsyncMock
|
|
)
|
|
@mock.patch("endpoints.screenshots.scan_screenshot", new_callable=mock.AsyncMock)
|
|
def test_upload_gallery_screenshot_sets_flags(
|
|
mock_scan,
|
|
_mock_write,
|
|
client,
|
|
access_token: str,
|
|
rom: Rom,
|
|
platform: Platform,
|
|
admin_user: User,
|
|
):
|
|
mock_scan.return_value = Screenshot(
|
|
file_name="shot1.png",
|
|
file_name_no_tags="shot1",
|
|
file_name_no_ext="shot1",
|
|
file_extension="png",
|
|
file_path=f"{platform.slug}/screenshots",
|
|
file_size_bytes=100,
|
|
)
|
|
|
|
response = client.post(
|
|
f"/api/screenshots?rom_id={rom.id}",
|
|
files={"screenshotFile": ("shot1.png", BytesIO(b"img"), "image/png")},
|
|
headers=bearer(access_token),
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
data = response.json()
|
|
assert data["is_gallery"] is True
|
|
assert data["is_public"] is False
|
|
|
|
|
|
@mock.patch(
|
|
"endpoints.screenshots.fs_asset_handler.write_file", new_callable=mock.AsyncMock
|
|
)
|
|
@mock.patch("endpoints.screenshots.scan_screenshot", new_callable=mock.AsyncMock)
|
|
def test_upload_rejects_a_jpeg_xl_screenshot(
|
|
_mock_scan,
|
|
mock_write,
|
|
client,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
response = client.post(
|
|
f"/api/screenshots?rom_id={rom.id}",
|
|
files={"screenshotFile": ("shot.jxl", BytesIO(b"\xff\x0a"), "image/jxl")},
|
|
headers=bearer(access_token),
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
assert ".jxl" not in response.json()["detail"]
|
|
mock_write.assert_not_called()
|
|
|
|
|
|
@mock.patch(
|
|
"endpoints.screenshots.fs_asset_handler.write_file", new_callable=mock.AsyncMock
|
|
)
|
|
@mock.patch("endpoints.screenshots.scan_screenshot", new_callable=mock.AsyncMock)
|
|
def test_upload_rejects_invalid_extension(
|
|
_mock_scan,
|
|
_mock_write,
|
|
client,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
response = client.post(
|
|
f"/api/screenshots?rom_id={rom.id}",
|
|
files={"screenshotFile": ("notes.txt", BytesIO(b"nope"), "text/plain")},
|
|
headers=bearer(access_token),
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
assert "Unsupported image file type" in response.json()["detail"]
|
|
|
|
|
|
@mock.patch(
|
|
"endpoints.screenshots.fs_asset_handler.write_file", new_callable=mock.AsyncMock
|
|
)
|
|
def test_upload_rejects_a_name_over_255_bytes(
|
|
mock_write,
|
|
client,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
response = client.post(
|
|
f"/api/screenshots?rom_id={rom.id}",
|
|
files={"screenshotFile": ("あ" * 86 + ".png", BytesIO(b"png"), "image/png")},
|
|
headers=bearer(access_token),
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
assert "255 bytes" in response.json()["detail"]
|
|
mock_write.assert_not_called()
|
|
|
|
|
|
# ---------- PUT /api/screenshots/{id} (visibility) ----------
|
|
|
|
|
|
def test_update_visibility_owner(client, access_token: str, screenshot: Screenshot):
|
|
response = client.put(
|
|
f"/api/screenshots/{screenshot.id}",
|
|
json={"is_public": True},
|
|
headers=bearer(access_token),
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.json()["is_public"] is True
|
|
|
|
refreshed = db_screenshot_handler.get_screenshot_by_id(screenshot.id)
|
|
assert refreshed is not None and refreshed.is_public is True
|
|
|
|
|
|
def test_update_visibility_other_user_returns_404(
|
|
client,
|
|
access_token: str,
|
|
rom: Rom,
|
|
editor_user: User,
|
|
):
|
|
others = make_screenshot(rom, editor_user, "other.png", file_size_bytes=10)
|
|
|
|
response = client.put(
|
|
f"/api/screenshots/{others.id}",
|
|
json={"is_public": True},
|
|
headers=bearer(access_token),
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
# ---------- DELETE /api/screenshots/{id} ----------
|
|
|
|
|
|
@mock.patch(
|
|
"endpoints.screenshots.fs_asset_handler.remove_file", new_callable=mock.AsyncMock
|
|
)
|
|
def test_delete_screenshot_owner(
|
|
_mock_remove,
|
|
client,
|
|
access_token: str,
|
|
screenshot: Screenshot,
|
|
):
|
|
response = client.delete(
|
|
f"/api/screenshots/{screenshot.id}",
|
|
headers=bearer(access_token),
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert db_screenshot_handler.get_screenshot_by_id(screenshot.id) is None
|
|
|
|
|
|
@mock.patch(
|
|
"endpoints.screenshots.fs_asset_handler.remove_file", new_callable=mock.AsyncMock
|
|
)
|
|
def test_delete_screenshot_other_user_returns_404(
|
|
_mock_remove,
|
|
client,
|
|
access_token: str,
|
|
rom: Rom,
|
|
editor_user: User,
|
|
):
|
|
others = make_screenshot(rom, editor_user, "other.png", file_size_bytes=10)
|
|
|
|
response = client.delete(
|
|
f"/api/screenshots/{others.id}",
|
|
headers=bearer(access_token),
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
assert db_screenshot_handler.get_screenshot_by_id(others.id) is not None
|
|
|
|
|
|
# ---------- Gallery visibility query ----------
|
|
|
|
|
|
def _add_screenshot(rom, user, name, *, is_gallery, is_public):
|
|
return make_screenshot(
|
|
rom,
|
|
user,
|
|
f"{name}.png",
|
|
file_size_bytes=10,
|
|
is_gallery=is_gallery,
|
|
is_public=is_public,
|
|
)
|
|
|
|
|
|
def test_gallery_query_excludes_thumbnails_and_others_private(
|
|
rom: Rom,
|
|
admin_user: User,
|
|
editor_user: User,
|
|
):
|
|
# Own save/state thumbnail, not a gallery upload.
|
|
_add_screenshot(rom, admin_user, "thumb", is_gallery=False, is_public=False)
|
|
# Own private gallery screenshot, visible to self.
|
|
mine = _add_screenshot(rom, admin_user, "mine", is_gallery=True, is_public=False)
|
|
# Another user's public gallery screenshot, visible (community).
|
|
others_public = _add_screenshot(
|
|
rom, editor_user, "pub", is_gallery=True, is_public=True
|
|
)
|
|
# Another user's private gallery screenshot, hidden.
|
|
_add_screenshot(rom, editor_user, "priv", is_gallery=True, is_public=False)
|
|
|
|
visible = {
|
|
s.id
|
|
for s in db_screenshot_handler.get_rom_gallery_screenshots(
|
|
rom_id=rom.id, user_id=admin_user.id
|
|
)
|
|
}
|
|
|
|
assert mine.id in visible
|
|
assert others_public.id in visible
|
|
assert len(visible) == 2
|
|
|
|
|
|
# ---------- GET /api/screenshots/{id}/content ----------
|
|
|
|
|
|
@mock.patch("endpoints.screenshots.fs_asset_handler.validate_path")
|
|
def test_owner_downloads_own_screenshot(
|
|
mock_validate_path,
|
|
client,
|
|
access_token: str,
|
|
admin_user: User,
|
|
rom: Rom,
|
|
tmp_path,
|
|
):
|
|
screenshot = _add_screenshot(
|
|
rom, admin_user, "shot", is_gallery=True, is_public=False
|
|
)
|
|
test_file = tmp_path / "shot.png"
|
|
test_file.write_bytes(b"SHOT_DATA")
|
|
mock_validate_path.return_value = test_file
|
|
|
|
response = client.get(
|
|
f"/api/screenshots/{screenshot.id}/content", headers=bearer(access_token)
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.content == b"SHOT_DATA"
|
|
|
|
|
|
def test_other_user_cannot_download_private_screenshot(
|
|
client,
|
|
viewer_access_token: str,
|
|
admin_user: User,
|
|
rom: Rom,
|
|
):
|
|
screenshot = _add_screenshot(
|
|
rom, admin_user, "shot", is_gallery=True, is_public=False
|
|
)
|
|
response = client.get(
|
|
f"/api/screenshots/{screenshot.id}/content",
|
|
headers=bearer(viewer_access_token),
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
@mock.patch("endpoints.screenshots.fs_asset_handler.validate_path")
|
|
def test_other_user_downloads_public_screenshot(
|
|
mock_validate_path,
|
|
client,
|
|
viewer_access_token: str,
|
|
admin_user: User,
|
|
rom: Rom,
|
|
tmp_path,
|
|
):
|
|
screenshot = _add_screenshot(
|
|
rom, admin_user, "shot", is_gallery=True, is_public=True
|
|
)
|
|
test_file = tmp_path / "shot.png"
|
|
test_file.write_bytes(b"SHARED_SHOT")
|
|
mock_validate_path.return_value = test_file
|
|
|
|
response = client.get(
|
|
f"/api/screenshots/{screenshot.id}/content",
|
|
headers=bearer(viewer_access_token),
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.content == b"SHARED_SHOT"
|
|
|
|
|
|
def test_hidden_rom_masks_public_screenshot_download(
|
|
client,
|
|
viewer_access_token: str,
|
|
viewer_user: User,
|
|
admin_user: User,
|
|
rom: Rom,
|
|
):
|
|
# A public screenshot on a ROM hidden from the caller must stay 404-masked;
|
|
# sharing cannot override the hidden-resource boundary.
|
|
screenshot = _add_screenshot(
|
|
rom, admin_user, "shot", is_gallery=True, is_public=True
|
|
)
|
|
hide(PermEntity.ROMS, rom.id, user_id=viewer_user.id)
|
|
|
|
response = client.get(
|
|
f"/api/screenshots/{screenshot.id}/content",
|
|
headers=bearer(viewer_access_token),
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
def test_hidden_platform_masks_public_screenshot_download(
|
|
client,
|
|
viewer_access_token: str,
|
|
viewer_user: User,
|
|
admin_user: User,
|
|
rom: Rom,
|
|
platform: Platform,
|
|
):
|
|
# Hiding the parent platform cascades to its screenshots as well.
|
|
screenshot = _add_screenshot(
|
|
rom, admin_user, "shot", is_gallery=True, is_public=True
|
|
)
|
|
hide(PermEntity.PLATFORMS, platform.id, user_id=viewer_user.id)
|
|
|
|
response = client.get(
|
|
f"/api/screenshots/{screenshot.id}/content",
|
|
headers=bearer(viewer_access_token),
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
def test_download_screenshot_not_found(client, access_token: str):
|
|
response = client.get(
|
|
"/api/screenshots/99999/content", headers=bearer(access_token)
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
@mock.patch(
|
|
"endpoints.screenshots.fs_asset_handler.write_file", new_callable=mock.AsyncMock
|
|
)
|
|
def test_hidden_rom_masks_screenshot_upload(
|
|
mock_write, client, viewer_access_token: str, viewer_user: User, rom: Rom
|
|
):
|
|
hide(PermEntity.ROMS, rom.id, user_id=viewer_user.id)
|
|
|
|
response = client.post(
|
|
f"/api/screenshots?rom_id={rom.id}",
|
|
files={"screenshotFile": ("shot.png", BytesIO(b"png"), "image/png")},
|
|
headers=bearer(viewer_access_token),
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
mock_write.assert_not_awaited()
|