2018-03-08 09:37:09 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
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 (
|
|
|
|
use_s3_backend,
|
2018-12-07 18:15:51 +01:00
|
|
|
create_s3_buckets,
|
2018-08-10 20:01:31 +02:00
|
|
|
override_settings,
|
|
|
|
get_test_image_file
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
from io import StringIO
|
|
|
|
import ujson
|
|
|
|
import urllib
|
|
|
|
import base64
|
|
|
|
|
|
|
|
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:
|
|
|
|
url_in_result = '/%s/%s' % (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
|
|
|
|
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "zulip.jpeg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
json = ujson.loads(result.content)
|
|
|
|
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.
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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.
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=thumbnail" % (quoted_uri,))
|
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)
|
|
|
|
|
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).
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_emoji_url,))
|
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
|
|
|
|
2018-03-08 09:37:09 +01:00
|
|
|
# Tests the /api/v1/thumbnail api endpoint with standard API auth
|
|
|
|
self.logout()
|
|
|
|
result = self.api_get(
|
|
|
|
self.example_email("hamlet"),
|
2018-07-30 21:26:01 +02:00
|
|
|
'/thumbnail?url=%s&size=full' %
|
2018-03-08 09:37:09 +01:00
|
|
|
(quoted_uri,))
|
|
|
|
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.
|
|
|
|
self.login(self.example_email("iago"))
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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.
|
2018-03-08 09:37:09 +01:00
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
quoted_url = urllib.parse.quote(image_url, safe='')
|
|
|
|
encoded_url = base64.urlsafe_b64encode(image_url.encode()).decode('utf-8')
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_url,))
|
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.
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=thumbnail" % (quoted_url,))
|
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 api endpoint with standard API authentication.
|
|
|
|
self.logout()
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
result = self.api_get(user_profile.email,
|
|
|
|
"/thumbnail?url=%s&size=thumbnail" % (quoted_url,))
|
|
|
|
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 api endpoint with legacy API authentication.
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
result = self.client_get("/thumbnail?url=%s&size=thumbnail&api_key=%s" % (
|
2018-08-01 10:53:40 +02:00
|
|
|
quoted_url, 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")
|
2018-08-01 10:53:40 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=thumbnail&api_key=%s" % (
|
|
|
|
quoted_url, 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
|
|
|
|
self.login(self.example_email("iago"))
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_url,))
|
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:
|
|
|
|
url_in_result = '/%s/%s' % (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
|
|
|
|
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "zulip.jpeg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
json = ujson.loads(result.content)
|
|
|
|
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
|
|
|
|
# bugdown behaviour.
|
|
|
|
quoted_uri = urllib.parse.quote(uri[1:], safe='')
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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.
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=thumbnail" % (quoted_uri,))
|
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)
|
|
|
|
|
|
|
|
# Test with a unicode filename.
|
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "μένει.jpg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
json = ujson.loads(result.content)
|
|
|
|
self.assertIn("uri", json)
|
|
|
|
uri = json["uri"]
|
|
|
|
|
|
|
|
# We remove the forward slash infront of the `/user_uploads/` to match
|
|
|
|
# bugdown behaviour.
|
|
|
|
quoted_uri = urllib.parse.quote(uri[1:], safe='')
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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).
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_emoji_url,))
|
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
|
|
|
|
|
|
|
# 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(
|
|
|
|
self.example_email("hamlet"),
|
2018-07-30 21:26:01 +02:00
|
|
|
'/thumbnail?url=%s&size=full' %
|
2018-03-08 09:37:09 +01:00
|
|
|
(quoted_uri,))
|
|
|
|
self.assertEqual(result.status_code, 302, result)
|
|
|
|
expected_part_url = get_file_path_urlpart(uri)
|
|
|
|
self.assertIn(expected_part_url, result.url)
|
|
|
|
|
|
|
|
# Tests the /api/v1/thumbnail api endpoint with ?api_key
|
|
|
|
# auth.
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
result = self.client_get(
|
2018-07-30 21:26:01 +02:00
|
|
|
'/thumbnail?url=%s&size=full&api_key=%s' %
|
2018-08-01 10:53:40 +02:00
|
|
|
(quoted_uri, 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.
|
|
|
|
self.login(self.example_email("iago"))
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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:
|
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
uri = '/static/images/cute/turtle.png'
|
|
|
|
quoted_uri = urllib.parse.quote(uri[1:], safe='')
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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:
|
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "zulip.jpeg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
json = ujson.loads(result.content)
|
|
|
|
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=''):
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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=''):
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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=''):
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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=''):
|
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
|
|
|
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:
|
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "zulip.jpeg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
json = ujson.loads(result.content)
|
|
|
|
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'):
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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:
|
|
|
|
url_in_result = '/%s/%s' % (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
|
|
|
|
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
fp = StringIO("zulip!")
|
|
|
|
fp.name = "zulip.jpeg"
|
|
|
|
|
|
|
|
result = self.client_post("/json/user_uploads", {'file': fp})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
json = ujson.loads(result.content)
|
|
|
|
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='')
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=thumbnail" % (quoted_uri,))
|
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)
|
|
|
|
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=full" % (quoted_uri,))
|
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.
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s&size=480x360" % (quoted_uri,))
|
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.
|
2019-04-20 01:00:46 +02:00
|
|
|
result = self.client_get("/thumbnail?url=%s" % (quoted_uri,))
|
2018-03-08 09:37:09 +01:00
|
|
|
self.assertEqual(result.status_code, 400, "Missing 'size' argument")
|