Unit test for cacheing svg images

merge-requests/30/head
Bob Mottram 2022-05-26 16:14:48 +01:00
parent 5852de9746
commit a6d4df628c
2 changed files with 70 additions and 19 deletions

View File

@ -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()

View File

@ -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)