45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
from types import SimpleNamespace
|
|
|
|
from app import screenshot_storage
|
|
|
|
|
|
def test_new_screenshot_is_stored_only_as_server_file(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(screenshot_storage.settings, "screenshot_storage_dir", tmp_path)
|
|
|
|
fields = screenshot_storage.screenshot_fields(
|
|
b"image-content", "proof.png", "image/png", "answers"
|
|
)
|
|
|
|
assert fields["image_data"] is None
|
|
assert (tmp_path / fields["file_path"]).read_bytes() == b"image-content"
|
|
|
|
|
|
def test_missing_file_falls_back_to_database_copy(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(screenshot_storage.settings, "screenshot_storage_dir", tmp_path)
|
|
image = SimpleNamespace(
|
|
file_path="answers/missing.png",
|
|
image_data=b"database-backup",
|
|
content_type="image/png",
|
|
)
|
|
|
|
assert screenshot_storage.screenshot_bytes(image) == b"database-backup"
|
|
|
|
|
|
def test_file_write_failure_returns_clear_service_error(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(screenshot_storage.settings, "screenshot_storage_dir", tmp_path)
|
|
monkeypatch.setattr(
|
|
screenshot_storage,
|
|
"store_screenshot",
|
|
lambda *args, **kwargs: (_ for _ in ()).throw(PermissionError("read-only")),
|
|
)
|
|
|
|
try:
|
|
screenshot_storage.screenshot_fields(
|
|
b"image-content", "proof.png", "image/png", "answers"
|
|
)
|
|
except screenshot_storage.HTTPException as exc:
|
|
assert exc.status_code == 503
|
|
assert "图片存储目录不可用" in exc.detail
|
|
else:
|
|
raise AssertionError("expected screenshot storage error")
|