-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathuploader_spec.js
More file actions
1721 lines (1656 loc) · 73.7 KB
/
uploader_spec.js
File metadata and controls
1721 lines (1656 loc) · 73.7 KB
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
const https = require('https');
const http = require('http');
const sinon = require('sinon');
const fs = require('fs');
const path = require('path');
const at = require('lodash/at');
const uniq = require('lodash/uniq');
const ClientRequest = require('_http_client').ClientRequest;
const cloudinary = require("../../../../cloudinary");
const helper = require("../../../spechelper");
const describe = require('../../../testUtils/suite');
const cloneDeep = require('lodash/cloneDeep');
const assert = require('assert');
const IMAGE_FILE = helper.IMAGE_FILE;
const LARGE_IMAGE_FILE = helper.LARGE_IMAGE_FILE;
const LARGE_RAW_FILE = helper.LARGE_RAW_FILE;
const LARGE_VIDEO = helper.LARGE_VIDEO;
const EMPTY_IMAGE = helper.EMPTY_IMAGE;
const RAW_FILE = helper.RAW_FILE;
const uploadImage = helper.uploadImage;
const shouldTestAddOn = helper.shouldTestAddOn;
const ADDON_OCR = helper.ADDON_OCR;
const TEST_ID = Date.now();
const METADATA_FIELD_UNIQUE_EXTERNAL_ID = 'metadata_field_external_id_' + TEST_ID;
const METADATA_FIELD_VALUE = 'metadata_field_value_' + TEST_ID;
const METADATA_SAMPLE_DATA = { metadata_color: "red", metadata_shape: "dodecahedron" };
const METADATA_SAMPLE_DATA_ENCODED = "metadata_color=red|metadata_shape=dodecahedron";
const createTestConfig = require('../../../testUtils/createTestConfig');
const testConstants = require('../../../testUtils/testConstants');
const { shouldTestFeature, DYNAMIC_FOLDERS } = require("../../../spechelper");
const allSettled = require('../../../testUtils/helpers/allSettled');
const UPLOADER_V2 = cloudinary.v2.uploader;
const {
TIMEOUT,
TAGS,
TEST_EVAL_STR,
TEST_IMG_WIDTH
} = testConstants;
const {
TEST_TAG,
UPLOAD_TAGS
} = TAGS;
const SAMPLE_IMAGE_URL_1 = "https://res.cloudinary.com/demo/image/upload/sample"
const SAMPLE_IMAGE_URL_2 = "https://res.cloudinary.com/demo/image/upload/car"
const { URL: NodeURL, URLSearchParams: NodeURLSearchParams } = require('url');
let cleanupJsdom;
describe("uploader", function () {
this.timeout(TIMEOUT.LONG);
before(function () {
cleanupJsdom = require('jsdom-global')();
// Keep Node's WHATWG URL constructors even when jsdom is active.
global.URL = NodeURL;
global.URLSearchParams = NodeURLSearchParams;
});
after(function () {
var config = cloudinary.config(true);
if (!(config.api_key && config.api_secret)) {
expect().fail("Missing key and secret. Please set CLOUDINARY_URL.");
}
return allSettled([
!cloudinary.config().keep_test_products ? cloudinary.v2.api.delete_resources_by_tag(TEST_TAG) : void 0,
!cloudinary.config().keep_test_products ? cloudinary.v2.api.delete_resources_by_tag(TEST_TAG,
{
resource_type: "video"
}) : void 0
]).finally(function () {
// Ensure globals don't leak to other test files.
if (typeof cleanupJsdom === 'function') cleanupJsdom();
global.URL = NodeURL;
global.URLSearchParams = NodeURLSearchParams;
});
});
beforeEach(function () {
cloudinary.config(true);
cloudinary.config(createTestConfig());
});
it("should successfully upload file", function () {
this.timeout(TIMEOUT.LONG);
return uploadImage().then(function (result) {
var expected_signature;
expect(result.width).to.eql(241);
expect(result.height).to.eql(51);
expected_signature = cloudinary.utils.api_sign_request({
public_id: result.public_id,
version: result.version
}, cloudinary.config().api_secret);
expect(result.signature).to.eql(expected_signature);
});
});
describe("in-memory uploads", function () {
it("should successfully upload a Buffer", function () {
const buffer = fs.readFileSync(IMAGE_FILE);
return cloudinary.v2.uploader.upload(buffer, {
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.width).to.eql(241);
expect(result.height).to.eql(51);
expect(result.format).to.eql("png");
});
});
it("should successfully upload a Uint8Array", function () {
const uint8Array = new Uint8Array(fs.readFileSync(IMAGE_FILE));
return cloudinary.v2.uploader.upload(uint8Array, {
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.width).to.eql(241);
expect(result.height).to.eql(51);
expect(result.format).to.eql("png");
});
});
it("should successfully upload an ArrayBuffer", function () {
const buffer = fs.readFileSync(IMAGE_FILE);
const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
return cloudinary.v2.uploader.upload(arrayBuffer, {
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.width).to.eql(241);
expect(result.height).to.eql(51);
expect(result.format).to.eql("png");
});
});
it("should successfully upload a Blob", function () {
const buffer = fs.readFileSync(IMAGE_FILE);
const blob = new Blob([buffer], { type: "image/png" });
return cloudinary.v2.uploader.upload(blob, {
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.width).to.eql(241);
expect(result.height).to.eql(51);
expect(result.format).to.eql("png");
});
});
it("should upload a raw buffer when resource_type is raw", function () {
const buffer = fs.readFileSync(RAW_FILE);
return cloudinary.v2.uploader.upload(buffer, {
resource_type: "raw",
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.resource_type).to.eql("raw");
});
});
it("should send buffer uploads without reading from the filesystem", function () {
const buffer = fs.readFileSync(IMAGE_FILE);
return helper.provideMockObjects(async function (mockXHR, writeSpy) {
const createReadStreamSpy = sinon.spy(fs, "createReadStream");
try {
await cloudinary.v2.uploader.upload(buffer, {
filename: "buffer-upload.png",
tags: UPLOAD_TAGS
}).catch(helper.ignoreApiFailure);
sinon.assert.notCalled(createReadStreamSpy);
sinon.assert.calledWith(writeSpy, sinon.match((arg) => Buffer.isBuffer(arg) && arg.equals(buffer)));
sinon.assert.calledWith(writeSpy, sinon.match((arg) => Buffer.isBuffer(arg) && arg.toString("utf8").includes('filename="buffer-upload.png"')));
} finally {
createReadStreamSpy.restore();
}
});
});
});
it("should successfully upload with metadata", function () {
return helper.provideMockObjects(async function (mockXHR, writeSpy, requestSpy) {
await uploadImage({ metadata: METADATA_SAMPLE_DATA }).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(requestSpy, sinon.match({
method: sinon.match("POST")
}));
sinon.assert.calledWith(writeSpy, sinon.match(helper.uploadParamMatcher("metadata", METADATA_SAMPLE_DATA_ENCODED)));
});
});
it('should upload a file with correctly encoded transformation string', () => {
return helper.provideMockObjects(async function (mockXHR, writeSpy, requestSpy) {
await cloudinary.v2.uploader.upload('irrelevant', { transformation: { overlay: { text: 'test / 火' } } }).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(writeSpy, sinon.match(helper.uploadParamMatcher('transformation', 'l_text:test %2F 火')));
});
});
it('should upload a file with correctly encoded transformation string incl 4bytes characters', () => {
return helper.provideMockObjects(async function (mockXHR, writeSpy, requestSpy) {
await cloudinary.v2.uploader.upload('irrelevant', { transformation: { overlay: { text: 'test 𩸽 🍺' } } }).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(writeSpy, sinon.match(helper.uploadParamMatcher('transformation', 'l_text:test 𩸽 🍺')));
});
});
it("should successfully upload url", function () {
return cloudinary.v2.uploader.upload("https://cloudinary.com/images/old_logo.png", {
tags: UPLOAD_TAGS
}).then(function (result) {
var expected_signature;
expect(result.width).to.eql(241);
expect(result.height).to.eql(51);
expected_signature = cloudinary.utils.api_sign_request({
public_id: result.public_id,
version: result.version
}, cloudinary.config().api_secret);
expect(result.signature).to.eql(expected_signature);
});
});
it("should successfully override original_filename", function () {
return cloudinary.v2.uploader.upload("https://cloudinary.com/images/old_logo.png", {
filename_override: 'overridden'
}).then((result) => {
expect(result.original_filename).to.eql('overridden');
});
});
it("Should upload a valid docx file as base64", function () {
let data = 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,UEsDBBQACAgIAI02LlAAAAAAAAAAAAAAAAASAAAAd29yZC9udW1iZXJpbmcueG1spZNNTsMwEIVPwB0i79skFSAUNe2CCjbsgAO4jpNYtT3W2Eno7XGbv1IklIZV5Izf98bj5/X2S8mg5mgF6JTEy4gEXDPIhC5S8vnxsngigXVUZ1SC5ik5cku2m7t1k+hK7Tn6fYFHaJsolpLSOZOEoWUlV9QuwXDtizmgos4vsQgVxUNlFgyUoU7shRTuGK6i6JF0GEhJhTrpEAslGIKF3J0kCeS5YLz79Aqc4ttKdsAqxbU7O4bIpe8BtC2FsT1NzaX5YtlD6r8OUSvZ72vMFLcMaePnrGRr1ABmBoFxa/3fXVsciHE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qihCwGAADbHQAAFQAAAAAAAAAAAAAAAACfDQAAd29yZC90aGVtZS90aGVtZTEueG1sUEsBAhQAFAAICAgAjTYuUDOvD7csAQAALQQAABMAAAAAAAAAAAAAAAAADhQAAFtDb250ZW50X1R5cGVzXS54bWxQSwUGAAAAAAkACQBCAgAAexUAAAAA';
return cloudinary.v2.uploader.upload(data, {
resource_type: 'auto', // this defaults to 'image' if not specified
tags: UPLOAD_TAGS
});
});
it('should allow uploading with parameters containing &', function () {
const publicId = `ampersand-test-${Date.now()}`;
return cloudinary.v2.uploader.upload('https://cloudinary.com/images/old_logo.png', {
notification_url: 'https://example.com?exampleparam1=aaa&exampleparam2=bbb',
public_id: publicId
}).then((result) => {
expect(result).to.have.property('public_id');
expect(result.public_id).to.equal(publicId);
}).catch((error) => {
expect(error).to.be(null);
});
});
it('should allow upload with url safe base64 in overlay', function () {
const overlayUrl = 'https://res.cloudinary.com/demo/image/upload/logos/cloudinary_full_logo_white_small.png';
const baseImageUrl = 'https://cloudinary.com/images/old_logo.png';
const options = { transformation: { overlay: { url: overlayUrl } } };
return cloudinary.v2.uploader.upload(baseImageUrl, options)
.then((result) => {
expect(result).to.have.key("created_at");
});
});
describe("remote urls ", function () {
const mocked = helper.mockTest();
it("should send s3:// URLs to server", async function () {
await cloudinary.v2.uploader.upload("s3://test/1.jpg", {
tags: UPLOAD_TAGS
}).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(mocked.write, sinon.match(helper.uploadParamMatcher('file', "s3://test/1.jpg")));
});
it("should send gs:// URLs to server", async function () {
await cloudinary.v2.uploader.upload("gs://test/1.jpg", {
tags: UPLOAD_TAGS
}).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(mocked.write, sinon.match(helper.uploadParamMatcher('file', "gs://test/1.jpg")));
});
it("should send ftp:// URLs to server", async function () {
await cloudinary.v2.uploader.upload("ftp://test/1.jpg", {
tags: UPLOAD_TAGS
}).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(mocked.write, sinon.match(helper.uploadParamMatcher('file', "ftp://test/1.jpg")));
});
});
describe("rename", function () {
this.timeout(TIMEOUT.LONG);
it("should successfully rename a file", function () {
return uploadImage().then(function (result) {
return cloudinary.v2.uploader.rename(result.public_id, result.public_id + "2").then(function () {
return result.public_id;
});
}).then(function (public_id) {
return cloudinary.v2.api.resource(public_id + "2");
});
});
it("should not rename to an existing public_id", function () {
return Promise.all([uploadImage(), uploadImage()]).then(function (results) {
return cloudinary.v2.uploader.rename(results[0].public_id, results[1].public_id);
}).then(function () {
expect().fail();
}).catch(function (error) {
expect(error).to.be.ok();
});
});
it("should allow to rename to an existing ID, if overwrite is true", function () {
return Promise.all([uploadImage(), uploadImage()]).then(function (results) {
return cloudinary.v2.uploader.rename(results[0].public_id, results[1].public_id, {
overwrite: true
});
}).then(function ({ public_id }) {
return cloudinary.v2.api.resource(public_id);
}).then(function ({ format }) {
expect(format).to.eql("png");
});
});
it('should include tags in rename response if requested explicitly', async () => {
const uploadResult = await cloudinary.v2.uploader.upload(IMAGE_FILE, { context: 'alt=Example|class=Example', tags: ['test-tag'] });
const renameResult = await cloudinary.v2.uploader.rename(uploadResult.public_id, `${uploadResult.public_id}-renamed`, { tags: true, context: true });
expect(renameResult).to.have.property('tags');
expect(renameResult).to.have.property('context');
});
it('should include notification_url in rename response if included in the request', async () => {
return helper.provideMockObjects(async function (mockXHR, writeSpy, requestSpy) {
await cloudinary.v2.uploader.rename('irrelevant', 'irrelevant', { notification_url: 'https://notification-url.com' }).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(writeSpy, sinon.match(helper.uploadParamMatcher('notification_url', 'https://notification-url.com')));
});
});
return context(":invalidate", function () {
var spy, xhr;
spy = void 0;
xhr = void 0;
before(function () {
xhr = sinon.useFakeXMLHttpRequest();
spy = sinon.spy(ClientRequest.prototype, 'write');
});
after(function () {
spy.restore();
return xhr.restore();
});
it("should pass the invalidate value in rename to the server", async function () {
await cloudinary.v2.uploader.rename("first_id", "second_id", {
invalidate: true
}).catch(helper.ignoreApiFailure);
expect(spy.calledWith(sinon.match(function (arg) {
return arg.toString().match(/name="invalidate"/);
}))).to.be.ok();
});
});
});
describe("destroy", function () {
this.timeout(TIMEOUT.MEDIUM);
it("should delete a resource", function () {
var public_id;
return uploadImage().then(function (result) {
public_id = result.public_id;
return cloudinary.v2.uploader.destroy(public_id);
}).then(function (result) {
expect(result.result).to.eql("ok");
return cloudinary.v2.api.resource(public_id);
}).then(function () {
expect().fail();
}).catch(function (error) {
expect(error).to.be.ok();
});
});
it('should pass notification_url', async () => {
return helper.provideMockObjects(async function (mockXHR, writeSpy, requestSpy) {
await cloudinary.v2.uploader.destroy('irrelevant', { notification_url: 'https://notification-url.com' }).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(writeSpy, sinon.match(helper.uploadParamMatcher('notification_url', 'https://notification-url.com')));
});
});
});
it("should support `async` option in explicit api", function () {
return cloudinary.v2.uploader.explicit("sample", {
type: "facebook",
eager: [
{
crop: "scale",
width: "2.0"
}
],
async: true
}).then(function (result) {
expect(result.status).to.eql('pending');
expect(result.resource_type).to.eql('image');
expect(result.type).to.eql('facebook');
expect(result.public_id).to.eql('sample');
});
});
it("should successfully call explicit api", function () {
return cloudinary.v2.uploader.explicit("sample", {
type: "upload",
eager: [
{
crop: "scale",
width: "2.0"
}
]
}).then(function (result) {
var url = cloudinary.utils.url("sample", {
type: "upload",
crop: "scale",
width: "2.0",
format: "jpg",
version: result.version
});
expect(result.eager[0].secure_url).to.eql(url);
});
});
it("should support eager in upload", function () {
this.timeout(TIMEOUT.SHORT);
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
eager: [
{
crop: "scale",
width: "2.0"
}
],
tags: UPLOAD_TAGS
});
});
describe("extra headers", function () {
it("should support extra headers in object format e.g. {Link: \"1\"}", function () {
return helper.provideMockObjects(async function (mockXHR, writeSpy, requestSpy) {
await cloudinary.v2.uploader.upload(IMAGE_FILE, {
extra_headers: {
Link: "1"
}
}).catch(helper.ignoreApiFailure);
assert.ok(requestSpy.args[0][0].headers.Link);
assert.equal(requestSpy.args[0][0].headers.Link, "1");
});
});
});
describe("text images", function () {
it("should successfully generate text image", function () {
return cloudinary.v2.uploader.text("hello world", {
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.width).to.within(50, 70);
expect(result.height).to.within(5, 15);
});
});
var mocked = helper.mockTest();
it("should pass text image parameters to server", function () {
cloudinary.v2.uploader.text("hello word",
{
font_family: "Arial",
font_size: 12,
font_weight: "black"
});
sinon.assert.calledWith(mocked.write, sinon.match(helper.uploadParamMatcher("font_family", "Arial")));
sinon.assert.calledWith(mocked.write, sinon.match(helper.uploadParamMatcher("font_size", "12")));
sinon.assert.calledWith(mocked.write, sinon.match(helper.uploadParamMatcher("font_weight", "black")));
});
});
it("should successfully upload stream", function (done) {
var file_reader, stream;
stream = cloudinary.v2.uploader.upload_stream({
tags: UPLOAD_TAGS
}, function (error, result) {
var expected_signature;
expect(result.width).to.eql(241);
expect(result.height).to.eql(51);
expected_signature = cloudinary.utils.api_sign_request({
public_id: result.public_id,
version: result.version
}, cloudinary.config().api_secret);
expect(result.signature).to.eql(expected_signature);
done();
});
file_reader = fs.createReadStream(IMAGE_FILE, {
encoding: 'binary'
});
file_reader.on('data', function (chunk) {
stream.write(chunk, 'binary');
});
file_reader.on('end', function () {
stream.end();
});
});
describe("tags", function () {
this.timeout(TIMEOUT.MEDIUM);
it("should add tags to existing resources", function () {
return uploadImage().then(function (result) {
return uploadImage().then(function (res) {
return [result.public_id, res.public_id];
});
}).then(function ([firstId, secondId]) {
return cloudinary.v2.uploader.add_tag("tag1", [firstId, secondId]).then(function () {
return [firstId, secondId];
});
}).then(function ([firstId, secondId]) {
return cloudinary.v2.api.resource(secondId).then(function (r1) {
expect(r1.tags).to.contain("tag1");
}).then(function () {
return [firstId, secondId];
});
}).then(function ([firstId, secondId]) {
return cloudinary.v2.uploader.remove_all_tags([firstId, secondId, 'noSuchId']).then(function (result) {
return [firstId, secondId, result];
});
}).then(function ([firstId, secondId, result]) {
expect(result.public_ids).to.contain(firstId);
expect(result.public_ids).to.contain(secondId);
expect(result.public_ids).to.not.contain('noSuchId');
});
});
it("should keep existing tags when adding a new tag", function () {
return uploadImage().then(function (result) {
return cloudinary.v2.uploader.add_tag("tag1", result.public_id).then(function () {
return result.public_id;
});
}).then(function (publicId) {
return cloudinary.v2.uploader.add_tag("tag2", publicId).then(function () {
return publicId;
});
}).then(function (publicId) {
return cloudinary.v2.api.resource(publicId);
}).then(function (result) {
expect(result.tags).to.contain("tag1").and.contain("tag2");
});
});
it("should replace existing tag", function () {
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
tags: ["tag1", "tag2", TEST_TAG]
}).then(function (result) {
var public_id = result.public_id;
return cloudinary.v2.uploader.replace_tag("tag3Å", public_id).then(function () {
return public_id;
});
}).then(function (public_id) { // TODO this also tests non ascii characters
return cloudinary.v2.api.resource(public_id);
}).then(function (result) {
expect(result.tags).to.eql(["tag3Å"]);
});
});
});
describe("context", function () {
this.timeout(TIMEOUT.MEDIUM);
before(async function () {
const [result1, result2] = await Promise.all([uploadImage(), uploadImage()]);
this.first_id = result1.public_id;
this.second_id = result2.public_id;
});
it("should add context to existing resources", function () {
return cloudinary.v2.uploader
.add_context('alt=testAlt|custom=testCustom', [this.first_id, this.second_id])
.then(() => cloudinary.v2.uploader.add_context({
alt2: "testAlt2",
custom2: "testCustom2"
}, [this.first_id, this.second_id]))
.then(() => cloudinary.v2.api.resource(this.second_id))
.then(({ context }) => {
expect(context.custom.alt).to.equal('testAlt');
expect(context.custom.alt2).to.equal('testAlt2');
expect(context.custom.custom).to.equal('testCustom');
expect(context.custom.custom2).to.equal('testCustom2');
return cloudinary.v2.uploader.remove_all_context([this.first_id, this.second_id, 'noSuchId']);
}).then(({ public_ids }) => {
expect(public_ids).to.contain(this.first_id);
expect(public_ids).to.contain(this.second_id);
expect(public_ids).to.not.contain('noSuchId');
return cloudinary.v2.api.resource(this.second_id);
}).then(function ({ context }) {
expect(context).to.be(void 0);
});
});
it("should upload with context containing reserved characters", function () {
var context = {
key1: 'value1',
key2: 'valu\e2',
key3: 'val=u|e3',
key4: 'val\=ue'
};
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
context: context
}).then(function (result) {
return cloudinary.v2.api.resource(result.public_id, {
context: true
});
}).then(function (result) {
expect(result.context.custom).to.eql(context);
});
});
});
it("should support timeouts", function () {
// testing a 1ms timeout, nobody is that fast.
return cloudinary.v2.uploader.upload("https://cloudinary.com/images/old_logo.png", {
timeout: 1,
tags: UPLOAD_TAGS
}).then(function () {
expect().fail();
}).catch(function ({ error }) {
expect(error.http_code).to.eql(499);
expect(error.message).to.eql("Request Timeout");
});
});
it("should upload a file and base public id on the filename if use_filename is set to true", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
use_filename: true,
tags: UPLOAD_TAGS
}).then(function ({ public_id }) {
expect(public_id).to.match(/logo_[a-zA-Z0-9]{6}/);
});
});
it("should upload a file and set the filename as the public_id if use_filename is set to true and unique_filename is set to false", function () {
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
use_filename: true,
unique_filename: false,
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.public_id).to.eql("logo");
});
});
describe("allowed_formats", function () {
it("should allow whitelisted formats", function () {
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
allowed_formats: ["png"],
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.format).to.eql("png");
});
});
it("should prevent non whitelisted formats from being uploaded", function () {
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
allowed_formats: ["jpg"],
tags: UPLOAD_TAGS
}).then(function () {
expect().fail();
}).catch(function (error) {
expect(error.http_code).to.eql(400);
});
});
it("should allow non whitelisted formats if type is specified and convert to that type", function () {
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
allowed_formats: ["jpg"],
format: "jpg",
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.format).to.eql("jpg");
});
});
});
it("should allow sending face coordinates", function () {
var coordinates, custom_coordinates, different_coordinates, out_coordinates;
this.timeout(TIMEOUT.LONG);
coordinates = [[120, 30, 109, 150], [121, 31, 110, 151]];
out_coordinates = [
[120,
30,
109,
51],
[
121,
31,
110,
51 // coordinates are limited to the image dimensions
]
];
different_coordinates = [[122, 32, 111, 152]];
custom_coordinates = [1, 2, 3, 4];
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
face_coordinates: coordinates,
faces: true,
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.faces).to.eql(out_coordinates);
return cloudinary.v2.uploader.explicit(result.public_id, {
faces: true,
face_coordinates: different_coordinates,
custom_coordinates: custom_coordinates,
type: "upload"
});
}).then(function (result) {
expect(result.faces).not.to.be(void 0);
return cloudinary.v2.api.resource(result.public_id, {
faces: true,
coordinates: true
});
}).then(function (info) {
expect(info.faces).to.eql(different_coordinates);
expect(info.coordinates).to.eql({
faces: different_coordinates,
custom: [custom_coordinates]
});
});
});
it("should allow sending context", function () {
this.timeout(TIMEOUT.LONG);
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
context: {
caption: "some caption",
alt: "alternative"
},
tags: UPLOAD_TAGS
}).then(function ({ public_id }) {
return cloudinary.v2.api.resource(public_id, {
context: true
});
}).then(function ({ context }) {
expect(context.custom.caption).to.eql("some caption");
expect(context.custom.alt).to.eql("alternative");
});
});
it("should support requesting manual moderation", function () {
this.timeout(TIMEOUT.LONG);
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
moderation: "manual",
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result.moderation[0].status).to.eql("pending");
expect(result.moderation[0].kind).to.eql("manual");
});
});
it("should support requesting raw conversion", function () {
return cloudinary.v2.uploader.upload(RAW_FILE, {
raw_convert: "illegal",
resource_type: "raw",
tags: UPLOAD_TAGS
}).then(function () {
expect().fail();
}).catch(function (error) {
expect(error != null).to.be(true);
expect(error.message).to.contain("Raw convert is invalid");
});
});
it("should support requesting categorization", function () {
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
categorization: "illegal",
tags: UPLOAD_TAGS
}).then(function () {
expect().fail();
}).catch(function (error) {
expect(error != null).to.be(true);
});
});
it("should support requesting detection", function () {
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
detection: "illegal",
tags: UPLOAD_TAGS
}).then(function () {
expect().fail();
}).catch(function (error) {
expect(error).not.to.be(void 0);
expect(error.message).to.contain("Detection invalid model 'illegal'");
});
});
it("should support requesting background_removal", function () {
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
background_removal: "illegal",
tags: UPLOAD_TAGS
}).then(function () {
expect().fail();
}).catch(function (error) {
expect(error != null).to.be(true);
expect(error.message).to.contain("is invalid");
});
});
it("should support requesting analysis", function () {
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
quality_analysis: true,
tags: UPLOAD_TAGS
}).then(function (result) {
expect(result).to.have.key("quality_analysis");
});
});
describe('when passing visual_search in parameters', () => {
var spy, xhr;
spy = void 0;
xhr = void 0;
before(function () {
xhr = sinon.useFakeXMLHttpRequest();
spy = sinon.spy(ClientRequest.prototype, 'write');
});
after(function () {
spy.restore();
return xhr.restore();
});
it('should pass its value to the upload api', () => {
cloudinary.v2.uploader.upload(IMAGE_FILE, {
visual_search: true
});
expect(spy.calledWith(sinon.match((arg) => {
return arg.toString().match(/visual_search=true/);
})));
});
});
describe('when passing on_success in parameters', () => {
var spy, xhr;
spy = void 0;
xhr = void 0;
before(function () {
xhr = sinon.useFakeXMLHttpRequest();
spy = sinon.spy(ClientRequest.prototype, 'write');
});
after(function () {
spy.restore();
return xhr.restore();
});
it('should pass its value to the upload api', async () => {
await cloudinary.v2.uploader.upload(IMAGE_FILE, {
on_success: 'current_asset.update({tags: ["autocaption"]});'
}).catch(helper.ignoreApiFailure);
expect(spy.calledWith(sinon.match((arg) => {
return arg.toString().match(/on_success='current_asset.update({tags: ["autocaption"]});'/);
})));
});
});
describe("upload_chunked", function () {
this.timeout(TIMEOUT.LONG * 10);
it("should specify chunk size", function (done) {
return fs.stat(LARGE_RAW_FILE, function (err, stat) {
cloudinary.v2.uploader.upload_large(LARGE_RAW_FILE, {
chunk_size: 7000000,
timeout: TIMEOUT.LONG,
tags: UPLOAD_TAGS
}, function (error, result) {
if (error != null) {
done(new Error(error.message));
}
expect(result.bytes).to.eql(stat.size);
expect(result.etag).to.eql("4c13724e950abcb13ec480e10f8541f5");
return done();
});
});
});
it("should return error if value is less than 5MB", function (done) {
fs.stat(LARGE_RAW_FILE, function (err, stat) {
cloudinary.v2.uploader.upload_large(LARGE_RAW_FILE, {
chunk_size: 40000,
tags: UPLOAD_TAGS,
disable_promises: true
}, function (error, result) {
expect(error.message).to.eql("All parts except EOF-chunk must be larger than 5mb");
done();
});
});
});
it("should use file name", function (done) {
fs.stat(LARGE_RAW_FILE, function (err, stat) {
return cloudinary.v2.uploader.upload_large(LARGE_RAW_FILE, {
use_filename: true
}, function (error, result) {
if (error != null) {
done(new Error(error.message));
}
expect(result.public_id).to.match(/TheCompleteWorksOfShakespeare_[a-zA-Z0-9]{6}/);
done();
});
});
});
it("should support uploading a small raw file", function (done) {
fs.stat(RAW_FILE, function (err, stat) {
cloudinary.v2.uploader.upload_large(RAW_FILE, {
tags: UPLOAD_TAGS
}, function (error, result) {
if (error != null) {
done(new Error(error.message));
}
expect(result.bytes).to.eql(stat.size);
expect(result.etag).to.eql("ffc265d8d1296247972b4d478048e448");
done();
});
});
});
it("should add original filename on upload large", function (done) {
fs.stat(RAW_FILE, function (err, stat) {
cloudinary.v2.uploader.upload_large(RAW_FILE, {
filename: 'my_file_name'
}, function (error, result) {
if (error != null) {
done(new Error(error.message));
}
expect(result.original_filename).to.eql('my_file_name');
done();
});
});
});
it("should support uploading a small image file", function (done) {
fs.stat(IMAGE_FILE, function (err, stat) {
return cloudinary.v2.uploader.upload_chunked(IMAGE_FILE, {
tags: UPLOAD_TAGS
}, function (error, result) {
if (error != null) {
done(new Error(error.message));
}
expect(result.bytes).to.eql(stat.size);
expect(result.etag).to.eql("7dc60722d4653261648038b579fdb89e");
done();
});
});
});
it("should support uploading large video files", function (done) {
var stat, writeSpy;
this.timeout(TIMEOUT.LONG * 10);
writeSpy = sinon.spy(ClientRequest.prototype, 'write');
stat = fs.statSync(LARGE_VIDEO);
expect(stat).to.be.ok();
cloudinary.v2.uploader.upload_chunked(LARGE_VIDEO, {
chunk_size: 6000000,
resource_type: 'video',
timeout: TIMEOUT.LONG * 10,
tags: UPLOAD_TAGS
}, function (err, result) {
var timestamps;
try {
expect(result.bytes).to.eql(stat.size);
expect(result.etag).to.eql("ff6c391d26be0837ee5229885b5bd571");
timestamps = writeSpy.args.map(function (a) {
return a[0].toString();
}).filter(function (p) {
return p.match(/timestamp/);
}).map(function (p) {
return p.match(/"timestamp"\s+(\d+)/)[1];
});
expect(timestamps.length).to.be.greaterThan(1);
expect(uniq(timestamps)).to.eql(uniq(timestamps)); // uniq b/c last timestamp may be duplicated
} finally {
writeSpy.restore();
done();
}
});
});
it("should update timestamp for each chunk", function (done) {
var writeSpy = sinon.spy(ClientRequest.prototype, 'write');
cloudinary.v2.uploader.upload_chunked(LARGE_VIDEO, {
chunk_size: 6000000,
resource_type: 'video',
timeout: TIMEOUT.LONG * 10,
tags: UPLOAD_TAGS
}, function () {
var timestamps = writeSpy.args.map(function (a) {
return a[0].toString();
}).filter(function (p) {
return p.match(/timestamp/);
}).map(function (p) {
return p.match(/"timestamp"\s+(\d+)/)[1];
});
try {
expect(timestamps.length).to.be.greaterThan(1);
expect(uniq(timestamps)).to.eql(uniq(timestamps));
} finally {
writeSpy.restore();
done();
}
});
});
it("should support uploading based on a url", function (done) {
this.timeout(TIMEOUT.MEDIUM);
cloudinary.v2.uploader.upload_large("https://cloudinary.com/images/old_logo.png", {
tags: UPLOAD_TAGS
}, function (error, result) {
if (error != null) {
done(new Error(error.message));
}
expect(result.etag).to.eql("7dc60722d4653261648038b579fdb89e");
done();
});
});
});
describe("dynamic folders", () => {
const mocked = helper.mockTest();
it('should pass dynamic folder params', async () => {
const public_id_prefix = "fd_public_id_prefix";
const asset_folder = "asset_folder";
const display_name = "display_name";
const use_filename_as_display_name = true;
const folder = "folder/test";
await UPLOADER_V2.upload(IMAGE_FILE, {
public_id_prefix,
asset_folder,
display_name,
use_filename_as_display_name,
folder
}).catch(helper.ignoreApiFailure);
sinon.assert.calledWithMatch(mocked.write, helper.uploadParamMatcher("public_id_prefix", public_id_prefix));
sinon.assert.calledWithMatch(mocked.write, helper.uploadParamMatcher("asset_folder", asset_folder));
sinon.assert.calledWithMatch(mocked.write, helper.uploadParamMatcher("display_name", display_name));
sinon.assert.calledWithMatch(mocked.write, helper.uploadParamMatcher("use_filename_as_display_name", 1));
sinon.assert.calledWithMatch(mocked.write, helper.uploadParamMatcher("folder", folder));
});
it('should not contain asset_folder in public_id', async function () {
if (!shouldTestFeature(DYNAMIC_FOLDERS)) {
this.skip();
}
const asset_folder = "asset_folder";
return UPLOADER_V2.upload(IMAGE_FILE, {
asset_folder
}).then((result) => {
expect(result.public_id).to.not.contain('asset_folder')
});
});
it('should not contain asset_folder in public_id when use_asset_folder_as_public_id_prefix is false', async function () {
if (!shouldTestFeature(DYNAMIC_FOLDERS)) {
this.skip();
}
const asset_folder = "asset_folder";
return UPLOADER_V2.upload(IMAGE_FILE, {
asset_folder,
use_asset_folder_as_public_id_prefix: false
}).then((result) => {
expect(result.public_id).to.not.contain('asset_folder')
});
});