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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
# -*- 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
import tempfile
from os.path import abspath, dirname, join
from preggy import expect
from tornado.testing import gen_test
from tests.base import TestCase
from tests.fixtures.transformer_test_data import (
FIT_IN_CROP_DATA,
TESTITEMS,
MockErrorSyncDetector,
MockSyncDetector,
TestData,
)
from thumbor.config import Config
from thumbor.context import Context, ServerParameters
from thumbor.importer import Importer
from thumbor.transformer import Transformer
class TransformerTestCase(TestCase):
@classmethod
def setUpClass(cls):
cls.root_folder = (
tempfile.TemporaryDirectory() # pylint: disable=consider-using-with
)
cls.root_path = cls.root_folder.name
cls.loader_path = abspath(
join(dirname(__file__), "../fixtures/images/")
)
cls.base_uri = "/image"
@classmethod
def tearDownClass(cls):
if cls.root_folder is not None:
cls.root_folder.cleanup()
def setUp(self):
super().setUp()
self.has_handled = False
def get_context(self):
cfg = Config(SECURITY_KEY="ACME-SEC")
cfg.LOADER = "thumbor.loaders.file_loader"
cfg.FILE_LOADER_ROOT_PATH = self.loader_path
cfg.STORAGE = "thumbor.storages.file_storage"
cfg.FILE_STORAGE_ROOT_PATH = self.root_path
importer = Importer(cfg)
importer.import_modules()
server = ServerParameters(
8889, "localhost", "thumbor.conf", None, "info", None
)
server.security_key = "ACME-SEC"
return Context(server, cfg, importer)
@gen_test
async def test_invalid_crop(self):
data = TestData(
source_width=800,
source_height=600,
target_width=800,
target_height=600,
halign="right",
valign="top",
focal_points=[],
crop_left=200,
crop_top=0,
crop_right=100,
crop_bottom=100,
)
ctx = data.to_context()
engine = ctx.modules.engine
trans = Transformer(ctx)
await trans.transform()
expect(engine.calls["crop"]).to_be_empty()
@staticmethod
def validate_resize(test_data):
expect(test_data).to_be_resized()
expect(test_data).to_be_cropped()
@gen_test
async def test_can_resize_images(self):
for item in TESTITEMS:
context = item.to_context()
trans = Transformer(context)
await trans.transform()
self.validate_resize(item)
@gen_test
async def test_can_resize_images_with_detectors(self):
for item in TESTITEMS:
context = item.to_context(detectors=[MockSyncDetector])
trans = Transformer(context)
await trans.transform()
self.validate_resize(item)
@gen_test
async def test_can_resize_images_with_detection_error(self):
test_data = TestData(
source_width=800,
source_height=600,
target_width=400,
target_height=150,
halign="center",
valign="middle",
focal_points=[],
crop_left=0,
crop_top=75,
crop_right=800,
crop_bottom=375,
)
context = test_data.to_context(
detectors=[MockErrorSyncDetector], ignore_detector_error=True
)
trans = Transformer(context)
await trans.transform()
self.validate_resize(test_data)
@gen_test
async def test_can_resize_images_with_detection_error_not_ignoring_it(
self,
):
test_data = TestData(
source_width=800,
source_height=600,
target_width=400,
target_height=150,
halign="center",
valign="middle",
focal_points=[],
crop_left=0,
crop_top=75,
crop_right=800,
crop_bottom=375,
)
context = test_data.to_context(
detectors=[MockErrorSyncDetector], ignore_detector_error=False
)
trans = Transformer(context)
with expect.error_to_happen(IOError, message="some-io-error"):
await trans.transform()
expect(test_data.engine.calls["resize"]).to_length(0)
@gen_test
async def test_can_fit_in(self):
for test_data, (width, height, should_resize) in FIT_IN_CROP_DATA:
context = test_data.to_context()
engine = context.modules.engine
trans = Transformer(context)
await trans.transform()
expect(engine.calls["crop"]).to_be_empty()
if should_resize:
expect(engine.calls["resize"]).to_length(1)
expect(engine.calls["resize"][0]["width"]).to_equal(width)
expect(engine.calls["resize"][0]["height"]).to_equal(height)
else:
expect(engine.calls["resize"]).to_be_empty()
@gen_test
async def test_can_transform_meta_with_orientation(self):
data = TestData(
source_width=800,
source_height=600,
target_width=100,
target_height=100,
halign="right",
valign="top",
focal_points=[],
crop_left=None,
crop_top=None,
crop_right=None,
crop_bottom=None,
meta=True,
)
ctx = data.to_context()
ctx.config.RESPECT_ORIENTATION = True
engine = ctx.modules.engine
trans = Transformer(ctx)
await trans.transform()
expect(engine.calls["reorientate"]).to_equal(1)
@gen_test
async def test_can_transform_with_flip(self):
data = TestData(
source_width=800,
source_height=600,
target_width=-800,
target_height=-600,
halign="right",
valign="top",
focal_points=[],
crop_left=None,
crop_top=None,
crop_right=None,
crop_bottom=None,
)
ctx = data.to_context()
engine = ctx.modules.engine
trans = Transformer(ctx)
await trans.transform()
expect(engine.calls["horizontal_flip"]).to_equal(1)
expect(engine.calls["vertical_flip"]).to_equal(1)
@gen_test
async def test_can_resize_with_stretch(self):
data = TestData(
source_width=800,
source_height=600,
target_width=800,
target_height=200,
halign="right",
valign="top",
focal_points=[],
crop_left=None,
crop_top=None,
crop_right=None,
crop_bottom=None,
stretch=True,
)
ctx = data.to_context()
engine = ctx.modules.engine
trans = Transformer(ctx)
await trans.transform()
expect(engine.calls["resize"]).to_equal(
[{"width": 800, "height": 200}]
)
expect(engine.calls["crop"]).to_be_empty()
@gen_test
async def test_can_extract_cover(self):
data = TestData(
source_width=800,
source_height=600,
target_width=-800,
target_height=-600,
halign="right",
valign="top",
focal_points=[],
crop_left=None,
crop_top=None,
crop_right=None,
crop_bottom=None,
)
ctx = data.to_context()
ctx.request.filters = "cover()"
ctx.request.image = "some.gif"
ctx.request.extension = "GIF"
ctx.request.engine.extension = ".gif"
ctx.config.USE_GIFSICLE_ENGINE = True
engine = ctx.modules.engine
trans = Transformer(ctx)
await trans.transform()
expect(engine.calls["cover"]).to_equal(1)
@gen_test
async def test_get_target_dimensions(self):
data = TestData(
source_width=800,
source_height=600,
target_width=600,
target_height=400,
halign="right",
valign="top",
focal_points=[],
crop_left=200,
crop_top=0,
crop_right=100,
crop_bottom=100,
)
ctx = data.to_context()
trans = Transformer(ctx)
dimensions = trans.get_target_dimensions()
expect(dimensions).to_equal((600, 400))
await trans.transform()
expect(dimensions).to_equal((600, 400))
|