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