feat(03-02): implement presigned upload flow, quota enforcement, cleanup task

- Replace POST /api/documents/upload with POST /api/documents/upload-url + /{id}/confirm
- upload-url: create pending Document row with user_id=None (Wave 2), return presigned PUT URL
- confirm: stat MinIO for authoritative size (T-03-05), atomic quota UPDATE (T-03-06, STORE-03)
- Confirm returns 413 with {used_bytes, limit_bytes, rejected_bytes} on quota exceeded (STORE-05)
- Wave 2 guard: skip quota UPDATE when doc.user_id is None (Plan 03-03 removes this)
- Add GET /api/auth/me/quota to api/auth.py (STORE-04)
- services/storage.py: remove save_upload (D-04); add GREATEST(0, used_bytes-delta) quota decrement to delete_document (STORE-06)
- tasks/document_tasks.py: add cleanup_abandoned_uploads Celery beat task (D-06)
- celery_app.py: add beat_schedule for cleanup-abandoned-uploads every 30 minutes
- tests/test_documents.py: replace legacy /upload tests with xfail; add real test logic for upload-url/confirm/get-quota
- tests/test_quota.py: implement real test logic with xfail for PostgreSQL-specific SQL
This commit is contained in:
curo1305
2026-05-23 14:32:12 +02:00
parent 3ed6dd494f
commit 0d51d023ce
7 changed files with 626 additions and 196 deletions
+146 -75
View File
@@ -11,7 +11,12 @@ import re
import pytest
@pytest.mark.xfail(strict=False, reason="POST /api/documents/upload removed in Plan 03-02 — replaced by upload-url + confirm flow")
async def test_upload_txt_no_classify(async_client, sample_txt):
"""Legacy multipart upload test — endpoint removed in Plan 03-02 (D-04).
Replaced by test_upload_url_endpoint + test_confirm_endpoint.
"""
with open(sample_txt, "rb") as f:
resp = await async_client.post(
"/api/documents/upload",
@@ -21,13 +26,11 @@ async def test_upload_txt_no_classify(async_client, sample_txt):
assert resp.status_code == 200
data = resp.json()
assert data["original_name"] == "sample.txt"
assert "extracted_text" in data
assert "invoices" in data["extracted_text"].lower() or len(data["extracted_text"]) > 0
assert data["topics"] == []
assert "id" in data
@pytest.mark.xfail(strict=False, reason="POST /api/documents/upload removed in Plan 03-02 — replaced by upload-url + confirm flow")
async def test_upload_pdf_no_classify(async_client, sample_pdf):
"""Legacy multipart upload test — endpoint removed in Plan 03-02 (D-04)."""
with open(sample_pdf, "rb") as f:
resp = await async_client.post(
"/api/documents/upload",
@@ -35,39 +38,39 @@ async def test_upload_pdf_no_classify(async_client, sample_pdf):
data={"auto_classify": "false"},
)
assert resp.status_code == 200
data = resp.json()
assert data["mime_type"] == "application/pdf"
assert len(data["extracted_text"]) > 0
async def test_list_documents(async_client, sample_txt):
with open(sample_txt, "rb") as f:
await async_client.post(
"/api/documents/upload",
files={"file": ("a.txt", f, "text/plain")},
data={"auto_classify": "false"},
)
async def test_list_documents(async_client):
"""GET /api/documents returns an empty list when no documents exist."""
resp = await async_client.get("/api/documents")
assert resp.status_code == 200
data = resp.json()
assert data["total"] == 1
assert len(data["items"]) == 1
assert data["total"] == 0
assert data["items"] == []
async def test_list_documents_filter_by_topic(async_client, db_session, sample_txt):
with open(sample_txt, "rb") as f:
upload = (
await async_client.post(
"/api/documents/upload",
files={"file": ("a.txt", f, "text/plain")},
data={"auto_classify": "false"},
)
).json()
# Wire a topic via the storage service directly (replaces old flat-file call)
async def test_list_documents_filter_by_topic(async_client, db_session):
"""GET /api/documents?topic=finance returns only matching documents."""
import uuid as _uuid
from db.models import Document
from services import storage
await storage.update_document_topics(db_session, upload["id"], ["finance"])
# Create a document directly via ORM (bypasses the upload endpoint)
doc_id = _uuid.uuid4()
doc = Document(
id=doc_id,
user_id=None,
filename="test.txt",
content_type="text/plain",
size_bytes=100,
storage_backend="minio",
status="uploaded",
object_key=f"null-user/{doc_id}/{_uuid.uuid4()}.txt",
)
db_session.add(doc)
await db_session.commit()
await storage.update_document_topics(db_session, str(doc_id), ["finance"])
resp = await async_client.get("/api/documents?topic=finance")
assert resp.json()["total"] == 1
@@ -76,19 +79,28 @@ async def test_list_documents_filter_by_topic(async_client, db_session, sample_t
assert resp2.json()["total"] == 0
async def test_get_document(async_client, sample_txt):
with open(sample_txt, "rb") as f:
upload = (
await async_client.post(
"/api/documents/upload",
files={"file": ("a.txt", f, "text/plain")},
data={"auto_classify": "false"},
)
).json()
async def test_get_document(async_client, db_session):
"""GET /api/documents/{id} returns metadata for an existing document."""
import uuid as _uuid
from db.models import Document
resp = await async_client.get(f"/api/documents/{upload['id']}")
doc_id = _uuid.uuid4()
doc = Document(
id=doc_id,
user_id=None,
filename="test.txt",
content_type="text/plain",
size_bytes=100,
storage_backend="minio",
status="uploaded",
object_key=f"null-user/{doc_id}/{_uuid.uuid4()}.txt",
)
db_session.add(doc)
await db_session.commit()
resp = await async_client.get(f"/api/documents/{doc_id}")
assert resp.status_code == 200
assert resp.json()["id"] == upload["id"]
assert resp.json()["id"] == str(doc_id)
async def test_get_document_not_found(async_client):
@@ -96,21 +108,34 @@ async def test_get_document_not_found(async_client):
assert resp.status_code == 404
async def test_delete_document(async_client, sample_txt):
with open(sample_txt, "rb") as f:
upload = (
await async_client.post(
"/api/documents/upload",
files={"file": ("a.txt", f, "text/plain")},
data={"auto_classify": "false"},
)
).json()
async def test_delete_document(async_client, db_session, monkeypatch):
"""DELETE /api/documents/{id} removes the document."""
import uuid as _uuid
from db.models import Document
from unittest.mock import AsyncMock
resp = await async_client.delete(f"/api/documents/{upload['id']}")
# Mock MinIO delete so we don't need a live MinIO
monkeypatch.setattr("services.storage._backend", lambda: type("B", (), {"delete_object": AsyncMock()})())
doc_id = _uuid.uuid4()
doc = Document(
id=doc_id,
user_id=None,
filename="test.txt",
content_type="text/plain",
size_bytes=0,
storage_backend="minio",
status="uploaded",
object_key=f"null-user/{doc_id}/{_uuid.uuid4()}.txt",
)
db_session.add(doc)
await db_session.commit()
resp = await async_client.delete(f"/api/documents/{doc_id}")
assert resp.status_code == 200
assert resp.json()["success"] is True
resp2 = await async_client.get(f"/api/documents/{upload['id']}")
resp2 = await async_client.get(f"/api/documents/{doc_id}")
assert resp2.status_code == 404
@@ -119,7 +144,9 @@ async def test_delete_document_not_found(async_client):
assert resp.status_code == 404
@pytest.mark.xfail(strict=False, reason="POST /api/documents/upload removed in Plan 03-02 — replaced by upload-url + confirm flow")
async def test_upload_empty_file(async_client):
"""Legacy empty file test — endpoint removed in Plan 03-02 (D-04)."""
resp = await async_client.post(
"/api/documents/upload",
files={"file": ("empty.txt", b"", "text/plain")},
@@ -128,8 +155,13 @@ async def test_upload_empty_file(async_client):
assert resp.status_code == 400
@pytest.mark.xfail(strict=False, reason="POST /api/documents/upload removed in Plan 03-02 — replaced by upload-url + confirm flow")
async def test_upload_persists_to_postgres_and_minio(async_client, sample_txt):
"""After a successful upload, document is persisted and queryable via GET (STORE-01, STORE-02)."""
"""Legacy upload+persist test — endpoint removed in Plan 03-02 (D-04).
Replaced by the upload-url + confirm flow tested in test_upload_url_endpoint
and test_confirm_endpoint.
"""
with open(sample_txt, "rb") as f:
resp = await async_client.post(
"/api/documents/upload",
@@ -137,21 +169,6 @@ async def test_upload_persists_to_postgres_and_minio(async_client, sample_txt):
data={"auto_classify": "false"},
)
assert resp.status_code == 200
data = resp.json()
# Response must include a UUID-format id
uuid_pattern = re.compile(
r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
)
assert "id" in data, "Upload response missing 'id'"
assert uuid_pattern.match(data["id"]), f"id '{data['id']}' is not a UUID"
# Metadata round-trips via GET
doc_id = data["id"]
get_resp = await async_client.get(f"/api/documents/{doc_id}")
assert get_resp.status_code == 200
get_data = get_resp.json()
assert get_data["original_name"] == "sample.txt"
# ---------------------------------------------------------------------------
@@ -159,7 +176,6 @@ async def test_upload_persists_to_postgres_and_minio(async_client, sample_txt):
# ---------------------------------------------------------------------------
@pytest.mark.xfail(strict=False, reason="implemented in plan 03-02")
async def test_upload_url_endpoint(async_client, auth_user, mock_minio_presigned):
"""POST /api/documents/upload-url returns {upload_url, document_id} and creates
a Document row with status='pending'.
@@ -167,28 +183,80 @@ async def test_upload_url_endpoint(async_client, auth_user, mock_minio_presigned
D-05: two-step upload flow — step 1 creates the pending Document row and
returns the presigned PUT URL (15-min TTL). Quota is NOT reserved here.
"""
assert True # scaffold
resp = await async_client.post(
"/api/documents/upload-url",
json={"filename": "report.pdf", "content_type": "application/pdf"},
headers=auth_user["headers"],
)
assert resp.status_code == 200, resp.text
data = resp.json()
assert "upload_url" in data, f"Missing upload_url: {data}"
assert "document_id" in data, f"Missing document_id: {data}"
assert "presigned" in data["upload_url"] or "localhost" in data["upload_url"], (
f"Expected a presigned URL: {data['upload_url']}"
)
# Verify mock was called
assert mock_minio_presigned.called, "generate_presigned_put_url was not called"
@pytest.mark.xfail(strict=False, reason="implemented in plan 03-02")
async def test_confirm_endpoint(async_client, auth_user, mock_minio_presigned, mock_minio_stat):
async def test_confirm_endpoint(
async_client, auth_user, mock_minio_presigned, mock_minio_stat, monkeypatch
):
"""POST /api/documents/{id}/confirm calls stat_object once, updates Document.size_bytes
from the stat return value, and sets Document.status='uploaded'.
D-05: step 3 of the presigned upload flow. stat_object provides the authoritative
file size (D-07). The atomic quota UPDATE runs here (STORE-03).
"""
assert True # scaffold
from unittest.mock import MagicMock
# Patch out the Celery delay call — no Redis in unit test environment
mock_delay = MagicMock()
monkeypatch.setattr("api.documents.extract_and_classify.delay", mock_delay)
mock_minio_stat.return_value = 2048
# Step 1: get upload URL
resp = await async_client.post(
"/api/documents/upload-url",
json={"filename": "doc.txt", "content_type": "text/plain"},
headers=auth_user["headers"],
)
assert resp.status_code == 200, resp.text
doc_id = resp.json()["document_id"]
# Step 2: confirm (Wave 2 — user_id is None so quota skipped, but stat is called)
conf_resp = await async_client.post(
f"/api/documents/{doc_id}/confirm",
headers=auth_user["headers"],
)
assert conf_resp.status_code == 200, conf_resp.text
conf_data = conf_resp.json()
assert conf_data["id"] == doc_id
assert conf_data["size_bytes"] == 2048
assert conf_data["status"] == "uploaded"
# stat_object was called once
assert mock_minio_stat.called, "stat_object was not called"
# Celery task was dispatched
assert mock_delay.called, "extract_and_classify.delay was not called"
@pytest.mark.xfail(strict=False, reason="implemented in plan 03-02")
async def test_get_quota(async_client, auth_user):
"""GET /api/auth/me/quota returns {used_bytes: 0, limit_bytes: 104857600}.
STORE-04: quota usage bar endpoint. Returns current usage and limit for the
authenticated user. Newly created users start at used_bytes=0.
"""
assert True # scaffold
resp = await async_client.get(
"/api/auth/me/quota",
headers=auth_user["headers"],
)
assert resp.status_code == 200, resp.text
data = resp.json()
assert "used_bytes" in data, f"Missing used_bytes: {data}"
assert "limit_bytes" in data, f"Missing limit_bytes: {data}"
assert data["used_bytes"] == 0, f"Expected 0 used_bytes for new user: {data}"
assert data["limit_bytes"] == 104_857_600, f"Expected 100 MB limit: {data}"
@pytest.mark.xfail(strict=False, reason="implemented in plan 03-03")
@@ -213,11 +281,14 @@ async def test_admin_cannot_access_documents(async_client, admin_user):
assert True # scaffold
@pytest.mark.xfail(strict=False, reason="implemented in plan 03-02")
@pytest.mark.xfail(strict=False, reason="implemented in plan 03-03: auth guard not yet added")
async def test_documents_require_auth(async_client):
"""Anonymous GET /api/documents (no Authorization header) returns 401 or 403.
D-16: all /api/documents/* endpoints require authentication via
get_current_user (Phase 2 D-07 fulfilled in Phase 3).
Note: auth guard is added in Plan 03-03 — this remains xfail until then.
"""
assert True # scaffold
resp = await async_client.get("/api/documents")
# Wave 2: no auth guard yet (Plan 03-03 adds it) — this will pass as xfail
assert resp.status_code in (401, 403), f"Expected 401 or 403, got {resp.status_code}"