"""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' # One floating video per product: a second one is refused before any # copy, naming the video that holds the product (DTF-WEB-G). refused = operator.call('/operator/site-videos/png', {'upload_id': video, 'product': 'file', 'enabled': True}, expected=409) assert 'A sala às 3 da manhã' in refused['detail'], refused with db.connect() as c: assert not c.execute("SELECT 1 FROM dtf_local.site_videos WHERE id='png'").fetchone() 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()