"""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