From 7204aee432546b3f298d15f3bd5accfc1a82cebf Mon Sep 17 00:00:00 2001 From: rbi-aap Date: Tue, 3 Sep 2024 15:26:59 +1000 Subject: [PATCH] block_media --- tests/test_block_content.py | 464 ++++++++++++++++++++++++++++ tests/test_block_media.py | 589 ++++++++++++++++++++++++++++++++++++ 2 files changed, 1053 insertions(+) create mode 100644 tests/test_block_content.py create mode 100644 tests/test_block_media.py diff --git a/tests/test_block_content.py b/tests/test_block_content.py new file mode 100644 index 00000000..b5cc74ea --- /dev/null +++ b/tests/test_block_content.py @@ -0,0 +1,464 @@ +import io +import json +import zipfile +from datetime import timedelta, datetime + +import re +import bson +import lxml.etree +from superdesk.utc import utcnow + +from .fixtures import items, init_items, init_auth, agenda_items, init_agenda_items # noqa +from .test_push import upload_binary +import pytest + +items_ids = [item['_id'] for item in items[:2]] +item = items[:2][0] + + +def download_zip_file(client, _format, section): + resp = client.get(f'/download/{",".join(items_ids)}?format={_format}&type={section}', follow_redirects=True) + assert resp.status_code == 200 + assert resp.mimetype == 'application/zip' + + content_disposition = resp.headers.get('Content-Disposition') + assert content_disposition is not None, "Content-Disposition header is missing" + + filename_match = re.search(r'filename=(\d{12})-newsroom\.zip', content_disposition) + assert filename_match, f"Filename in Content-Disposition does not match expected pattern: {content_disposition}" + + filename_timestamp = filename_match.group(1) + file_datetime = datetime.strptime(filename_timestamp, "%Y%m%d%H%M") + + now = datetime.utcnow() + assert now - timedelta( + minutes=5) <= file_datetime <= now, f"Filename timestamp {filename_timestamp} is not within the expected range" + + return io.BytesIO(resp.get_data()) + + +def text_content_test(content): + content = content.decode('utf-8').split('\n') + assert 'AMAZON-BOOKSTORE-OPENING' in content[0] + assert 'Amazon Is Opening More Bookstores' in content[1] + assert '

' not in content + assert 'Block 4' == content[-2] + + +def nitf_content_test(content): + root = lxml.etree.fromstring(content) + assert 'nitf' == root.tag + head = root.find('head') + assert items[0]['headline'] == head.find('title').text + + +def ninjs_content_test(content): + data = json.loads(content) + assert data.get('associations', {}).get('editor_1') + assert not data.get('associations', {}).get('editor_0') + assert not data.get('associations', {}).get('editor_2') + assert data['headline'] == 'Amazon Is Opening More Bookstores' + assert 'editor_1' in data['body_html'] + assert 'editor_0' not in data['body_html'] + + +def ninjs_block_download_example(content): + data = json.loads(content) + assert data.get('associations', {}).get('editor_1') + assert not data.get('associations', {}).get('editor_0') + assert not data.get('associations', {}).get('editor_2') + assert data['headline'] == 'Amazon Is Opening More Bookstores' + assert 'video' in data['body_html'] + assert 'img' not in data['body_html'] + assert 'blockquote' not in data['body_html'] + assert 'audio' not in data['body_html'] + + +def newsmlg2_content_test(content): + root = lxml.etree.fromstring(content) + assert 'newsMessage' in root.tag + + +def filename(name, item): + return f'{item["versioncreated"].strftime("%Y%m%d%H%M")}-{name}' + + +wire_formats = [ + { + 'format': 'text', + 'mimetype': 'text/plain', + 'filename': filename('amazon-bookstore-opening.txt', item), + 'test_content': text_content_test, + }, + { + 'format': 'nitf', + 'mimetype': 'application/xml', + 'filename': filename('amazon-bookstore-opening.xml', item), + 'test_content': nitf_content_test, + }, + { + 'format': 'newsmlg2', + 'mimetype': 'application/vnd.iptc.g2.newsitem+xml', + 'filename': filename('amazon-bookstore-opening.xml', item), + 'test_content': newsmlg2_content_test, + }, + { + 'format': 'picture', + 'mimetype': 'image/jpeg', + 'filename': 'baseimage.jpg', + }, +] + + +def setup_block_embeds(client, app): + media_id = bson.ObjectId() + associations = { + 'featuremedia': { + 'mimetype': 'image/jpeg', + 'type': 'picture', + 'renditions': { + 'baseImage': { + 'mimetype': 'image/jpeg', + 'media': str(media_id), + 'href': 'http://a.b.c/xxx.jpg', + }, + '16-9': { + 'mimetype': 'image/jpeg', + 'href': 'http://a.b.c/xxx.jpg', + 'media': str(media_id), + 'width': 1280, + 'height': 720, + }, + '4-3': { + "href": "/assets/633d11b9fb5122dcf06a6f02", + "width": 800, + "height": 600, + 'media': str(media_id), + "mimetype": "image/jpeg", + }, + }, + }, + "editor_1": { + "type": "video", + "renditions": { + "original": { + "mimetype": "video/mp4", + "href": "/assets/640ff0bdfb5122dcf06a6fc3", + 'media': str(media_id), + }, + }, + "mimetype": "video/mp4", + "products": [ + {"code": "123", "name": "Product A"}, + {"code": "321", "name": "Product B"}, + ], + }, + "editor_0": { + "type": "audio", + "renditions": { + "original": { + "mimetype": "audio/mp3", + "href": "/assets/640feb9bfb5122dcf06a6f7c", + "media": "640feb9bfb5122dcf06a6f7c", + }, + }, + "mimetype": "audio/mp3", + "products": [{"code": "999", "name": "NSW News"}], + }, + "editor_2": { + "type": "picture", + "renditions": { + "4-3": { + "href": "/assets/633d11b9fb5122dcf06a6f02", + "width": 800, + "height": 600, + "mimetype": "image/jpeg", + "media": "633d11b9fb5122dcf06a6f02", + }, + "16-9": { + "href": "/assets/633d0f59fb5122dcf06a6ee8", + "width": 1280, + "height": 720, + "mimetype": "image/jpeg", + "media": "633d0f59fb5122dcf06a6ee8", + "poi": {}, + }, + }, + "products": [{"code": "888"}], + }, + "editor_3": None, + } + upload_binary('picture.jpg', client, media_id=str(media_id)) + + app.data.update('items', item['_id'], { + 'associations': associations, + 'body_html': ( + '

Block 1

' + '' + '
' + '' + '
minns
' + '
' + '' + '


' + '

Block 2

' + '' + '
' + '' + '
Scomo whinging
' + '
' + '' + '


Block 3

' + '' + '
' + '' + '
Prime Minister Scott Morrison and Liberal member for Higgins Katie Allen
' + '
' + '' + '

Block 4

' + '
' + '' + '' + '
' + ) + }, item) + + +def test_download_single_block(client, app): + setup_block_embeds(client, app) + for _format in wire_formats: + resp = client.get(f'/download/{item["_id"]}?format={_format["format"]}', follow_redirects=True) + assert resp.status_code == 200 + assert resp.mimetype == _format['mimetype'] + assert (resp.headers.get('Content-Disposition') in + [f'attachment; filename={_format["filename"]}', f'attachment; filename="{_format["filename"]}"']) + + +@pytest.fixture +def setup_data(client, app): + setup_block_embeds(client, app) + app.config['EMBED_PRODUCT_FILTERING'] = True + app.data.insert('companies', [{ + '_id': '3', + 'name': 'Block Conent.', + 'is_enabled': True, + 'embedded': { + "social_media_display": True, + "sdpermit_display": True, + "video_display": False, + "audio_display": True, + "images_display": True, + "all_display": False, + "social_media_download": True, + "video_download": True, + "audio_download": False, + "images_download": True, + "all_download": False, + "sdpermit_download": True + } + }]) + user = app.data.find_one('users', req=None, first_name='admin') + assert user + app.data.update('users', user['_id'], {'company': '3'}, user) + app.data.insert('products', [{ + '_id': 13, + 'name': 'product test', + 'sd_product_id': '123', + 'companies': ['3'], + 'is_enabled': True, + 'product_type': 'wire' + }]) + + +def start_test_block_download(client, app, setup_data): + for _format in wire_formats: + _file = download_zip_file(client, _format['format'], 'wire') + with zipfile.ZipFile(_file) as zf: + assert _format['filename'] in zf.namelist() + content = zf.open(_format['filename']).read() + if _format.get('test_content'): + _format['test_content'](content) + + +def assert_history(app): + history = app.data.find('history', None, None) + assert (len(wire_formats) * len(items_ids)) == history.count() + assert 'download' == history[0]['action'] + assert history[0].get('user') + assert history[0].get('versioncreated') + timedelta(seconds=2) >= utcnow() + assert history[0].get('item') in items_ids + assert history[0].get('version') + assert history[0].get('company') == '3' + assert history[0].get('section') == 'wire' + + +def test_block_download_with_config(client, app, setup_data): + start_test_block_download(client, app, setup_data) + assert_history(app) + + +def test_ninjs_download(client, app): + setup_block_embeds(client, app) + app.config['EMBED_PRODUCT_FILTERING'] = True + app.data.insert('companies', [{ + '_id': '1', + 'name': 'Press co.', + 'is_enabled': True, + 'embedded': { + "social_media_display": True, + "video_display": True, + "audio_display": True, + "images_display": True, + "all_display": True, + "social_media_download": True, + "video_download": True, + "audio_download": True, + "images_download": True, + "all_download": True, + "sdpermit_display": True, + "sdpermit_download": True + } + }]) + user = app.data.find_one('users', req=None, first_name='admin') + assert user + app.data.update('users', user['_id'], {'company': '1'}, user) + app.data.insert('products', [{ + '_id': 10, + 'name': 'product test', + 'sd_product_id': '123', + 'companies': ['1'], + 'is_enabled': True, + 'product_type': 'wire' + }]) + app.general_setting('news_api_allowed_renditions', 'Foo', default='16-9,4-3') + + _file = download_zip_file(client, 'downloadninjs', 'wire') + with zipfile.ZipFile(_file) as zf: + assert filename('amazon-bookstore-opening.json', item) in zf.namelist() + content = zf.open(filename('amazon-bookstore-opening.json', item)).read() + ninjs_content_test(content) + + history = app.data.find('history', None, None) + assert 4 == history.count() + assert 'download' in history[0]['action'] + assert 'download' in history[1]['action'] + assert history[0].get('user') + assert history[0].get('versioncreated') + timedelta(seconds=2) >= utcnow() + assert history[0].get('item') in items_ids + assert history[0].get('version') + assert history[0].get('company') == '1' + assert history[0].get('section') == 'wire' + + +def test_ninjs_block_download_default(client, app): + setup_block_embeds(client, app) + app.config['EMBED_PRODUCT_FILTERING'] = True + app.data.insert('companies', [{ + '_id': '1', + 'name': 'Press co.', + 'is_enabled': True, + 'embedded': { + "social_media_display": True, + "video_display": True, + "audio_display": True, + "images_display": True, + "all_display": True, + "social_media_download": False, + "video_download": False, + "audio_download": False, + "images_download": False, + "all_download": False, + "sdpermit_display": True, + "sdpermit_download": False + } + }]) + user = app.data.find_one('users', req=None, first_name='admin') + assert user + app.data.update('users', user['_id'], {'company': '1'}, user) + app.data.insert('products', [{ + '_id': 10, + 'name': 'product test', + 'sd_product_id': '123', + 'companies': ['1'], + 'is_enabled': True, + 'product_type': 'wire' + }]) + app.general_setting('news_api_allowed_renditions', 'Foo', default='16-9,4-3') + _file = download_zip_file(client, 'downloadninjs', 'wire') + with zipfile.ZipFile(_file) as zf: + assert filename('amazon-bookstore-opening.json', item) in zf.namelist() + content = zf.open(filename('amazon-bookstore-opening.json', item)).read() + ninjs_content_test(content) + + history = app.data.find('history', None, None) + assert 4 == history.count() + assert 'download' in history[0]['action'] + assert 'download' in history[1]['action'] + assert history[0].get('user') + assert history[0].get('versioncreated') + timedelta(seconds=2) >= utcnow() + assert history[0].get('item') in items_ids + assert history[0].get('version') + assert history[0].get('company') == '1' + assert history[0].get('section') == 'wire' + + +def test_ninjs_block_download_example(client, app): + setup_block_embeds(client, app) + app.config['EMBED_PRODUCT_FILTERING'] = True + app.data.insert('companies', [{ + '_id': '2', + 'name': 'Press01 co.', + 'is_enabled': True, + 'embedded': { + "social_media_display": True, + "video_display": True, + "audio_display": True, + "images_display": True, + "all_display": True, + "social_media_download": False, + "video_download": True, + "audio_download": False, + "images_download": False, + "all_download": False, + "sdpermit_display": True, + "sdpermit_download": False + } + }]) + user = app.data.find_one('users', req=None, first_name='admin') + assert user + app.data.update('users', user['_id'], {'company': '2'}, user) + app.data.insert('products', [{ + '_id': 10, + 'name': 'product test', + 'sd_product_id': '123', + 'companies': ['2'], + 'is_enabled': True, + 'product_type': 'wire' + }]) + app.general_setting('news_api_allowed_renditions', 'Foo', default='16-9,4-3') + _file = download_zip_file(client, 'downloadninjs', 'wire') + with zipfile.ZipFile(_file) as zf: + assert filename('amazon-bookstore-opening.json', item) in zf.namelist() + content = zf.open(filename('amazon-bookstore-opening.json', item)).read() + ninjs_block_download_example(content) + + history = app.data.find('history', None, None) + assert 4 == history.count() + assert 'download' in history[0]['action'] + assert 'download' in history[1]['action'] + assert history[0].get('user') + assert history[0].get('versioncreated') + timedelta(seconds=2) >= utcnow() + assert history[0].get('item') in items_ids + assert history[0].get('version') + assert history[0].get('company') == '2' + assert history[0].get('section') == 'wire' diff --git a/tests/test_block_media.py b/tests/test_block_media.py new file mode 100644 index 00000000..f98d32fd --- /dev/null +++ b/tests/test_block_media.py @@ -0,0 +1,589 @@ +import io +import json +import zipfile +from datetime import timedelta +import re +import bson +import lxml.etree +import pytest +from superdesk.utc import utcnow + +from .fixtures import items, init_items, init_auth, agenda_items, init_agenda_items # noqa +from .test_push import upload_binary + +items_ids = [item['_id'] for item in items[:2]] +item = items[:2][0] + + +@pytest.fixture +def setup_block_embeds(client, app): + media_id = bson.ObjectId() + associations = { + 'featuremedia': { + 'mimetype': 'image/jpeg', + 'type': 'picture', + 'renditions': { + 'baseImage': { + 'mimetype': 'image/jpeg', + 'media': str(media_id), + 'href': 'http://a.b.c/xxx.jpg', + }, + '16-9': { + 'mimetype': 'image/jpeg', + 'href': 'http://a.b.c/xxx.jpg', + 'media': str(media_id), + 'width': 1280, + 'height': 720, + }, + '4-3': { + "href": "/assets/633d11b9fb5122dcf06a6f02", + "width": 800, + "height": 600, + 'media': str(media_id), + "mimetype": "image/jpeg", + }, + }, + }, + "editor_1": { + "type": "video", + "renditions": { + "original": { + "mimetype": "video/mp4", + "href": "/assets/640ff0bdfb5122dcf06a6fc3", + 'media': str(media_id), + }, + }, + "mimetype": "video/mp4", + # for base permission check,pass + "products": [ + {"code": "123", "name": "Product A"}, + {"code": "321", "name": "Product B"}, + ], + }, + "editor_0": { + "type": "audio", + "renditions": { + "original": { + "mimetype": "audio/mp3", + "href": "/assets/640feb9bfb5122dcf06a6f7c", + "media": "640feb9bfb5122dcf06a6f7c", + }, + }, + "mimetype": "audio/mp3", + # for base permission check ,disable + "products": [{"code": "999", "name": "NSW News"}], + }, + "editor_2": { + "type": "picture", + "renditions": { + "4-3": { + "href": "/assets/633d11b9fb5122dcf06a6f02", + "width": 800, + "height": 600, + "mimetype": "image/jpeg", + "media": "633d11b9fb5122dcf06a6f02", + }, + "16-9": { + "href": "/assets/633d0f59fb5122dcf06a6ee8", + "width": 1280, + "height": 720, + "mimetype": "image/jpeg", + "media": "633d0f59fb5122dcf06a6ee8", + "poi": {}, + }, + }, + # for base permission check, pass + "products": [{"code": "123"}], + }, + "editor_3": None, + } + upload_binary('picture.jpg', client, media_id=str(media_id)) + + app.data.update('items', item['_id'], { + 'associations': associations, + 'body_html': ( + '

Block 1

' + '' + '
' + '' + '
minns
' + '
' + '' + '


' + '

Block 2

' + '' + '
' + '' + '
Scomo whinging
' + '
' + '' + '


Block 3

' + '' + '
' + '' + '
Prime Minister Scott Morrison and Liberal member for Higgins Katie Allen
' + '
' + '' + '

Block 4

' + '
' + '' + '' + '
' + ) + }, item) + + +@pytest.fixture +def configure_app(app): + app.config['EMBED_PRODUCT_FILTERING'] = True + return app + + +def setup_company_data(app, company_id, company_name, embedded): + app.data.insert('companies', [{ + '_id': company_id, + 'name': company_name, + 'is_enabled': True, + 'embedded': embedded + }]) + user = app.data.find_one('users', req=None, first_name='admin') + assert user + app.data.update('users', user['_id'], {'company': company_id}, user) + app.data.insert('products', [{ + '_id': int(company_id) * 10, + 'name': 'product test', + # base product check + 'sd_product_id': '123', + 'companies': [company_id], + 'is_enabled': True, + 'product_type': 'wire' + }]) + + +@pytest.fixture(params=[ + ('3', 'Block Conent.', { + "social_media_display": True, "sdpermit_display": True, "video_display": False, + "audio_display": True, "images_display": True, "all_display": False, + "social_media_download": True, "video_download": True, "audio_download": False, + "images_download": True, "all_download": False, "sdpermit_download": True + }), + ('2', 'Press01 co.', { + "social_media_display": True, "video_display": True, "audio_display": True, + "images_display": True, "all_display": True, "social_media_download": False, + "video_download": True, "audio_download": False, "images_download": False, + "all_download": False, "sdpermit_display": True, "sdpermit_download": False + }), + ('1', 'Press co.', { + "social_media_display": True, "video_display": True, "audio_display": True, + "images_display": True, "all_display": True, "social_media_download": False, + "video_download": False, "audio_download": False, "images_download": False, + "all_download": False, "sdpermit_display": True, "sdpermit_download": False + }) +]) +def company_data(request): + return request.param + + +@pytest.fixture +def setup_data(client, app, configure_app, setup_block_embeds, company_data): + company_id, company_name, embedded = company_data + + app.data.insert('companies', [{ + '_id': company_id, + 'name': company_name, + 'is_enabled': True, + 'embedded': embedded + }]) + + user = app.data.find_one('users', req=None, first_name='admin') + assert user + app.data.update('users', user['_id'], {'company': company_id}, user) + + app.data.insert('products', [{ + '_id': int(company_id * 10), + 'name': 'product test', + 'sd_product_id': '123', + 'companies': [company_id], + 'is_enabled': True, + 'product_type': 'wire' + }]) + return app, company_id + + +def download_zip_file(client, _format, section): + resp = client.get('/download/{0}?format={1}&type={2}'.format(','.join(items_ids), _format, section), + follow_redirects=True) + assert resp.status_code == 200 + assert resp.mimetype == 'application/zip' + assert resp.headers.get('Content-Disposition') == ( + 'attachment; filename={0}-newsroom.zip'.format(utcnow().strftime("%Y%m%d%H%M")) + ) + return io.BytesIO(resp.get_data()) + + +def text_content_test(content): + content = content.decode('utf-8').split('\n') + assert 'AMAZON-BOOKSTORE-OPENING' in content[0] + assert 'Amazon Is Opening More Bookstores' in content[1] + assert '

' not in content + + +def nitf_content_test(content): + root = lxml.etree.fromstring(content) + assert 'nitf' == root.tag + head = root.find('head') + assert items[0]['headline'] == head.find('title').text + + +def ninjs_content_test(content): + data = json.loads(content) + assert data.get('associations', {}).get('editor_1') + assert not data.get('associations', {}).get('editor_0') + assert not data.get('associations', {}).get('editor_2') + assert data['headline'] == 'Amazon Is Opening More Bookstores' + assert 'editor_1' in data['body_html'] + assert 'editor_0' not in data['body_html'] + + +def ninjs_block_download_video(content): + data = json.loads(content) + assert data.get('associations', {}).get('editor_1') + assert not data.get('associations', {}).get('editor_0') + assert not data.get('associations', {}).get('editor_2') + assert data['headline'] == 'Amazon Is Opening More Bookstores' + assert 'video' in data['body_html'] + assert 'img' not in data['body_html'] + assert 'blockquote' not in data['body_html'] + assert 'audio' not in data['body_html'] + + +def ninjs_block_download_audio_image(content): + data = json.loads(content) + assert not data.get('associations', {}).get('editor_1') + assert not data.get('associations', {}).get('editor_0') + assert data.get('associations', {}).get('editor_2') + assert data['headline'] == 'Amazon Is Opening More Bookstores' + assert 'video' not in data['body_html'] + assert 'img' in data['body_html'] + assert 'blockquote' not in data['body_html'] + assert 'audio' not in data['body_html'] + + +def htmlpackage_block_download_video(content): + data = json.loads(content) + assert data.get('associations', {}).get('editor_1') + assert not data.get('associations', {}).get('editor_0') + assert not data.get('associations', {}).get('editor_2') + assert data['headline'] == 'Amazon Is Opening More Bookstores' + assert 'video' in data['body_html'] + assert 'img' not in data['body_html'] + assert 'blockquote' not in data['body_html'] + assert 'audio' not in data['body_html'] + + +def htmlpackage_block_download_audio_image(html_content_file): + html_content = html_content_file.decode('utf-8') + assert re.search(r'

Amazon Is Opening More Bookstores

', html_content) + assert not re.search(r'= utcnow() + assert history[0].get('item') in items_ids + assert history[0].get('version') + assert history[0].get('company') == company_id + assert history[0].get('section') == 'wire' + + +COMPANY_DATA = [ + ( + '11', + 'AAP01', + { + "social_media_display": True, + "video_display": True, + "audio_display": True, + "images_display": True, + "all_display": True, + "social_media_download": False, + "video_download": True, + "audio_download": False, + "images_download": False, + "all_download": False, + "sdpermit_display": True, + "sdpermit_download": False + }, + ninjs_block_download_video + ), + ( + '12', + 'AAP02', + { + "social_media_display": False, + "video_display": True, + "audio_display": True, + "images_display": True, + "all_display": False, + "social_media_download": False, + "video_download": False, + "audio_download": True, + "images_download": True, + "all_download": False, + "sdpermit_display": True, + "sdpermit_download": True + }, + ninjs_block_download_audio_image, + ), +] + + +@pytest.mark.parametrize('company_data', COMPANY_DATA) +def test_ninjs_download(client, app, configure_app, setup_block_embeds, company_data): + company_id, company_name, embedded, expected_content_test = company_data + + company = app.data.find_one('companies', req=None, _id=company_id) + if company: + app.data.update('companies', company_id, { + 'name': company_name, + 'is_enabled': True, + 'embedded': embedded + }, company) + else: + app.data.insert('companies', [{ + '_id': company_id, + 'name': company_name, + 'is_enabled': True, + 'embedded': embedded + }]) + + user = app.data.find_one('users', req=None, first_name='admin') + assert user + app.data.update('users', user['_id'], {'company': company_id}, user) + + app.data.insert('products', [{ + '_id': int(company_id), + 'name': 'product test', + 'sd_product_id': '123', + 'companies': [company_id], + 'is_enabled': True, + 'product_type': 'wire' + }]) + + app.general_setting('news_api_allowed_renditions', 'Foo', default='16-9,4-3') + + _file = download_zip_file(client, 'downloadninjs', 'wire') + with zipfile.ZipFile(_file) as zf: + assert filename('amazon-bookstore-opening.json', item) in zf.namelist() + content = zf.open(filename('amazon-bookstore-opening.json', item)).read() + expected_content_test(content) + + history = app.data.find('history', None, None) + assert 4 == history.count() + assert 'download' in history[0]['action'] + assert 'download' in history[1]['action'] + assert history[0].get('user') + assert history[0].get('versioncreated') + timedelta(seconds=2) >= utcnow() + assert history[0].get('item') in items_ids + assert history[0].get('version') + assert history[0].get('company') == company_id + assert history[0].get('section') == 'wire' + + +COMPANY_DATA_HTML = [ + ( + '13', + 'AAP03', + { + "social_media_display": False, + "video_display": True, + "audio_display": True, + "images_display": True, + "all_display": False, + "social_media_download": False, + "video_download": False, + "audio_download": True, + "images_download": True, + "all_download": False, + "sdpermit_display": True, + "sdpermit_download": True + }, + htmlpackage_block_download_audio_image, + ), +] + + +@pytest.mark.parametrize('company_data_demo', COMPANY_DATA_HTML) +def test_htmlpackage_download(client, app, configure_app, setup_block_embeds, company_data_demo): + def extract_nr_timestamps(filenames): + timestamps = [] + for filename in filenames: + match = re.match(r'(\d{12})-', filename) + if match: + timestamps.append(match.group(1)) + return sorted(timestamps, reverse=True) + + company_id, company_name, embedded, expected_content_test = company_data_demo + + company = app.data.find_one('companies', req=None, _id=company_id) + if company: + app.data.update('companies', company_id, { + 'name': company_name, + 'is_enabled': True, + 'embedded': embedded + }, company) + else: + app.data.insert('companies', [{ + '_id': company_id, + 'name': company_name, + 'is_enabled': True, + 'embedded': embedded + }]) + + user = app.data.find_one('users', req=None, first_name='admin') + assert user + app.data.update('users', user['_id'], {'company': company_id}, user) + + app.data.insert('products', [{ + '_id': int(company_id), + 'name': 'product test', + 'sd_product_id': '123', + 'companies': [company_id], + 'is_enabled': True, + 'product_type': 'wire' + }]) + + app.general_setting('news_api_allowed_renditions', 'Foo', default='16-9,4-3') + + _file = download_zip_file(client, 'htmlpackage', 'wire') + with zipfile.ZipFile(_file) as zf: + filenames = [info.filename for info in zf.filelist] + content = zf.open(filename('amazon-bookstore-opening.html', item)).read() + expected_content_test(content) + + timestamps = extract_nr_timestamps(filenames) + + if len(timestamps) >= 2: + current_datetime = timestamps[0] + previous_datetime = timestamps[-1] + + expected_files = [ + f'{current_datetime}-amazon-bookstore-opening.html', + f'{previous_datetime}-weather.html', + 'assets/633d11b9fb5122dcf06a6f02' + ] + + missing_files = [file for file in expected_files if file not in filenames] + + if not missing_files: + print("All files found, Test Pass.") + else: + raise AssertionError( + f"The following expected files were not found in the ZIP file list: {', '.join(missing_files)}") + else: + raise AssertionError("Not enough timestamped files found in the ZIP archive") + + history = app.data.find('history', None, None) + assert 4 == history.count() + assert 'download' in history[0]['action'] + assert 'download' in history[1]['action'] + assert history[0].get('user') + assert history[0].get('versioncreated') + timedelta(seconds=2) >= utcnow() + assert history[0].get('item') in items_ids + assert history[0].get('version') + assert history[0].get('company') == company_id + assert history[0].get('section') == 'wire'