Files
romm/backend/tests/endpoints/roms/test_patch.py
Sam Dornan bd16648c9a chore(mypy): switch to strict mode with listed exceptions (#4817)
* chore(mypy): switch to strict mode with listed exceptions

Replace the hand-picked flags with `strict = True`, turning off only the
four flags that aren't clean yet (disallow_any_generics,
warn_return_any, disallow_untyped_calls, disallow_untyped_decorators).
Any other strict flag, including those future mypy releases add to the
preset, is on by default.

That turns on no_implicit_reexport, strict_equality and extra_checks:
- Import each name from the module that defines it. About 240 test
  sites patched a dependency through the module that imported it
  (`scan_module.fs_rom_handler`); they now import the same object from
  its source, so every patch targets what it did before. Tests that
  replace a module attribute are unchanged.
- handler.streaming.memory_cards re-exports store_memory_card_version
  explicitly, since endpoints call it there and tests patch it there.
- mutagen gets implicit_reexport, as it re-exports without __all__.
- Test comparisons build typed values (CoverResult, a typed expected
  dict, enum .value) and the CORS lookup casts for its identity check.
- One ss_handler ** spread keeps a coded ignore: extra_checks wants its
  NotRequired dump_* keys spelled out, and the media dict never has them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

* test(upload): assert cleanup against the patched upload directory

The 409 test checked config's ROM_UPLOAD_TMP_BASE while the fixture patches
the endpoint's copy to tmp_path/uploads, so it passed even if cleanup failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 15:45:37 -05:00

95 lines
2.7 KiB
Python

from pathlib import Path
import pytest
from anyio import Path as AnyioPath
from fastapi import status
from fastapi.testclient import TestClient
from endpoints.roms import patch as patch_endpoint
from handler.database import db_rom_handler
from handler.filesystem import fs_rom_handler
from models.rom import Rom, RomFile, RomFileCategory
from utils.rom_patcher import PatcherInputError
def _auth(token: str) -> dict[str, str]:
return {"Authorization": f"Bearer {token}"}
def _patch_file(rom: Rom) -> RomFile:
return db_rom_handler.add_rom_file(
RomFile(
rom_id=rom.id,
file_name="translation.bps",
file_path=rom.fs_path,
file_size_bytes=100,
category=RomFileCategory.PATCH,
)
)
def test_patch_rom_passes_archive_member_and_validation_header(
client: TestClient,
access_token: str,
rom: Rom,
rom_file: RomFile,
monkeypatch: pytest.MonkeyPatch,
):
patch_file = _patch_file(rom)
received_member: str | None = None
async def patch(
_rom_path: Path,
_patch_path: Path,
output_path: Path,
archive_member_name: str | None,
) -> bool:
nonlocal received_member
received_member = archive_member_name
await AnyioPath(output_path).write_bytes(b"patched zip")
return False
monkeypatch.setattr(fs_rom_handler, "validate_path", lambda path: Path(path))
monkeypatch.setattr(patch_endpoint, "apply_patch", patch)
response = client.post(
f"/api/roms/{rom_file.id}/patch",
headers=_auth(access_token),
data={
"patch_file_id": str(patch_file.id),
"archive_member_name": "roms/game.sfc",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.content == b"patched zip"
assert response.headers["x-patch-validated"] == "false"
assert received_member == "roms/game.sfc"
def test_patch_rom_returns_bad_request_for_invalid_archive(
client: TestClient,
access_token: str,
rom: Rom,
rom_file: RomFile,
monkeypatch: pytest.MonkeyPatch,
):
patch_file = _patch_file(rom)
async def reject_patch(*_args, **_kwargs) -> bool:
raise PatcherInputError("Select which file inside the ROM archive to patch")
monkeypatch.setattr(fs_rom_handler, "validate_path", lambda path: Path(path))
monkeypatch.setattr(patch_endpoint, "apply_patch", reject_patch)
response = client.post(
f"/api/roms/{rom_file.id}/patch",
headers=_auth(access_token),
data={"patch_file_id": str(patch_file.id)},
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json() == {
"detail": "Select which file inside the ROM archive to patch"
}