Files
bincio-activity/tests/test_shared_images.py
T
Davide Scaini e61d05fc41 Refactor: extract shared image upload utilities into bincio/shared/images.py
ALLOWED_IMAGE_TYPES, MAX_IMAGE_BYTES, and unique_image_name() were
duplicated identically in both the edit and serve servers. Centralising
them means a single change point for any future extension (e.g. adding
image/avif support).

Tests added in tests/test_shared_images.py cover no-collision, single
and chained collisions, no-suffix filenames, and constant values.
2026-05-13 23:13:08 +02:00

54 lines
1.6 KiB
Python

"""Tests for bincio.shared.images — shared image upload utilities."""
from __future__ import annotations
from pathlib import Path
import pytest
from bincio.shared.images import ALLOWED_IMAGE_TYPES, MAX_IMAGE_BYTES, unique_image_name
def test_allowed_types_contains_expected():
assert "image/jpeg" in ALLOWED_IMAGE_TYPES
assert "image/png" in ALLOWED_IMAGE_TYPES
assert "image/webp" in ALLOWED_IMAGE_TYPES
assert "image/gif" in ALLOWED_IMAGE_TYPES
def test_allowed_types_excludes_unexpected():
assert "image/avif" not in ALLOWED_IMAGE_TYPES
assert "image/svg+xml" not in ALLOWED_IMAGE_TYPES
assert "application/octet-stream" not in ALLOWED_IMAGE_TYPES
def test_max_image_bytes():
assert MAX_IMAGE_BYTES == 10 * 1024 * 1024
def test_unique_name_no_collision(tmp_path: Path):
assert unique_image_name(tmp_path, "photo.jpg") == "photo.jpg"
def test_unique_name_single_collision(tmp_path: Path):
(tmp_path / "photo.jpg").touch()
assert unique_image_name(tmp_path, "photo.jpg") == "photo_1.jpg"
def test_unique_name_multiple_collisions(tmp_path: Path):
(tmp_path / "photo.jpg").touch()
(tmp_path / "photo_1.jpg").touch()
assert unique_image_name(tmp_path, "photo.jpg") == "photo_2.jpg"
def test_unique_name_no_suffix(tmp_path: Path):
(tmp_path / "photo").touch()
assert unique_image_name(tmp_path, "photo") == "photo_1"
def test_unique_name_preserves_extension_case(tmp_path: Path):
assert unique_image_name(tmp_path, "MyPhoto.PNG") == "MyPhoto.PNG"
def test_unique_name_empty_directory_returns_original(tmp_path: Path):
assert unique_image_name(tmp_path, "img.webp") == "img.webp"