from unittest.mock import AsyncMock, patch import pytest from fastapi import HTTPException from handler.metadata.csdb_handler import ( CsdbHandler, csdb_id_from_url, extract_csdb_id_from_filename, production_from_xml, ) WORKING_STONE_XML = """ 75330 Working Stone +2P C64 Crack 1993 https://csdb.dk/gfx/releases/75000/75330.png Fairlight Crack Bacchus """ 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("")["csdb_id"] is None assert production_from_xml("not xml")["csdb_id"] is None assert production_from_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 def test_production_from_xml_rejects_entity_declarations(): """defusedxml refuses entities with a ValueError, not a ParseError.""" xxe = """ ]> 1&xxe; """ assert production_from_xml(xxe)["csdb_id"] is None @pytest.mark.asyncio async def test_request_returns_empty_when_over_the_cap(): handler = CsdbHandler() with patch.object(CsdbHandler, "_fetch_capped", AsyncMock(return_value=None)): assert await handler._request("https://csdb.dk/webservice/?id=1") == "" @pytest.mark.asyncio async def test_get_rom_by_id_propagates_an_unreachable_csdb(): """A dead connection has to stay distinguishable from a missing release.""" handler = CsdbHandler() with ( patch.object(CsdbHandler, "is_enabled", return_value=True), patch.object( CsdbHandler, "_request", new_callable=AsyncMock, side_effect=HTTPException(status_code=503, detail="down"), ), pytest.raises(HTTPException), ): await handler.get_rom_by_id(75330)