-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_giraffe.py
609 lines (504 loc) · 23.1 KB
/
test_giraffe.py
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
"""
"""
from collections import OrderedDict
import unittest
import mock
import pytest
import requests
from requests.exceptions import HTTPError
from wand.color import Color
from wand.drawing import Drawing
from wand.exceptions import MissingDelegateError
from wand.image import Image
from werkzeug.exceptions import BadRequest
import giraffe
class FlaskTestCase(unittest.TestCase):
def setUp(self):
giraffe.app.config['TESTING'] = True
self.app = giraffe.app.test_client()
def tearDown(self):
pass
def make_httperror(code):
response = mock.Mock()
response.status_code = code
e = requests.exceptions.HTTPError()
e.response = response
return e
class TestBuildPipelineFromParams(unittest.TestCase):
def test_resize_only(self):
pipeline = giraffe.build_pipeline({"w": 100, "h": 50})
self.assertEqual(
pipeline, [giraffe.ImageOp('resize', {'width': 100, 'height': 50})]
)
def test_resize_fit_crop_center(self):
pipeline = giraffe.build_pipeline(
{
"w": 100,
"h": 50,
"fit": "crop",
# "crop": None
}
)
self.assertEqual(len(pipeline), 1)
self.assertEqual(pipeline[0].function.__name__, "fit_crop")
self.assertEqual(
pipeline[0].params, {'anchor': 'center', 'height': 50, 'width': 100}
)
def test_resize_fit_liquid(self):
pipeline = giraffe.build_pipeline(
{
"w": 100,
"h": 50,
"fit": "liquid",
# "crop": None
}
)
self.assertEqual(len(pipeline), 1)
self.assertEqual(pipeline[0].function, "liquid")
self.assertEqual(pipeline[0].params, {'height': 50, 'width': 100})
def test_format_png(self):
pipeline = giraffe.build_pipeline({"w": 100, "h": 50, "fm": "png"})
self.assertEqual(len(pipeline), 2)
self.assertEqual(pipeline[0].function, "resize")
self.assertEqual(pipeline[0].params, {'height': 50, 'width': 100})
self.assertEqual(pipeline[1].function, "format")
self.assertEqual(pipeline[1].params, {'format': 'png'})
def test_rotate(self):
pipeline = giraffe.build_pipeline({"rot": 90})
self.assertEqual(pipeline, [giraffe.ImageOp('rotate', {'degrees': 90})])
def test_bad_rotate(self):
self.assertRaises(BadRequest, giraffe.build_pipeline, ({"rot": -1}))
self.assertRaises(BadRequest, giraffe.build_pipeline, ({"rot": 360}))
self.assertRaises(BadRequest, giraffe.build_pipeline, ({"rot": "stringy"}))
giraffe.build_pipeline({"rot": 1.0})
giraffe.build_pipeline({"rot": 1.1})
class TestExtractingFormats(unittest.TestCase):
def test_dot_jpg(self):
self.assertEqual(giraffe.extension_to_format(".jpg"), "jpg")
def test_jpg(self):
self.assertEqual(giraffe.extension_to_format("jpg"), "jpg")
def test_JPG(self):
self.assertEqual(giraffe.extension_to_format("JPG"), "jpg")
def test_jpe(self):
self.assertEqual(giraffe.extension_to_format("JPE"), "jpg")
def test_jpeg(self):
self.assertEqual(giraffe.extension_to_format("JpEg"), "jpg")
def test_gif(self):
self.assertEqual(giraffe.extension_to_format("gif"), "gif")
def test_GIF(self):
self.assertEqual(giraffe.extension_to_format("GIF"), "gif")
def test_dot_gif(self):
self.assertEqual(giraffe.extension_to_format(".GIF"), "gif")
def test_filename_jpg(self):
self.assertEqual(giraffe.path_to_format("hello.jpeg"), "jpg")
def test_filename_gif(self):
self.assertEqual(giraffe.path_to_format("hello.gif"), "gif")
def test_path_jpg(self):
self.assertEqual(giraffe.path_to_format("foo/bar/baz/hello.jpg"), "jpg")
class TestGetImageArgs(unittest.TestCase):
def test_no_params(self):
self.assertEqual(giraffe.get_image_args({}), {})
def test_negative_height(self):
self.assertEqual(giraffe.get_image_args({'h': -100}), {})
def test_negative_width(self):
self.assertEqual(giraffe.get_image_args({'w': -100}), {})
def test_valid_width(self):
self.assertEqual(giraffe.get_image_args({'w': 100}), {'w': 100})
def test_valid_height(self):
self.assertEqual(giraffe.get_image_args({'h': 100}), {'h': 100})
def test_valid_height_invalid_width(self):
self.assertEqual(giraffe.get_image_args({'h': 100, 'w': -100}), {'h': 100})
def test_valid_height_and_width(self):
self.assertEqual(
giraffe.get_image_args({'h': 100, 'w': 100}), {'h': 100, 'w': 100}
)
def test_valid_height_and_width_extra_param_ignored(self):
self.assertEqual(
giraffe.get_image_args({'h': 100, 'w': 100, 'extra': 'hello world'}),
{'h': 100, 'w': 100},
)
def test_fit_crop(self):
self.assertEqual(
giraffe.get_image_args(
{'h': 100, 'w': 100, 'fit': 'crop', 'crop': 'center'}
),
{'h': 100, 'w': 100, 'fit': 'crop'},
)
def test_flip_vertical(self):
self.assertEqual(giraffe.get_image_args({"flip": "v"}), {'flip': 'v'})
def test_flip_horizontal(self):
self.assertEqual(giraffe.get_image_args({"flip": "h"}), {'flip': 'h'})
def test_flip_both(self):
self.assertEqual(giraffe.get_image_args({"flip": "hv"}), {'flip': 'hv'})
def test_rotate(self):
self.assertEqual(giraffe.get_image_args({"rot": 123}), {"rot": 123})
def test_fm_jpg(self):
self.assertEqual(giraffe.get_image_args({"fm": "jpg"}), {"fm": "jpg"})
def test_fm_png(self):
self.assertEqual(giraffe.get_image_args({"fm": "png"}), {"fm": "png"})
class TestGetObjectOrNone(unittest.TestCase):
"""
This function is used to retrieve an object from S3
or simply return None.
"""
bucket = "test.giraffe.bucket"
@mock.patch('giraffe.s3')
def test_missing_object(self, s3):
s3.get.side_effect = make_httperror(404)
self.assertIsNone(giraffe.get_object_or_none(self.bucket, "redbull.jpg"))
@mock.patch('giraffe.s3')
def test_existing_object(self, s3):
s3.get.return_value = 'foo'
self.assertEqual(giraffe.get_object_or_none(self.bucket, "redbull.jpg"), 'foo')
class TestGetFileOr404(unittest.TestCase):
pass
class TestImageToBuffer(unittest.TestCase):
def setUp(self):
with Color('red') as bg:
self.image = Image(width=1920, height=1080, background=bg)
def test_buffer(self):
buffer = giraffe.image_to_buffer(self.image)
# a valid jpeg buffer should have JFIF in the first few bytes
self.assertIn(b"JFIF", buffer.getvalue()[:100])
def test_compressed_buffer(self):
# because this is just a simple red background it compresses VERY well
# from 12528 to 206 bytes.
#
# Of course, for real images it may not be worth compressing them at all.
buffer = giraffe.image_to_buffer(self.image)
compressed_buffer = giraffe.image_to_buffer(self.image, compress=True)
compressed_size = len(compressed_buffer.getvalue())
uncompressed_size = len(buffer.getvalue())
print("normal: %s, compressed: %s" % (uncompressed_size, compressed_size))
self.assertLess(compressed_size, uncompressed_size)
def test_image_to_binary(self):
buffer = giraffe.image_to_buffer(self.image)
self.assertEqual(buffer.getvalue(), giraffe.image_to_binary(self.image))
class TestImageResize(unittest.TestCase):
def setUp(self):
with Color('red') as bg:
self.image = Image(width=1920, height=1080, background=bg)
def test_resize(self):
pipeline = [giraffe.ImageOp("resize", {'width': 100, 'height': 100})]
img = giraffe.process_image(self.image, pipeline)
self.assertEqual(img.size, (100, 100))
def test_fit_crop_center(self):
pipeline = [
giraffe.ImageOp(
giraffe.fit_crop, {'width': 100, 'height': 100, 'anchor': 'center'}
)
]
img = giraffe.process_image(self.image, pipeline)
self.assertEqual(img.size, (100, 100))
def test_fit_liquid(self):
pipeline = [giraffe.ImageOp('liquid', {'width': 100, 'height': 100})]
try:
img = giraffe.process_image(self.image, pipeline)
except MissingDelegateError:
pytest.skip("ImageMagick doesn't have Liquid Rescale support compiled in")
self.assertEqual(img.size, (100, 100))
class TestImageRotate(unittest.TestCase):
def test_rotate(self):
# draw an image with a single red column in the middle
with Color('white') as bg, Color('red') as fg:
image = Image(width=100, height=100, background=bg)
with Drawing() as draw:
draw.fill_color = fg
draw.stroke_color = fg
draw.rectangle(left=50, top=0, right=54, bottom=image.height)
draw(image)
pipeline = [giraffe.ImageOp('rotate', {'degrees': 90})]
rotated_image = giraffe.process_image(image, pipeline)
# verify that that image has a sideways bar of red in the middle
for i, row in enumerate(rotated_image):
for col in row:
self.assertEqual(col.red, 1.0)
if 50 <= i <= 54:
self.assertEqual(col.blue, 0.0)
self.assertEqual(col.green, 0.0)
else:
self.assertNotEqual(col.blue, 0.0)
self.assertNotEqual(col.green, 0.0)
class TestIndexRoute(FlaskTestCase):
"""
Just a placeholder test:
"""
def test_index(self):
r = self.app.get("/")
self.assertEqual(r.status_code, 200)
class TestImageRoute(FlaskTestCase):
bucket = "wtf"
def setUp(self):
super(TestImageRoute, self).setUp()
with Color('red') as bg:
self.image = Image(width=1920, height=1080, background=bg)
@mock.patch('giraffe.s3')
def test_image_doesnt_exist(self, s3):
s3.get.side_effect = make_httperror(404)
r = self.app.get("/{}/redbull.jpg".format(self.bucket))
self.assertEqual(r.status_code, 404)
@mock.patch('giraffe.s3')
def test_image_resize_original_doesnt_exist(self, s3):
s3.get.side_effect = make_httperror(404)
r = self.app.get("/{}/redbull.jpg?w=100&h=100".format(self.bucket))
self.assertEqual(r.status_code, 404)
def test_image_has_no_extension(self):
r = self.app.get("/{}/foo".format(self.bucket))
self.assertEqual(r.status_code, 404)
def test_bucket_only(self):
r = self.app.get("/{}".format(self.bucket))
self.assertEqual(r.status_code, 404)
# original image as jpeg:
@mock.patch('giraffe.s3')
def test_jpeg_exists(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("jpeg")
obj.headers = {'content-type': 'image/jpeg'}
s3.get.return_value = obj
r = self.app.get("/{}/redbull.jpg".format(self.bucket))
self.assertEqual(r.status_code, 200)
content_type = r.headers.get("content-type")
self.assertEqual(content_type, "image/jpeg")
self.assertEqual(Image(blob=r.data).format, 'JPEG')
@mock.patch('giraffe.s3')
def test_jpeg_exists_but_format_as_png(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("jpeg")
obj.headers = {'content-type': 'image/jpeg'}
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/redbull.jpg?fm=png".format(self.bucket))
self.assertEqual(r.status_code, 200)
content_type = r.headers.get("content-type")
self.assertEqual(content_type, "image/png")
args, kwargs = s3.upload.call_args
self.assertEqual(args[0], "giraffe/redbull.png")
self.assertEqual(kwargs['content_type'], "image/png")
self.assertEqual(Image(blob=r.data).format, 'PNG')
@mock.patch('giraffe.s3')
def test_image_exists_but_needs_to_be_resized(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("jpeg")
# we'll call s3.get twice, the first time we'll get the original file, the second time
# we'll be calling to check for the specific version of the object.
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/redbull.jpg?w=100&h=100".format(self.bucket))
self.assertEqual(r.status_code, 200)
self.assertEqual(Image(blob=r.data).size, (100, 100))
@mock.patch('giraffe.s3')
def test_image_exists_but_user_wants_unnecessary_resize(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("jpeg")
# we'll call s3.get twice, the first time we'll get the original file, the second time
# we'll be calling to check for the specific version of the object.
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/redbull.jpg?w=1920&h=1080".format(self.bucket))
self.assertEqual(r.status_code, 200)
self.assertEqual(Image(blob=r.data).size, (1920, 1080))
@mock.patch('giraffe.s3')
def test_image_exists_and_has_already_been_resized(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("jpeg")
obj2 = mock.Mock()
with self.image.clone() as img:
img.resize(100, 100)
obj2.content = img.make_blob("jpeg")
# we'll call s3.get twice, the first time we'll get the original file, the second time
# we'll be calling to check for the specific version of the object.
s3.get.side_effect = [obj, obj2]
r = self.app.get("/{}/redbull.jpg?w=100&h=100".format(self.bucket))
self.assertEqual(r.status_code, 200)
self.assertEqual(Image(blob=r.data).size, (100, 100))
# original image as png:
@mock.patch('giraffe.s3')
def test_png_exists(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("png")
obj.headers = {'content-type': 'image/png'}
s3.get.return_value = obj
r = self.app.get("/{}/redbull.png".format(self.bucket))
self.assertEqual(r.status_code, 200)
content_type = r.headers.get("content-type")
self.assertEqual(content_type, "image/png")
self.assertEqual(Image(blob=r.data).format, 'PNG')
@mock.patch('giraffe.s3')
def test_png_exists_but_needs_format_as_jpg(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("png")
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/redbull.png?fm=jpg".format(self.bucket))
self.assertEqual(r.status_code, 200)
content_type = r.headers.get("content-type")
self.assertEqual(content_type, "image/jpeg")
args, kwargs = s3.upload.call_args
self.assertEqual(args[0], "giraffe/redbull.jpg")
self.assertEqual(kwargs['content_type'], "image/jpeg")
self.assertEqual(Image(blob=r.data).format, 'JPEG')
@mock.patch('giraffe.s3')
def test_png_exists_but_needs_format_as_jpeg(self, s3):
# yep, if someone uses "fm=jpeg" instead of "fm=jpg" it should still work
obj = mock.Mock()
obj.content = self.image.make_blob("png")
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/redbull.png?fm=jpeg".format(self.bucket))
self.assertEqual(r.status_code, 200)
content_type = r.headers.get("content-type")
self.assertEqual(content_type, "image/jpeg")
self.assertEqual(Image(blob=r.data).format, 'JPEG')
@mock.patch('giraffe.s3')
def test_png_exists_but_needs_to_be_resized(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("png")
# we'll call s3.get twice, the first time we'll get the original file, the second time
# we'll be calling to check for the specific version of the object.
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/redbull.png?w=100&h=100".format(self.bucket))
self.assertEqual(r.status_code, 200)
self.assertEqual(Image(blob=r.data).size, (100, 100))
@mock.patch('giraffe.s3')
def test_png_exists_but_user_wants_unnecessary_resize(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("png")
# we'll call s3.get twice, the first time we'll get the original file, the second time
# we'll be calling to check for the specific version of the object.
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/redbull.png?w=1920&h=1080".format(self.bucket))
self.assertEqual(r.status_code, 200)
self.assertEqual(Image(blob=r.data).size, (1920, 1080))
@mock.patch('giraffe.s3')
def test_png_exists_and_has_already_been_resized(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("png")
obj2 = mock.Mock()
with self.image.clone() as img:
img.resize(100, 100)
obj2.content = img.make_blob("png")
# we'll call s3.get twice, the first time we'll get the original file, the second time
# we'll be calling to check for the specific version of the object.
s3.get.side_effect = [obj, obj2]
r = self.app.get("/{}/redbull.jpg?w=100&h=100".format(self.bucket))
self.assertEqual(r.status_code, 200)
self.assertEqual(Image(blob=r.data).size, (100, 100))
# original image as bmp:
@mock.patch('giraffe.s3')
def test_bmp_exists(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("bmp")
obj.headers = {'content-type': 'image/bmp'}
s3.get.return_value = obj
r = self.app.get("/{}/redbull.bmp".format(self.bucket))
self.assertEqual(r.status_code, 200)
content_type = r.headers.get("content-type")
self.assertEqual(content_type, "image/bmp")
self.assertEqual(Image(blob=r.data).format, 'BMP')
@mock.patch('giraffe.s3')
def test_masquerading_gif_converted_to_jpeg(self, s3):
"""
Assuming that a user uploads a gif file but it is named "foo.jpg"
that gif can be resized but it won't resize correctly if we don't
convert the format of the file from gif to jpg.
"""
obj = mock.Mock()
self.image = Image(width=160, height=120)
obj.content = self.image.make_blob("gif")
obj.headers = {'content-type': 'image/jpeg'}
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/masquerading_gif.jpg?w=120&h=120".format(self.bucket))
self.assertEqual(r.status_code, 200)
content_type = r.headers.get("content-type")
self.assertEqual(content_type, "image/jpeg")
self.assertEqual(Image(blob=r.data).format, 'JPEG')
self.assertEqual(Image(blob=r.data).size, (120, 120))
@mock.patch('giraffe.s3')
def test_giant_image_resize(self, s3):
obj = mock.Mock()
self.image = Image(width=12402, height=8770)
obj.content = self.image.make_blob("jpg")
obj.headers = {'content-type': 'image/jpeg'}
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/giant.jpg?w=120&h=120".format(self.bucket))
self.assertEqual(r.status_code, 200)
content_type = r.headers.get("content-type")
self.assertEqual(content_type, "image/jpeg")
self.assertEqual(Image(blob=r.data).size, (120, 120))
@mock.patch('giraffe.s3')
def test_ico_masquerading_as_jpg(self, s3):
obj = mock.Mock()
image = Image(width=16, height=16)
obj.content = image.make_blob('ico')
obj.headers = {'content-type': 'image/jpeg'} # this is what S3 tells us =(
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/giant.jpg?w=64&h=64".format(self.bucket))
self.assertEqual(r.status_code, 200)
content_type = r.headers.get("content-type")
self.assertEqual(content_type, "image/jpeg")
self.assertEqual(Image(blob=r.data, format='jpeg').size, (64, 64))
@mock.patch('giraffe.s3')
def test_ico_masquerading_as_jpg_big(self, s3):
obj = mock.Mock()
image = Image(width=16, height=16)
obj.content = image.make_blob('ico')
obj.headers = {'content-type': 'image/jpeg'} # this is what S3 tells us =(
s3.get.side_effect = [obj, make_httperror(404)]
r = self.app.get("/{}/giant.jpg?w=400&h=400".format(self.bucket))
self.assertEqual(r.status_code, 200)
content_type = r.headers.get("content-type")
self.assertEqual(content_type, "image/jpeg")
self.assertEqual(Image(blob=r.data, format='jpeg').size, (400, 400))
class TestOverlayRoutes(FlaskTestCase):
bucket = "wtf"
def setUp(self):
super(TestOverlayRoutes, self).setUp()
with Color('red') as bg:
self.image = Image(width=1920, height=1080, background=bg)
@mock.patch('giraffe.s3')
def test_image_overlay_relative_url(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("png")
# s3 requests for 1. original image, 2. generated image with overlay, 3. overlay
s3.get.side_effect = [obj, make_httperror(404), obj]
r = self.app.get(
"/{b}/art.png?overlay=/{b}/tshirts/overlay.png&bg=451D74".format(
b=self.bucket
)
)
self.assertEqual(r.status_code, 200)
self.assertEqual(Image(blob=r.data).size, (1920, 1080))
@mock.patch('giraffe.s3')
def test_image_overlay_no_bg_color(self, s3):
# background isn't required, if your original image doesn't include transparency
# then you don't need the background color
obj = mock.Mock()
obj.content = self.image.make_blob("jpg")
# s3 requests for 1. original image, 2. generated image with overlay, 3. overlay
s3.get.side_effect = [obj, make_httperror(404), obj]
r = self.app.get(
"/{b}/art.jpg?overlay=/{b}/tshirts/overlay.png".format(b=self.bucket)
)
self.assertEqual(r.status_code, 200)
self.assertEqual(Image(blob=r.data).size, (1920, 1080))
@mock.patch('giraffe.requests')
@mock.patch('giraffe.s3')
def test_image_overlay_absolute_url(self, s3, requests):
obj = mock.Mock()
obj.content = self.image.make_blob("png")
s3.get.side_effect = [obj, make_httperror(404)]
requests.get.side_effect = [obj]
r = self.app.get(
"/{}/art.png?overlay=https://cloudfront.whatever.org/tshirts/overlay.png&bg=451D74".format(
self.bucket
)
)
self.assertEqual(r.status_code, 200)
self.assertEqual(Image(blob=r.data).size, (1920, 1080))
@mock.patch('giraffe.s3')
def test_image_overlay_resize(self, s3):
obj = mock.Mock()
obj.content = self.image.make_blob("png")
s3.get.side_effect = [obj, make_httperror(404), obj]
r = self.app.get(
"/{b}/art.png?overlay=/{b}/tshirts/overlay.png&bg=451D74&w=100&h=100".format(
b=self.bucket
)
)
self.assertEqual(r.status_code, 200)
self.assertEqual(Image(blob=r.data).size, (100, 100))