diff --git a/backend/endpoints/responses/assets.py b/backend/endpoints/responses/assets.py index e2bfec804..bc7734a50 100644 --- a/backend/endpoints/responses/assets.py +++ b/backend/endpoints/responses/assets.py @@ -72,12 +72,13 @@ class UserScreenshotSchema(ScreenshotSchema): class ManagedAsset(BaseModel): channel_id: uuid.UUID | None = None """The ID of the sync channel this was pushed to. Null for a backup that the user manages themselves.""" + content_hash: str | None = None + """MD5 of the file's bytes, or for a zip, of its sorted `name:md5` entry lines. Null for a file never hashed.""" class SaveSchema(BaseAsset, AssetAnnotations, ManagedAsset): emulator: str | None slot: str | None = None - content_hash: str | None = None is_public: bool = False screenshot: ScreenshotSchema | None origin_device_id: str | None = None diff --git a/backend/tests/endpoints/test_states.py b/backend/tests/endpoints/test_states.py index d73319882..5b19d7c2c 100644 --- a/backend/tests/endpoints/test_states.py +++ b/backend/tests/endpoints/test_states.py @@ -1,3 +1,4 @@ +import hashlib import os from contextlib import contextmanager from datetime import datetime, timezone @@ -362,6 +363,46 @@ def test_kiosk_mode_anonymous_visitor_cannot_upload_state(client, rom: Rom): assert response.status_code == status.HTTP_403_FORBIDDEN +def test_state_list_detail_and_rom_detail_send_the_content_hash( + client, access_token: str, rom: Rom, state: State +): + content_hash = "a" * 32 + db_state_handler.update_state(state.id, {"content_hash": content_hash}) + + listed = client.get(f"/api/states?rom_id={rom.id}", headers=bearer(access_token)) + single = client.get(f"/api/states/{state.id}", headers=bearer(access_token)) + rom_detail = client.get(f"/api/roms/{rom.id}", headers=bearer(access_token)) + + assert listed.status_code == status.HTTP_200_OK + assert [item["content_hash"] for item in listed.json()] == [content_hash] + assert single.status_code == status.HTTP_200_OK + assert single.json()["content_hash"] == content_hash + assert rom_detail.status_code == status.HTTP_200_OK + for key in ("user_states", "all_user_states"): + assert [item["content_hash"] for item in rom_detail.json()[key]] == [ + content_hash + ] + + +def test_uploaded_state_reports_its_content_hash( + client, access_token: str, rom: Rom, _isolated_assets_dir +): + data = b"STATE BYTES" + + uploaded = client.post( + f"/api/states?rom_id={rom.id}&emulator=test_emulator", + files={"stateFile": ("game.state", data, "application/octet-stream")}, + headers=bearer(access_token), + ) + assert uploaded.status_code == status.HTTP_200_OK + + listed = client.get(f"/api/states?rom_id={rom.id}", headers=bearer(access_token)) + + expected = hashlib.md5(data, usedforsecurity=False).hexdigest() + assert uploaded.json()["content_hash"] == expected + assert [item["content_hash"] for item in listed.json()] == [expected] + + class TestRomIdsScope: def test_scopes_results_to_listed_roms( self, client, access_token: str, rom: Rom, state: State, second_state: State diff --git a/frontend/src/__generated__/models/SaveSchema.ts b/frontend/src/__generated__/models/SaveSchema.ts index 27386aa59..22baa11e5 100644 --- a/frontend/src/__generated__/models/SaveSchema.ts +++ b/frontend/src/__generated__/models/SaveSchema.ts @@ -9,6 +9,10 @@ export type SaveSchema = { * The ID of the sync channel this was pushed to. Null for a backup that the user manages themselves. */ channel_id?: (string | null); + /** + * MD5 of the file's bytes, or for a zip, of its sorted `name:md5` entry lines. Null for a file never hashed. + */ + content_hash?: (string | null); is_favorite?: boolean; /** * The owner's free-text labels. @@ -39,7 +43,6 @@ export type SaveSchema = { updated_at: string; emulator: (string | null); slot?: (string | null); - content_hash?: (string | null); is_public?: boolean; screenshot: (ScreenshotSchema | null); origin_device_id?: (string | null); diff --git a/frontend/src/__generated__/models/StateSchema.ts b/frontend/src/__generated__/models/StateSchema.ts index 503b5b8fb..c96584d95 100644 --- a/frontend/src/__generated__/models/StateSchema.ts +++ b/frontend/src/__generated__/models/StateSchema.ts @@ -8,6 +8,10 @@ export type StateSchema = { * The ID of the sync channel this was pushed to. Null for a backup that the user manages themselves. */ channel_id?: (string | null); + /** + * MD5 of the file's bytes, or for a zip, of its sorted `name:md5` entry lines. Null for a file never hashed. + */ + content_hash?: (string | null); is_favorite?: boolean; /** * The owner's free-text labels. diff --git a/frontend/src/__generated__/models/UserSaveSchema.ts b/frontend/src/__generated__/models/UserSaveSchema.ts index 82e5aec7c..7052d602a 100644 --- a/frontend/src/__generated__/models/UserSaveSchema.ts +++ b/frontend/src/__generated__/models/UserSaveSchema.ts @@ -13,6 +13,10 @@ export type UserSaveSchema = { * The ID of the sync channel this was pushed to. Null for a backup that the user manages themselves. */ channel_id?: (string | null); + /** + * MD5 of the file's bytes, or for a zip, of its sorted `name:md5` entry lines. Null for a file never hashed. + */ + content_hash?: (string | null); is_favorite?: boolean; /** * The owner's free-text labels. @@ -43,7 +47,6 @@ export type UserSaveSchema = { updated_at: string; emulator: (string | null); slot?: (string | null); - content_hash?: (string | null); is_public?: boolean; screenshot: (ScreenshotSchema | null); origin_device_id?: (string | null); diff --git a/frontend/src/__generated__/models/UserStateSchema.ts b/frontend/src/__generated__/models/UserStateSchema.ts index 24d338773..baec60796 100644 --- a/frontend/src/__generated__/models/UserStateSchema.ts +++ b/frontend/src/__generated__/models/UserStateSchema.ts @@ -12,6 +12,10 @@ export type UserStateSchema = { * The ID of the sync channel this was pushed to. Null for a backup that the user manages themselves. */ channel_id?: (string | null); + /** + * MD5 of the file's bytes, or for a zip, of its sorted `name:md5` entry lines. Null for a file never hashed. + */ + content_hash?: (string | null); is_favorite?: boolean; /** * The owner's free-text labels.