- conftest.py: pytest_configure guard rejects non-postgresql+asyncpg:// URLs before any test collects (per constitution §2.5/§5.2 v1.3.0) - docker-compose.test.yml: isolated postgres-test (5433) + minio-test (9002) + api-test runner; one command runs the full suite against real PostgreSQL - Makefile: test-unit and test-integration targets - .env.test.example: documents variables needed to run tests outside Docker - Fix pre-existing test bug: integration tests using client fixture (NoOpAuthProvider) for write operations (upload/delete/patch) now use authed_client with Bearer token — these were never caught because tests never ran against a live stack Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
97 lines
3.1 KiB
Python
97 lines
3.1 KiB
Python
"""
|
|
T065 — DELETE /api/v1/images/{id} → 204; subsequent GET returns 404
|
|
T066 — DELETE verifies MinIO object is removed
|
|
T067 — DELETE of unknown ID → 404 image_not_found
|
|
"""
|
|
import io
|
|
import uuid
|
|
|
|
import pytest
|
|
from PIL import Image as PILImage
|
|
|
|
|
|
def _minimal_jpeg_v2() -> bytes:
|
|
# Slightly different from test_upload.py to avoid cross-test dedup
|
|
return (
|
|
b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x01"
|
|
b"\xff\xd9"
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_removes_record(authed_client):
|
|
client, token = authed_client
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
data = _minimal_jpeg_v2()
|
|
upload = await client.post(
|
|
"/api/v1/images",
|
|
files={"file": ("del-test.jpg", io.BytesIO(data), "image/jpeg")},
|
|
headers=headers,
|
|
)
|
|
image_id = upload.json()["id"]
|
|
|
|
delete_resp = await client.delete(f"/api/v1/images/{image_id}", headers=headers)
|
|
assert delete_resp.status_code == 204
|
|
|
|
get_resp = await client.get(f"/api/v1/images/{image_id}")
|
|
assert get_resp.status_code == 404
|
|
assert get_resp.json()["code"] == "image_not_found"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_removes_storage_object(authed_client):
|
|
client, token = authed_client
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
data = _minimal_jpeg_v2() + b"\x00"
|
|
upload = await client.post(
|
|
"/api/v1/images",
|
|
files={"file": ("del-storage-test.jpg", io.BytesIO(data), "image/jpeg")},
|
|
headers=headers,
|
|
)
|
|
assert upload.status_code in (200, 201)
|
|
image_id = upload.json()["id"]
|
|
|
|
delete_resp = await client.delete(f"/api/v1/images/{image_id}", headers=headers)
|
|
assert delete_resp.status_code == 204
|
|
|
|
# Confirm storage redirect no longer works (404 since record is gone)
|
|
file_resp = await client.get(f"/api/v1/images/{image_id}/file")
|
|
assert file_resp.status_code == 404
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_unknown_id_returns_404(authed_client):
|
|
client, token = authed_client
|
|
response = await client.delete(
|
|
f"/api/v1/images/{uuid.uuid4()}",
|
|
headers={"Authorization": f"Bearer {token}"},
|
|
)
|
|
assert response.status_code == 404
|
|
body = response.json()
|
|
assert body["code"] == "image_not_found"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_removes_thumbnail(authed_client):
|
|
client, token = authed_client
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
buf = io.BytesIO()
|
|
PILImage.new("RGB", (200, 150), color=(60, 90, 120)).save(buf, format="JPEG")
|
|
data = buf.getvalue()
|
|
|
|
upload = await client.post(
|
|
"/api/v1/images",
|
|
files={"file": ("thumb-del.jpg", io.BytesIO(data), "image/jpeg")},
|
|
headers=headers,
|
|
)
|
|
assert upload.status_code == 201
|
|
image_id = upload.json()["id"]
|
|
assert upload.json()["thumbnail_key"] is not None
|
|
|
|
delete_resp = await client.delete(f"/api/v1/images/{image_id}", headers=headers)
|
|
assert delete_resp.status_code == 204
|
|
|
|
thumb_resp = await client.get(f"/api/v1/images/{image_id}/thumbnail")
|
|
assert thumb_resp.status_code == 404
|
|
assert thumb_resp.json()["code"] == "image_not_found"
|