|
| 1 | +from fastapi import status |
| 2 | +from fastapi.testclient import TestClient |
| 3 | + |
| 4 | +from handler.database import db_rom_handler |
| 5 | +from models.platform import Platform |
| 6 | +from models.rom import Rom, RomFile, RomFileCategory |
| 7 | +from models.user import User |
| 8 | + |
| 9 | + |
| 10 | +def _auth(token: str) -> dict[str, str]: |
| 11 | + return {"Authorization": f"Bearer {token}"} |
| 12 | + |
| 13 | + |
| 14 | +def _add_file(rom: Rom, name: str, category: RomFileCategory | None) -> RomFile: |
| 15 | + return db_rom_handler.add_rom_file( |
| 16 | + RomFile( |
| 17 | + rom_id=rom.id, |
| 18 | + file_name=name, |
| 19 | + file_path=f"{rom.fs_path}/{rom.fs_name}", |
| 20 | + file_size_bytes=10, |
| 21 | + category=category, |
| 22 | + ) |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +def _make_rom(admin_user: User, platform: Platform) -> Rom: |
| 27 | + rom = db_rom_handler.add_rom( |
| 28 | + Rom( |
| 29 | + platform_id=platform.id, |
| 30 | + name="media_rom", |
| 31 | + slug="media_rom_slug", |
| 32 | + fs_name="media_rom", |
| 33 | + fs_name_no_tags="media_rom", |
| 34 | + fs_name_no_ext="media_rom", |
| 35 | + fs_extension="", |
| 36 | + fs_path=f"{platform.slug}/roms", |
| 37 | + ) |
| 38 | + ) |
| 39 | + db_rom_handler.add_rom_user(rom_id=rom.id, user_id=admin_user.id) |
| 40 | + return rom |
| 41 | + |
| 42 | + |
| 43 | +def test_image_file_served_inline( |
| 44 | + client: TestClient, access_token: str, admin_user: User, platform: Platform |
| 45 | +): |
| 46 | + rom = _make_rom(admin_user, platform) |
| 47 | + file = _add_file(rom, "trailer_thumb.png", RomFileCategory.GAME) |
| 48 | + |
| 49 | + r = client.get( |
| 50 | + f"/api/roms/{file.id}/files/content/trailer_thumb.png", |
| 51 | + headers=_auth(access_token), |
| 52 | + ) |
| 53 | + |
| 54 | + assert r.status_code == status.HTTP_200_OK |
| 55 | + assert r.headers["content-type"].startswith("image/png") |
| 56 | + assert r.headers["content-disposition"].startswith("inline") |
| 57 | + |
| 58 | + |
| 59 | +def test_video_file_served_inline( |
| 60 | + client: TestClient, access_token: str, admin_user: User, platform: Platform |
| 61 | +): |
| 62 | + rom = _make_rom(admin_user, platform) |
| 63 | + file = _add_file(rom, "trailer.mp4", RomFileCategory.GAME) |
| 64 | + |
| 65 | + r = client.get( |
| 66 | + f"/api/roms/{file.id}/files/content/trailer.mp4", |
| 67 | + headers=_auth(access_token), |
| 68 | + ) |
| 69 | + |
| 70 | + assert r.status_code == status.HTTP_200_OK |
| 71 | + assert r.headers["content-type"].startswith("video/mp4") |
| 72 | + assert r.headers["content-disposition"].startswith("inline") |
| 73 | + |
| 74 | + |
| 75 | +def test_rom_file_served_as_attachment( |
| 76 | + client: TestClient, access_token: str, admin_user: User, platform: Platform |
| 77 | +): |
| 78 | + rom = _make_rom(admin_user, platform) |
| 79 | + file = _add_file(rom, "game.bin", RomFileCategory.GAME) |
| 80 | + |
| 81 | + r = client.get( |
| 82 | + f"/api/roms/{file.id}/files/content/game.bin", |
| 83 | + headers=_auth(access_token), |
| 84 | + ) |
| 85 | + |
| 86 | + assert r.status_code == status.HTTP_200_OK |
| 87 | + assert r.headers["content-type"].startswith("application/octet-stream") |
| 88 | + assert r.headers["content-disposition"].startswith("attachment") |
| 89 | + |
| 90 | + |
| 91 | +def test_content_type_derived_from_db_not_path_param( |
| 92 | + client: TestClient, access_token: str, admin_user: User, platform: Platform |
| 93 | +): |
| 94 | + # A caller must not be able to force an inline image content-type on a |
| 95 | + # non-media file by tacking a fake extension onto the URL path param. |
| 96 | + rom = _make_rom(admin_user, platform) |
| 97 | + file = _add_file(rom, "game.bin", RomFileCategory.GAME) |
| 98 | + |
| 99 | + r = client.get( |
| 100 | + f"/api/roms/{file.id}/files/content/game.bin.png", |
| 101 | + headers=_auth(access_token), |
| 102 | + ) |
| 103 | + |
| 104 | + assert r.status_code == status.HTTP_200_OK |
| 105 | + assert r.headers["content-type"].startswith("application/octet-stream") |
| 106 | + assert r.headers["content-disposition"].startswith("attachment") |
0 commit comments