2020-06-11 00:54:34 +02:00
|
|
|
import base64
|
|
|
|
import urllib
|
|
|
|
from io import StringIO
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2018-03-08 09:37:09 +01:00
|
|
|
from django.conf import settings
|
|
|
|
|
2019-02-02 23:53:44 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2018-08-10 20:01:31 +02:00
|
|
|
from zerver.lib.test_helpers import (
|
2018-12-07 18:15:51 +01:00
|
|
|
create_s3_buckets,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
get_test_image_file,
|
2020-06-11 00:54:34 +02:00
|
|
|
override_settings,
|
|
|
|
use_s3_backend,
|
2018-08-10 20:01:31 +02:00
|
|
|
)
|
|
|
|
from zerver.lib.upload import upload_backend, upload_emoji_image
|
2018-08-01 10:53:40 +02:00
|
|
|
from zerver.lib.users import get_api_key
|
2018-03-08 09:37:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ThumbnailTest(ZulipTestCase):
|
|
|
|
|
|
|
|
@use_s3_backend
|
|
|
|
def test_s3_source_type(self) -> None:
|
|
|
|
def get_file_path_urlpart(uri: str, size: str='') -> str:
|
2019-01-04 14:35:58 +01:00
|
|
|
url_in_result = 'smart/filters:no_upscale()%s/%s/source_type/s3'
|
|
|
|
sharpen_filter = ''
|
2018-03-08 09:37:09 +01:00
|
|
|
if size:
|
2020-06-10 06:41:04 +02:00
|
|
|
url_in_result = f'/{size}/{url_in_result}'
|
2019-01-04 14:35:58 +01:00
|
|
|
sharpen_filter = ':sharpen(0.5,0.2,true)'
|
2018-08-10 19:21:16 +02:00
|
|
|
hex_uri = base64.urlsafe_b64encode(uri.encode()).decode('utf-8')
|
2019-01-04 14:35:58 +01:00
|
|
|
return url_in_result % (sharpen_filter, hex_uri)
|
2018-03-08 09:37:09 +01:00
|
|
|
|
2018-12-07 18:15:51 +01:00
|
|
|
create_s3_buckets(
|
|
|
|
settings.S3_AUTH_UPLOADS_BUCKET,
|
|
|
|
settings.S3_AVATAR_BUCKET)
|
2018-03-08 09:37:09 +01:00
|
|
|
|
2020-03-10 11:48:26 +01:00
|
|
|
hamlet = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(hamlet)
|
2018-03-08 09:37:09 +01:00
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "zulip.jpeg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
2020-08-07 01:09:47 +02:00
|
|
|
json = orjson.loads(result.content)
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn("uri", json)
|
|
|
|
uri = json["uri"]
|
|
|
|
base = '/user_uploads/'
|
|
|
|
self.assertEqual(base, uri[:len(base)])
|
|
|
|
|
|
|
|
quoted_uri = urllib.parse.quote(uri[1:], safe='')
|
|
|
|
|
2018-07-30 21:26:01 +02:00
|
|
|
# Test full size image.
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
expected_part_url = get_file_path_urlpart(uri)
|
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
# Test thumbnail size.
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=thumbnail")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2018-09-03 13:54:17 +02:00
|
|
|
expected_part_url = get_file_path_urlpart(uri, '0x300')
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# Test custom emoji URLs in Zulip messages.
|
2018-08-10 20:01:31 +02:00
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
image_file = get_test_image_file("img.png")
|
|
|
|
file_name = "emoji.png"
|
|
|
|
|
|
|
|
upload_emoji_image(image_file, file_name, user_profile)
|
|
|
|
custom_emoji_url = upload_backend.get_emoji_url(file_name, user_profile.realm_id)
|
|
|
|
emoji_url_base = '/user_avatars/'
|
|
|
|
self.assertEqual(emoji_url_base, custom_emoji_url[:len(emoji_url_base)])
|
|
|
|
|
|
|
|
quoted_emoji_url = urllib.parse.quote(custom_emoji_url[1:], safe='')
|
|
|
|
|
|
|
|
# Test full size custom emoji image (for emoji link in messages case).
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_emoji_url}&size=full")
|
2018-08-10 20:01:31 +02:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2018-09-01 13:44:59 +02:00
|
|
|
self.assertIn(custom_emoji_url, result.url)
|
2018-08-10 20:01:31 +02:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# Tests the /api/v1/thumbnail API endpoint with standard API auth
|
2018-03-08 09:37:09 +01:00
|
|
|
self.logout()
|
|
|
|
result = self.api_get(
|
2020-03-10 11:48:26 +01:00
|
|
|
hamlet,
|
2020-06-14 02:57:50 +02:00
|
|
|
f'/thumbnail?url={quoted_uri}&size=full')
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
expected_part_url = get_file_path_urlpart(uri)
|
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
# Test with another user trying to access image using thumbor.
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 403, result)
|
|
|
|
self.assert_in_response("You are not authorized to view this file.", result)
|
|
|
|
|
|
|
|
def test_external_source_type(self) -> None:
|
|
|
|
def run_test_with_image_url(image_url: str) -> None:
|
2018-07-30 21:26:01 +02:00
|
|
|
# Test full size image.
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2018-03-08 09:37:09 +01:00
|
|
|
quoted_url = urllib.parse.quote(image_url, safe='')
|
|
|
|
encoded_url = base64.urlsafe_b64encode(image_url.encode()).decode('utf-8')
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_url}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2019-01-04 14:35:58 +01:00
|
|
|
expected_part_url = '/smart/filters:no_upscale()/' + encoded_url + '/source_type/external'
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
# Test thumbnail size.
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_url}&size=thumbnail")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2018-10-22 02:22:20 +02:00
|
|
|
expected_part_url = '/0x300/smart/filters:no_upscale():sharpen(0.5,0.2,true)/' + encoded_url + '/source_type/external'
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# Test API endpoint with standard API authentication.
|
2018-03-08 09:37:09 +01:00
|
|
|
self.logout()
|
|
|
|
user_profile = self.example_user("hamlet")
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_get(user_profile,
|
2020-06-10 06:41:04 +02:00
|
|
|
f"/thumbnail?url={quoted_url}&size=thumbnail")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2018-10-22 02:22:20 +02:00
|
|
|
expected_part_url = '/0x300/smart/filters:no_upscale():sharpen(0.5,0.2,true)/' + encoded_url + '/source_type/external'
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# Test API endpoint with legacy API authentication.
|
2018-03-08 09:37:09 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_url}&size=thumbnail&api_key={get_api_key(user_profile)}")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2018-10-22 02:22:20 +02:00
|
|
|
expected_part_url = '/0x300/smart/filters:no_upscale():sharpen(0.5,0.2,true)/' + encoded_url + '/source_type/external'
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
# Test a second logged-in user; they should also be able to access it
|
|
|
|
user_profile = self.example_user("iago")
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_url}&size=thumbnail&api_key={get_api_key(user_profile)}")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2018-10-22 02:22:20 +02:00
|
|
|
expected_part_url = '/0x300/smart/filters:no_upscale():sharpen(0.5,0.2,true)/' + encoded_url + '/source_type/external'
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
# Test with another user trying to access image using thumbor.
|
|
|
|
# File should be always accessible to user in case of external source
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_url}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2019-01-04 14:35:58 +01:00
|
|
|
expected_part_url = '/smart/filters:no_upscale()/' + encoded_url + '/source_type/external'
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
image_url = 'https://images.foobar.com/12345'
|
|
|
|
run_test_with_image_url(image_url)
|
|
|
|
|
|
|
|
image_url = 'http://images.foobar.com/12345'
|
|
|
|
run_test_with_image_url(image_url)
|
|
|
|
|
2019-12-12 01:28:29 +01:00
|
|
|
image_url = '//images.foobar.com/12345'
|
|
|
|
run_test_with_image_url(image_url)
|
|
|
|
|
2018-03-08 09:37:09 +01:00
|
|
|
def test_local_file_type(self) -> None:
|
|
|
|
def get_file_path_urlpart(uri: str, size: str='') -> str:
|
2019-01-04 14:35:58 +01:00
|
|
|
url_in_result = 'smart/filters:no_upscale()%s/%s/source_type/local_file'
|
|
|
|
sharpen_filter = ''
|
2018-03-08 09:37:09 +01:00
|
|
|
if size:
|
2020-06-10 06:41:04 +02:00
|
|
|
url_in_result = f'/{size}/{url_in_result}'
|
2019-01-04 14:35:58 +01:00
|
|
|
sharpen_filter = ':sharpen(0.5,0.2,true)'
|
2018-08-10 19:21:16 +02:00
|
|
|
hex_uri = base64.urlsafe_b64encode(uri.encode()).decode('utf-8')
|
2019-01-04 14:35:58 +01:00
|
|
|
return url_in_result % (sharpen_filter, hex_uri)
|
2018-03-08 09:37:09 +01:00
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2018-03-08 09:37:09 +01:00
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "zulip.jpeg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
2020-08-07 01:09:47 +02:00
|
|
|
json = orjson.loads(result.content)
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn("uri", json)
|
|
|
|
uri = json["uri"]
|
|
|
|
base = '/user_uploads/'
|
|
|
|
self.assertEqual(base, uri[:len(base)])
|
|
|
|
|
2018-07-30 21:26:01 +02:00
|
|
|
# Test full size image.
|
2018-03-08 09:37:09 +01:00
|
|
|
# We remove the forward slash infront of the `/user_uploads/` to match
|
2020-08-11 01:47:49 +02:00
|
|
|
# Markdown behaviour.
|
2018-03-08 09:37:09 +01:00
|
|
|
quoted_uri = urllib.parse.quote(uri[1:], safe='')
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
expected_part_url = get_file_path_urlpart(uri)
|
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
# Test thumbnail size.
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=thumbnail")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2018-09-03 13:54:17 +02:00
|
|
|
expected_part_url = get_file_path_urlpart(uri, '0x300')
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# Test with a Unicode filename.
|
2018-03-08 09:37:09 +01:00
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "μένει.jpg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
2020-08-07 01:09:47 +02:00
|
|
|
json = orjson.loads(result.content)
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn("uri", json)
|
|
|
|
uri = json["uri"]
|
|
|
|
|
|
|
|
# We remove the forward slash infront of the `/user_uploads/` to match
|
2020-08-11 01:47:49 +02:00
|
|
|
# Markdown behaviour.
|
2018-03-08 09:37:09 +01:00
|
|
|
quoted_uri = urllib.parse.quote(uri[1:], safe='')
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
expected_part_url = get_file_path_urlpart(uri)
|
|
|
|
self.assertIn(expected_part_url, result.url)
|
2018-08-10 20:01:31 +02:00
|
|
|
|
|
|
|
# Test custom emoji urls in Zulip messages.
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
image_file = get_test_image_file("img.png")
|
|
|
|
file_name = "emoji.png"
|
|
|
|
|
|
|
|
upload_emoji_image(image_file, file_name, user_profile)
|
|
|
|
custom_emoji_url = upload_backend.get_emoji_url(file_name, user_profile.realm_id)
|
|
|
|
emoji_url_base = '/user_avatars/'
|
|
|
|
self.assertEqual(emoji_url_base, custom_emoji_url[:len(emoji_url_base)])
|
|
|
|
|
|
|
|
quoted_emoji_url = urllib.parse.quote(custom_emoji_url[1:], safe='')
|
|
|
|
|
|
|
|
# Test full size custom emoji image (for emoji link in messages case).
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_emoji_url}&size=full")
|
2018-08-10 20:01:31 +02:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2018-09-01 13:44:59 +02:00
|
|
|
self.assertIn(custom_emoji_url, result.url)
|
2018-03-08 09:37:09 +01:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# Tests the /api/v1/thumbnail API endpoint with HTTP basic auth.
|
2018-08-10 20:01:31 +02:00
|
|
|
self.logout()
|
2018-03-08 09:37:09 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
result = self.api_get(
|
2020-03-10 11:48:26 +01:00
|
|
|
user_profile,
|
2020-06-14 02:57:50 +02:00
|
|
|
f'/thumbnail?url={quoted_uri}&size=full')
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
expected_part_url = get_file_path_urlpart(uri)
|
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
# Tests the /api/v1/thumbnail API endpoint with ?api_key
|
2018-03-08 09:37:09 +01:00
|
|
|
# auth.
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
result = self.client_get(
|
2020-06-14 02:57:50 +02:00
|
|
|
f'/thumbnail?url={quoted_uri}&size=full&api_key={get_api_key(user_profile)}')
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
expected_part_url = get_file_path_urlpart(uri)
|
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
# Test with another user trying to access image using thumbor.
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 403, result)
|
|
|
|
self.assert_in_response("You are not authorized to view this file.", result)
|
|
|
|
|
|
|
|
@override_settings(THUMBOR_URL='127.0.0.1:9995')
|
|
|
|
def test_with_static_files(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2018-03-08 09:37:09 +01:00
|
|
|
uri = '/static/images/cute/turtle.png'
|
|
|
|
quoted_uri = urllib.parse.quote(uri[1:], safe='')
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
self.assertEqual(uri, result.url)
|
|
|
|
|
|
|
|
def test_with_thumbor_disabled(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2018-03-08 09:37:09 +01:00
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "zulip.jpeg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
2020-08-07 01:09:47 +02:00
|
|
|
json = orjson.loads(result.content)
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn("uri", json)
|
|
|
|
uri = json["uri"]
|
|
|
|
base = '/user_uploads/'
|
|
|
|
self.assertEqual(base, uri[:len(base)])
|
|
|
|
|
|
|
|
quoted_uri = urllib.parse.quote(uri[1:], safe='')
|
|
|
|
|
|
|
|
with self.settings(THUMBOR_URL=''):
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
self.assertEqual(uri, result.url)
|
|
|
|
|
|
|
|
uri = 'https://www.google.com/images/srpr/logo4w.png'
|
|
|
|
quoted_uri = urllib.parse.quote(uri, safe='')
|
|
|
|
with self.settings(THUMBOR_URL=''):
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2019-12-12 01:28:29 +01:00
|
|
|
base = 'https://external-content.zulipcdn.net/external_content/56c362a24201593891955ff526b3b412c0f9fcd2/68747470733a2f2f7777772e676f6f676c652e636f6d2f696d616765732f737270722f6c6f676f34772e706e67'
|
|
|
|
self.assertEqual(base, result.url)
|
2018-03-08 09:37:09 +01:00
|
|
|
|
|
|
|
uri = 'http://www.google.com/images/srpr/logo4w.png'
|
|
|
|
quoted_uri = urllib.parse.quote(uri, safe='')
|
|
|
|
with self.settings(THUMBOR_URL=''):
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2018-10-25 23:44:54 +02:00
|
|
|
base = 'https://external-content.zulipcdn.net/external_content/7b6552b60c635e41e8f6daeb36d88afc4eabde79/687474703a2f2f7777772e676f6f676c652e636f6d2f696d616765732f737270722f6c6f676f34772e706e67'
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(base, result.url)
|
|
|
|
|
2019-12-12 01:28:29 +01:00
|
|
|
uri = '//www.google.com/images/srpr/logo4w.png'
|
|
|
|
quoted_uri = urllib.parse.quote(uri, safe='')
|
|
|
|
with self.settings(THUMBOR_URL=''):
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2019-12-12 01:28:29 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
base = 'https://external-content.zulipcdn.net/external_content/676530cf4b101d56f56cc4a37c6ef4d4fd9b0c03/2f2f7777772e676f6f676c652e636f6d2f696d616765732f737270722f6c6f676f34772e706e67'
|
|
|
|
self.assertEqual(base, result.url)
|
|
|
|
|
2018-03-08 09:37:09 +01:00
|
|
|
def test_with_different_THUMBOR_URL(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2018-03-08 09:37:09 +01:00
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "zulip.jpeg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
2020-08-07 01:09:47 +02:00
|
|
|
json = orjson.loads(result.content)
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn("uri", json)
|
|
|
|
uri = json["uri"]
|
|
|
|
base = '/user_uploads/'
|
|
|
|
self.assertEqual(base, uri[:len(base)])
|
|
|
|
|
|
|
|
quoted_uri = urllib.parse.quote(uri[1:], safe='')
|
2018-08-10 19:21:16 +02:00
|
|
|
hex_uri = base64.urlsafe_b64encode(uri.encode()).decode('utf-8')
|
2018-03-08 09:37:09 +01:00
|
|
|
with self.settings(THUMBOR_URL='http://test-thumborhost.com'):
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
base = 'http://test-thumborhost.com/'
|
|
|
|
self.assertEqual(base, result.url[:len(base)])
|
2019-01-04 14:35:58 +01:00
|
|
|
expected_part_url = '/smart/filters:no_upscale()/' + hex_uri + '/source_type/local_file'
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
def test_with_different_sizes(self) -> None:
|
|
|
|
def get_file_path_urlpart(uri: str, size: str='') -> str:
|
2019-01-04 14:35:58 +01:00
|
|
|
url_in_result = 'smart/filters:no_upscale()%s/%s/source_type/local_file'
|
|
|
|
sharpen_filter = ''
|
2018-03-08 09:37:09 +01:00
|
|
|
if size:
|
2020-06-10 06:41:04 +02:00
|
|
|
url_in_result = f'/{size}/{url_in_result}'
|
2019-01-04 14:35:58 +01:00
|
|
|
sharpen_filter = ':sharpen(0.5,0.2,true)'
|
2018-08-10 19:21:16 +02:00
|
|
|
hex_uri = base64.urlsafe_b64encode(uri.encode()).decode('utf-8')
|
2019-01-04 14:35:58 +01:00
|
|
|
return url_in_result % (sharpen_filter, hex_uri)
|
2018-03-08 09:37:09 +01:00
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2018-03-08 09:37:09 +01:00
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "zulip.jpeg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
2020-08-07 01:09:47 +02:00
|
|
|
json = orjson.loads(result.content)
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn("uri", json)
|
|
|
|
uri = json["uri"]
|
|
|
|
base = '/user_uploads/'
|
|
|
|
self.assertEqual(base, uri[:len(base)])
|
|
|
|
|
|
|
|
# Test with size supplied as a query parameter.
|
2018-09-03 13:54:17 +02:00
|
|
|
# size=thumbnail should return a 0x300 sized image.
|
2018-07-30 21:26:01 +02:00
|
|
|
# size=full should return the original resolution image.
|
2018-03-08 09:37:09 +01:00
|
|
|
quoted_uri = urllib.parse.quote(uri[1:], safe='')
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=thumbnail")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
2018-09-03 13:54:17 +02:00
|
|
|
expected_part_url = get_file_path_urlpart(uri, '0x300')
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
expected_part_url = get_file_path_urlpart(uri)
|
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
# Test with size supplied as a query parameter where size is anything
|
2018-07-30 21:26:01 +02:00
|
|
|
# else than 'full' or 'thumbnail'. Result should be an error message.
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}&size=480x360")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 403, result)
|
|
|
|
self.assert_in_response("Invalid size.", result)
|
|
|
|
|
|
|
|
# Test with no size param supplied. In this case as well we show an
|
|
|
|
# error message.
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.client_get(f"/thumbnail?url={quoted_uri}")
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 400, "Missing 'size' argument")
|