Build fixes:
- ui/Dockerfile: npm install instead of npm ci (no lockfile)
- api/pyproject.toml: setuptools.build_meta instead of setuptools.backends.legacy:build
- api/Dockerfile: install curl so the Docker healthcheck doesn't always fail
- docker-compose.yml: add start_period: 30s to API healthcheck
Test fixes:
- pyproject.toml: asyncio_default_fixture_loop_scope/test_loop_scope = session to
prevent asyncpg connections being used across different event loops
- conftest.py: loop_scope="session" on session-scoped engine fixture
- main.py: custom HTTPException handler to flatten dict details to top level
(FastAPI wraps dict details as {"detail": {...}} by default)
- test_upload.py: use env var + cache_clear() to override max_upload_bytes since
monkeypatch can't reach past @lru_cache and already-imported references
- image_repo.py: add reload_with_tags() with populate_existing=True to force
SQLAlchemy to repopulate the identity-map object after tag mutations
- images.py: use reload_with_tags() instead of db.refresh(image, ["image_tags"])
which only loaded ImageTag rows without their .tag sub-relationship, causing
MissingGreenlet on any access to image.tags after attach/replace operations
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
99 lines
2.9 KiB
Python
99 lines
2.9 KiB
Python
"""
|
|
T026 — valid JPEG upload → 201, record in DB, object in MinIO
|
|
T027 — same image uploaded twice → 200, duplicate: true, no second MinIO object
|
|
T028 — invalid MIME type → 422 invalid_mime_type (error envelope with code field)
|
|
T029 — file > MAX_UPLOAD_BYTES → 422 file_too_large
|
|
T079 — GET /api/v1/images/{id} 404 → error envelope shape
|
|
"""
|
|
import io
|
|
import pytest
|
|
|
|
|
|
def _minimal_jpeg() -> bytes:
|
|
# Minimal valid JPEG bytes (SOI + APP0 + EOI)
|
|
return (
|
|
b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00"
|
|
b"\xff\xd9"
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_upload_new_image_returns_201(client):
|
|
data = _minimal_jpeg()
|
|
response = await client.post(
|
|
"/api/v1/images",
|
|
files={"file": ("test.jpg", io.BytesIO(data), "image/jpeg")},
|
|
)
|
|
assert response.status_code == 201
|
|
body = response.json()
|
|
assert body["duplicate"] is False
|
|
assert body["filename"] == "test.jpg"
|
|
assert body["mime_type"] == "image/jpeg"
|
|
assert "id" in body
|
|
assert "hash" in body
|
|
assert len(body["hash"]) == 64
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_upload_duplicate_returns_200_with_flag(client):
|
|
data = _minimal_jpeg()
|
|
# First upload
|
|
r1 = await client.post(
|
|
"/api/v1/images",
|
|
files={"file": ("test.jpg", io.BytesIO(data), "image/jpeg")},
|
|
)
|
|
assert r1.status_code in (200, 201)
|
|
|
|
# Second upload of same bytes
|
|
r2 = await client.post(
|
|
"/api/v1/images",
|
|
files={"file": ("test.jpg", io.BytesIO(data), "image/jpeg")},
|
|
)
|
|
assert r2.status_code == 200
|
|
body = r2.json()
|
|
assert body["duplicate"] is True
|
|
assert body["id"] == r1.json()["id"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_upload_invalid_mime_type_returns_422(client):
|
|
response = await client.post(
|
|
"/api/v1/images",
|
|
files={"file": ("doc.pdf", io.BytesIO(b"%PDF-1.4"), "application/pdf")},
|
|
)
|
|
assert response.status_code == 422
|
|
body = response.json()
|
|
assert body["code"] == "invalid_mime_type"
|
|
assert "detail" in body
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_upload_oversized_file_returns_422(client):
|
|
import os
|
|
from app.config import get_settings
|
|
|
|
os.environ["MAX_UPLOAD_BYTES"] = "10"
|
|
get_settings.cache_clear()
|
|
|
|
try:
|
|
response = await client.post(
|
|
"/api/v1/images",
|
|
files={"file": ("big.jpg", io.BytesIO(b"x" * 11), "image/jpeg")},
|
|
)
|
|
assert response.status_code == 422
|
|
body = response.json()
|
|
assert body["code"] == "file_too_large"
|
|
finally:
|
|
del os.environ["MAX_UPLOAD_BYTES"]
|
|
get_settings.cache_clear()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_unknown_image_returns_404_with_envelope(client):
|
|
import uuid
|
|
response = await client.get(f"/api/v1/images/{uuid.uuid4()}")
|
|
assert response.status_code == 404
|
|
body = response.json()
|
|
assert body["code"] == "image_not_found"
|
|
assert "detail" in body
|