""" 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, monkeypatch): import app.config as config_module original_settings = config_module.get_settings() class SmallSettings: def __getattr__(self, name): val = getattr(original_settings, name) if name == "max_upload_bytes": return 10 return val monkeypatch.setattr(config_module, "get_settings", lambda: SmallSettings()) 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" @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