mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 18:18:47 +01:00
fix(states): send each state's content hash in the state list (#5668)
* fix(states): send each state's content hash in the state list
RomM stores a content hash for every state, and the save list already
sends each save's. StateSchema never had the field, so GET /api/states,
GET /api/states/{id} and a ROM's user_states left it out. A client that
keeps its own copies of states can now tell it already has one without
downloading it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* chore(pr-ready): code-review fixes
Document what a state's content_hash covers in the schema and cover the
ROM detail user_states/all_user_states in the endpoint test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* chore(pr-ready): simplify
Declare content_hash once on ManagedAsset, the schema side of the
ChannelContent model that owns the column, so saves and states share one
documented field. Build the hash test on the state fixture with a single
hash value.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
5
frontend/src/__generated__/models/SaveSchema.ts
generated
5
frontend/src/__generated__/models/SaveSchema.ts
generated
@@ -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);
|
||||
|
||||
4
frontend/src/__generated__/models/StateSchema.ts
generated
4
frontend/src/__generated__/models/StateSchema.ts
generated
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user