1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
# -*- coding: utf-8 -*-
# thumbor imaging service
# https://github.com/thumbor/thumbor/wiki
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 globo.com thumbor@googlegroups.com
from os.path import abspath, dirname, join
from unittest.mock import patch
from preggy import expect
from tornado.testing import gen_test
import thumbor
import thumbor.loaders.file_loader_http_fallback as loader
from tests.base import TestCase
from thumbor.config import Config
from thumbor.context import Context
from thumbor.loaders import LoaderResult
STORAGE_PATH = abspath(join(dirname(__file__), "../fixtures/images/"))
async def dummy_file_load(
context, url, normalize_url_func=None
): # pylint: disable=unused-argument
result = LoaderResult(
successful=True,
buffer="file",
)
return result
async def dummy_http_load(
context, url, normalize_url_func=None
): # pylint: disable=unused-argument
result = LoaderResult(
successful=True,
buffer="http",
)
return result
class FileLoaderHttpFallbackFileTestCase(TestCase):
def setUp(self):
super().setUp()
config = Config(FILE_LOADER_ROOT_PATH=STORAGE_PATH)
self.ctx = Context(config=config)
@patch.object(thumbor.loaders.file_loader, "load", dummy_file_load)
@gen_test
async def test_should_load_file(self):
result = await loader.load(self.ctx, "image.jpg")
expect(result).to_be_instance_of(LoaderResult)
expect(result.buffer).to_equal("file")
class FileLoaderHttpFallbackHttpTestCase(TestCase):
@patch.object(thumbor.loaders.http_loader, "load", dummy_http_load)
@gen_test
async def test_should_load_http(self):
url = self.get_url("http:/www.google.com/example_image.png")
config = Config()
ctx = Context(None, config, None)
result = await loader.load(ctx, url)
expect(result).to_be_instance_of(LoaderResult)
expect(result.buffer).to_equal("http")
@patch.object(thumbor.loaders.http_loader, "load", dummy_http_load)
@gen_test
async def test_should_fail_with_disallowed_origin(self):
url = "http:/www.google.com/example_image.png"
config = Config(ALLOWED_SOURCES=[".+.domain1.com"])
ctx = Context(None, config, None)
result = await loader.load(ctx, url)
expect(result).to_be_instance_of(LoaderResult)
expect(result.successful).to_be_false()
expect(result.error).to_equal(LoaderResult.ERROR_BAD_REQUEST)
expect(result.extras["reason"]).to_equal("Unallowed domain")
expect(result.extras["source"]).to_equal(url)
|