From a6d4df628c9ce7598172bc2beda738b1d4f8c56d Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 26 May 2022 16:14:48 +0100 Subject: [PATCH] Unit test for cacheing svg images --- inbox.py | 44 +++++++++++++++++++++++++------------------- tests.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 19 deletions(-) diff --git a/inbox.py b/inbox.py index d49a57260..7bb051371 100644 --- a/inbox.py +++ b/inbox.py @@ -131,11 +131,12 @@ from content import valid_url_lengths from content import remove_script -def _cache_svg_images(session, base_dir: str, http_prefix: str, - nickname: str, domain: str, domain_full: str, - onion_domain: str, i2p_domain: str, - post_json_object: {}, - federation_list: [], debug: bool) -> bool: +def cache_svg_images(session, base_dir: str, http_prefix: str, + nickname: str, domain: str, domain_full: str, + onion_domain: str, i2p_domain: str, + post_json_object: {}, + federation_list: [], debug: bool, + test_image_filename: str) -> bool: """Creates a local copy of a remote svg file """ if has_object_dict(post_json_object): @@ -169,19 +170,24 @@ def _cache_svg_images(session, base_dir: str, http_prefix: str, # validated on upload if '://' + domain in url: continue - if '://' + onion_domain in url: - continue - if '://' + i2p_domain in url: - continue + if onion_domain: + if '://' + onion_domain in url: + continue + if i2p_domain: + if '://' + i2p_domain in url: + continue if '/' in url: filename = url.split('/')[-1] else: filename = url - image_filename = \ - base_dir + '/media/' + post_id + '_' + filename - if not download_image(session, base_dir, url, - image_filename, debug): - continue + if not test_image_filename: + image_filename = \ + base_dir + '/media/' + post_id + '_' + filename + if not download_image(session, base_dir, url, + image_filename, debug): + continue + else: + image_filename = test_image_filename image_data = None try: with open(image_filename, 'rb') as fp_svg: @@ -4107,11 +4113,11 @@ def _inbox_after_initial(server, inbox_start_time, # cache any svg image attachments locally # This is so that any scripts can be removed - _cache_svg_images(session, base_dir, http_prefix, - nickname, domain, domain_full, - onion_domain, i2p_domain, - post_json_object, - federation_list, debug) + cache_svg_images(session, base_dir, http_prefix, + nickname, domain, domain_full, + onion_domain, i2p_domain, + post_json_object, + federation_list, debug, None) inbox_start_time = time.time() diff --git a/tests.py b/tests.py index e5657fc1a..202747f00 100644 --- a/tests.py +++ b/tests.py @@ -128,6 +128,7 @@ from delete import send_delete_via_server from inbox import json_post_allows_comments from inbox import valid_inbox from inbox import valid_inbox_filenames +from inbox import cache_svg_images from categories import guess_hashtag_category from content import remove_script from content import create_edits_html @@ -4013,6 +4014,50 @@ def _test_danger_svg(base_dir: str) -> None: if cleaned_up != svg_clean: print(cleaned_up) assert cleaned_up == svg_clean + + session = None + http_prefix = 'https' + nickname = 'amplifier' + domain = 'ratsratsrats.live' + domain_full = domain + onion_domain = None + i2p_domain = None + federation_list = [] + debug = True + svg_image_filename = base_dir + '/.unit_test_safe.svg' + post_json_object = { + "object": { + "id": "1234", + "attributedTo": "someactor", + "attachment": [ + { + "mediaType": "svg", + "url": "https://somesiteorother.net/media/wibble.svg" + } + ] + } + } + + with open(svg_image_filename, 'wb+') as fp_svg: + fp_svg.write(svg_content.encode('utf-8')) + assert os.path.isfile(svg_image_filename) + assert svg_content != svg_clean + + assert cache_svg_images(session, base_dir, http_prefix, + nickname, domain, domain_full, + onion_domain, i2p_domain, + post_json_object, + federation_list, debug, + svg_image_filename) + + url = post_json_object['object']['attachment'][0]['url'] + assert url == 'https://ratsratsrats.live/media/1234_wibble.svg' + + with open(svg_image_filename, 'rb') as fp_svg: + cached_content = fp_svg.read().decode() + os.remove(svg_image_filename) + assert cached_content == svg_clean + assert not scan_themes_for_scripts(base_dir)