|
| 1 | +from pathlib import Path |
| 2 | +from unittest.mock import AsyncMock |
| 3 | + |
| 4 | +import pytest |
1 | 5 | from fastapi import status |
2 | 6 | from fastapi.testclient import TestClient |
3 | 7 |
|
| 8 | +from endpoints.roms import files as files_endpoint |
4 | 9 | from handler.database import db_rom_handler |
5 | 10 | from models.platform import Platform |
6 | 11 | from models.rom import Rom, RomFile, RomFileCategory |
@@ -157,3 +162,144 @@ def test_content_type_derived_from_db_not_path_param( |
157 | 162 | assert r.status_code == status.HTTP_200_OK |
158 | 163 | assert r.headers["content-type"].startswith("application/octet-stream") |
159 | 164 | assert r.headers["content-disposition"].startswith("attachment") |
| 165 | + |
| 166 | + |
| 167 | +# ---------- DELETE /api/roms/{rom_id}/files/{file_id} ---------- |
| 168 | + |
| 169 | + |
| 170 | +@pytest.fixture |
| 171 | +def files_fs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: |
| 172 | + """Mock fs_rom_handler so file operations hit a temporary directory.""" |
| 173 | + library_dir = tmp_path / "library" |
| 174 | + library_dir.mkdir() |
| 175 | + |
| 176 | + def validate_path(path: str) -> Path: |
| 177 | + return library_dir / Path(path).name |
| 178 | + |
| 179 | + async def remove_file(path: str) -> None: |
| 180 | + target = library_dir / Path(path).name |
| 181 | + if target.exists(): |
| 182 | + target.unlink() |
| 183 | + else: |
| 184 | + raise FileNotFoundError(path) |
| 185 | + |
| 186 | + monkeypatch.setattr(files_endpoint.fs_rom_handler, "validate_path", validate_path) |
| 187 | + monkeypatch.setattr( |
| 188 | + files_endpoint.fs_rom_handler, |
| 189 | + "remove_file", |
| 190 | + AsyncMock(side_effect=remove_file), |
| 191 | + ) |
| 192 | + return library_dir |
| 193 | + |
| 194 | + |
| 195 | +def test_delete_rom_file_success( |
| 196 | + client: TestClient, |
| 197 | + access_token: str, |
| 198 | + admin_user: User, |
| 199 | + platform: Platform, |
| 200 | + files_fs: Path, |
| 201 | +): |
| 202 | + rom = _make_rom(admin_user, platform) |
| 203 | + (files_fs / "game.bin").write_bytes(b"\x00" * 16) |
| 204 | + rom_file = _add_file(rom, "game.bin", RomFileCategory.GAME) |
| 205 | + |
| 206 | + response = client.delete( |
| 207 | + f"/api/roms/{rom.id}/files/{rom_file.id}", |
| 208 | + headers=_auth(access_token), |
| 209 | + ) |
| 210 | + |
| 211 | + assert response.status_code == status.HTTP_200_OK |
| 212 | + assert db_rom_handler.get_rom_file_by_id(rom_file.id) is None |
| 213 | + assert not (files_fs / "game.bin").exists() |
| 214 | + |
| 215 | + |
| 216 | +def test_delete_rom_file_wrong_rom_returns_404( |
| 217 | + client: TestClient, |
| 218 | + access_token: str, |
| 219 | + admin_user: User, |
| 220 | + platform: Platform, |
| 221 | + files_fs: Path, |
| 222 | +): |
| 223 | + rom_a = _make_rom(admin_user, platform) |
| 224 | + # Use the game_folder_rom fixture name to avoid a duplicate fs_name constraint; |
| 225 | + # create a second ROM directly with a distinct slug and fs_name. |
| 226 | + rom_b = db_rom_handler.add_rom( |
| 227 | + Rom( |
| 228 | + platform_id=platform.id, |
| 229 | + name="other_rom", |
| 230 | + slug="other_rom_slug", |
| 231 | + fs_name="other_rom", |
| 232 | + fs_name_no_tags="other_rom", |
| 233 | + fs_name_no_ext="other_rom", |
| 234 | + fs_extension="", |
| 235 | + fs_path=f"{platform.slug}/roms", |
| 236 | + ) |
| 237 | + ) |
| 238 | + db_rom_handler.add_rom_user(rom_id=rom_b.id, user_id=admin_user.id) |
| 239 | + rom_file = _add_file(rom_a, "game.bin", RomFileCategory.GAME) |
| 240 | + |
| 241 | + response = client.delete( |
| 242 | + f"/api/roms/{rom_b.id}/files/{rom_file.id}", |
| 243 | + headers=_auth(access_token), |
| 244 | + ) |
| 245 | + |
| 246 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 247 | + # File must NOT have been deleted from the database. |
| 248 | + assert db_rom_handler.get_rom_file_by_id(rom_file.id) is not None |
| 249 | + |
| 250 | + |
| 251 | +def test_delete_rom_file_unknown_file_returns_404( |
| 252 | + client: TestClient, |
| 253 | + access_token: str, |
| 254 | + admin_user: User, |
| 255 | + platform: Platform, |
| 256 | + files_fs: Path, |
| 257 | +): |
| 258 | + rom = _make_rom(admin_user, platform) |
| 259 | + |
| 260 | + response = client.delete( |
| 261 | + f"/api/roms/{rom.id}/files/999999", |
| 262 | + headers=_auth(access_token), |
| 263 | + ) |
| 264 | + |
| 265 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 266 | + |
| 267 | + |
| 268 | +def test_delete_rom_file_tolerates_missing_disk_file( |
| 269 | + client: TestClient, |
| 270 | + access_token: str, |
| 271 | + admin_user: User, |
| 272 | + platform: Platform, |
| 273 | + files_fs: Path, |
| 274 | +): |
| 275 | + """DB row must be dropped even when the on-disk file is already gone.""" |
| 276 | + rom = _make_rom(admin_user, platform) |
| 277 | + rom_file = _add_file(rom, "missing.bin", RomFileCategory.GAME) |
| 278 | + |
| 279 | + response = client.delete( |
| 280 | + f"/api/roms/{rom.id}/files/{rom_file.id}", |
| 281 | + headers=_auth(access_token), |
| 282 | + ) |
| 283 | + |
| 284 | + assert response.status_code == status.HTTP_200_OK |
| 285 | + assert db_rom_handler.get_rom_file_by_id(rom_file.id) is None |
| 286 | + |
| 287 | + |
| 288 | +def test_delete_rom_file_forbidden_viewer( |
| 289 | + client: TestClient, |
| 290 | + viewer_access_token: str, |
| 291 | + admin_user: User, |
| 292 | + platform: Platform, |
| 293 | + files_fs: Path, |
| 294 | +): |
| 295 | + rom = _make_rom(admin_user, platform) |
| 296 | + rom_file = _add_file(rom, "game.bin", RomFileCategory.GAME) |
| 297 | + |
| 298 | + response = client.delete( |
| 299 | + f"/api/roms/{rom.id}/files/{rom_file.id}", |
| 300 | + headers=_auth(viewer_access_token), |
| 301 | + ) |
| 302 | + |
| 303 | + assert response.status_code == status.HTTP_403_FORBIDDEN |
| 304 | + # File must NOT have been deleted. |
| 305 | + assert db_rom_handler.get_rom_file_by_id(rom_file.id) is not None |
0 commit comments