mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 18:42:34 +01:00
Three tests reached the public internet on every CI run. `test_update_rom` posts an igdb cover url and never mocked `get_cover`, so the fetch went out; the two lock tests do the same with a manual url. The fix for the manual is not just a decorator: `test_manual.py` patched `get_manual` on the `fs_resource_handler` singleton with `monkeypatch.setattr`, and monkeypatch restores by *setting* the bound method back as an instance attribute, which then shadows a later class-level `patch.object` for the rest of the worker's run. `mock.patch.object` deletes instead, so the patch in test_rom.py now takes effect. Same change for the other singleton patches in that file, which leak the same way. The placeholder host moves to www.screenscraper.fr. `ss.fr` is a live domain-parking host, unrelated to ScreenScraper, and the update path appends `ssid` and `sspassword` to the url before fetching it. The suite now makes no outbound request at all, bar the two ssrf tests that exercise the rejection hook. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2230 lines
73 KiB
Python
2230 lines
73 KiB
Python
import json
|
|
from unittest.mock import AsyncMock, patch
|
|
from urllib.parse import unquote
|
|
|
|
from fastapi import status
|
|
from fastapi.testclient import TestClient
|
|
|
|
from config.config_manager import MetadataMediaType
|
|
from handler.database import db_collection_handler, db_rom_handler
|
|
from handler.database.base_handler import sync_session
|
|
from handler.filesystem.resources_handler import FSResourcesHandler
|
|
from handler.filesystem.roms_handler import FSRomsHandler
|
|
from handler.metadata.flashpoint_handler import FlashpointHandler, FlashpointRom
|
|
from handler.metadata.hltb_handler import HLTBHandler, HLTBRom
|
|
from handler.metadata.igdb_handler import IGDBHandler, IGDBRom
|
|
from handler.metadata.launchbox_handler.handler import LaunchboxHandler
|
|
from handler.metadata.launchbox_handler.types import LaunchboxRom
|
|
from handler.metadata.moby_handler import MobyGamesHandler, MobyGamesRom
|
|
from handler.metadata.ra_handler import RAGameRom, RAHandler
|
|
from handler.metadata.ss_handler import SSHandler, SSRom
|
|
from handler.metadata.steam_handler import SteamHandler, SteamRom
|
|
from models.collection import Collection, SmartCollection
|
|
from models.permission import HiddenEntity, PermEntity
|
|
from models.platform import Platform
|
|
from models.rom import Rom, RomFile, compute_name_sort_key
|
|
from models.user import User
|
|
|
|
MOCK_IGDB_ID = 11111
|
|
MOCK_MOBY_ID = 22222
|
|
MOCK_SS_ID = 33333
|
|
MOCK_RA_ID = 44444
|
|
MOCK_LAUNCHBOX_ID = 55555
|
|
MOCK_FLASHPOINT_ID = 66666
|
|
MOCK_HLTB_ID = 77777
|
|
MOCK_SGDB_ID = 88888
|
|
MOCK_HASHEOUS_ID = 99999
|
|
MOCK_STEAM_ID = 101010
|
|
|
|
|
|
def test_get_rom(client: TestClient, access_token: str, rom: Rom):
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["id"] == rom.id
|
|
|
|
|
|
def test_get_rom_simple(client: TestClient, access_token: str, rom: Rom):
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}/simple",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["id"] == rom.id
|
|
# SimpleRomSchema stays lightweight: none of the detail-only arrays are
|
|
# present, so the endpoint must not eager-load them.
|
|
assert "user_saves" not in body
|
|
assert "user_states" not in body
|
|
assert "user_screenshots" not in body
|
|
assert "user_collections" not in body
|
|
assert "all_user_notes" not in body
|
|
|
|
|
|
def test_get_rom_simple_missing_returns_404(client: TestClient, access_token: str):
|
|
response = client.get(
|
|
"/api/roms/999999/simple",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
def _user_collections_by_name(body: dict) -> dict:
|
|
return {c["name"]: c for c in body["user_collections"]}
|
|
|
|
|
|
def test_get_rom_lists_standard_and_smart_collections(
|
|
client: TestClient, access_token: str, admin_user: User, rom: Rom
|
|
):
|
|
"""The detail response lists both standard and smart collections the ROM
|
|
belongs to, tagging smart ones with `is_smart` (issue #3934)."""
|
|
standard = db_collection_handler.add_collection(
|
|
Collection(name="My Standard", description="", user_id=admin_user.id)
|
|
)
|
|
db_collection_handler.add_roms_to_collection(standard.id, [rom.id])
|
|
|
|
smart = db_collection_handler.add_smart_collection(
|
|
SmartCollection(
|
|
name="My Smart",
|
|
description="",
|
|
user_id=admin_user.id,
|
|
rom_ids=[rom.id],
|
|
filter_criteria={},
|
|
)
|
|
)
|
|
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
collections = _user_collections_by_name(response.json())
|
|
assert set(collections) == {"My Standard", "My Smart"}
|
|
assert collections["My Standard"]["id"] == standard.id
|
|
assert collections["My Standard"]["is_smart"] is False
|
|
assert collections["My Smart"]["id"] == smart.id
|
|
assert collections["My Smart"]["is_smart"] is True
|
|
|
|
|
|
def test_get_rom_omits_smart_collection_without_rom(
|
|
client: TestClient, access_token: str, admin_user: User, rom: Rom
|
|
):
|
|
"""A smart collection whose membership doesn't include the ROM is not listed."""
|
|
db_collection_handler.add_smart_collection(
|
|
SmartCollection(
|
|
name="Unrelated Smart",
|
|
description="",
|
|
user_id=admin_user.id,
|
|
rom_ids=[rom.id + 999],
|
|
filter_criteria={},
|
|
)
|
|
)
|
|
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert "Unrelated Smart" not in _user_collections_by_name(response.json())
|
|
|
|
|
|
def test_get_rom_omits_other_users_private_smart_collection(
|
|
client: TestClient, access_token: str, viewer_user: User, rom: Rom
|
|
):
|
|
"""A private smart collection owned by another user is hidden even when it
|
|
contains the ROM."""
|
|
db_collection_handler.add_smart_collection(
|
|
SmartCollection(
|
|
name="Private Smart",
|
|
description="",
|
|
user_id=viewer_user.id,
|
|
is_public=False,
|
|
rom_ids=[rom.id],
|
|
filter_criteria={},
|
|
)
|
|
)
|
|
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert "Private Smart" not in _user_collections_by_name(response.json())
|
|
|
|
|
|
def test_get_rom_lists_public_smart_collection_from_other_user(
|
|
client: TestClient, access_token: str, viewer_user: User, rom: Rom
|
|
):
|
|
"""A public smart collection owned by another user is listed when it contains
|
|
the ROM."""
|
|
smart = db_collection_handler.add_smart_collection(
|
|
SmartCollection(
|
|
name="Public Smart",
|
|
description="",
|
|
user_id=viewer_user.id,
|
|
is_public=True,
|
|
rom_ids=[rom.id],
|
|
filter_criteria={},
|
|
)
|
|
)
|
|
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
collections = _user_collections_by_name(response.json())
|
|
assert "Public Smart" in collections
|
|
assert collections["Public Smart"]["id"] == smart.id
|
|
assert collections["Public Smart"]["is_smart"] is True
|
|
|
|
|
|
def test_download_multi_file_rom_content(
|
|
client: TestClient, access_token: str, multi_file_rom: Rom
|
|
):
|
|
"""Downloading a multi-file (game folder) ROM must not 500.
|
|
|
|
The download endpoint builds each manifest entry's name from
|
|
`file.rom.full_path` after the handler session has closed; a missing
|
|
`RomFile.rom` back-reference previously raised `DetachedInstanceError`.
|
|
"""
|
|
response = client.get(
|
|
f"/api/roms/{multi_file_rom.id}/content/{multi_file_rom.fs_name}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
# mod_zip manifest: one line per file, plus a generated .m3u playlist.
|
|
assert response.headers["X-Archive-Files"] == "zip"
|
|
body = response.text
|
|
assert "disc1.bin" in body
|
|
assert "disc2.bin" in body
|
|
assert f"{multi_file_rom.fs_name}.m3u" in body
|
|
|
|
|
|
def test_download_roms_by_platform(
|
|
client: TestClient,
|
|
access_token: str,
|
|
platform: Platform,
|
|
rom_file: RomFile,
|
|
):
|
|
"""The `platform_id` selector expands server-side to every ROM in the
|
|
platform, so no ID list rides in the URL."""
|
|
response = client.get(
|
|
f"/api/roms/download?platform_id={platform.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.headers["X-Archive-Files"] == "zip"
|
|
assert rom_file.file_name in response.text
|
|
|
|
|
|
def test_download_roms_by_platform_skips_roms_without_a_file(
|
|
client: TestClient,
|
|
access_token: str,
|
|
platform: Platform,
|
|
rom_file: RomFile,
|
|
):
|
|
"""A physical game has no files to zip, so it must not swell the archive's
|
|
ROM count (and therefore its generated name)."""
|
|
db_rom_handler.add_rom(
|
|
Rom(
|
|
platform_id=platform.id,
|
|
name="Physical Game",
|
|
fs_name="Physical Game",
|
|
fs_path=f"{platform.slug}/roms/.physical",
|
|
fs_size_bytes=0,
|
|
is_physical=True,
|
|
)
|
|
)
|
|
|
|
response = client.get(
|
|
f"/api/roms/download?platform_id={platform.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert "1 ROMs" in unquote(response.headers["Content-Disposition"])
|
|
assert rom_file.file_name in response.text
|
|
|
|
|
|
def test_download_roms_by_collection(
|
|
client: TestClient,
|
|
access_token: str,
|
|
admin_user: User,
|
|
rom_file: RomFile,
|
|
):
|
|
"""The `collection_id` selector expands to the collection's ROMs."""
|
|
collection = db_collection_handler.add_collection(
|
|
Collection(
|
|
name="Download Test",
|
|
description="",
|
|
is_public=False,
|
|
is_favorite=False,
|
|
user_id=admin_user.id,
|
|
)
|
|
)
|
|
db_collection_handler.add_roms_to_collection(collection.id, [rom_file.rom_id])
|
|
|
|
response = client.get(
|
|
f"/api/roms/download?collection_id={collection.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.headers["X-Archive-Files"] == "zip"
|
|
assert rom_file.file_name in response.text
|
|
|
|
|
|
def test_download_roms_without_selector_is_bad_request(
|
|
client: TestClient, access_token: str
|
|
):
|
|
"""Neither an ID list nor a platform/collection selector -> 400."""
|
|
response = client.get(
|
|
"/api/roms/download",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
|
|
|
|
def test_get_all_roms(
|
|
client: TestClient, access_token: str, rom: Rom, platform: Platform
|
|
):
|
|
response = client.get(
|
|
"/api/roms",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={"platform_id": platform.id},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
|
|
assert body["total"] == 1
|
|
assert body["limit"] == 50
|
|
assert body["offset"] == 0
|
|
|
|
items = body["items"]
|
|
assert len(items) == 1
|
|
assert items[0]["id"] == rom.id
|
|
assert items[0]["files"] == []
|
|
assert items[0]["sibling_roms"] == []
|
|
|
|
|
|
def test_get_roms_without_rom_id_index(
|
|
client: TestClient, access_token: str, rom: Rom, platform: Platform
|
|
):
|
|
response = client.get(
|
|
"/api/roms",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={
|
|
"platform_id": platform.id,
|
|
"limit": 15,
|
|
"with_rom_id_index": False,
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
|
|
# The page and total stay correct, but the full id index is not built.
|
|
assert body["total"] == 1
|
|
assert body["rom_id_index"] == []
|
|
|
|
items = body["items"]
|
|
assert len(items) == 1
|
|
assert items[0]["id"] == rom.id
|
|
|
|
|
|
def test_get_roms_without_total(
|
|
client: TestClient, access_token: str, rom: Rom, platform: Platform
|
|
):
|
|
params = {
|
|
"platform_id": platform.id,
|
|
"limit": 15,
|
|
"with_rom_id_index": False,
|
|
}
|
|
|
|
with patch.object(
|
|
db_rom_handler, "get_rom_count", wraps=db_rom_handler.get_rom_count
|
|
) as get_rom_count:
|
|
response = client.get(
|
|
"/api/roms",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={**params, "with_total": False},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
# The point of the opt-out: no second scan of the filtered set.
|
|
get_rom_count.assert_not_called()
|
|
|
|
body = response.json()
|
|
assert body["total"] is None
|
|
|
|
items = body["items"]
|
|
assert len(items) == 1
|
|
assert items[0]["id"] == rom.id
|
|
|
|
# Control: the count still runs for callers that ask for it.
|
|
response = client.get(
|
|
"/api/roms",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params=params,
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.json()["total"] == 1
|
|
get_rom_count.assert_called_once()
|
|
|
|
|
|
def test_get_roms_keeps_total_from_the_rom_id_index(
|
|
client: TestClient, access_token: str, rom: Rom, platform: Platform
|
|
):
|
|
# The index already carries the count, so opting out of the separate
|
|
# count query costs the caller nothing there.
|
|
response = client.get(
|
|
"/api/roms",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={"platform_id": platform.id, "with_total": False},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["total"] == 1
|
|
assert body["rom_id_index"] == [rom.id]
|
|
|
|
|
|
def test_get_roms_filter_by_metadata_providers(
|
|
client: TestClient, access_token: str, rom: Rom, platform: Platform
|
|
):
|
|
rom_igdb = db_rom_handler.add_rom(
|
|
Rom(
|
|
platform_id=platform.id,
|
|
name="rom_igdb",
|
|
slug="rom_igdb",
|
|
fs_name="rom_igdb.zip",
|
|
fs_name_no_tags="rom_igdb",
|
|
fs_name_no_ext="rom_igdb",
|
|
fs_extension="zip",
|
|
fs_path=f"{platform.slug}/roms",
|
|
igdb_id=MOCK_IGDB_ID,
|
|
)
|
|
)
|
|
|
|
response = client.get(
|
|
"/api/roms",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={"platform_id": platform.id, "metadata_providers": ["igdb"]},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert {item["id"] for item in body["items"]} == {rom_igdb.id}
|
|
|
|
# "none" logic returns the ROMs not matched to the selected provider.
|
|
response = client.get(
|
|
"/api/roms",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={
|
|
"platform_id": platform.id,
|
|
"metadata_providers": ["igdb"],
|
|
"metadata_providers_logic": "none",
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert {item["id"] for item in body["items"]} == {rom.id}
|
|
|
|
|
|
def test_get_roms_filter_by_tags(
|
|
client: TestClient, access_token: str, rom: Rom, platform: Platform
|
|
):
|
|
rom_proto = db_rom_handler.add_rom(
|
|
Rom(
|
|
platform_id=platform.id,
|
|
name="rom_proto",
|
|
slug="rom_proto",
|
|
fs_name="rom_proto.zip",
|
|
fs_name_no_tags="rom_proto",
|
|
fs_name_no_ext="rom_proto",
|
|
fs_extension="zip",
|
|
fs_path=f"{platform.slug}/roms",
|
|
tags=["Proto"],
|
|
)
|
|
)
|
|
|
|
response = client.get(
|
|
"/api/roms",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={"platform_id": platform.id, "tags": ["Proto"]},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert {item["id"] for item in body["items"]} == {rom_proto.id}
|
|
# The selectable tag list is surfaced through filter_values.
|
|
assert "Proto" in body["filter_values"]["tags"]
|
|
|
|
# "none" logic returns the ROMs not carrying the selected tag.
|
|
response = client.get(
|
|
"/api/roms",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={
|
|
"platform_id": platform.id,
|
|
"tags": ["Proto"],
|
|
"tags_logic": "none",
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert {item["id"] for item in body["items"]} == {rom.id}
|
|
|
|
|
|
def test_get_all_roms_with_files(
|
|
client: TestClient, access_token: str, rom: Rom, platform: Platform
|
|
):
|
|
db_rom_handler.add_rom_file(
|
|
RomFile(
|
|
rom_id=rom.id,
|
|
file_name="test_rom.zip",
|
|
file_path=f"{platform.slug}/roms",
|
|
file_size_bytes=1024,
|
|
last_modified=1700000000.0,
|
|
)
|
|
)
|
|
|
|
response = client.get(
|
|
"/api/roms",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={"platform_id": platform.id, "with_files": True},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
item = response.json()["items"][0]
|
|
assert item["id"] == rom.id
|
|
assert len(item["files"]) == 1
|
|
assert item["files"][0]["file_name"] == "test_rom.zip"
|
|
# with_files alone must not pull in sibling_roms
|
|
assert item["sibling_roms"] == []
|
|
|
|
|
|
def test_get_rom_content_requires_auth(client: TestClient, rom: Rom, rom_file):
|
|
response = client.get(f"/api/roms/{rom.id}/content/test_rom.zip")
|
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
|
|
|
|
|
def test_get_rom_content_single_file(
|
|
client: TestClient, access_token: str, rom: Rom, rom_file
|
|
):
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}/content/test_rom.zip",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
follow_redirects=False,
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
# Single-file roms are proxied through nginx via X-Accel-Redirect.
|
|
assert "X-Accel-Redirect" in response.headers
|
|
|
|
|
|
def test_get_rom_content_single_file_missing_on_disk_returns_404(
|
|
client: TestClient, access_token: str, rom: Rom, rom_file, mocker
|
|
):
|
|
# In DEV_MODE the endpoint serves the file directly. If the file is gone
|
|
# from disk (e.g. a renamed/moved ROM whose old entry is now missing), it
|
|
# must return a clean 404 instead of raising a RuntimeError from
|
|
# FileResponse when starlette fails to stat the path.
|
|
mocker.patch("endpoints.roms.DEV_MODE", True)
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}/content/test_rom.zip",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
follow_redirects=False,
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
def test_get_rom_content_valid_file_id(
|
|
client: TestClient, access_token: str, rom: Rom, rom_file
|
|
):
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}/content/test_rom.zip",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={"file_ids": str(rom_file.id)},
|
|
follow_redirects=False,
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert "X-Accel-Redirect" in response.headers
|
|
|
|
|
|
def test_get_rom_content_stale_file_id_returns_404(
|
|
client: TestClient, access_token: str, rom: Rom, rom_file
|
|
):
|
|
# Regression test for #3470: a remembered file id that no longer exists
|
|
# (e.g. after a rename gave the file a new id) must return a clean 404
|
|
# instead of an empty-.m3u ZIP that nginx aborts as a 0-byte response.
|
|
stale_file_id = rom_file.id + 999
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}/content/test_rom.zip",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={"file_ids": str(stale_file_id)},
|
|
follow_redirects=False,
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
def test_get_rom_content_missing_rom_returns_404(client: TestClient, access_token: str):
|
|
response = client.get(
|
|
"/api/roms/999999/content/missing.zip",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
follow_redirects=False,
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
def _hide_rom_for_user(rom_id: int, user_id: int) -> None:
|
|
with sync_session.begin() as s:
|
|
s.add(HiddenEntity(entity=PermEntity.ROMS, entity_id=rom_id, user_id=user_id))
|
|
|
|
|
|
def test_get_romfile_content_visible_rom(
|
|
client: TestClient, viewer_access_token: str, rom: Rom, rom_file
|
|
):
|
|
# Baseline: a rom the viewer can see is downloadable by direct RomFile.id.
|
|
response = client.get(
|
|
f"/api/roms/{rom_file.id}/files/content/whatever.bin",
|
|
headers={"Authorization": f"Bearer {viewer_access_token}"},
|
|
follow_redirects=False,
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert "X-Accel-Redirect" in response.headers
|
|
|
|
|
|
def test_get_romfile_content_hidden_rom_returns_404(
|
|
client: TestClient, viewer_access_token: str, viewer_user, rom: Rom, rom_file
|
|
):
|
|
# A file belonging to a hidden rom must 404 even by direct RomFile.id,
|
|
# matching the ROM-level content endpoint (visibility-bypass regression).
|
|
_hide_rom_for_user(rom.id, viewer_user.id)
|
|
response = client.get(
|
|
f"/api/roms/{rom_file.id}/files/content/whatever.bin",
|
|
headers={"Authorization": f"Bearer {viewer_access_token}"},
|
|
follow_redirects=False,
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
def test_get_romfile_hidden_rom_returns_404(
|
|
client: TestClient, viewer_access_token: str, viewer_user, rom: Rom, rom_file
|
|
):
|
|
# The file metadata endpoint must not leak files of a hidden rom either.
|
|
_hide_rom_for_user(rom.id, viewer_user.id)
|
|
response = client.get(
|
|
f"/api/roms/{rom_file.id}/files",
|
|
headers={"Authorization": f"Bearer {viewer_access_token}"},
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
@patch.object(FSRomsHandler, "rename_fs_rom")
|
|
@patch.object(IGDBHandler, "get_rom_by_id", return_value=IGDBRom(igdb_id=None))
|
|
@patch.object(
|
|
FSResourcesHandler,
|
|
"get_cover",
|
|
new_callable=AsyncMock,
|
|
return_value=("path/to/small.png", "path/to/big.png"),
|
|
)
|
|
def test_update_rom(
|
|
get_cover_mock: AsyncMock,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
rename_fs_rom_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"igdb_id": str(MOCK_IGDB_ID),
|
|
"name": "Metroid Prime Remastered",
|
|
"name_sort_key": "Metroid Prime",
|
|
"slug": "metroid-prime-remastered",
|
|
"fs_name": "Metroid Prime Remastered.zip",
|
|
"summary": "summary test",
|
|
"url_cover": "https://images.igdb.com/igdb/image/upload/t_cover_big/co2l7z.jpg",
|
|
"genres": '[{"id": 5, "name": "Shooter"}, {"id": 8, "name": "Platform"}, {"id": 31, "name": "Adventure"}]',
|
|
"franchises": '[{"id": 756, "name": "Metroid"}]',
|
|
"collections": '[{"id": 243, "name": "Metroid"}, {"id": 6240, "name": "Metroid Prime"}]',
|
|
"player_count": "1",
|
|
"expansions": "[]",
|
|
"dlcs": "[]",
|
|
"companies": '[{"id": 203227, "company": {"id": 70, "name": "Nintendo"}}, {"id": 203307, "company": {"id": 766, "name": "Retro Studios"}}]',
|
|
"first_release_date": "1675814400",
|
|
"youtube_video_id": "dQw4w9WgXcQ",
|
|
"remasters": "[]",
|
|
"remakes": "[]",
|
|
"expanded_games": "[]",
|
|
"ports": "[]",
|
|
"similar_games": "[]",
|
|
"age_ratings": "[1, 2]",
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["fs_name"] == "Metroid Prime Remastered.zip"
|
|
assert body["name_sort_key"] == compute_name_sort_key("Metroid Prime")
|
|
|
|
assert rename_fs_rom_mock.called
|
|
assert get_rom_by_id_mock.called
|
|
|
|
|
|
@patch.object(FSRomsHandler, "rename_fs_rom")
|
|
@patch.object(IGDBHandler, "get_rom_by_id", return_value=IGDBRom(igdb_id=None))
|
|
def test_update_rom_reparses_tags_on_fs_name_change(
|
|
rename_fs_rom_mock: AsyncMock,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"fs_name": "Patapon (Fr, En) (Rev 1).iso"},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["fs_name"] == "Patapon (Fr, En) (Rev 1).iso"
|
|
assert body["languages"] == ["French", "English"]
|
|
assert body["regions"] == []
|
|
assert body["revision"] == "1"
|
|
assert body["tags"] == []
|
|
|
|
|
|
@patch.object(FSRomsHandler, "rename_fs_rom")
|
|
@patch.object(IGDBHandler, "get_rom_by_id", return_value=IGDBRom(igdb_id=None))
|
|
def test_update_rom_adds_region_tag_on_rename(
|
|
rename_fs_rom_mock: AsyncMock,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Renaming an untagged ROM to add ``(Europe)`` surfaces the region (issue #3471)."""
|
|
assert rom.regions == []
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"fs_name": "test_rom (Europe).zip"},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["fs_name"] == "test_rom (Europe).zip"
|
|
assert body["regions"] == ["Europe"]
|
|
|
|
|
|
@patch.object(FSRomsHandler, "rename_fs_rom")
|
|
@patch.object(IGDBHandler, "get_rom_by_id", return_value=IGDBRom(igdb_id=None))
|
|
def test_update_rom_refreshes_smart_collection_membership(
|
|
rename_fs_rom_mock: AsyncMock,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
admin_user: User,
|
|
rom: Rom,
|
|
):
|
|
# An edit changes what the saved filters match, so the cached counts have
|
|
# to follow it rather than wait for the next scan.
|
|
smart_collection = db_collection_handler.add_smart_collection(
|
|
SmartCollection(
|
|
name="European games",
|
|
description="",
|
|
user_id=admin_user.id,
|
|
filter_criteria={"regions": ["Europe"]},
|
|
)
|
|
)
|
|
db_collection_handler.refresh_smart_collection(smart_collection.id)
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"fs_name": "test_rom (Europe).zip"},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
refreshed = db_collection_handler.get_smart_collection(smart_collection.id)
|
|
assert refreshed is not None
|
|
assert refreshed.rom_ids == [rom.id]
|
|
|
|
|
|
# Minimal valid PNG (1x1 transparent pixel)
|
|
_PNG_BYTES = (
|
|
b"\x89PNG\r\n\x1a\n"
|
|
b"\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
|
|
b"\x08\x06\x00\x00\x00\x1f\x15\xc4\x89"
|
|
b"\x00\x00\x00\rIDATx\x9cc\xfc\xff\xff?\x00\x05\xfe\x02\xfe\xa75\x81\x84"
|
|
b"\x00\x00\x00\x00IEND\xaeB`\x82"
|
|
)
|
|
|
|
|
|
def test_update_rom_rejects_non_image_artwork(
|
|
client: TestClient, access_token: str, rom: Rom
|
|
):
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
files={"artwork": ("cover.png", b"<script>alert(1)</script>", "image/png")},
|
|
)
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
assert "PNG, JPEG, WebP, or GIF" in response.json()["detail"]
|
|
|
|
|
|
@patch.object(
|
|
FSResourcesHandler,
|
|
"store_artwork",
|
|
new_callable=AsyncMock,
|
|
return_value=("path/to/big.png", "path/to/small.png"),
|
|
)
|
|
def test_update_rom_artwork_uses_detected_extension(
|
|
store_artwork_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
files={"artwork": ("payload.html", _PNG_BYTES, "image/png")},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
# The handler is called with the trusted extension from libmagic, not the
|
|
# extension parsed off the user-supplied filename.
|
|
assert store_artwork_mock.called
|
|
_, _, file_ext = store_artwork_mock.call_args.args
|
|
assert file_ext == "png"
|
|
|
|
|
|
@patch.object(
|
|
FSResourcesHandler,
|
|
"store_artwork",
|
|
new_callable=AsyncMock,
|
|
return_value=("path/to/big.png", "path/to/small.png"),
|
|
)
|
|
def test_update_rom_artwork_locks_the_cover(
|
|
store_artwork_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
# Supplying a file is the explicit act that locks the cover.
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
files={"artwork": ("cover.png", _PNG_BYTES, "image/png")},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert db_rom_handler.get_rom(rom.id).locked_fields == ["url_cover"]
|
|
|
|
|
|
@patch.object(
|
|
FSResourcesHandler,
|
|
"remove_cover",
|
|
new_callable=AsyncMock,
|
|
return_value={"path_cover_s": "", "path_cover_l": ""},
|
|
)
|
|
def test_remove_cover_releases_the_lock(
|
|
remove_cover_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
# Dropping the hand-supplied cover hands the slot back to the providers.
|
|
db_rom_handler.update_rom(rom.id, {"locked_fields": ["url_cover"]})
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}?remove_cover=true",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert db_rom_handler.get_rom(rom.id).locked_fields == []
|
|
|
|
|
|
@patch.object(
|
|
FSResourcesHandler,
|
|
"get_manual",
|
|
new_callable=AsyncMock,
|
|
return_value="path/to/manual.pdf",
|
|
)
|
|
@patch.object(
|
|
FSResourcesHandler,
|
|
"get_cover",
|
|
new_callable=AsyncMock,
|
|
return_value=("path/to/small.png", "path/to/big.png"),
|
|
)
|
|
def test_saving_without_changing_urls_keeps_locks(
|
|
get_cover_mock: AsyncMock,
|
|
get_manual_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
# The client posts the stored urls on every save, so a url merely being
|
|
# present must not release the lock.
|
|
db_rom_handler.update_rom(
|
|
rom.id,
|
|
{
|
|
"url_cover": "",
|
|
"url_manual": "https://www.screenscraper.fr/manual?id=1",
|
|
"locked_fields": ["url_cover", "url_manual"],
|
|
},
|
|
)
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"url_cover": "",
|
|
"url_manual": "https://www.screenscraper.fr/manual?id=1",
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert db_rom_handler.get_rom(rom.id).locked_fields == [
|
|
"url_cover",
|
|
"url_manual",
|
|
]
|
|
|
|
|
|
@patch.object(
|
|
FSResourcesHandler,
|
|
"get_manual",
|
|
new_callable=AsyncMock,
|
|
return_value="path/to/manual.pdf",
|
|
)
|
|
@patch.object(
|
|
FSResourcesHandler,
|
|
"get_cover",
|
|
new_callable=AsyncMock,
|
|
return_value=("path/to/small.png", "path/to/big.png"),
|
|
)
|
|
def test_naming_new_source_urls_releases_both_locks(
|
|
get_cover_mock: AsyncMock,
|
|
get_manual_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
# Choosing a provider's artwork is a handover, and both slots release in
|
|
# the one request.
|
|
db_rom_handler.update_rom(
|
|
rom.id,
|
|
{
|
|
"url_cover": "",
|
|
"url_manual": "https://www.screenscraper.fr/manual?id=1",
|
|
"locked_fields": ["url_cover", "url_manual"],
|
|
},
|
|
)
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"url_cover": "https://www.screenscraper.fr/cover?id=2",
|
|
"url_manual": "https://www.screenscraper.fr/manual?id=2",
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert db_rom_handler.get_rom(rom.id).locked_fields == []
|
|
|
|
|
|
def test_delete_roms(client: TestClient, access_token: str, rom: Rom):
|
|
response = client.post(
|
|
"/api/roms/delete",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={"roms": [rom.id], "delete_from_fs": []},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["successful_items"] == 1
|
|
|
|
|
|
def test_delete_roms_reports_results_when_the_refresh_fails(
|
|
client: TestClient, access_token: str, rom: Rom, mocker
|
|
):
|
|
# The deletes are already committed by this point, so a failure updating
|
|
# cached smart collection membership must not cost the caller its report.
|
|
mocker.patch.object(
|
|
db_collection_handler,
|
|
"refresh_smart_collections_for_roms",
|
|
side_effect=RuntimeError("refresh exploded"),
|
|
)
|
|
|
|
response = client.post(
|
|
"/api/roms/delete",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={"roms": [rom.id], "delete_from_fs": []},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.json()["successful_items"] == 1
|
|
assert db_rom_handler.get_rom(rom.id) is None
|
|
|
|
|
|
def test_delete_roms_reports_failed_ids(
|
|
client: TestClient, access_token: str, rom: Rom
|
|
):
|
|
missing_id = rom.id + 999999
|
|
response = client.post(
|
|
"/api/roms/delete",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={"roms": [rom.id, missing_id], "delete_from_fs": []},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["successful_items"] == 1
|
|
# The failed id stays reported so the client can keep it selected for retry.
|
|
assert body["failed_ids"] == [missing_id]
|
|
|
|
|
|
@patch(
|
|
"endpoints.roms.fs_rom_handler.remove_directory",
|
|
new_callable=AsyncMock,
|
|
)
|
|
@patch(
|
|
"endpoints.roms.fs_rom_handler.remove_file",
|
|
new_callable=AsyncMock,
|
|
)
|
|
@patch(
|
|
"endpoints.roms.fs_rom_handler.validate_path",
|
|
)
|
|
def test_delete_roms_from_fs_flat(
|
|
mock_validate_path,
|
|
mock_remove_file,
|
|
mock_remove_directory,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that flat (non-directory) ROM files are deleted from filesystem."""
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock
|
|
|
|
mock_path = MagicMock(spec=Path)
|
|
mock_path.is_dir.return_value = False
|
|
mock_path.parent.is_dir.return_value = False
|
|
mock_validate_path.return_value = mock_path
|
|
|
|
response = client.post(
|
|
"/api/roms/delete",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={"roms": [rom.id], "delete_from_fs": [rom.id]},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["successful_items"] == 1
|
|
assert body["failed_ids"] == []
|
|
mock_remove_file.assert_called_once()
|
|
mock_remove_directory.assert_not_called()
|
|
|
|
|
|
@patch(
|
|
"endpoints.roms.fs_rom_handler.remove_directory",
|
|
new_callable=AsyncMock,
|
|
)
|
|
@patch(
|
|
"endpoints.roms.fs_rom_handler.remove_file",
|
|
new_callable=AsyncMock,
|
|
)
|
|
@patch(
|
|
"endpoints.roms.fs_rom_handler.validate_path",
|
|
)
|
|
def test_delete_roms_from_fs_flat_cleans_empty_parent(
|
|
mock_validate_path,
|
|
mock_remove_file,
|
|
mock_remove_directory,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that empty parent directories are cleaned up after flat ROM file deletion."""
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock
|
|
|
|
mock_path = MagicMock(spec=Path)
|
|
mock_path.is_dir.return_value = False
|
|
# Parent is not the base_path (a MagicMock will not equal a real Path), is a dir, and is empty
|
|
mock_path.parent.is_dir.return_value = True
|
|
mock_path.parent.__iter__ = lambda self: iter([]) # empty directory
|
|
mock_validate_path.return_value = mock_path
|
|
|
|
response = client.post(
|
|
"/api/roms/delete",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={"roms": [rom.id], "delete_from_fs": [rom.id]},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["successful_items"] == 1
|
|
assert body["failed_ids"] == []
|
|
mock_remove_file.assert_called_once()
|
|
# remove_directory should be called to clean up the empty parent dir
|
|
mock_remove_directory.assert_called_once()
|
|
|
|
|
|
@patch(
|
|
"endpoints.roms.fs_rom_handler.remove_directory",
|
|
new_callable=AsyncMock,
|
|
)
|
|
@patch(
|
|
"endpoints.roms.fs_rom_handler.validate_path",
|
|
)
|
|
def test_delete_roms_from_fs_nested(
|
|
mock_validate_path,
|
|
mock_remove_directory,
|
|
client: TestClient,
|
|
access_token: str,
|
|
platform: Platform,
|
|
):
|
|
"""Test that nested (directory) ROMs are deleted using remove_directory."""
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock
|
|
|
|
from handler.database import db_rom_handler
|
|
from models.rom import Rom
|
|
|
|
nested_rom = Rom(
|
|
platform_id=platform.id,
|
|
name="Nested Game",
|
|
slug="nested-game",
|
|
fs_name="Nested Game",
|
|
fs_name_no_tags="Nested Game",
|
|
fs_name_no_ext="Nested Game",
|
|
fs_extension="",
|
|
fs_path=f"{platform.slug}/roms",
|
|
)
|
|
nested_rom = db_rom_handler.add_rom(nested_rom)
|
|
|
|
mock_path = MagicMock(spec=Path)
|
|
mock_path.is_dir.return_value = True
|
|
mock_validate_path.return_value = mock_path
|
|
|
|
response = client.post(
|
|
"/api/roms/delete",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={"roms": [nested_rom.id], "delete_from_fs": [nested_rom.id]},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["successful_items"] == 1
|
|
assert body["failed_ids"] == []
|
|
mock_remove_directory.assert_called_once()
|
|
|
|
|
|
@patch("endpoints.roms.fs_rom_handler.validate_path")
|
|
def test_delete_roms_from_fs_missing_file_still_deletes_db_entry(
|
|
mock_validate_path,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that missing ROM files do not block database deletion."""
|
|
from handler.database import db_rom_handler
|
|
|
|
mock_validate_path.side_effect = FileNotFoundError
|
|
|
|
response = client.post(
|
|
"/api/roms/delete",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={"roms": [rom.id], "delete_from_fs": [rom.id]},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["successful_items"] == 1
|
|
assert body["failed_ids"] == []
|
|
assert body["errors"] == []
|
|
assert db_rom_handler.get_rom(rom.id) is None
|
|
|
|
|
|
def test_update_rom_user_props_flat_payload(
|
|
client: TestClient, access_token: str, rom: Rom
|
|
):
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}/props",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={"backlogged": True, "rating": 7},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["backlogged"] is True
|
|
assert body["rating"] == 7
|
|
|
|
|
|
def test_update_rom_user_props_partial_update(
|
|
client: TestClient, access_token: str, rom: Rom
|
|
):
|
|
# Set initial values
|
|
setup_response = client.put(
|
|
f"/api/roms/{rom.id}/props",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={"backlogged": True, "rating": 5, "hidden": True},
|
|
)
|
|
assert setup_response.status_code == status.HTTP_200_OK
|
|
|
|
# Partial update: only update rating, backlogged and hidden should remain unchanged
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}/props",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={"rating": 9},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["rating"] == 9
|
|
assert body["backlogged"] is True
|
|
assert body["hidden"] is True
|
|
|
|
|
|
def test_update_rom_user_props_last_played_flags(
|
|
client: TestClient, access_token: str, rom: Rom
|
|
):
|
|
mark_played_response = client.put(
|
|
f"/api/roms/{rom.id}/props?update_last_played=true",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={},
|
|
)
|
|
assert mark_played_response.status_code == status.HTTP_200_OK
|
|
assert mark_played_response.json()["last_played"] is not None
|
|
|
|
clear_played_response = client.put(
|
|
f"/api/roms/{rom.id}/props?remove_last_played=true",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
json={},
|
|
)
|
|
assert clear_played_response.status_code == status.HTTP_200_OK
|
|
assert clear_played_response.json()["last_played"] is None
|
|
|
|
|
|
class TestUpdateMetadataIDs:
|
|
@patch.object(
|
|
IGDBHandler, "get_rom_by_id", return_value=IGDBRom(igdb_id=MOCK_IGDB_ID)
|
|
)
|
|
def test_update_rom_igdb_id(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating IGDB ID."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"igdb_id": str(MOCK_IGDB_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["igdb_id"] == MOCK_IGDB_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(IGDBHandler, "get_rom_by_id", return_value=IGDBRom(igdb_id=None))
|
|
def test_update_rom_igdb_id_persists_when_handler_disabled(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that IGDB ID persists when handler is disabled or game not found."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"igdb_id": str(MOCK_IGDB_ID),
|
|
"name_sort_key": "Imported sort title",
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["igdb_id"] == MOCK_IGDB_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(
|
|
MobyGamesHandler,
|
|
"get_rom_by_id",
|
|
return_value=MobyGamesRom(moby_id=MOCK_MOBY_ID),
|
|
)
|
|
def test_update_rom_moby_id(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating MobyGames ID."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"moby_id": str(MOCK_MOBY_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["moby_id"] == MOCK_MOBY_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(
|
|
MobyGamesHandler,
|
|
"get_rom_by_id",
|
|
return_value=MobyGamesRom(moby_id=None),
|
|
)
|
|
def test_update_rom_moby_id_persists_when_handler_disabled(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that MobyGames ID persists when handler is disabled or game not found."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"moby_id": str(MOCK_MOBY_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["moby_id"] == MOCK_MOBY_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(SSHandler, "get_rom_by_id", return_value=SSRom(ss_id=MOCK_SS_ID))
|
|
def test_update_rom_ss_id(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating ScreenScraper ID."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"ss_id": str(MOCK_SS_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["ss_id"] == MOCK_SS_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(SSHandler, "get_rom_by_id", return_value=SSRom(ss_id=None))
|
|
def test_update_rom_ss_id_persists_when_handler_disabled(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that ScreenScraper ID persists when handler is disabled or game not found."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"ss_id": str(MOCK_SS_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["ss_id"] == MOCK_SS_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(RAHandler, "get_rom_by_id", return_value=RAGameRom(ra_id=MOCK_RA_ID))
|
|
def test_update_rom_ra_id(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating RetroAchievements ID."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"ra_id": str(MOCK_RA_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["ra_id"] == MOCK_RA_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(RAHandler, "get_rom_by_id", return_value=RAGameRom(ra_id=None))
|
|
def test_update_rom_ra_id_persists_when_handler_disabled(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that RetroAchievements ID persists when handler is disabled or game not found."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"ra_id": str(MOCK_RA_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["ra_id"] == MOCK_RA_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(
|
|
LaunchboxHandler,
|
|
"get_rom_by_id",
|
|
return_value=LaunchboxRom(launchbox_id=MOCK_LAUNCHBOX_ID),
|
|
)
|
|
def test_update_rom_launchbox_id(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating LaunchBox ID."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"launchbox_id": str(MOCK_LAUNCHBOX_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["launchbox_id"] == MOCK_LAUNCHBOX_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(FSResourcesHandler, "store_media_file", new_callable=AsyncMock)
|
|
@patch(
|
|
"handler.metadata.launchbox_handler.media.get_preferred_media_types",
|
|
return_value=[MetadataMediaType.VIDEO],
|
|
)
|
|
@patch(
|
|
"endpoints.roms.get_preferred_media_types",
|
|
return_value=[MetadataMediaType.VIDEO],
|
|
)
|
|
@patch.object(
|
|
LaunchboxHandler,
|
|
"get_rom_by_id",
|
|
return_value=LaunchboxRom(
|
|
launchbox_id=MOCK_LAUNCHBOX_ID,
|
|
launchbox_metadata={ # type: ignore[typeddict-item]
|
|
"video_url": "launchbox-file://Videos/NES/Mario.mp4",
|
|
},
|
|
),
|
|
)
|
|
def test_update_rom_launchbox_id_imports_local_video(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
_get_preferred_endpoint_mock: AsyncMock,
|
|
_get_preferred_media_mock: AsyncMock,
|
|
store_media_file_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""A LaunchBox match with a local video resolves a path and copies the file."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"launchbox_id": str(MOCK_LAUNCHBOX_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["launchbox_id"] == MOCK_LAUNCHBOX_ID
|
|
# get_rom_by_id must receive fs_name/platform_slug so local media resolves
|
|
assert get_rom_by_id_mock.call_args.kwargs.get("fs_name") == rom.fs_name
|
|
assert (
|
|
get_rom_by_id_mock.call_args.kwargs.get("platform_slug")
|
|
== rom.platform_slug
|
|
)
|
|
# populate_rom_specific_paths must have set video_path
|
|
video_path = body["launchbox_metadata"].get("video_path", "")
|
|
assert video_path.endswith("/video.mp4")
|
|
# and the video file must have been copied into the resource store
|
|
store_media_file_mock.assert_awaited_once()
|
|
await_args = store_media_file_mock.await_args
|
|
assert await_args is not None
|
|
called_url, called_path = await_args.args
|
|
assert called_url == "launchbox-file://Videos/NES/Mario.mp4"
|
|
assert called_path == video_path
|
|
|
|
@patch.object(
|
|
LaunchboxHandler,
|
|
"get_rom_by_id",
|
|
return_value=LaunchboxRom(launchbox_id=None),
|
|
)
|
|
def test_update_rom_launchbox_id_persists_when_handler_disabled(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that LaunchBox ID persists when handler is disabled or game not found."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"launchbox_id": str(MOCK_LAUNCHBOX_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["launchbox_id"] == MOCK_LAUNCHBOX_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(
|
|
FlashpointHandler,
|
|
"get_rom_by_id",
|
|
return_value=FlashpointRom(flashpoint_id=str(MOCK_FLASHPOINT_ID)),
|
|
)
|
|
def test_update_rom_flashpoint_id(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating Flashpoint ID."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"flashpoint_id": str(MOCK_FLASHPOINT_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["flashpoint_id"] == str(MOCK_FLASHPOINT_ID)
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(
|
|
FlashpointHandler,
|
|
"get_rom_by_id",
|
|
return_value=FlashpointRom(flashpoint_id=None),
|
|
)
|
|
def test_update_rom_flashpoint_id_persists_when_handler_disabled(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that Flashpoint ID persists when handler is disabled or game not found."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"flashpoint_id": str(MOCK_FLASHPOINT_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["flashpoint_id"] == str(MOCK_FLASHPOINT_ID)
|
|
assert get_rom_by_id_mock.called
|
|
|
|
# These metadata sources are not called when updating roms
|
|
def test_update_rom_sgdb_id(self, client: TestClient, access_token: str, rom: Rom):
|
|
"""Test updating SteamGridDB ID."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"sgdb_id": str(MOCK_SGDB_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["sgdb_id"] == MOCK_SGDB_ID
|
|
|
|
def test_update_rom_hasheous_id(
|
|
self, client: TestClient, access_token: str, rom: Rom
|
|
):
|
|
"""Test updating Hasheous ID."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"hasheous_id": str(MOCK_HASHEOUS_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["hasheous_id"] == MOCK_HASHEOUS_ID
|
|
|
|
@patch.object(
|
|
HLTBHandler,
|
|
"get_rom_by_id",
|
|
return_value=HLTBRom(hltb_id=MOCK_HLTB_ID, hltb_metadata={"main_story": 92822}),
|
|
)
|
|
def test_update_rom_hltb_id(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""A hand-entered HowLongToBeat ID has to pull its times down with it."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"hltb_id": str(MOCK_HLTB_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["hltb_id"] == MOCK_HLTB_ID
|
|
assert body["hltb_metadata"]["main_story"] == 92822
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(HLTBHandler, "get_rom_by_id", return_value=HLTBRom(hltb_id=None))
|
|
def test_update_rom_hltb_id_persists_when_handler_disabled(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that HLTB ID persists when handler is disabled or game not found."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"hltb_id": str(MOCK_HLTB_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["hltb_id"] == MOCK_HLTB_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(
|
|
SteamHandler,
|
|
"get_rom_by_id",
|
|
return_value=SteamRom(
|
|
steam_id=MOCK_STEAM_ID, steam_metadata={"total_rating": "86"}
|
|
),
|
|
)
|
|
def test_update_rom_steam_id(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""A hand-entered Steam app ID has to pull its store payload down with it."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"steam_id": str(MOCK_STEAM_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["steam_id"] == MOCK_STEAM_ID
|
|
assert body["steam_metadata"]["total_rating"] == "86"
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(SteamHandler, "get_rom_by_id", return_value=SteamRom(steam_id=None))
|
|
def test_update_rom_steam_id_persists_when_handler_disabled(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that Steam ID persists when handler is disabled or game not found."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"steam_id": str(MOCK_STEAM_ID)},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["steam_id"] == MOCK_STEAM_ID
|
|
assert get_rom_by_id_mock.called
|
|
|
|
@patch.object(
|
|
SteamHandler,
|
|
"get_rom_by_id",
|
|
return_value=SteamRom(
|
|
steam_id=MOCK_STEAM_ID, steam_metadata={"total_rating": "86"}
|
|
),
|
|
)
|
|
def test_update_rom_clearing_steam_id_drops_its_metadata(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Clearing the ID has to take the stored store payload with it."""
|
|
matched = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"steam_id": str(MOCK_STEAM_ID)},
|
|
)
|
|
assert matched.status_code == status.HTTP_200_OK
|
|
assert matched.json()["steam_metadata"]["total_rating"] == "86"
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"steam_id": ""},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["steam_id"] is None
|
|
assert body["steam_metadata"] == {}
|
|
|
|
|
|
class TestUpdateRawMetadata:
|
|
@patch.object(
|
|
IGDBHandler,
|
|
"get_rom_by_id",
|
|
return_value=IGDBRom(igdb_id=MOCK_IGDB_ID),
|
|
)
|
|
def test_update_raw_igdb_metadata(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating raw IGDB metadata."""
|
|
raw_metadata = {
|
|
"genres": ["Action"],
|
|
"franchises": ["Metroid"],
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"igdb_id": str(MOCK_IGDB_ID),
|
|
"raw_igdb_metadata": json.dumps(raw_metadata),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["igdb_metadata"] is not None
|
|
assert body["igdb_metadata"]["genres"] == ["Action"]
|
|
assert body["igdb_metadata"]["franchises"] == ["Metroid"]
|
|
|
|
@patch.object(
|
|
MobyGamesHandler,
|
|
"get_rom_by_id",
|
|
return_value=MobyGamesRom(moby_id=MOCK_MOBY_ID),
|
|
)
|
|
def test_update_raw_moby_metadata(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating raw MobyGames metadata."""
|
|
raw_metadata = {
|
|
"genres": ["Action", "Adventure"],
|
|
"moby_score": "90",
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"moby_id": str(MOCK_MOBY_ID),
|
|
"raw_moby_metadata": json.dumps(raw_metadata),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["moby_metadata"] is not None
|
|
assert body["moby_metadata"]["moby_score"] == "90"
|
|
assert body["moby_metadata"]["genres"] == ["Action", "Adventure"]
|
|
|
|
@patch.object(
|
|
SSHandler,
|
|
"get_rom_by_id",
|
|
return_value=SSRom(ss_id=MOCK_SS_ID),
|
|
)
|
|
def test_update_raw_ss_metadata(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating raw ScreenScraper metadata."""
|
|
raw_metadata = {
|
|
"ss_score": "85",
|
|
"alternative_names": ["Test SS Game"],
|
|
"player_count": "1-4",
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"ss_id": str(MOCK_SS_ID),
|
|
"raw_ss_metadata": json.dumps(raw_metadata),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["ss_metadata"] is not None
|
|
assert body["ss_metadata"]["ss_score"] == "85"
|
|
assert body["ss_metadata"]["alternative_names"] == ["Test SS Game"]
|
|
assert body["ss_metadata"]["player_count"] == "1-4"
|
|
|
|
@patch.object(
|
|
LaunchboxHandler,
|
|
"get_rom_by_id",
|
|
return_value=LaunchboxRom(launchbox_id=MOCK_LAUNCHBOX_ID),
|
|
)
|
|
def test_update_raw_launchbox_metadata(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating raw LaunchBox metadata."""
|
|
raw_metadata = {
|
|
"first_release_date": "1675814400",
|
|
"max_players": 4,
|
|
"release_type": "Single",
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"launchbox_id": str(MOCK_LAUNCHBOX_ID),
|
|
"raw_launchbox_metadata": json.dumps(raw_metadata),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["launchbox_metadata"] is not None
|
|
assert body["launchbox_metadata"]["first_release_date"] == 1675814400
|
|
assert body["launchbox_metadata"]["max_players"] == 4
|
|
assert body["launchbox_metadata"]["release_type"] == "Single"
|
|
|
|
def test_update_raw_hasheous_metadata(
|
|
self, client: TestClient, access_token: str, rom: Rom
|
|
):
|
|
"""Test updating raw Hasheous metadata."""
|
|
raw_metadata = {
|
|
"tosec_match": True,
|
|
"mame_arcade_match": False,
|
|
"mame_mess_match": True,
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"hasheous_id": str(MOCK_HASHEOUS_ID),
|
|
"raw_hasheous_metadata": json.dumps(raw_metadata),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["hasheous_metadata"] is not None
|
|
assert body["hasheous_metadata"]["tosec_match"] is True
|
|
assert body["hasheous_metadata"]["mame_arcade_match"] is False
|
|
assert body["hasheous_metadata"]["mame_mess_match"] is True
|
|
|
|
@patch.object(
|
|
FlashpointHandler,
|
|
"get_rom_by_id",
|
|
return_value=FlashpointRom(flashpoint_id=str(MOCK_FLASHPOINT_ID)),
|
|
)
|
|
def test_update_raw_flashpoint_metadata(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating raw Flashpoint metadata."""
|
|
raw_metadata = {
|
|
"franchises": ["Metroid"],
|
|
"companies": ["Nintendo"],
|
|
"source": "Flashpoint",
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"flashpoint_id": str(MOCK_FLASHPOINT_ID),
|
|
"raw_flashpoint_metadata": json.dumps(raw_metadata),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["flashpoint_metadata"] is not None
|
|
assert body["flashpoint_metadata"]["franchises"] == ["Metroid"]
|
|
assert body["flashpoint_metadata"]["companies"] == ["Nintendo"]
|
|
assert body["flashpoint_metadata"]["source"] == "Flashpoint"
|
|
|
|
@patch.object(
|
|
HLTBHandler, "get_rom_by_id", return_value=HLTBRom(hltb_id=MOCK_HLTB_ID)
|
|
)
|
|
def test_update_raw_hltb_metadata(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating raw HowLongToBeat metadata."""
|
|
raw_metadata = {
|
|
"main_story": 10000,
|
|
"main_story_count": 1,
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"hltb_id": str(MOCK_HLTB_ID),
|
|
"raw_hltb_metadata": json.dumps(raw_metadata),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["hltb_metadata"] is not None
|
|
assert body["hltb_metadata"]["main_story"] == 10000
|
|
assert body["hltb_metadata"]["main_story_count"] == 1
|
|
|
|
@patch.object(
|
|
SteamHandler, "get_rom_by_id", return_value=SteamRom(steam_id=MOCK_STEAM_ID)
|
|
)
|
|
def test_update_raw_steam_metadata(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating raw Steam metadata."""
|
|
raw_metadata = {
|
|
"total_rating": "91",
|
|
"genres": ["Action"],
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"steam_id": str(MOCK_STEAM_ID),
|
|
"raw_steam_metadata": json.dumps(raw_metadata),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["steam_metadata"] is not None
|
|
assert body["steam_metadata"]["total_rating"] == "91"
|
|
assert body["steam_metadata"]["genres"] == ["Action"]
|
|
|
|
# Tests for combined updates
|
|
@patch.object(
|
|
IGDBHandler, "get_rom_by_id", return_value=IGDBRom(igdb_id=MOCK_IGDB_ID)
|
|
)
|
|
def test_update_rom_metadata_id_and_raw_metadata(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating both metadata ID and raw metadata in the same request."""
|
|
raw_igdb_metadata = {
|
|
"genres": ["Action"],
|
|
"franchises": ["Metroid"],
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"igdb_id": str(MOCK_IGDB_ID),
|
|
"raw_igdb_metadata": json.dumps(raw_igdb_metadata),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert get_rom_by_id_mock.called
|
|
|
|
body = response.json()
|
|
assert body["igdb_id"] == MOCK_IGDB_ID
|
|
assert body["igdb_metadata"] is not None
|
|
assert body["igdb_metadata"]["genres"] == ["Action"]
|
|
assert body["igdb_metadata"]["franchises"] == ["Metroid"]
|
|
|
|
@patch.object(
|
|
IGDBHandler, "get_rom_by_id", return_value=IGDBRom(igdb_id=MOCK_IGDB_ID)
|
|
)
|
|
@patch.object(
|
|
MobyGamesHandler,
|
|
"get_rom_by_id",
|
|
return_value=MobyGamesRom(moby_id=MOCK_MOBY_ID),
|
|
)
|
|
@patch.object(SSHandler, "get_rom_by_id", return_value=SSRom(ss_id=MOCK_SS_ID))
|
|
def test_update_rom_multiple_metadata_ids(
|
|
self,
|
|
igdb_get_rom_by_id_mock: AsyncMock,
|
|
moby_get_rom_by_id_mock: AsyncMock,
|
|
ss_get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating multiple metadata IDs in the same request."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"igdb_id": str(MOCK_IGDB_ID),
|
|
"moby_id": str(MOCK_MOBY_ID),
|
|
"ss_id": str(MOCK_SS_ID),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert igdb_get_rom_by_id_mock.called
|
|
assert moby_get_rom_by_id_mock.called
|
|
assert ss_get_rom_by_id_mock.called
|
|
|
|
body = response.json()
|
|
assert body["igdb_id"] == MOCK_IGDB_ID
|
|
assert body["moby_id"] == MOCK_MOBY_ID
|
|
assert body["ss_id"] == MOCK_SS_ID
|
|
|
|
@patch.object(
|
|
IGDBHandler, "get_rom_by_id", return_value=IGDBRom(igdb_id=MOCK_IGDB_ID)
|
|
)
|
|
@patch.object(
|
|
MobyGamesHandler,
|
|
"get_rom_by_id",
|
|
return_value=MobyGamesRom(moby_id=MOCK_MOBY_ID),
|
|
)
|
|
@patch.object(SSHandler, "get_rom_by_id", return_value=SSRom(ss_id=MOCK_SS_ID))
|
|
def test_update_rom_multiple_raw_metadata(
|
|
self,
|
|
igdb_get_rom_by_id_mock: AsyncMock,
|
|
moby_get_rom_by_id_mock: AsyncMock,
|
|
ss_get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test updating multiple raw metadata fields in the same request."""
|
|
raw_igdb = {
|
|
"genres": ["Action"],
|
|
"franchises": ["Metroid"],
|
|
}
|
|
raw_moby = {
|
|
"genres": ["Action", "Adventure"],
|
|
"moby_score": "90",
|
|
}
|
|
raw_ss = {
|
|
"ss_score": "85",
|
|
"alternative_names": ["Test SS Game"],
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"igdb_id": str(MOCK_IGDB_ID),
|
|
"moby_id": str(MOCK_MOBY_ID),
|
|
"ss_id": str(MOCK_SS_ID),
|
|
"raw_igdb_metadata": json.dumps(raw_igdb),
|
|
"raw_moby_metadata": json.dumps(raw_moby),
|
|
"raw_ss_metadata": json.dumps(raw_ss),
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert igdb_get_rom_by_id_mock.called
|
|
assert moby_get_rom_by_id_mock.called
|
|
assert ss_get_rom_by_id_mock.called
|
|
|
|
body = response.json()
|
|
assert body["igdb_metadata"] is not None
|
|
assert body["igdb_metadata"]["genres"] == ["Action"]
|
|
assert body["igdb_metadata"]["franchises"] == ["Metroid"]
|
|
|
|
assert body["moby_metadata"] is not None
|
|
assert body["moby_metadata"]["genres"] == ["Action", "Adventure"]
|
|
assert body["moby_metadata"]["moby_score"] == "90"
|
|
|
|
assert body["ss_metadata"] is not None
|
|
assert body["ss_metadata"]["ss_score"] == "85"
|
|
assert body["ss_metadata"]["alternative_names"] == ["Test SS Game"]
|
|
|
|
# Tests for invalid JSON handling
|
|
def test_update_rom_invalid_json_raw_metadata(
|
|
self,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test that invalid JSON in raw metadata is handled gracefully."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"raw_igdb_metadata": "invalid json {["},
|
|
)
|
|
# Should still succeed, but raw metadata should not be updated
|
|
assert response.status_code == status.HTTP_200_OK
|
|
# The invalid JSON should be ignored, so igdb_metadata should remain unchanged
|
|
body = response.json()
|
|
assert body["igdb_metadata"] == {}
|
|
|
|
def test_update_rom_empty_raw_metadata(
|
|
self, client: TestClient, access_token: str, rom: Rom
|
|
):
|
|
"""Test that empty raw metadata is handled correctly."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={"raw_igdb_metadata": ""},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
# Empty string should be ignored, so igdb_metadata should remain unchanged
|
|
body = response.json()
|
|
assert body["igdb_metadata"] == {}
|
|
|
|
|
|
class TestUnmatchMetadata:
|
|
@patch.object(
|
|
IGDBHandler, "get_rom_by_id", return_value=IGDBRom(igdb_id=MOCK_IGDB_ID)
|
|
)
|
|
def test_update_rom_unmatch_metadata(
|
|
self,
|
|
get_rom_by_id_mock: AsyncMock,
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
):
|
|
"""Test the unmatch_metadata functionality that clears all metadata."""
|
|
# Verify the ROM has existing metadata
|
|
initial_response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
data={
|
|
"igdb_id": str(MOCK_IGDB_ID),
|
|
"name_sort_key": "Imported sort title",
|
|
},
|
|
)
|
|
assert initial_response.status_code == status.HTTP_200_OK
|
|
assert get_rom_by_id_mock.called
|
|
|
|
initial_body = initial_response.json()
|
|
assert initial_body["igdb_id"] == MOCK_IGDB_ID
|
|
assert initial_body["igdb_metadata"] is not None
|
|
assert initial_body["name_sort_key"] == compute_name_sort_key(
|
|
"Imported sort title"
|
|
)
|
|
|
|
# Now unmatch all metadata
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={"unmatch_metadata": True},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
|
|
assert body["igdb_id"] is None
|
|
assert body["moby_id"] is None
|
|
assert body["ss_id"] is None
|
|
assert body["ra_id"] is None
|
|
assert body["launchbox_id"] is None
|
|
assert body["hasheous_id"] is None
|
|
assert body["tgdb_id"] is None
|
|
assert body["flashpoint_id"] is None
|
|
assert body["hltb_id"] is None
|
|
assert body["steam_id"] is None
|
|
|
|
assert body["name"] == rom.fs_name
|
|
assert body["name_sort_key"] == compute_name_sort_key(rom.fs_name)
|
|
assert body["summary"] == ""
|
|
assert body["url_cover"] == ""
|
|
assert body["slug"] == ""
|
|
|
|
assert body["igdb_metadata"] == {}
|
|
assert body["moby_metadata"] == {}
|
|
assert body["ss_metadata"] == {}
|
|
assert body["merged_ra_metadata"] == {} # Special case
|
|
assert body["launchbox_metadata"] == {}
|
|
assert body["hasheous_metadata"] == {}
|
|
assert body["flashpoint_metadata"] == {}
|
|
assert body["hltb_metadata"] == {}
|
|
assert body["steam_metadata"] == {}
|
|
|
|
def test_update_rom_unmatch_metadata_with_other_data(
|
|
self, client: TestClient, access_token: str, rom: Rom
|
|
):
|
|
"""Test that unmatch_metadata works even when other data is provided."""
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
params={"unmatch_metadata": True},
|
|
data={
|
|
"igdb_id": str(MOCK_IGDB_ID), # This should be ignored
|
|
"name": "Should be ignored", # This should be ignored
|
|
"summary": "Should be ignored", # This should be ignored
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
|
|
# All metadata should still be cleared despite other data being provided
|
|
assert body["igdb_id"] is None
|
|
assert body["name"] == rom.fs_name
|
|
assert body["summary"] == ""
|