Files
dtf-system/tests/site_video_test.py
Cauê Faleiros 2f85a38562 feat: the videos panel in the Site tab, captions optional (front 8.4)
Dropstar uploads the six videos themselves, so the panel moves from
Integrações to the Site tab, next to products and prices. A video can be
published without a VTT: the Site plays it without a captions track.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-09 14:50:18 -03:00

90 lines
4.3 KiB
Python

"""Published site videos retain their file and expose same-origin captions.
Run against the local stack with an internal S3 public endpoint:
SITE_BASE_URL=http://site SITE_HOST_HEADER=localhost python -m tests.site_video_test
"""
from urllib.request import Request, urlopen
from app.core import db
from app.runtime import storage
from tests.smoke_test import BASE, Client, with_host, wait_scan
VIDEO = b'\x00\x00\x00\x18ftypisom\x00\x00\x00\x00isomiso2'
CAPTIONS = b'WEBVTT\n\n00:00:00.000 --> 00:00:01.000\nTeste de legenda.\n'
def upload(operator, name, content):
start = operator.call('/operator/site-videos/uploads', {'name': name, 'size': len(content)})
uid = start['id']
url = operator.call(f'/operator/uploads/{uid}/parts/1', {})['url']
with urlopen(Request(url, data=content, method='PUT'), timeout=30) as response:
assert response.status == 200
operator.call(f'/operator/uploads/{uid}/complete', {})
wait_scan(operator, uid, operator=True)
return uid
def run():
client = Client()
client.call('/session')
with db.connect() as c:
assert not c.execute("SELECT 1 FROM dtf_local.site_videos WHERE id='sala'").fetchone(), \
'The isolated test catalog entry is already configured'
ids = []
keys = []
try:
client.call('/operator/site-videos/sala', {'enabled': True}, expected=401)
operator = client.operator_client or Client()
# Client.call(operator=True) signs in and forwards to its own client.
client.call('/operator/site-videos', operator=True)
operator = client.operator_client
video = upload(operator, 'LOCAL-VIDEO.mp4', VIDEO)
captions = upload(operator, 'LOCAL-VIDEO.vtt', CAPTIONS)
ids = [video, captions]
operator.call('/operator/site-videos/sala', {'upload_id': video,
'captions_id': captions, 'product': 'file', 'enabled': True})
published = client.call('/site/videos')['videos']
entry = next(row for row in published if row['id'] == 'sala')
assert entry['product'] == 'file'
assert entry['captions'] == '/api/site/videos/sala/captions'
with urlopen(entry['src'], timeout=30) as response:
assert response.read() == VIDEO
with urlopen(Request(BASE + entry['captions'], headers=with_host()), timeout=30) as response:
assert response.headers['Content-Type'].startswith('text/vtt')
assert response.read() == CAPTIONS
operator.call('/operator/site-videos/sala', {'upload_id': video,
'captions_id': captions, 'product': 'file', 'enabled': False})
assert not any(row['id'] == 'sala' for row in client.call('/site/videos')['videos'])
# Captions are optional (front 8.4): the video alone is published. The
# published VTT copy stops being referenced, so keep it for cleanup.
with db.connect() as c:
old = c.execute("SELECT captions_key FROM dtf_local.site_videos WHERE id='sala'").fetchone()
if old and old['captions_key']:
keys.append(old['captions_key'])
operator.call('/operator/site-videos/sala', {'upload_id': video,
'product': 'file', 'enabled': True})
entry = next(row for row in client.call('/site/videos')['videos'] if row['id'] == 'sala')
assert entry['captions'] is None, entry
client.call('/site/videos/sala/captions', expected=404)
operator.call('/operator/site-videos/sala', {'upload_id': video,
'product': 'file', 'enabled': False})
assert not any(row['id'] == 'sala' for row in client.call('/site/videos')['videos'])
print('PASS: Gestão publishes a scanned video with signed media, with or without captions;'
' hiding removes it from the Site')
finally:
with db.connect() as c:
row = c.execute("DELETE FROM dtf_local.site_videos WHERE id='sala' RETURNING video_key,captions_key").fetchone()
if row:
keys.extend(key for key in row.values() if key)
if ids:
sources = c.execute('DELETE FROM dtf_local.uploads WHERE id=ANY(%s) RETURNING object_key',
(ids,)).fetchall()
keys.extend(row['object_key'] for row in sources)
for key in keys:
storage.client.delete_object(Bucket=storage.bucket, Key=key)
if __name__ == '__main__':
run()