mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 18:11:16 +01:00
Cleanup pass on save-sync addressing three independent failure modes that interact in production data: content_hash drift between client and server, null-slot archival saves leaking into sync flows, and content-hash dedupe collapsing legitimately-distinct slots. Bug fixes - compute_content_hash dispatched on zipfile.is_zipfile(relative_path), which silently returned False whenever the process's CWD wasn't ASSETS_BASE_PATH. Every zip save fell through to the raw-MD5 branch, persisting hashes that disagreed with clients computing the intended per-entry zip-hash. Resolve to a full path before the dispatch. - _build_negotiate_plan, sync_push_pull_task, and sync_watcher all treated null-slot saves as sync-eligible. Null-slot saves represent web-UI / archival uploads; including them in negotiate plans matched them against device pushes by filename and overwrote archival data. Filter null-slot saves at all three call sites. - get_save_by_content_hash matched on (rom_id, user_id, content_hash) only, so identical bytes uploaded to different slots collapsed into one record. Scope the lookup by slot when provided so clone-save- to-new-slot creates a distinct row per slot. - get_save_by_filename matched on (rom_id, user_id, file_name) only. When two uploads to different slots happened in the same wall-clock second (the datetime tag is per-second), the second upload UPDATED the first record's slot instead of creating a distinct row. Scope the filename lookup by slot too. One-shot recovery - New recompute_save_content_hashes manual task walks every Save row, recomputes via the fixed dispatch, and updates rows whose values differ. Idempotent; safe to re-run. - Backend startup runs a COUNT(content_hash IS NULL) query and, if any rows exist, enqueues the recompute task on the low-priority RQ queue. The API process moves on; the worker handles the recompute out-of-band. Subsequent restarts find zero NULL hashes and skip. Admins can also trigger the task manually. Test infrastructure - Added tests/_zipfile_shim.reload_zipfile() mirroring the pattern from utils/zip_cache.py for the same zipfile-inflate64 + CPython 3.13.5 incompatibility. Test fixtures that build ZIPs call it immediately before opening the archive.
212 lines
6.2 KiB
Python
212 lines
6.2 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
|
|
import alembic.config
|
|
import pytest
|
|
from joserfc import jwt
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from config.config_manager import ConfigManager
|
|
from handler.auth import auth_handler
|
|
from handler.auth.base_handler import ALGORITHM, oct_key
|
|
from handler.database import (
|
|
db_platform_handler,
|
|
db_rom_handler,
|
|
db_save_handler,
|
|
db_screenshot_handler,
|
|
db_state_handler,
|
|
db_user_handler,
|
|
)
|
|
from models.assets import Save, Screenshot, State
|
|
from models.client_token import ClientToken
|
|
from models.device import Device
|
|
from models.device_save_sync import DeviceSaveSync
|
|
from models.platform import Platform
|
|
from models.play_session import PlaySession
|
|
from models.rom import Rom
|
|
from models.sync_session import SyncSession
|
|
from models.user import Role, User
|
|
|
|
engine = create_engine(ConfigManager.get_db_engine(), pool_pre_ping=True)
|
|
session = sessionmaker(bind=engine, expire_on_commit=False)
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def setup_database():
|
|
alembic.config.main(argv=["upgrade", "head"])
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clear_database():
|
|
with session.begin() as s:
|
|
s.query(PlaySession).delete(synchronize_session="evaluate")
|
|
s.query(ClientToken).delete(synchronize_session="evaluate")
|
|
s.query(SyncSession).delete(synchronize_session="evaluate")
|
|
s.query(DeviceSaveSync).delete(synchronize_session="evaluate")
|
|
s.query(Device).delete(synchronize_session="evaluate")
|
|
s.query(Save).delete(synchronize_session="evaluate")
|
|
s.query(State).delete(synchronize_session="evaluate")
|
|
s.query(Screenshot).delete(synchronize_session="evaluate")
|
|
s.query(Rom).delete(synchronize_session="evaluate")
|
|
s.query(Platform).delete(synchronize_session="evaluate")
|
|
s.query(User).delete(synchronize_session="evaluate")
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def vcr_config():
|
|
"""Fixture to configure VCR.py settings."""
|
|
return {
|
|
# Default `match_on`, plus raw_body.
|
|
"match_on": ["method", "scheme", "host", "port", "path", "query", "raw_body"],
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def platform():
|
|
platform = Platform(
|
|
name="test_platform", slug="test_platform_slug", fs_slug="test_platform_slug"
|
|
)
|
|
return db_platform_handler.add_platform(platform)
|
|
|
|
|
|
@pytest.fixture
|
|
def rom(admin_user: User, platform: Platform):
|
|
rom = Rom(
|
|
platform_id=platform.id,
|
|
name="test_rom",
|
|
slug="test_rom_slug",
|
|
fs_name="test_rom.zip",
|
|
fs_name_no_tags="test_rom",
|
|
fs_name_no_ext="test_rom",
|
|
fs_extension="zip",
|
|
fs_path=f"{platform.slug}/roms",
|
|
)
|
|
rom = db_rom_handler.add_rom(rom)
|
|
|
|
db_rom_handler.add_rom_user(rom_id=rom.id, user_id=admin_user.id)
|
|
|
|
return rom
|
|
|
|
|
|
@pytest.fixture
|
|
def save(rom: Rom, platform: Platform, admin_user: User):
|
|
"""Slot-bound save (the canonical device-uploaded shape).
|
|
|
|
Sync negotiation only considers saves with a non-null slot — null-slot
|
|
saves are treated as web-UI / archival backups. Tests that need to
|
|
represent an archival save should use the `archival_save` fixture.
|
|
"""
|
|
save = Save(
|
|
rom_id=rom.id,
|
|
user_id=admin_user.id,
|
|
file_name="test_save.sav",
|
|
file_name_no_tags="test_save",
|
|
file_name_no_ext="test_save",
|
|
file_extension="sav",
|
|
emulator="test_emulator",
|
|
slot="autosave",
|
|
file_path=f"{platform.slug}/saves/test_emulator",
|
|
file_size_bytes=1.0,
|
|
)
|
|
return db_save_handler.add_save(save)
|
|
|
|
|
|
@pytest.fixture
|
|
def archival_save(rom: Rom, platform: Platform, admin_user: User):
|
|
"""Null-slot save representing a web-UI / archival upload.
|
|
|
|
These should never appear in negotiate plans.
|
|
"""
|
|
save = Save(
|
|
rom_id=rom.id,
|
|
user_id=admin_user.id,
|
|
file_name="archival.sav",
|
|
file_name_no_tags="archival",
|
|
file_name_no_ext="archival",
|
|
file_extension="sav",
|
|
emulator="test_emulator",
|
|
slot=None,
|
|
file_path=f"{platform.slug}/saves/test_emulator",
|
|
file_size_bytes=1.0,
|
|
)
|
|
return db_save_handler.add_save(save)
|
|
|
|
|
|
@pytest.fixture
|
|
def state(rom: Rom, platform: Platform, admin_user: User):
|
|
state = State(
|
|
rom_id=rom.id,
|
|
user_id=admin_user.id,
|
|
file_name="test_state.state",
|
|
file_name_no_tags="test_state",
|
|
file_name_no_ext="test_state",
|
|
file_extension="state",
|
|
emulator="test_emulator",
|
|
file_path=f"{platform.slug}/states/test_emulator",
|
|
file_size_bytes=2.0,
|
|
)
|
|
return db_state_handler.add_state(state)
|
|
|
|
|
|
@pytest.fixture
|
|
def screenshot(rom: Rom, platform: Platform, admin_user: User):
|
|
screenshot = Screenshot(
|
|
rom_id=rom.id,
|
|
user_id=admin_user.id,
|
|
file_name="test_screenshot.png",
|
|
file_name_no_tags="test_screenshot",
|
|
file_name_no_ext="test_screenshot",
|
|
file_extension="png",
|
|
file_path=f"{platform.slug}/screenshots",
|
|
file_size_bytes=3.0,
|
|
)
|
|
return db_screenshot_handler.add_screenshot(screenshot)
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_user():
|
|
user = User(
|
|
username="test_admin",
|
|
hashed_password=auth_handler.get_password_hash("test_admin_password"),
|
|
role=Role.ADMIN,
|
|
)
|
|
return db_user_handler.add_user(user)
|
|
|
|
|
|
@pytest.fixture
|
|
def editor_user():
|
|
user = User(
|
|
username="test_editor",
|
|
hashed_password=auth_handler.get_password_hash("test_editor_password"),
|
|
role=Role.EDITOR,
|
|
)
|
|
return db_user_handler.add_user(user)
|
|
|
|
|
|
@pytest.fixture
|
|
def viewer_user():
|
|
user = User(
|
|
username="test_viewer",
|
|
hashed_password=auth_handler.get_password_hash("test_viewer_password"),
|
|
role=Role.VIEWER,
|
|
)
|
|
return db_user_handler.add_user(user)
|
|
|
|
|
|
@pytest.fixture
|
|
def expired_refresh_token(admin_user: User) -> str:
|
|
expire = int((datetime.now(timezone.utc) + timedelta(seconds=-1)).timestamp())
|
|
|
|
return jwt.encode(
|
|
{"alg": ALGORITHM},
|
|
{
|
|
"sub": admin_user.username,
|
|
"iss": "romm:oauth",
|
|
"scopes": " ".join(admin_user.oauth_scopes),
|
|
"type": "refresh",
|
|
"jti": "expired-test-jti",
|
|
"exp": expire,
|
|
},
|
|
oct_key,
|
|
)
|