-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathtest_utils.py
1459 lines (1222 loc) · 75.5 KB
/
test_utils.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import io
import re
import tempfile
import unittest
import uuid
import time
from collections import OrderedDict
from datetime import datetime, date
from fractions import Fraction
from os.path import getsize
import six
import cloudinary.utils
from cloudinary import CL_BLANK
from cloudinary.utils import (
api_sign_request,
build_list_of_dicts,
json_encode,
encode_unicode_url,
base64url_encode,
patch_fetch_format,
cloudinary_scaled_url,
chain_transformations,
generate_transformation_string,
build_eager,
compute_hex_hash,
verify_notification_signature,
verify_api_response_signature,
)
from cloudinary.compat import to_bytes
from test.helper_test import TEST_IMAGE, REMOTE_TEST_IMAGE, patch
from test.test_api import (
API_TEST_TRANS_SCALE100,
API_TEST_TRANS_SCALE100_STR,
API_TEST_TRANS_SEPIA_STR,
API_TEST_TRANS_SEPIA
)
DEFAULT_ROOT_PATH = 'http://res.cloudinary.com/test123/'
DEFAULT_UPLOAD_PATH = DEFAULT_ROOT_PATH + 'image/upload/'
DEFAULT_FETCH_PATH = DEFAULT_ROOT_PATH + 'image/fetch/'
VIDEO_UPLOAD_PATH = DEFAULT_ROOT_PATH + 'video/upload/'
TEST_ID = 'test'
FETCH_URL = "https://cloudinary.com/images/logo.png"
FETCH_VIDEO_URL = "https://demo-res.cloudinary.com/videos/dog.mp4"
IMAGE_VERSION = "1234"
IMAGE_VERSION_STR = "v" + IMAGE_VERSION
DEFAULT_VERSION_STR = 'v1'
TEST_FOLDER = 'folder/test'
MOCKED_NOW = 1549533574
API_SECRET = 'X7qLTrsES31MzxxkxPPA-pAGGfU'
class TestUtils(unittest.TestCase):
crop_transformation = {'crop': 'crop', 'width': 100}
crop_transformation_str = 'c_crop,w_100'
raw_transformation = "c_fill,e_grayscale,q_auto"
custom_function_wasm = {"function_type": "wasm", "source": "blur.wasm"}
custom_function_wasm_str = "wasm:blur.wasm"
custom_function_remote = {
"function_type": "remote",
"source": "https://df34ra4a.execute-api.us-west-2.amazonaws.com/default/cloudinaryFn"}
custom_function_remote_str = \
"remote:aHR0cHM6Ly9kZjM0cmE0YS5leGVjdXRlLWFwaS51cy13ZXN0LTIuYW1hem9uYXdzLmNvbS9kZWZhdWx0L2Nsb3VkaW5hcnlGbg=="
def setUp(self):
cloudinary.config(cloud_name="test123",
cname=None, # for these tests without actual upload, we ignore cname
api_key="a", api_secret="b",
secure_distribution=None,
private_cdn=False)
def __test_cloudinary_url(self, public_id=TEST_ID, options=None, expected_url=None, expected_options=None):
if expected_options is None:
expected_options = {}
if options is None:
options = {}
url, options = cloudinary.utils.cloudinary_url(public_id, **options)
self.assertEqual(expected_url, url)
self.assertEqual(expected_options, options)
def test_cloud_name(self):
"""should use cloud_name from config"""
self.__test_cloudinary_url(options={}, expected_url=DEFAULT_UPLOAD_PATH + "test")
def test_cloud_name_options(self):
"""should allow overriding cloud_name in options"""
self.__test_cloudinary_url(options={"cloud_name": "test321"},
expected_url="http://res.cloudinary.com/test321/image/upload/test")
def test_secure_distribution(self):
"""should use default secure distribution if secure=True"""
self.__test_cloudinary_url(options={"secure": True},
expected_url="https://res.cloudinary.com/test123/image/upload/test")
def test_secure_distribution_overwrite(self):
"""should allow overwriting secure distribution if secure=True"""
self.__test_cloudinary_url(options={"secure": True, "secure_distribution": "something.else.com"},
expected_url="https://something.else.com/test123/image/upload/test")
def test_secure_distibution(self):
"""should take secure distribution from config if secure=True"""
cloudinary.config().secure_distribution = "config.secure.distribution.com"
self.__test_cloudinary_url(options={"secure": True},
expected_url="https://config.secure.distribution.com/test123/image/upload/test")
def test_secure_akamai(self):
"""should default to akamai if secure is given with private_cdn and no secure_distribution"""
self.__test_cloudinary_url(options={"secure": True, "private_cdn": True},
expected_url="https://test123-res.cloudinary.com/image/upload/test")
def test_secure_non_akamai(self):
"""should not add cloud_name if private_cdn and secure non akamai secure_distribution"""
self.__test_cloudinary_url(
options={"secure": True, "private_cdn": True, "secure_distribution": "something.cloudfront.net"},
expected_url="https://something.cloudfront.net/image/upload/test")
def test_http_private_cdn(self):
"""should not add cloud_name if private_cdn and not secure"""
self.__test_cloudinary_url(options={"private_cdn": True},
expected_url="http://test123-res.cloudinary.com/image/upload/test")
def test_format(self):
"""should use format from options"""
self.__test_cloudinary_url(options={"format": "jpg"}, expected_url=DEFAULT_UPLOAD_PATH + "test.jpg")
def test_crop(self):
"""should always use width and height from options"""
self.__test_cloudinary_url(
options={"width": 100, "height": 100},
expected_url=DEFAULT_UPLOAD_PATH + "h_100,w_100/test",
expected_options={"width": 100, "height": 100})
self.__test_cloudinary_url(
options={"width": 100, "height": 100, "crop": "crop"},
expected_url=DEFAULT_UPLOAD_PATH + "c_crop,h_100,w_100/test",
expected_options={"width": 100, "height": 100})
def test_html_width_height_on_crop_fit_limit(self):
"""should not pass width and height to html in case of fit or limit crop"""
self.__test_cloudinary_url(options={"width": 100, "height": 100, "crop": "limit"},
expected_url=DEFAULT_UPLOAD_PATH + "c_limit,h_100,w_100/test")
self.__test_cloudinary_url(options={"width": 100, "height": 100, "crop": "fit"},
expected_url=DEFAULT_UPLOAD_PATH + "c_fit,h_100,w_100/test")
def test_html_width_height_on_angle(self):
"""should not pass width and height to html in case angle was used"""
self.__test_cloudinary_url(options={"width": 100, "height": 100, "crop": "scale", "angle": "auto"},
expected_url=DEFAULT_UPLOAD_PATH + "a_auto,c_scale,h_100,w_100/test")
def test_various_options(self):
"""should use x, y, radius, prefix, gravity and quality from options"""
self.__test_cloudinary_url(
options={"x": 1, "y": 2, "opacity": 20, "radius": 3, "gravity": "center", "quality": 0.4, "prefix": "a"},
expected_url=DEFAULT_UPLOAD_PATH + "g_center,o_20,p_a,q_0.4,r_3,x_1,y_2/test")
self.__test_cloudinary_url(options={"gravity": "auto", "width": 0.5, "crop": "crop"},
expected_url=DEFAULT_UPLOAD_PATH + "c_crop,g_auto,w_0.5/test")
def test_radius(self):
cases = (
({"radius": 10}, "r_10"),
({"radius": "10"}, "r_10"),
({"radius": "$v", "variables": [("$v", 10)]}, "$v_10,r_$v"),
({"radius": "width * 2"}, "r_w_mul_2"),
({"radius": [10, 20]}, "r_10:20"),
({"radius": "10:20"}, "r_10:20"),
({"radius": "10:$v", "variables": [("$v", 20)]}, "$v_20,r_10:$v"),
({"radius": [10, 20, 30]}, "r_10:20:30"),
({"radius": "10:20:30"}, "r_10:20:30"),
({"radius": "10:$v:30", "variables": [("$v", 20)]}, "$v_20,r_10:$v:30"),
({"radius": [10, 20, 30, 40]}, "r_10:20:30:40"),
({"radius": "10:20:30:40"}, "r_10:20:30:40"),
({"radius": "10:$v:30:40", "variables": [("$v", 20)]}, "$v_20,r_10:$v:30:40"),
)
for options, expected_part_url in cases:
self.__test_cloudinary_url(options=options, expected_url=DEFAULT_UPLOAD_PATH + expected_part_url + "/test")
wrong_options = (
{"radius": []},
{"radius": ()},
{"radius": [10, 20, 30, 40, 50]},
)
for options in wrong_options:
with self.assertRaises(ValueError):
cloudinary.utils.cloudinary_url("test", **options)
def test_should_support_auto_width(self):
"""should support auto width"""
self.__test_cloudinary_url(options={"width": "auto:20", "crop": 'fill'},
expected_url=DEFAULT_UPLOAD_PATH + "c_fill,w_auto:20/test",
expected_options={'responsive': True})
self.__test_cloudinary_url(options={"width": "auto:20:350", "crop": 'fill'},
expected_url=DEFAULT_UPLOAD_PATH + "c_fill,w_auto:20:350/test",
expected_options={'responsive': True})
self.__test_cloudinary_url(options={"width": "auto:breakpoints", "crop": 'fill'},
expected_url=DEFAULT_UPLOAD_PATH + "c_fill,w_auto:breakpoints/test",
expected_options={'responsive': True})
self.__test_cloudinary_url(options={"width": "auto:breakpoints_100_1900_20_15", "crop": 'fill'},
expected_url=DEFAULT_UPLOAD_PATH + "c_fill,w_auto:breakpoints_100_1900_20_15/test",
expected_options={'responsive': True})
self.__test_cloudinary_url(options={"width": "auto:breakpoints:json", "crop": 'fill'},
expected_url=DEFAULT_UPLOAD_PATH + "c_fill,w_auto:breakpoints:json/test",
expected_options={'responsive': True})
def test_original_width_and_height(self):
"""should support original width and height"""
self.__test_cloudinary_url(options={"crop": "crop", "width": "ow", "height": "oh"},
expected_url=DEFAULT_UPLOAD_PATH + "c_crop,h_oh,w_ow/test")
def test_support_a_percent_value(self):
"""quality support a percent value"""
self.__test_cloudinary_url(
options={"x": 1, "y": 2, "radius": 3, "gravity": "center", "quality": 80, "prefix": "a"},
expected_url=DEFAULT_UPLOAD_PATH + "g_center,p_a,q_80,r_3,x_1,y_2/test")
self.__test_cloudinary_url(
options={"x": 1, "y": 2, "radius": 3, "gravity": "center", "quality": "80:444", "prefix": "a"},
expected_url=DEFAULT_UPLOAD_PATH + "g_center,p_a,q_80:444,r_3,x_1,y_2/test")
def test_should_support_auto_value(self):
"""quality should support auto value"""
self.__test_cloudinary_url(
options={"x": 1, "y": 2, "radius": 3, "gravity": "center", "quality": "auto", "prefix": "a"},
expected_url=DEFAULT_UPLOAD_PATH + "g_center,p_a,q_auto,r_3,x_1,y_2/test")
self.__test_cloudinary_url(
options={"x": 1, "y": 2, "radius": 3, "gravity": "center", "quality": "auto:good", "prefix": "a"},
expected_url=DEFAULT_UPLOAD_PATH + "g_center,p_a,q_auto:good,r_3,x_1,y_2/test")
self.__test_cloudinary_url(
options={"width": 100, "height": 100, "crop": 'crop', "gravity": "auto:ocr_text"},
expected_url=DEFAULT_UPLOAD_PATH + "c_crop,g_auto:ocr_text,h_100,w_100/test",
expected_options={"width": 100, "height": 100})
self.__test_cloudinary_url(
options={"width": 100, "height": 100, "crop": 'crop', "gravity": "ocr_text"},
expected_url=DEFAULT_UPLOAD_PATH + "c_crop,g_ocr_text,h_100,w_100/test",
expected_options={"width": 100, "height": 100})
self.__test_cloudinary_url(
options={"width": 100, "height": 100, "crop": 'crop', "gravity": "ocr_text:adv_ocr"},
expected_url=DEFAULT_UPLOAD_PATH + "c_crop,g_ocr_text:adv_ocr,h_100,w_100/test",
expected_options={"width": 100, "height": 100})
def test_transformation_simple(self):
"""should support named transformation"""
self.__test_cloudinary_url(options={"transformation": "blip"}, expected_url=DEFAULT_UPLOAD_PATH + "t_blip/test")
def test_transformation_array(self):
"""should support array of named transformations"""
self.__test_cloudinary_url(options={"transformation": ["blip", "blop"]},
expected_url=DEFAULT_UPLOAD_PATH + "t_blip.blop/test")
def test_base_transformations(self):
"""should support base transformation"""
self.__test_cloudinary_url(
options={"transformation": {"x": 100, "y": 100, "crop": "fill"}, "crop": "crop", "width": 100},
expected_url=DEFAULT_UPLOAD_PATH + "c_fill,x_100,y_100/c_crop,w_100/test",
expected_options={"width": 100})
def test_base_transformation_array(self):
"""should support array of base transformations"""
options = {"transformation": [{"x": 100, "y": 100, "width": 200, "crop": "fill"}, {"radius": 10}],
"crop": "crop", "width": 100}
result, options = cloudinary.utils.cloudinary_url("test", **options)
self.assertEqual(options, {"width": 100})
self.assertEqual(result, DEFAULT_UPLOAD_PATH + "c_fill,w_200,x_100,y_100/r_10/c_crop,w_100/test")
def test_no_empty_transformation(self):
"""should not include empty transformations"""
self.__test_cloudinary_url(options={"transformation": [{}, {"x": 100, "y": 100, "crop": "fill"}, {}]},
expected_url=DEFAULT_UPLOAD_PATH + "c_fill,x_100,y_100/test")
def test_raw_transformation(self):
"""should include raw_transformation"""
self.__test_cloudinary_url(options={"transformation": {"width": 100, "raw_transformation": "g_north_west"}},
expected_url=DEFAULT_UPLOAD_PATH + "w_100,g_north_west/test")
self.__test_cloudinary_url(options={"transformation": {"raw_transformation": "g_north_west"}},
expected_url=DEFAULT_UPLOAD_PATH + "g_north_west/test")
self.__test_cloudinary_url(options={"transformation": {"width": 100, "raw_transformation": ""}},
expected_url=DEFAULT_UPLOAD_PATH + "w_100/test")
def test_chain_transformations(self):
"""Should support chaining transformations at the end"""
options = {"effect": "art:incognito", "format": "png"}
chained_transformations = [
{"x": 100, "y": 100, "width": 200, "crop": "fill"},
{"radius": 10},
{"raw_transformation": self.raw_transformation}
]
actual_options = chain_transformations(options, chained_transformations)
actual_transformation_str = generate_transformation_string(**actual_options)[0]
self.assertEqual("e_art:incognito/c_fill,w_200,x_100,y_100/r_10/" + self.raw_transformation,
actual_transformation_str)
# Should support chaining transformations, when default options have no transformations
actual_options = chain_transformations({}, chained_transformations)
actual_transformation_str = generate_transformation_string(**actual_options)[0]
self.assertEqual("c_fill,w_200,x_100,y_100/r_10/" + self.raw_transformation,
actual_transformation_str)
# Should handle empty list of chained transformations
actual_options = chain_transformations(options, [])
actual_transformation_str = generate_transformation_string(**actual_options)[0]
self.assertEqual("e_art:incognito", actual_transformation_str)
# Should handle empty options and empty list of chained transformations
actual_options = chain_transformations({}, [])
actual_transformation_str = generate_transformation_string(**actual_options)[0]
self.assertEqual("", actual_transformation_str)
# Should remove transformation options from resulting options
actual_options = chain_transformations(dict(width=200, height=100), chained_transformations)
self.assertNotIn("width", actual_options)
self.assertNotIn("height", actual_options)
actual_transformation_str = generate_transformation_string(**actual_options)[0]
self.assertEqual("h_100,w_200/c_fill,w_200,x_100,y_100/r_10/c_fill,e_grayscale,q_auto",
actual_transformation_str)
# Should chain transformations with a fetch option
options["type"] = "fetch"
patch_fetch_format(options)
actual_options = chain_transformations(options, chained_transformations)
# format should be removed when we use fetch
self.assertNotIn("format", actual_options)
actual_transformation_str = generate_transformation_string(**actual_options)[0]
# Should use url format as a fetch_format
self.assertEqual("e_art:incognito,f_png/c_fill,w_200,x_100,y_100/r_10/" + self.raw_transformation,
actual_transformation_str)
options["fetch_format"] = "gif"
actual_options = chain_transformations(options, chained_transformations)
actual_transformation_str = generate_transformation_string(**actual_options)[0]
# Should use fetch_format
self.assertEqual("e_art:incognito,f_gif/c_fill,w_200,x_100,y_100/r_10/" + self.raw_transformation,
actual_transformation_str)
def test_size(self):
"""should support size"""
options = {"size": "10x10", "crop": "crop"}
result, options = cloudinary.utils.cloudinary_url("test", **options)
self.assertEqual(options, {"width": "10", "height": "10"})
self.assertEqual(result, DEFAULT_UPLOAD_PATH + "c_crop,h_10,w_10/test")
def test_type(self):
"""should use type from options"""
self.__test_cloudinary_url(options={"type": "facebook"}, expected_url=DEFAULT_ROOT_PATH + "image/facebook/test")
def test_resource_type(self):
"""should use resource_type from options"""
self.__test_cloudinary_url(options={"resource_type": "raw"}, expected_url=DEFAULT_ROOT_PATH + "raw/upload/test")
def test_ignore_http(self):
"""should ignore http links only if type is not given or is asset"""
options = {}
result, options = cloudinary.utils.cloudinary_url("http://test", **options)
self.assertEqual(options, {})
self.assertEqual(result, "http://test")
options = {"type": "fetch"}
result, options = cloudinary.utils.cloudinary_url("http://test", **options)
self.assertEqual(options, {})
self.assertEqual(result, DEFAULT_ROOT_PATH + "image/fetch/http://test")
def test_fetch(self):
"""should escape fetch urls"""
options = {"type": "fetch"}
result, options = cloudinary.utils.cloudinary_url("http://blah.com/hello?a=b", **options)
self.assertEqual(options, {})
self.assertEqual(result, DEFAULT_ROOT_PATH + "image/fetch/http://blah.com/hello%3Fa%3Db")
def test_http_escape(self):
"""should escape http urls"""
options = {"type": "youtube"}
result, options = cloudinary.utils.cloudinary_url("http://www.youtube.com/watch?v=d9NF2edxy-M", **options)
self.assertEqual(options, {})
self.assertEqual(result, DEFAULT_ROOT_PATH + "image/youtube/http://www.youtube.com/watch%3Fv%3Dd9NF2edxy-M")
def test_cname(self):
"""should support extenal cname"""
self.__test_cloudinary_url(options={"cname": "hello.com"},
expected_url="http://hello.com/test123/image/upload/test")
def test_cname_subdomain(self):
"""should support extenal cname with cdn_subdomain on"""
self.__test_cloudinary_url(options={"cname": "hello.com", "cdn_subdomain": True},
expected_url="http://a2.hello.com/test123/image/upload/test")
def test_background(self):
"""should support background"""
self.__test_cloudinary_url(options={"background": "red"}, expected_url=DEFAULT_UPLOAD_PATH + "b_red/test")
self.__test_cloudinary_url(options={"background": "#112233"},
expected_url=DEFAULT_UPLOAD_PATH + "b_rgb:112233/test")
def test_default_image(self):
"""should support default_image"""
self.__test_cloudinary_url(options={"default_image": "default"},
expected_url=DEFAULT_UPLOAD_PATH + "d_default/test")
def test_angle(self):
"""should support angle"""
self.__test_cloudinary_url(options={"angle": 12}, expected_url=DEFAULT_UPLOAD_PATH + "a_12/test")
def test_overlay(self):
"""should support overlay"""
self.__test_cloudinary_url(options={"overlay": "text:hello"},
expected_url=DEFAULT_UPLOAD_PATH + "l_text:hello/test")
# Should not pass width height to HTML with overlay
self.__test_cloudinary_url(options={"overlay": "text:hello", "height": 100, "width": 100},
expected_url=DEFAULT_UPLOAD_PATH + "h_100,l_text:hello,w_100/test")
self.__test_cloudinary_url(
options={"overlay": {"font_family": "arial", "font_size": 20, "text": "hello"}, "height": 100,
"width": 100}, expected_url=DEFAULT_UPLOAD_PATH + "h_100,l_text:arial_20:hello,w_100/test")
def test_fetch_overlay(self):
"""should support overlay"""
self.__test_cloudinary_url(
options={"overlay": "fetch:" + REMOTE_TEST_IMAGE},
expected_url=(
DEFAULT_UPLOAD_PATH
+ "l_fetch:aHR0cDovL2Nsb3VkaW5hcnkuY29tL2ltYWdlcy9vbGRfbG9nby5wbmc=/"
+ "test"))
"""should support video overlay"""
self.__test_cloudinary_url(
options={"overlay": "video:fetch:" + FETCH_VIDEO_URL},
expected_url=(
DEFAULT_UPLOAD_PATH
+ "l_video:fetch:aHR0cHM6Ly9kZW1vLXJlcy5jbG91ZGluYXJ5LmNvbS92aWRlb3MvZG9nLm1wNA==/"
+ "test"))
self.__test_cloudinary_url(
options={
"overlay": {
"url":
"https://upload.wikimedia.org/wikipedia/commons/2/2b/고창갯벌.jpg"}},
expected_url=(
DEFAULT_UPLOAD_PATH +
"l_fetch:"
"aHR0cHM6Ly91cGxvYWQud2lraW1lZGlhLm9yZy93aWtpcGVkaWEvY29tbW9ucy8yLzJiL-qzoOywveqwr-uyjC5qcGc=/"
"test"))
self.__test_cloudinary_url(
options={
"overlay": {
"url":
"https://www.test.com/test/JE01118-YGP900_1_lar.jpg?version=432023"}},
expected_url=(
DEFAULT_UPLOAD_PATH +
"l_fetch:"
"aHR0cHM6Ly93d3cudGVzdC5jb20vdGVzdC9KRTAxMTE4LVlHUDkwMF8xX2xhci5qcGc_dmVyc2lvbj00MzIwMjM=/"
"test"))
def test_underlay(self):
"""should support underlay"""
self.__test_cloudinary_url(options={"underlay": "text:hello"},
expected_url=DEFAULT_UPLOAD_PATH + "u_text:hello/test")
# Should not pass width height to HTML with underlay
self.__test_cloudinary_url(options={"underlay": "text:hello", "height": 100, "width": 100},
expected_url=DEFAULT_UPLOAD_PATH + "h_100,u_text:hello,w_100/test")
def test_custom_function(self):
# should support custom function from string
options = {"custom_function": self.custom_function_wasm_str}
self.__test_cloudinary_url(
options=options,
expected_url=DEFAULT_UPLOAD_PATH + "fn_" + self.custom_function_wasm_str + "/test"
)
# should support custom function from dictionary
options = {"custom_function": self.custom_function_wasm}
self.__test_cloudinary_url(
options=options,
expected_url=DEFAULT_UPLOAD_PATH + "fn_" + self.custom_function_wasm_str + "/test"
)
# should encode custom function source for remote function
options = {"custom_function": self.custom_function_remote}
self.__test_cloudinary_url(
options=options,
expected_url=DEFAULT_UPLOAD_PATH + "fn_" + self.custom_function_remote_str + "/test"
)
def test_custom_pre_function_wasm_str(self):
# should support custom pre function from string
options = {"custom_pre_function": self.custom_function_wasm_str}
self.__test_cloudinary_url(
options=options,
expected_url=DEFAULT_UPLOAD_PATH + "fn_pre:" + self.custom_function_wasm_str + "/test"
)
def test_custom_pre_function_wasm_dictionary(self):
# should support custom pre function from dictionary
options = {"custom_pre_function": self.custom_function_wasm}
self.__test_cloudinary_url(
options=options,
expected_url=DEFAULT_UPLOAD_PATH + "fn_pre:" + self.custom_function_wasm_str + "/test"
)
def test_custom_pre_function_remote(self):
# should encode custom function source for remote function
options = {"custom_pre_function": self.custom_function_remote}
self.__test_cloudinary_url(
options=options,
expected_url=DEFAULT_UPLOAD_PATH + "fn_pre:" + self.custom_function_remote_str + "/test"
)
def test_fetch_format(self):
"""should support format for fetch urls"""
self.__test_cloudinary_url(
public_id="http://cloudinary.com/images/logo.png",
options={"format": "jpg", "type": "fetch"},
expected_url=DEFAULT_ROOT_PATH + "image/fetch/f_jpg/http://cloudinary.com/images/logo.png"
)
def test_effect(self):
"""should support effect"""
self.__test_cloudinary_url(options={"effect": "sepia"}, expected_url=DEFAULT_UPLOAD_PATH + "e_sepia/test")
def test_effect_with_dict(self):
"""should support effect with dict"""
self.__test_cloudinary_url(options={"effect": {"sepia": -10}},
expected_url=DEFAULT_UPLOAD_PATH + "e_sepia:-10/test")
def test_effect_with_array(self):
"""should support effect with array"""
self.__test_cloudinary_url(options={"effect": ["sepia", 10]},
expected_url=DEFAULT_UPLOAD_PATH + "e_sepia:10/test")
def test_keyframe_interval(self):
"""should support keyframe_interval"""
test_values = (
(10, "ki_10.0"),
(0.05, "ki_0.05"),
(3.45, "ki_3.45"),
(300, "ki_300.0"),
("10", "ki_10"),
)
for value, expected in test_values:
self.__test_cloudinary_url(options={"resource_type": "video", "keyframe_interval": value},
expected_url=VIDEO_UPLOAD_PATH + expected + "/test")
with self.assertRaises(ValueError):
cloudinary.utils.cloudinary_url("test", keyframe_interval=-1)
def test_streaming_profile(self):
"""should support streaming_profile"""
self.__test_cloudinary_url(options={"streaming_profile": "some-profile"},
expected_url=DEFAULT_UPLOAD_PATH + "sp_some-profile/test")
def test_density(self):
"""should support density"""
self.__test_cloudinary_url(options={"density": 150}, expected_url=DEFAULT_UPLOAD_PATH + "dn_150/test")
def test_page(self):
"""should support page"""
self.__test_cloudinary_url(options={"page": 3}, expected_url=DEFAULT_UPLOAD_PATH + "pg_3/test")
def test_border(self):
"""should support border"""
self.__test_cloudinary_url(options={"border": {"width": 5}},
expected_url=DEFAULT_UPLOAD_PATH + "bo_5px_solid_black/test")
self.__test_cloudinary_url(options={"border": {"width": 5, "color": "#ffaabbdd"}},
expected_url=DEFAULT_UPLOAD_PATH + "bo_5px_solid_rgb:ffaabbdd/test")
self.__test_cloudinary_url(options={"border": "1px_solid_blue"},
expected_url=DEFAULT_UPLOAD_PATH + "bo_1px_solid_blue/test")
def test_flags(self):
"""should support flags"""
self.__test_cloudinary_url(options={"flags": "abc"}, expected_url=DEFAULT_UPLOAD_PATH + "fl_abc/test")
self.__test_cloudinary_url(options={"flags": ["abc", "def"]},
expected_url=DEFAULT_UPLOAD_PATH + "fl_abc.def/test")
def test_dpr(self):
"""should support dpr (device pixel radio)"""
self.__test_cloudinary_url(options={"dpr": "2.0"}, expected_url=DEFAULT_UPLOAD_PATH + "dpr_2.0/test")
def test_folder_version(self):
"""should add version if public_id contains / """
self.__test_cloudinary_url(public_id="folder/test", expected_url=DEFAULT_UPLOAD_PATH + "v1/folder/test")
self.__test_cloudinary_url(public_id="folder/test", options={"version": 123},
expected_url=DEFAULT_UPLOAD_PATH + "v123/folder/test")
self.__test_cloudinary_url(public_id="v1234/test", expected_url=DEFAULT_UPLOAD_PATH + "v1234/test")
def test_force_version(self):
"""Should not set default version v1 to resources stored in folders if force_version is set to False"""
self.__test_cloudinary_url(TEST_FOLDER,
expected_url=DEFAULT_UPLOAD_PATH + DEFAULT_VERSION_STR + "/" + TEST_FOLDER)
self.__test_cloudinary_url(TEST_FOLDER,
options={"force_version": False},
expected_url=DEFAULT_UPLOAD_PATH + TEST_FOLDER)
# Explicitly set version is always passed
self.__test_cloudinary_url(TEST_ID,
options={"force_version": False, "version": IMAGE_VERSION},
expected_url=DEFAULT_UPLOAD_PATH + IMAGE_VERSION_STR + "/" + TEST_ID)
self.__test_cloudinary_url(TEST_FOLDER,
options={"force_version": False, "version": IMAGE_VERSION},
expected_url=DEFAULT_UPLOAD_PATH + IMAGE_VERSION_STR + "/" + TEST_FOLDER)
# Should use force_version from config
cloudinary.config(force_version=False)
self.__test_cloudinary_url(TEST_FOLDER, expected_url=DEFAULT_UPLOAD_PATH + TEST_FOLDER)
# Should override config with options
self.__test_cloudinary_url(TEST_FOLDER,
options={"force_version": True},
expected_url=DEFAULT_UPLOAD_PATH + DEFAULT_VERSION_STR + "/" + TEST_FOLDER)
def test_shorten(self):
self.__test_cloudinary_url(options={"shorten": True}, expected_url=DEFAULT_ROOT_PATH + "iu/test")
self.__test_cloudinary_url(options={"shorten": True, "type": "private"},
expected_url=DEFAULT_ROOT_PATH + "image/private/test")
def test_signed_url(self):
self.__test_cloudinary_url(
public_id="image.jpg",
options={"version": 1234, "transformation": {"crop": "crop", "width": 10, "height": 20}, "sign_url": True},
expected_url=DEFAULT_UPLOAD_PATH + "s--Ai4Znfl3--/c_crop,h_20,w_10/v1234/image.jpg")
self.__test_cloudinary_url(
public_id="image.jpg",
options={"version": 1234, "sign_url": True},
expected_url=DEFAULT_UPLOAD_PATH + "s----SjmNDA--/v1234/image.jpg")
self.__test_cloudinary_url(
public_id="image.jpg",
options={"transformation": {"crop": "crop", "width": 10, "height": 20}, "sign_url": True},
expected_url=DEFAULT_UPLOAD_PATH + "s--Ai4Znfl3--/c_crop,h_20,w_10/image.jpg")
self.__test_cloudinary_url(
public_id="image.jpg",
options={"type": "authenticated", "transformation": {"crop": "crop", "width": 10, "height": 20},
"sign_url": True},
expected_url=DEFAULT_ROOT_PATH + "image/authenticated/s--Ai4Znfl3--/c_crop,h_20,w_10/image.jpg")
self.__test_cloudinary_url(
public_id="http://google.com/path/to/image.png",
options={"version": 1234, "type": "fetch", "sign_url": True},
expected_url=DEFAULT_ROOT_PATH + "image/fetch/s--hH_YcbiS--/v1234/http://google.com/path/to/image.png")
self.__test_cloudinary_url(
public_id="image.jpg",
options={"type": "authenticated", "transformation": {"color": "red", "overlay": {"text": "Cool%F0%9F%98%8D", "font_family": "Times", "font_size": 70, "font_weight": "bold"}},
"sign_url": True},
expected_url="http://res.cloudinary.com/test123/image/authenticated/s--Uqk1a-5W--/co_red,l_text:Times_70_bold:Cool%25F0%259F%2598%258D/image.jpg")
self.__test_cloudinary_url(
public_id="image.jpg",
options={"type": "authenticated", "transformation": {"raw_transformation": "co_red,l_text:Times_70_bold:Cool%25F0%259F%2598%258D"},
"sign_url": True},
expected_url="http://res.cloudinary.com/test123/image/authenticated/s--Uqk1a-5W--/co_red,l_text:Times_70_bold:Cool%25F0%259F%2598%258D/image.jpg")
def test_signed_url_sha256(self):
sha256_config = cloudinary.Config()
sha256_config.update(**vars(cloudinary.config()))
sha256_config.update(signature_algorithm=cloudinary.utils.SIGNATURE_SHA256)
with patch('cloudinary.config', return_value=sha256_config):
self.__test_cloudinary_url(
public_id="sample.jpg",
options={"sign_url": True},
expected_url=DEFAULT_UPLOAD_PATH + "s--2hbrSMPO--/sample.jpg")
def test_disallow_url_suffix_in_non_upload_types(self):
with self.assertRaises(ValueError):
cloudinary.utils.cloudinary_url("test", url_suffix="hello", private_cdn=True, type="facebook")
def test_disallow_url_suffix_with_slash_or_dot(self):
with self.assertRaises(ValueError):
cloudinary.utils.cloudinary_url("test", url_suffix="hello/world", private_cdn=True)
with self.assertRaises(ValueError):
cloudinary.utils.cloudinary_url("test", url_suffix="hello.world", private_cdn=True)
def test_support_url_suffix_for_private_cdn(self):
self.__test_cloudinary_url(options={"url_suffix": "hello", "private_cdn": True},
expected_url="http://test123-res.cloudinary.com/images/test/hello")
self.__test_cloudinary_url(options={"url_suffix": "hello", "angle": 0, "private_cdn": True},
expected_url="http://test123-res.cloudinary.com/images/a_0/test/hello")
def test_put_format_after_url_suffix(self):
self.__test_cloudinary_url(options={"url_suffix": "hello", "private_cdn": True, "format": "jpg"},
expected_url="http://test123-res.cloudinary.com/images/test/hello.jpg")
def test_not_sign_the_url_suffix(self):
url, options = cloudinary.utils.cloudinary_url("test", format="jpg", sign_url=True)
expected_signature = re.search(r's--[0-9A-Za-z_-]{8}--', url)
self.__test_cloudinary_url(
options={"url_suffix": "hello", "private_cdn": True, "format": "jpg", "sign_url": True},
expected_url="http://test123-res.cloudinary.com/images/" + expected_signature.group(0) + "/test/hello.jpg")
url, options = cloudinary.utils.cloudinary_url("test", format="jpg", angle=0, sign_url=True)
expected_signature = re.search(r's--[0-9A-Za-z_-]{8}--', url)
self.__test_cloudinary_url(
options={"url_suffix": "hello", "private_cdn": True, "format": "jpg", "angle": 0, "sign_url": True},
expected_url="http://test123-res.cloudinary.com/images/" + expected_signature.group(
0) + "/a_0/test/hello.jpg")
def test_support_url_suffix_for_raw_uploads(self):
self.__test_cloudinary_url(options={"url_suffix": "hello", "private_cdn": True, "resource_type": "raw"},
expected_url="http://test123-res.cloudinary.com/files/test/hello")
def test_support_use_root_path_for_shared_cdn(self):
self.__test_cloudinary_url(options={"use_root_path": True, "private_cdn": False},
expected_url=DEFAULT_ROOT_PATH + "test")
self.__test_cloudinary_url(options={"use_root_path": True, "private_cdn": False, "angle": 0},
expected_url=DEFAULT_ROOT_PATH + "a_0/test")
def test_support_use_root_path_for_private_cdn(self):
self.__test_cloudinary_url(options={"use_root_path": True, "private_cdn": True},
expected_url="http://test123-res.cloudinary.com/test")
self.__test_cloudinary_url(options={"use_root_path": True, "private_cdn": True, "angle": 0},
expected_url="http://test123-res.cloudinary.com/a_0/test")
def test_support_use_root_path_together_with_url_suffix_for_private_cdn(self):
self.__test_cloudinary_url(options={"use_root_path": True, "url_suffix": "hello", "private_cdn": True},
expected_url="http://test123-res.cloudinary.com/test/hello")
def test_disallow_use_root_path_if_not_image_upload(self):
with self.assertRaises(ValueError):
cloudinary.utils.cloudinary_url("test", use_root_path=True, private_cdn=True, type="facebook")
with self.assertRaises(ValueError):
cloudinary.utils.cloudinary_url("test", use_root_path=True, private_cdn=True, resource_type="raw")
def test_support_cdn_subdomain_with_secure_on_if_using_shared_domain(self):
self.__test_cloudinary_url(options={"secure": True, "cdn_subdomain": True},
expected_url="https://res-2.cloudinary.com/test123/image/upload/test")
def test_support_secure_cdn_subdomain_false_override_with_secure(self):
self.__test_cloudinary_url(options={"secure": True, "cdn_subdomain": True, "secure_cdn_subdomain": False},
expected_url="https://res.cloudinary.com/test123/image/upload/test")
def test_support_secure_cdn_subdomain_true_override_with_secure(self):
self.__test_cloudinary_url(
options={"secure": True, "cdn_subdomain": True, "secure_cdn_subdomain": True, "private_cdn": True},
expected_url="https://test123-res-2.cloudinary.com/image/upload/test")
def test_escape_public_id(self):
""" should escape public_ids """
tests = {
"a b": "a%20b",
"a+b": "a%2Bb",
"a%20b": "a%20b",
"a-b": "a-b",
"a??b": "a%3F%3Fb"
}
for source, target in tests.items():
result, options = cloudinary.utils.cloudinary_url(source)
self.assertEqual(DEFAULT_UPLOAD_PATH + "" + target, result)
def test_escape_public_id_with_non_ascii_characters(self):
self.__test_cloudinary_url(u"ß", expected_url=DEFAULT_UPLOAD_PATH + "%C3%9F")
def test_responsive_width(self):
"""should support responsive width"""
options = {"width": 100, "height": 100, "crop": "crop", "responsive_width": True}
result, options = cloudinary.utils.cloudinary_url("test", **options)
self.assertEqual(options, {"responsive": True})
self.assertEqual(result, DEFAULT_UPLOAD_PATH + "c_crop,h_100,w_100/c_limit,w_auto/test")
cloudinary.config(responsive_width_transformation={"width": "auto", "crop": "pad"})
options = {"width": 100, "height": 100, "crop": "crop", "responsive_width": True}
result, options = cloudinary.utils.cloudinary_url("test", **options)
self.assertEqual(options, {"responsive": True})
self.assertEqual(result, DEFAULT_UPLOAD_PATH + "c_crop,h_100,w_100/c_pad,w_auto/test")
def test_norm_range_value(self):
# should parse integer range values
self.assertEqual(cloudinary.utils.norm_range_value("200"), "200")
# should parse float range values
self.assertEqual(cloudinary.utils.norm_range_value("200.0"), "200.0")
# should parse a percent range value
self.assertEqual(cloudinary.utils.norm_range_value("20p"), "20p")
self.assertEqual(cloudinary.utils.norm_range_value("20P"), "20p")
self.assertEqual(cloudinary.utils.norm_range_value("20%"), "20p")
self.assertEqual(cloudinary.utils.norm_range_value("p"), None)
def test_norm_auto_range_value(self):
"""Should parse both auto and values supported by norm_range_value"""
self.assertEqual("auto", cloudinary.utils.norm_auto_range_value("auto"))
self.assertEqual(None, cloudinary.utils.norm_auto_range_value("non_auto"))
# Should handle regular norm_range_value values
self.assertEqual(cloudinary.utils.norm_auto_range_value("20P"), "20p")
def test_fps(self):
""" Should support a single number, a list of mixed type and a string, including open-ended and closed ranges"""
fps_test_values = (
('24-29.97', 'fps_24-29.97'),
(24, 'fps_24'),
(24.973, 'fps_24.973'),
('24', 'fps_24'),
('-24', 'fps_-24'),
('$v', 'fps_$v'),
((24, 29.97), 'fps_24-29.97'),
(['24', '$v'], 'fps_24-$v')
)
for value, expected in fps_test_values:
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'fps': value},
expected_url=VIDEO_UPLOAD_PATH + expected + "/video_id")
def test_video_codec(self):
# should support a string value
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'video_codec': 'auto'},
expected_url=VIDEO_UPLOAD_PATH + "vc_auto/video_id")
# should support a hash value
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video',
'video_codec': {'codec': 'h264', 'profile': 'basic',
'level': '3.1'}},
expected_url=VIDEO_UPLOAD_PATH + "vc_h264:basic:3.1/video_id")
# b_frames=True -> should not add b_frames parameter
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video',
'video_codec': {'codec': 'h265', 'profile': 'auto',
'level': 'auto', 'b_frames': True}},
expected_url=VIDEO_UPLOAD_PATH + "vc_h265:auto:auto/video_id")
# should support a b_frames parameter - b_frames=False -> bframes_no
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video',
'video_codec': {'codec': 'h265', 'profile': 'auto',
'level': 'auto', 'b_frames': False}},
expected_url=VIDEO_UPLOAD_PATH + "vc_h265:auto:auto:bframes_no/video_id")
def test_audio_codec(self):
# should support a string value
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'audio_codec': 'acc'},
expected_url=VIDEO_UPLOAD_PATH + "ac_acc/video_id")
def test_bit_rate(self):
# should support an integer value
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'bit_rate': 2048},
expected_url=VIDEO_UPLOAD_PATH + "br_2048/video_id")
# should support "<integer>k"
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'bit_rate': '44k'},
expected_url=VIDEO_UPLOAD_PATH + "br_44k/video_id")
# should support "<integer>m"
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'bit_rate': '1m'},
expected_url=VIDEO_UPLOAD_PATH + "br_1m/video_id")
def test_audio_frequency(self):
# should support an integer value
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'audio_frequency': 44100},
expected_url=VIDEO_UPLOAD_PATH + "af_44100/video_id")
def test_video_sampling(self):
# should support an integer value
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'video_sampling': 20},
expected_url=VIDEO_UPLOAD_PATH + "vs_20/video_id")
# should support an string value in the a form of \"<float>s\"
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'video_sampling': "2.3s"},
expected_url=VIDEO_UPLOAD_PATH + "vs_2.3s/video_id")
def test_start_offset(self):
# should support decimal seconds
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'start_offset': 2.63},
expected_url=VIDEO_UPLOAD_PATH + "so_2.63/video_id")
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'start_offset': '2.63'},
expected_url=VIDEO_UPLOAD_PATH + "so_2.63/video_id")
# should support percents of the video length as "<number>p"
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'start_offset': '35p'},
expected_url=VIDEO_UPLOAD_PATH + "so_35p/video_id")
# should support percents of the video length as "<number>%"
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'start_offset': '35%'},
expected_url=VIDEO_UPLOAD_PATH + "so_35p/video_id")
# should support auto
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'start_offset': 'auto'},
expected_url=VIDEO_UPLOAD_PATH + "so_auto/video_id")
def test_end_offset(self):
# should support decimal seconds
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'end_offset': 2.63},
expected_url=VIDEO_UPLOAD_PATH + "eo_2.63/video_id")
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'end_offset': '2.63'},
expected_url=VIDEO_UPLOAD_PATH + "eo_2.63/video_id")
# should support percents of the video length as "<number>p"
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'end_offset': '35p'},
expected_url=VIDEO_UPLOAD_PATH + "eo_35p/video_id")
# should support percents of the video length as "<number>%"
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'end_offset': '35%'},
expected_url=VIDEO_UPLOAD_PATH + "eo_35p/video_id")
def test_duration(self):
# should support decimal seconds
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'duration': 2.63},
expected_url=VIDEO_UPLOAD_PATH + "du_2.63/video_id")
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'duration': '2.63'},
expected_url=VIDEO_UPLOAD_PATH + "du_2.63/video_id")
# should support percents of the video length as "<number>p"
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'duration': '35p'},
expected_url=VIDEO_UPLOAD_PATH + "du_35p/video_id")
# should support percents of the video length as "<number>%"
self.__test_cloudinary_url(public_id="video_id", options={'resource_type': 'video', 'duration': '35%'},
expected_url=VIDEO_UPLOAD_PATH + "du_35p/video_id")
def test_offset(self):
test_cases = {
'eo_3.21,so_2.66': '2.66..3.21',
'eo_3.22,so_2.67': (2.67, 3.22),
'eo_70p,so_35p': ('35%', '70%'),
'eo_71p,so_36p': ('36p', '71p'),
'eo_70.5p,so_35.5p': ['35.5p', '70.5p']
}
for transformation, offset in test_cases.items():
self.__test_cloudinary_url(
public_id="video_id",
options={'resource_type': 'video', 'offset': offset},
expected_url=VIDEO_UPLOAD_PATH + transformation + "/video_id")
def test_user_agent(self):
with patch('cloudinary.USER_PLATFORM', ''):
agent = cloudinary.get_user_agent()
six.assertRegex(self, agent, r'^CloudinaryPython\/\d\.\d+\.\d+ \(.*; Python \d\.\d+\.\d+\)$')
platform = 'MyPlatform/1.2.3 (Test code)'
with patch('cloudinary.USER_PLATFORM', platform):
result = cloudinary.get_user_agent()
self.assertEqual(result, platform + ' ' + agent)
def test_aspect_ratio(self):
self.__test_cloudinary_url(
public_id="test",
options={"aspect_ratio": "1.0"},
expected_url=DEFAULT_UPLOAD_PATH + "ar_1.0/test")
self.__test_cloudinary_url(
public_id="test",
options={"aspect_ratio": "3:2"},
expected_url=DEFAULT_UPLOAD_PATH + "ar_3:2/test")
self.__test_cloudinary_url(
public_id="test",
options={"aspect_ratio": Fraction(3.0 / 4)},
expected_url=DEFAULT_UPLOAD_PATH + "ar_3:4/test")
def test_overlay_options(self):
tests = [
({'public_id': "logo"}, "logo"),
({'public_id': "folder/logo"}, "folder:logo"),
({'public_id': "logo", 'type': "private"}, "private:logo"),
({'public_id': "logo", 'format': "png"}, "logo.png"),
({'resource_type': "video", 'public_id': "cat"}, "video:cat"),
({'text': "Hello World, Nice to meet you?", 'font_family': "Arial", 'font_size': "18"},
"text:Arial_18:Hello%20World%252C%20Nice%20to%20meet%20you%3F"),
({'text': "Hello World, Nice to meet you?", 'font_family': "Arial", 'font_size': "18",
'font_weight': "bold", 'font_style': "italic", 'letter_spacing': 4,
'line_spacing': 3},
"text:Arial_18_bold_italic_letter_spacing_4_line_spacing_3:Hello%20World"
"%252C%20Nice%20to%20meet%20you%3F"),
({'resource_type': "subtitles", 'public_id': "sample_sub_en.srt"},
"subtitles:sample_sub_en.srt"),
({'text': "Hello World, Nice to meet you?", 'font_family': "Arial", 'font_size': "18",
'font_antialiasing': "best", 'font_hinting': "medium"},
"text:Arial_18_antialias_best_hinting_medium:Hello%20World%252C%20Nice%20to%20meet%20you%3F"),
({'resource_type': "subtitles", 'public_id': "sample_sub_he.srt",
'font_family': "Arial", 'font_size': 40},
"subtitles:Arial_40:sample_sub_he.srt"),
({'url': "https://upload.wikimedia.org/wikipedia/commons/2/2b/고창갯벌.jpg"},
"fetch:aHR0cHM6Ly91cGxvYWQud2lraW1lZGlhLm9yZy93aWtpcGVkaWEvY29tbW9ucy8yLzJiL-qzoOywveqwr-uyjC5qcGc="),
({'url': FETCH_VIDEO_URL, "resource_type": "video"},
"video:fetch:aHR0cHM6Ly9kZW1vLXJlcy5jbG91ZGluYXJ5LmNvbS92aWRlb3MvZG9nLm1wNA==")
]
for options, expected in tests:
result = cloudinary.utils.process_layer(options, "overlay")
self.assertEqual(expected, result)
def test_text_layer_style_identifier_variables(self):
options = {
"transformation": [
{
"variables": [
["$style", "!Arial_12!"],
]
},
{
"overlay": {
"text": "hello-world",
"text_style": "$style"
}
}
]
}
public_id = "sample"
url, _ = cloudinary.utils.cloudinary_url(public_id, **options)
self.assertEqual(DEFAULT_UPLOAD_PATH + "$style_!Arial_12!/l_text:$style:hello-world/sample", url)