Files
romm/backend/tests/handler/metadata/test_csdb_handler.py
Axel Quack 45bcb2bed3 feat(metadata): add Demozoo, Pouët, and CSDb handlers
Identify demoscene productions as ROMs via filename tags, Edit-ROM
id/URL paste, Demozoo ?title= + rank, unique Pouët search.php 302,
and CSDb XML by id (no HTML scrape). Scene ids lock name/summary/cover
so game catalogs cannot attach a similarly titled box.

CSDb XML is parsed with defusedxml and capped; screenshot and extra
links are stored only when they are http(s).

Fixes rommapp/romm#1796 (Phase 0–2).
2026-08-17 08:59:54 +01:00

100 lines
3.4 KiB
Python

from unittest.mock import AsyncMock, patch
import pytest
from handler.metadata.csdb_handler import (
CsdbHandler,
csdb_id_from_url,
extract_csdb_id_from_filename,
production_from_xml,
)
WORKING_STONE_XML = """<?xml version="1.0" encoding="UTF-8"?>
<CSDbData><Release>
<ID>75330</ID>
<Name>Working Stone +2P</Name>
<Type>C64 Crack</Type>
<ReleaseYear>1993</ReleaseYear>
<ScreenShot>https://csdb.dk/gfx/releases/75000/75330.png</ScreenShot>
<ReleasedBy><Group><Name>Fairlight</Name></Group></ReleasedBy>
<Credits><Credit><CreditType>Crack</CreditType>
<Handle><Handle>Bacchus</Handle></Handle></Credit></Credits>
</Release></CSDbData>
"""
def test_extract_csdb_id_from_filename():
assert extract_csdb_id_from_filename("Working Stone +2P (csdb-75330).zip") == 75330
assert extract_csdb_id_from_filename("Foo (demozoo-2)(CSDB-9).d64") == 9
assert extract_csdb_id_from_filename("untagged.zip") is None
def test_csdb_id_from_url():
assert csdb_id_from_url("https://csdb.dk/release/?id=75330") == 75330
assert csdb_id_from_url("https://csdb.dk/release/?id=75330&x=1") == 75330
assert csdb_id_from_url("http://www.csdb.dk/release/75330") == 75330
assert csdb_id_from_url("https://demozoo.org/productions/1/") is None
def test_production_from_xml_cover():
rom = production_from_xml(WORKING_STONE_XML)
assert rom["csdb_id"] == 75330
assert rom["name"] == "Working Stone +2P"
assert rom["url_cover"] == "https://csdb.dk/gfx/releases/75000/75330.png"
assert rom["url_screenshots"] == [rom["url_cover"]]
assert rom["csdb_metadata"]["groups"] == ["Fairlight"]
assert "https://csdb.dk/release/?id=75330" in (rom.get("summary") or "")
assert "Fairlight" in (rom.get("summary") or "")
def test_production_from_xml_empty():
assert production_from_xml("<CSDbData/>")["csdb_id"] is None
assert production_from_xml("not xml")["csdb_id"] is None
def test_production_from_xml_rejects_non_http_screenshot():
xml = WORKING_STONE_XML.replace(
"https://csdb.dk/gfx/releases/75000/75330.png",
"javascript:alert(1)",
)
rom = production_from_xml(xml)
assert rom["url_cover"] == ""
assert rom["url_screenshots"] == []
@pytest.mark.asyncio
async def test_get_rom_disabled():
handler = CsdbHandler()
with patch.object(CsdbHandler, "is_enabled", return_value=False):
result = await handler.get_rom("Working Stone +2P (csdb-75330).zip", "c64")
assert result["csdb_id"] is None
@pytest.mark.asyncio
async def test_get_rom_uses_filename_tag():
handler = CsdbHandler()
with (
patch.object(CsdbHandler, "is_enabled", return_value=True),
patch.object(
handler, "get_rom_by_id", new_callable=AsyncMock
) as mock_by_id,
):
mock_by_id.return_value = production_from_xml(WORKING_STONE_XML)
result = await handler.get_rom("Working Stone +2P (csdb-75330).zip", "c64")
mock_by_id.assert_awaited_once_with(75330)
assert result["csdb_id"] == 75330
@pytest.mark.asyncio
async def test_get_rom_no_tag_does_not_search():
handler = CsdbHandler()
with (
patch.object(CsdbHandler, "is_enabled", return_value=True),
patch.object(
handler, "get_rom_by_id", new_callable=AsyncMock
) as mock_by_id,
):
result = await handler.get_rom("Working Stone +2P (demozoo-396051).zip", "c64")
mock_by_id.assert_not_called()
assert result["csdb_id"] is None