85 lines
2.8 KiB
Python
85 lines
2.8 KiB
Python
"""Phase 2 tests: Video upload endpoint.
|
|
|
|
Covers:
|
|
- POST /api/v1/video/upload with size validation (<300MB)
|
|
- Format validation (MP4 and common formats)
|
|
- Static file serving
|
|
- Error handling for oversized/invalid files
|
|
"""
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
@pytest.fixture
|
|
def video_client(tmp_path, monkeypatch):
|
|
upload_dir = tmp_path / "test_uploads"
|
|
upload_dir.mkdir()
|
|
monkeypatch.setenv("VIDEO_UPLOAD_DIR", str(upload_dir))
|
|
monkeypatch.setenv("MAX_VIDEO_SIZE_MB", "10")
|
|
|
|
from app.routers.video import router
|
|
from app.core.config import get_settings
|
|
|
|
get_settings.cache_clear()
|
|
app = FastAPI()
|
|
app.include_router(router, prefix="/api/v1")
|
|
return TestClient(app), upload_dir
|
|
|
|
|
|
class TestVideoUpload:
|
|
def test_upload_mp4_success(self, video_client):
|
|
client, upload_dir = video_client
|
|
content = b"\x00" * 1024 * 100 # 100KB dummy data
|
|
resp = client.post(
|
|
"/api/v1/video/upload",
|
|
files={"file": ("test.mp4", content, "video/mp4")},
|
|
)
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert "video_id" in data
|
|
assert data["filename"] == "test.mp4"
|
|
assert data["size_bytes"] == 102400
|
|
assert data["url"].startswith("/api/v1/video/")
|
|
assert len(list(upload_dir.glob("*"))) == 1
|
|
|
|
def test_upload_size_limit(self, video_client):
|
|
client, upload_dir = video_client
|
|
content = b"\x00" * (11 * 1024 * 1024) # 11MB, limit is 10MB
|
|
resp = client.post(
|
|
"/api/v1/video/upload",
|
|
files={"file": ("big.mp4", content, "video/mp4")},
|
|
)
|
|
assert resp.status_code == 413
|
|
assert len(list(upload_dir.glob("*"))) == 0
|
|
|
|
def test_upload_invalid_format(self, video_client):
|
|
client, upload_dir = video_client
|
|
content = b"hello world"
|
|
resp = client.post(
|
|
"/api/v1/video/upload",
|
|
files={"file": ("doc.txt", content, "text/plain")},
|
|
)
|
|
assert resp.status_code == 400
|
|
assert "Unsupported format" in resp.json()["detail"]
|
|
|
|
def test_static_file_serving(self, video_client):
|
|
client, upload_dir = video_client
|
|
content = b"\x00" * 512
|
|
resp = client.post(
|
|
"/api/v1/video/upload",
|
|
files={"file": ("serve_test.mp4", content, "video/mp4")},
|
|
)
|
|
assert resp.status_code == 200
|
|
video_id = resp.json()["video_id"]
|
|
|
|
resp = client.get(f"/api/v1/video/{video_id}")
|
|
assert resp.status_code == 200
|
|
assert resp.headers["content-type"] == "video/mp4"
|
|
assert resp.content == content
|
|
|
|
def test_unknown_video_returns_404(self, video_client):
|
|
client, _ = video_client
|
|
resp = client.get("/api/v1/video/nonexistent")
|
|
assert resp.status_code == 404
|