mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
Check screenshot names before the state or save is stored, move the update_rom fs_name check ahead of provider fetches, check the stamped state name on pull, skip the length check on a no-op rename, and share the sanitize-and-check block through sanitize_asset_filename. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
from io import BytesIO
|
|
from unittest import mock
|
|
|
|
import pytest
|
|
from fastapi import HTTPException, UploadFile
|
|
|
|
from utils import uploads
|
|
from utils.uploads import check_asset_upload_size, sanitize_asset_filename
|
|
|
|
|
|
def _upload(size: int | None) -> UploadFile:
|
|
file = UploadFile(filename="save.sav", file=BytesIO(b""))
|
|
file.size = size
|
|
return file
|
|
|
|
|
|
class TestCheckAssetUploadSize:
|
|
def test_accepts_none(self):
|
|
check_asset_upload_size(None, "Save file")
|
|
|
|
def test_accepts_file_within_limit(self):
|
|
with mock.patch.object(uploads, "MAX_ASSET_UPLOAD_SIZE_BYTES", 100):
|
|
check_asset_upload_size(_upload(100), "Save file")
|
|
|
|
def test_rejects_file_over_limit(self):
|
|
with mock.patch.object(uploads, "MAX_ASSET_UPLOAD_SIZE_BYTES", 100):
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
check_asset_upload_size(_upload(101), "Save file")
|
|
|
|
assert exc_info.value.status_code == 413
|
|
assert "Save file" in exc_info.value.detail
|
|
|
|
def test_disabled_when_limit_is_zero(self):
|
|
with mock.patch.object(uploads, "MAX_ASSET_UPLOAD_SIZE_BYTES", 0):
|
|
check_asset_upload_size(_upload(10**9), "Save file")
|
|
|
|
def test_skips_files_of_unknown_size(self):
|
|
with mock.patch.object(uploads, "MAX_ASSET_UPLOAD_SIZE_BYTES", 100):
|
|
check_asset_upload_size(_upload(None), "Save file")
|
|
|
|
|
|
class TestSanitizeAssetFilename:
|
|
def test_returns_the_sanitized_name(self):
|
|
assert sanitize_asset_filename("dir/a:b.sav", "save") == "a-b.sav"
|
|
|
|
@pytest.mark.parametrize("name", [" ", "あ" * 86 + ".sav"], ids=["empty", "long"])
|
|
def test_rejects_with_the_label(self, name: str):
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
sanitize_asset_filename(name, "save")
|
|
|
|
assert exc_info.value.status_code == 400
|
|
assert exc_info.value.detail.startswith("Invalid save filename")
|