clipwright/tests/e2e/api/test_uploads.py
pedro-bento 026f3ff271
Some checks failed
CI / unit (pull_request) Failing after 24s
CI / backend-e2e (pull_request) Successful in 1m18s
CI / dashboard-e2e (pull_request) Successful in 1m44s
test: add full E2E coverage (API, pipeline, browser)
Three-level end-to-end suite, all deterministic (no retries, no sleeps, no
unmocked network):

- tests/e2e/api (209 tests): every route in clipwright/api/app.py +
  free_tools — health/config, uploads, process submit matrix + worker/webhook/
  auto-retry/resume, status/source/download-all, edit/effects, transcript/EDL/
  rerender, scenes/reframe, subtitle, hook, Postiz, thumbnails, free tools,
  and the security/path guards.
- tests/e2e/pipeline (136 tests): every stage with real ffmpeg and stubbed
  Gemini/yt-dlp/ASR/ML — download, transcribe, scene detect, selection,
  layout, reframe/render, subtitles, hooks, watermark, thumbnails, plus the
  real CLI full pipeline via stub_env.
- dashboard/e2e (19 Playwright specs): route-interception state machine
  covering every dashboard flow; one real-backend spec left test.fixme.
- Foundation: tests/e2e/conftest.py (state isolation, no-network guard, ffmpeg
  media factory, disk-seeded jobs), FakePopen worker harness, child-process
  stub_env, architecture-consistent fixtures.
- CI: unit + backend-e2e + dashboard-e2e jobs in the python+node container;
  ffmpeg + Playwright installed in-job; no setup-node/cache (act runner).

Verified: unit 790 passed; API E2E 209 passed; pipeline E2E 136 passed;
Playwright 19 passed / 1 skipped.
2026-10-04 21:58:28 +01:00

130 lines
4.9 KiB
Python

"""L1 API core: the two-step agent upload path (POST reserve, PUT bytes,
DELETE drop).
The PUT is capped at ``MAX_FILE_SIZE_MB`` (0 = unlimited) and probes the body
with ffprobe, so a non-video is refused with 400 rather than queued.
"""
import os
import pytest
pytestmark = pytest.mark.e2e
async def _reserve(client, filename=None):
body = {} if filename is None else {"filename": filename}
resp = await client.post("/api/uploads", json=body)
assert resp.status_code == 200, resp.text
return resp.json()
async def test_reserve_returns_slot_shape_and_sanitises_basename(client, app_state):
data = await _reserve(client, "../../etc/evil.mp4")
uid = data["upload_id"]
slot = app_state.app.pending_uploads[uid]
# basename() strips every path component the client tried to smuggle in.
assert slot["filename"] == "evil.mp4"
assert os.path.basename(slot["path"]).startswith(f"pending_{uid}_")
assert os.path.basename(slot["path"]).endswith("evil.mp4")
assert slot["complete"] is False and slot["bytes"] == 0
assert data["upload_url"].endswith(f"/api/uploads/{uid}")
assert data["method"] == "PUT"
assert data["max_mb"] == app_state.app.MAX_FILE_SIZE_MB
assert data["expires_in"] == app_state.app.UPLOAD_TTL_SECONDS
assert "PUT the raw video bytes" in data["hint"]
async def test_reserve_defaults_filename_when_body_absent(client, app_state):
resp = await client.post("/api/uploads")
assert resp.status_code == 200
uid = resp.json()["upload_id"]
assert app_state.app.pending_uploads[uid]["filename"] == "video.mp4"
async def test_put_unknown_id_is_404(client, app_state):
resp = await client.put("/api/uploads/does-not-exist", content=b"x")
assert resp.status_code == 404
assert "Unknown or expired" in resp.json()["detail"]
async def test_put_expired_slot_is_404_and_dropped(client, app_state):
uid = (await _reserve(client))["upload_id"]
slot = app_state.app.pending_uploads[uid]
slot["created"] -= app_state.app.UPLOAD_TTL_SECONDS + 10
resp = await client.put(f"/api/uploads/{uid}", content=b"anything")
assert resp.status_code == 404
assert uid not in app_state.app.pending_uploads
async def test_put_empty_body_is_400_and_removes_file(client, app_state):
uid = (await _reserve(client))["upload_id"]
path = app_state.app.pending_uploads[uid]["path"]
resp = await client.put(f"/api/uploads/{uid}", content=b"")
assert resp.status_code == 400
assert resp.json()["detail"] == "Empty body"
assert not os.path.exists(path)
assert app_state.app.pending_uploads[uid]["complete"] is False
async def test_put_unreadable_body_is_400_and_removes_file(client, app_state):
uid = (await _reserve(client))["upload_id"]
path = app_state.app.pending_uploads[uid]["path"]
resp = await client.put(f"/api/uploads/{uid}", content=b"this is not a video")
assert resp.status_code == 400
assert "not a readable video file" in resp.json()["detail"]
assert not os.path.exists(path)
assert app_state.app.pending_uploads[uid]["complete"] is False
async def test_put_oversized_body_is_413_and_removes_file(client, app_state, monkeypatch):
monkeypatch.setattr(app_state.app, "MAX_FILE_SIZE_MB", 1)
uid = (await _reserve(client))["upload_id"]
path = app_state.app.pending_uploads[uid]["path"]
oversized = b"x" * (1024 * 1024 + 1)
resp = await client.put(f"/api/uploads/{uid}", content=oversized)
assert resp.status_code == 413
assert "File too large" in resp.json()["detail"]
assert not os.path.exists(path)
async def test_second_put_replaces_the_first(client, app_state, media_factory):
uid = (await _reserve(client))["upload_id"]
path = app_state.app.pending_uploads[uid]["path"]
first = media_factory.six.read_bytes()
resp = await client.put(f"/api/uploads/{uid}", content=first)
assert resp.status_code == 200
assert resp.json()["bytes"] == len(first)
assert resp.json()["duration_seconds"] == pytest.approx(6.0, abs=1.0)
second = media_factory.silent.read_bytes()
resp = await client.put(f"/api/uploads/{uid}", content=second)
assert resp.status_code == 200
assert resp.json()["bytes"] == len(second)
# The slot now points at the replacement, not the first body.
assert open(path, "rb").read() == second
async def test_delete_removes_slot_and_file(client, app_state, media_factory):
uid = (await _reserve(client))["upload_id"]
path = app_state.app.pending_uploads[uid]["path"]
assert (await client.put(f"/api/uploads/{uid}",
content=media_factory.six.read_bytes())).status_code == 200
resp = await client.delete(f"/api/uploads/{uid}")
assert resp.status_code == 200
assert resp.json() == {"deleted": uid}
assert uid not in app_state.app.pending_uploads
assert not os.path.exists(path)
async def test_delete_unknown_id_is_404(client, app_state):
resp = await client.delete("/api/uploads/nope")
assert resp.status_code == 404