Files
romm/backend/tests/endpoints/test_screenshots.py
zurdi a8d2fcb605 feat: implement RDropzone component for file uploads with drag-and-drop support
- Added RDropzone component for handling file uploads with a customizable interface.
- Integrated RDropzone into Patcher and Upload views, replacing previous drop zone implementations.
- Enhanced ScreenshotsTab with additional functionality for community screenshots, including visibility toggles and owner display.
- Updated styles for improved user experience and responsiveness.
- Created Storybook stories for RDropzone to demonstrate its usage and interaction.
2026-06-18 11:05:52 +00:00

228 lines
6.3 KiB
Python

from io import BytesIO
from unittest import mock
from fastapi import status
from handler.database import db_screenshot_handler
from models.assets import Screenshot
from models.platform import Platform
from models.rom import Rom
from models.user import User
def _auth(token: str) -> dict[str, str]:
return {"Authorization": f"Bearer {token}"}
# ---------- POST /api/screenshots ----------
@mock.patch(
"endpoints.screenshots.fs_asset_handler.write_file", new_callable=mock.AsyncMock
)
@mock.patch("endpoints.screenshots.scan_screenshot", new_callable=mock.AsyncMock)
def test_upload_gallery_screenshot_sets_flags(
mock_scan,
_mock_write,
client,
access_token: str,
rom: Rom,
platform: Platform,
admin_user: User,
):
mock_scan.return_value = Screenshot(
file_name="shot1.png",
file_name_no_tags="shot1",
file_name_no_ext="shot1",
file_extension="png",
file_path=f"{platform.slug}/screenshots",
file_size_bytes=100,
)
response = client.post(
f"/api/screenshots?rom_id={rom.id}",
files={"screenshotFile": ("shot1.png", BytesIO(b"img"), "image/png")},
headers=_auth(access_token),
)
assert response.status_code == status.HTTP_200_OK
data = response.json()
assert data["is_gallery"] is True
assert data["is_public"] is False
@mock.patch(
"endpoints.screenshots.fs_asset_handler.write_file", new_callable=mock.AsyncMock
)
@mock.patch("endpoints.screenshots.scan_screenshot", new_callable=mock.AsyncMock)
def test_upload_rejects_invalid_extension(
_mock_scan,
_mock_write,
client,
access_token: str,
rom: Rom,
):
response = client.post(
f"/api/screenshots?rom_id={rom.id}",
files={"screenshotFile": ("notes.txt", BytesIO(b"nope"), "text/plain")},
headers=_auth(access_token),
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "Unsupported image file type" in response.json()["detail"]
# ---------- PUT /api/screenshots/{id} (visibility) ----------
def test_update_visibility_owner(client, access_token: str, screenshot: Screenshot):
response = client.put(
f"/api/screenshots/{screenshot.id}",
json={"is_public": True},
headers=_auth(access_token),
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["is_public"] is True
refreshed = db_screenshot_handler.get_screenshot_by_id(screenshot.id)
assert refreshed is not None and refreshed.is_public is True
def test_update_visibility_other_user_returns_404(
client,
access_token: str,
rom: Rom,
platform: Platform,
editor_user: User,
):
others = db_screenshot_handler.add_screenshot(
Screenshot(
rom_id=rom.id,
user_id=editor_user.id,
file_name="other.png",
file_name_no_tags="other",
file_name_no_ext="other",
file_extension="png",
file_path=f"{platform.slug}/screenshots",
file_size_bytes=10,
)
)
response = client.put(
f"/api/screenshots/{others.id}",
json={"is_public": True},
headers=_auth(access_token),
)
assert response.status_code == status.HTTP_404_NOT_FOUND
# ---------- DELETE /api/screenshots/{id} ----------
@mock.patch(
"endpoints.screenshots.fs_asset_handler.remove_file", new_callable=mock.AsyncMock
)
def test_delete_screenshot_owner(
_mock_remove,
client,
access_token: str,
screenshot: Screenshot,
):
response = client.delete(
f"/api/screenshots/{screenshot.id}",
headers=_auth(access_token),
)
assert response.status_code == status.HTTP_200_OK
assert db_screenshot_handler.get_screenshot_by_id(screenshot.id) is None
@mock.patch(
"endpoints.screenshots.fs_asset_handler.remove_file", new_callable=mock.AsyncMock
)
def test_delete_screenshot_other_user_returns_404(
_mock_remove,
client,
access_token: str,
rom: Rom,
platform: Platform,
editor_user: User,
):
others = db_screenshot_handler.add_screenshot(
Screenshot(
rom_id=rom.id,
user_id=editor_user.id,
file_name="other.png",
file_name_no_tags="other",
file_name_no_ext="other",
file_extension="png",
file_path=f"{platform.slug}/screenshots",
file_size_bytes=10,
)
)
response = client.delete(
f"/api/screenshots/{others.id}",
headers=_auth(access_token),
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert db_screenshot_handler.get_screenshot_by_id(others.id) is not None
# ---------- Gallery visibility query ----------
def _add_screenshot(rom, platform, user_id, name, *, is_gallery, is_public):
return db_screenshot_handler.add_screenshot(
Screenshot(
rom_id=rom.id,
user_id=user_id,
file_name=f"{name}.png",
file_name_no_tags=name,
file_name_no_ext=name,
file_extension="png",
file_path=f"{platform.slug}/screenshots",
file_size_bytes=10,
is_gallery=is_gallery,
is_public=is_public,
)
)
def test_gallery_query_excludes_thumbnails_and_others_private(
rom: Rom,
platform: Platform,
admin_user: User,
editor_user: User,
):
# Own save/state thumbnail — not a gallery upload.
_add_screenshot(
rom, platform, admin_user.id, "thumb", is_gallery=False, is_public=False
)
# Own private gallery screenshot — visible to self.
mine = _add_screenshot(
rom, platform, admin_user.id, "mine", is_gallery=True, is_public=False
)
# Another user's public gallery screenshot — visible (community).
others_public = _add_screenshot(
rom, platform, editor_user.id, "pub", is_gallery=True, is_public=True
)
# Another user's private gallery screenshot — hidden.
_add_screenshot(
rom, platform, editor_user.id, "priv", is_gallery=True, is_public=False
)
visible = {
s.id
for s in db_screenshot_handler.get_rom_gallery_screenshots(
rom_id=rom.id, user_id=admin_user.id
)
}
assert mine.id in visible
assert others_public.id in visible
assert len(visible) == 2