github-actions[bot]
Automated Deploy: 3ea2037ab7d55d5b498f7e2ea0336818fc24f725
fe40b87
Raw History Blame Contribute Delete
6.23 kB
"""D1: presigned upload targets."""
import uuid
import httpx
from sqlalchemy import select
from app.core.database import get_session_factory
from app.integrations.storage import get_storage
from app.modules.uploads.models import Upload
from tests.factories import make_account
MB = 1024 * 1024
async def _post(client, headers, **body):
return await client.post("/v1/uploads", json=body, headers=headers)
async def _row(upload_id: str) -> Upload:
async with get_session_factory()() as db:
return await db.scalar(select(Upload).where(Upload.id == uuid.UUID(upload_id)))
async def test_video_upload_and_presigned_put(client):
player = await make_account()
response = await _post(client, player["headers"], kind="video", content_type="video/mp4",
size_bytes=12, duration_sec=3.87)
assert response.status_code == 201, response.text
body = response.json()
assert set(body) == {"upload_id", "url", "headers", "expires_at"}
assert body["headers"] == {"Content-Type": "video/mp4"}
row = await _row(body["upload_id"])
assert row.object_key == f"videos/{player['id']}/{body['upload_id']}.mp4"
assert row.kind == "video" and row.size_bytes == 12 and row.attached_at is None and row.owner_id == player["id"]
async with httpx.AsyncClient() as s3:
put = await s3.put(body["url"], content=b"0123456789ab", headers=body["headers"])
assert put.status_code == 200
info = await get_storage().head(row.object_key)
assert info.size_bytes == 12 and info.content_type == "video/mp4"
async def test_mov_and_thumbnail_keys(client):
player = await make_account()
mov = await _post(client, player["headers"], kind="video", content_type="video/quicktime",
size_bytes=MB, duration_sec=60)
assert mov.status_code == 201
assert (await _row(mov.json()["upload_id"])).object_key.endswith(".mov")
thumb = await _post(client, player["headers"], kind="thumbnail", content_type="image/jpeg", size_bytes=2000)
assert thumb.status_code == 201
upload_id = thumb.json()["upload_id"]
assert (await _row(upload_id)).object_key == f"thumbnails/{player['id']}/{upload_id}.jpg"
async def test_upload_validation(client):
player = await make_account()
h = player["headers"]
cases = [
({"kind": "video", "content_type": "video/webm", "size_bytes": 10, "duration_sec": 3}, "content_type"),
({"kind": "thumbnail", "content_type": "image/png", "size_bytes": 10}, "content_type"),
({"kind": "video", "content_type": "video/mp4", "size_bytes": 10}, "duration_sec"),
({"kind": "video", "content_type": "video/mp4", "size_bytes": 10, "duration_sec": 60.5}, "duration_sec"),
({"kind": "document", "content_type": "video/mp4", "size_bytes": 10}, "kind"),
({"kind": "thumbnail", "content_type": "image/jpeg", "size_bytes": 0}, "size_bytes"),
({"kind": "thumbnail", "content_type": "image/jpeg", "size_bytes": 5, "bytes": 5}, "bytes"),
]
for body, field in cases:
response = await _post(client, h, **body)
assert response.status_code == 400, body
error = response.json()["error"]
assert error["code"] == "VALIDATION" and field in error["fields"], (body, error)
async def test_upload_too_large(client):
player = await make_account()
for body in ({"kind": "video", "content_type": "video/mp4", "size_bytes": 250 * MB + 1, "duration_sec": 5},
{"kind": "thumbnail", "content_type": "image/jpeg", "size_bytes": 5 * MB + 1}):
response = await _post(client, player["headers"], **body)
assert response.status_code == 413
assert response.json()["error"]["code"] == "TOO_LARGE"
async def test_upload_requires_auth(client):
response = await client.post("/v1/uploads", json={"kind": "thumbnail", "content_type": "image/jpeg",
"size_bytes": 10})
assert response.status_code == 401
assert response.json()["error"]["code"] == "UNAUTHENTICATED"
async def test_audio_upload(client):
player = await make_account()
ok = await _post(client, player["headers"], kind="audio", content_type="audio/mp4", size_bytes=1000,
duration_sec=12)
assert ok.status_code == 201, ok.text
row = await _row(ok.json()["upload_id"])
assert row.kind == "audio" and row.object_key == f"audio/{player['id']}/{ok.json()['upload_id']}.m4a"
for content_type, ext in (("audio/aac", "aac"), ("audio/mpeg", "mp3")):
response = await _post(client, player["headers"], kind="audio", content_type=content_type, size_bytes=10,
duration_sec=1)
assert response.status_code == 201
assert (await _row(response.json()["upload_id"])).object_key.endswith(f".{ext}")
async def test_audio_upload_validation(client):
player = await make_account()
base = {"kind": "audio", "content_type": "audio/mp4", "size_bytes": 1000, "duration_sec": 12}
for override, status in [({"content_type": "video/mp4"}, 400), ({"duration_sec": None}, 400),
({"duration_sec": 121}, 400), ({"size_bytes": 5 * MB + 1}, 413)]:
response = await _post(client, player["headers"], **{**base, **override})
assert response.status_code == status, (override, response.text)
async def test_drill_video_upload_requires_library_permission(client):
from tests.factories import make_team_row
coach = await make_account("coach")
await make_team_row(coach)
base = {"kind": "drill_video", "content_type": "video/quicktime", "size_bytes": MB, "duration_sec": 280}
ok = await _post(client, coach["headers"], **base)
assert ok.status_code == 201, ok.text
assert (await _row(ok.json()["upload_id"])).object_key == (
f"drill-videos/{coach['id']}/{ok.json()['upload_id']}.mov")
assert (await _post(client, coach["headers"], **{**base, "duration_sec": 301})).status_code == 400
assert (await _post(client, coach["headers"], **{**base, "content_type": "audio/mp4"})).status_code == 400
player = await make_account()
assert (await _post(client, player["headers"], **base)).status_code == 403