File size: 6,232 Bytes
2756f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe40b87
2756f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe40b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""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