-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathapi_spec.js
More file actions
1622 lines (1554 loc) · 63.6 KB
/
api_spec.js
File metadata and controls
1622 lines (1554 loc) · 63.6 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 sinon = require('sinon');
const formatDate = require("date-fns").format;
const subDate = require("date-fns").sub;
const https = require('https');
const ClientRequest = require('_http_client').ClientRequest;
const Q = require('q');
const cloudinary = require("../../../../cloudinary");
const helper = require("../../../spechelper");
const describe = require('../../../testUtils/suite');
const wait = require('../../../testUtils/helpers/wait');
const uploadImage = helper.uploadImage;
const shouldTestAddOn = helper.shouldTestAddOn;
const ADDON_OCR = helper.ADDON_OCR;
const callReusableTest = require('../../../testUtils/reusableTests/reusableTests').callReusableTest;
const testConstants = require('../../../testUtils/testConstants');
const retry = require('../../../testUtils/helpers/retry');
const {shouldTestFeature} = require("../../../spechelper");
const API_V2 = cloudinary.v2.api;
const DYNAMIC_FOLDERS = helper.DYNAMIC_FOLDERS;
const assert = require('assert');
const {only} = require("../../../../lib/utils");
const {
TIMEOUT,
TAGS,
PUBLIC_IDS,
UNIQUE_JOB_SUFFIX_ID,
PRESETS,
TRANSFORMATIONS,
PUBLIC_ID_PREFIX,
UNIQUE_TEST_FOLDER,
TEST_EVAL_STR
} = testConstants;
const {
PUBLIC_ID,
PUBLIC_ID_1,
PUBLIC_ID_2,
PUBLIC_ID_3,
PUBLIC_ID_4,
PUBLIC_ID_5,
PUBLIC_ID_6,
PUBLIC_ID_BACKUP_1,
PUBLIC_ID_BACKUP_2,
PUBLIC_ID_OCR_1
} = PUBLIC_IDS;
const {
TEST_TAG,
UPLOAD_TAGS
} = TAGS;
const {
NAMED_TRANSFORMATION,
NAMED_TRANSFORMATION2,
EXPLICIT_TRANSFORMATION_NAME,
EXPLICIT_TRANSFORMATION_NAME2
} = TRANSFORMATIONS;
const {
API_TEST_UPLOAD_PRESET1,
API_TEST_UPLOAD_PRESET2,
API_TEST_UPLOAD_PRESET3,
API_TEST_UPLOAD_PRESET4
} = PRESETS;
const EXPLICIT_TRANSFORMATION = {
width: 100,
crop: "scale",
overlay: `text:Arial_60:${TEST_TAG}`
};
const EXPLICIT_TRANSFORMATION2 = {
width: 200,
crop: "scale",
overlay: `text:Arial_60:${TEST_TAG}`
};
const METADATA_EXTERNAL_ID = "metadata_external_id_" + TEST_TAG;
const METADATA_DEFAULT_VALUE = "metadata_default_value_" + TEST_TAG;
function getAllTags({resources}) {
return resources
.map(e => e.tags)
.reduce(((a, b) => a.concat(b)), []);
}
function findByAttr(elements, attr, value) {
return elements.find(element => element[attr] === value);
}
describe("api", function () {
var contextKey = `test-key${UNIQUE_JOB_SUFFIX_ID}`;
before(async function () {
this.timeout(TIMEOUT.LONG);
await cloudinary.v2.api.add_metadata_field({
external_id: METADATA_EXTERNAL_ID,
label: METADATA_EXTERNAL_ID,
type: 'string',
default_value: METADATA_DEFAULT_VALUE
});
await Q.all([
uploadImage({
public_id: PUBLIC_ID,
tags: UPLOAD_TAGS,
context: "key=value",
eager: [EXPLICIT_TRANSFORMATION]
}),
uploadImage({
public_id: PUBLIC_ID_2,
tags: UPLOAD_TAGS,
context: "key=value",
eager: [EXPLICIT_TRANSFORMATION]
}),
uploadImage({
public_id: PUBLIC_ID_5,
tags: UPLOAD_TAGS,
context: `${contextKey}=test`,
eager: [EXPLICIT_TRANSFORMATION]
}),
uploadImage({
public_id: PUBLIC_ID_6,
tags: UPLOAD_TAGS,
context: `${contextKey}=alt-test`,
eager: [EXPLICIT_TRANSFORMATION]
})
]);
});
after(function () {
var config = cloudinary.config(true);
this.timeout(TIMEOUT.LONG);
if (config.keep_test_products) {
return Promise.resolve();
}
if (!(config.api_key && config.api_secret)) {
expect().fail("Missing key and secret. Please set CLOUDINARY_URL.");
}
return Q.allSettled([
cloudinary.v2.api.delete_metadata_field(METADATA_EXTERNAL_ID),
cloudinary.v2.api.delete_resources_by_tag(TEST_TAG),
cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET1),
cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET2),
cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET3),
cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET4)
]);
});
describe("resources", function () {
callReusableTest("a list with a cursor", cloudinary.v2.api.resources);
callReusableTest("a list with a cursor", cloudinary.v2.api.resources_by_tag, TEST_TAG);
it("should allow listing resource_types", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.resource_types().then(function (result) {
expect(result.resource_types).to.contain("image");
});
});
it("should allow listing resources", function () {
var publicId;
this.timeout(TIMEOUT.MEDIUM);
publicId = '';
return uploadImage({
tags: UPLOAD_TAGS
}).then(function (result) {
publicId = result.public_id;
return cloudinary.v2.api.resources();
}).then(function (result) {
let resource = findByAttr(result.resources, "public_id", publicId);
expect(resource).not.to.eql(void 0);
expect(resource.type).to.eql("upload");
});
});
it("should allow listing resources with metadata", async function () {
this.timeout(TIMEOUT.MEDIUM);
await retry(async function () {
let result = await cloudinary.v2.api.resources({
type: "upload",
prefix: PUBLIC_ID,
metadata: true
});
result.resources.forEach((resource) => {
expect(resource).to.have.property('metadata');
});
result = await cloudinary.v2.api.resources({
type: "upload",
prefix: PUBLIC_ID,
metadata: false
});
result.resources.forEach((resource) => {
expect(resource).to.not.have.property('metadata');
});
});
});
it("should allow listing resources by tag with metadata", async function () {
this.timeout(TIMEOUT.MEDIUM);
await retry(async function () {
let result = await cloudinary.v2.api.resources_by_tag(TEST_TAG, {
metadata: true
});
result.resources.forEach((resource) => {
expect(resource).to.have.property('metadata');
});
result = await cloudinary.v2.api.resources_by_tag(TEST_TAG, {
metadata: false
});
result.resources.forEach((resource) => {
expect(resource).to.not.have.property('metadata');
});
});
});
it("should allow listing resources by context with metadata", async function () {
this.timeout(TIMEOUT.MEDIUM);
await retry(async function () {
let result = await cloudinary.v2.api.resources_by_context(contextKey, null, {
metadata: true
});
result.resources.forEach((resource) => {
expect(resource).to.have.property('metadata');
});
result = await cloudinary.v2.api.resources_by_context(contextKey, null, {
metadata: false
});
result.resources.forEach((resource) => {
expect(resource).to.not.have.property('metadata');
});
});
});
it("should allow listing resources by moderation with metadata", async function () {
this.timeout(TIMEOUT.MEDIUM);
const moderation = "manual";
const status = "pending";
await uploadImage({
moderation,
tags: [TEST_TAG]
});
let result = await cloudinary.v2.api.resources_by_moderation(moderation, status, {
metadata: true
});
result.resources.forEach((resource) => {
expect(resource).to.have.property('metadata');
});
result = await cloudinary.v2.api.resources_by_moderation(moderation, status, {
metadata: false
});
result.resources.forEach((resource) => {
expect(resource).to.not.have.property('metadata');
});
});
it("should allow listing resources by type", function () {
this.timeout(TIMEOUT.MEDIUM);
return uploadImage({
tags: UPLOAD_TAGS
}).then(
({public_id}) => cloudinary.v2.api.resources({type: "upload"})
.then(result => [public_id, result])
.then(([resources_public_id, result]) => {
let resource = findByAttr(result.resources, "public_id", resources_public_id);
expect(resource).to.be.an(Object);
expect(resource.type).to.eql("upload");
}));
});
it("should allow listing resources by prefix", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.resources({
type: "upload",
prefix: PUBLIC_ID_PREFIX,
max_results: 500
}).then(function (result) {
let public_ids = result.resources.map(resource => resource.public_id);
public_ids.forEach((id) => {
expect(id.indexOf(PUBLIC_ID_PREFIX)).to.be(0)
});
});
});
it("should allow listing resources by tag", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.resources_by_tag(TEST_TAG, {
context: true,
tags: true,
max_results: 500
}).then((result) => {
expect(result.resources.map(e => e.public_id))
.to.contain(PUBLIC_ID).and.contain(PUBLIC_ID_2);
expect(getAllTags(result)).to.contain(TEST_TAG);
expect(result.resources.map(e => e.context && e.context.custom.key))
.to.contain("value");
});
});
it("should allow listing resources by context only", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.resources_by_context(contextKey, null)
.then(result => expect(result.resources).to.have.length(2));
});
it("should allow listing resources by context key and value", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.resources_by_context(contextKey, "test").then(function (result) {
expect(result.resources).to.have.length(1);
});
});
it("should allow get resource details by asset id", async () => {
const {asset_id} = await uploadImage({tags: TEST_TAG})
const resource = await API_V2.resource_by_asset_id(asset_id)
expect(resource).not.to.be.empty();
expect(resource.asset_id).to.equal(asset_id);
expect(resource).not.to.have.property('accessibility_analysis');
expect(resource).not.to.have.property('colors');
expect(resource).not.to.have.property('exif');
expect(resource).not.to.have.property('faces');
});
it("should allow get resource details by asset id including explicitly requested properties", async () => {
const {asset_id} = await uploadImage({tags: TEST_TAG})
const resource = await API_V2.resource_by_asset_id(asset_id, {
colors: true,
faces: true,
exif: true,
related: true
});
expect(resource).not.to.be.empty();
expect(resource.asset_id).to.equal(asset_id);
expect(resource).to.have.property('colors');
expect(resource).to.have.property('exif');
expect(resource).to.have.property('faces');
expect(resource).to.have.property('related_assets');
});
it('should allow listing resources by asset ids', async () => {
this.timeout(TIMEOUT.MEDIUM);
const uploads = await Promise.all([uploadImage({tags: TEST_TAG}), uploadImage({tags: TEST_TAG})]);
const assetIds = uploads.map(item => item.asset_id);
const publicIds = uploads.map(item => item.public_id);
const {resources} = await API_V2.resources_by_asset_ids(assetIds);
expect(resources).not.to.be.empty();
expect(resources.length).to.eql(2);
expect(publicIds).to.contain(resources[0].public_id);
expect(publicIds).to.contain(resources[1].public_id);
});
it("should allow listing resources by public ids", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.resources_by_ids([PUBLIC_ID, PUBLIC_ID_2], {
context: true,
tags: true
}).then((result) => {
expect(result.resources.map(e => e.public_id).sort()).to.eql([PUBLIC_ID, PUBLIC_ID_2]);
expect(getAllTags(result)).to.contain(TEST_TAG);
expect(result.resources.map(e => e.context.custom.key)).to.contain("value");
});
});
it("should allow listing resources specifying direction", function () {
this.timeout(TIMEOUT.LONG);
Q.all(
cloudinary.v2.api.resources_by_tag(TEST_TAG, {
type: "upload",
max_results: 500,
direction: "asc"
}),
cloudinary.v2.api.resources_by_tag(TEST_TAG, {
type: "upload",
max_results: 500,
direction: "desc"
})
).then(([resultAsc, resultDesc]) => [
resultAsc.resources.map(r => r.public_id),
resultDesc.resources.map(r => r.public_id)
]).then(([asc, desc]) => expect(asc.reverse()).to.eql(desc));
});
it("should allow listing resources by start_at", function () {
let start_at = new Date().toString();
helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.resources({
type: "upload",
start_at: start_at,
direction: "asc"
});
sinon.assert.calledWith(requestSpy, sinon.match({
query: sinon.match(`start_at=${encodeURIComponent(start_at)}`)
}));
});
});
it("should allow get resource metadata", function () {
this.timeout(TIMEOUT.LONG);
return uploadImage({
tags: UPLOAD_TAGS,
eager: [EXPLICIT_TRANSFORMATION]
}).then(({public_id}) => cloudinary.v2.api.resource(public_id)
.then(resource => [public_id, resource]))
.then(([public_id, resource]) => {
expect(resource).not.to.eql(void 0);
expect(resource.public_id).to.eql(public_id);
expect(resource.bytes).to.eql(3381);
expect(resource.derived).to.have.length(1);
});
});
it("should allow get resource details by public id including explicitly requested properties", async function () {
// Get a resource and include a cinemagraph analysis value in the response
const result = await API_V2.resource(PUBLIC_ID, {
cinemagraph_analysis: true,
related: true
});
// Ensure result includes a cinemagraph_analysis with a cinemagraph_score
expect(result).not.to.be.empty();
expect(result.cinemagraph_analysis).to.be.an("object");
expect(result.cinemagraph_analysis).to.have.property("cinemagraph_score");
expect(result).to.have.property("related_assets");
});
describe("derived pagination", function () {
it("should send the derived_next_cursor to the server", function () {
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.resource(PUBLIC_ID, {derived_next_cursor: 'aaa'});
return sinon.assert.calledWith(
requestSpy, sinon.match(sinon.match({
query: sinon.match('derived_next_cursor=aaa')
}, 'derived_next_cursor=aaa')));
});
});
});
it("should send `accessibility_analysis` param to the server", function () {
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.resource(PUBLIC_ID, {accessibility_analysis: true});
return sinon.assert.calledWith(requestSpy, sinon.match({
query: sinon.match(helper.apiParamMatcher("accessibility_analysis", "true"))
}));
});
});
describe('selective response', () => {
const expectedKeys = ['public_id', 'asset_id', 'folder', 'tags'].sort();
it('should allow listing', async () => {
const {resources} = await cloudinary.v2.api.resources({fields: ['tags']})
const actualKeys = Object.keys(resources[0]);
assert.deepStrictEqual(actualKeys.sort(), expectedKeys);
});
it('should allow listing by public_ids', async () => {
const {resources} = await cloudinary.v2.api.resources_by_ids([PUBLIC_ID], {fields: ['tags']})
const actualKeys = Object.keys(resources[0]);
assert.deepStrictEqual(actualKeys.sort(), expectedKeys);
});
it('should allow listing by tag', async () => {
const {resources} = await cloudinary.v2.api.resources_by_tag(TEST_TAG, {fields: ['tags']})
const actualKeys = Object.keys(resources[0]);
assert.deepStrictEqual(actualKeys.sort(), expectedKeys);
});
it('should allow listing by context', async () => {
const {resources} = await cloudinary.v2.api.resources_by_context(contextKey, "test", {fields: ['tags']})
const actualKeys = Object.keys(resources[0]);
assert.deepStrictEqual(actualKeys.sort(), expectedKeys);
});
it('should allow listing by moderation', async () => {
await uploadImage({
moderation: 'manual',
tags: [TEST_TAG]
});
const {resources} = await cloudinary.v2.api.resources_by_moderation('manual', 'pending', {fields: ['tags']})
const actualKeys = Object.keys(resources[0]);
assert.deepStrictEqual(actualKeys.sort(), expectedKeys);
});
it('should allow listing by asset_ids', async () => {
const {asset_id} = await uploadImage();
const {resources} = await cloudinary.v2.api.resources_by_asset_ids([asset_id], {fields: ['tags']})
const actualKeys = Object.keys(resources[0]);
assert.deepStrictEqual(actualKeys.sort(), expectedKeys);
});
});
});
describe("backup resource", function () {
this.timeout(TIMEOUT.MEDIUM);
const publicId = "api_test_backup" + UNIQUE_JOB_SUFFIX_ID;
before(() => uploadImage({
public_id: publicId,
backup: true
}).then(() => cloudinary.v2.api.resource(publicId)).then((resource) => {
expect(resource).not.to.be(null);
}));
after(function () {
return cloudinary.v2.api.delete_resources(publicId).then((response) => {
expect(response).to.have.property("deleted");
});
});
it("should return the asset details together with all of its backed up versions when versions is true", function () {
return cloudinary.v2.api.resource(publicId, {versions: true})
.then((resource) => {
expect(resource.versions).to.be.an('array');
});
});
it("should return the asset details together without backed up versions when versions is false", function () {
return cloudinary.v2.api.resource(publicId, {versions: false})
.then((resource) => {
expect(resource.versions).to.be(undefined);
});
});
});
describe("delete", function () {
it("should allow deleting derived resource", function () {
this.timeout(TIMEOUT.MEDIUM);
return uploadImage({
tags: UPLOAD_TAGS,
eager: [
{
width: 101,
crop: "scale"
}
]
}).then(wait(2000)).then(
({public_id}) => cloudinary.v2.api.resource(public_id)
.then(resource => [public_id, resource])
).then(([public_id, resource]) => {
expect(resource).not.to.eql(void 0);
expect(resource.bytes).to.eql(3381);
expect(resource.derived).to.have.length(1);
let derived_resource_id = resource.derived[0].id;
// ignore results and pass-through the public_id
return cloudinary.v2.api.delete_derived_resources(derived_resource_id)
.then(() => public_id);
}).then(public_id => cloudinary.v2.api.resource(public_id))
.then((resource) => {
expect(resource).not.to.eql(void 0);
expect(resource.derived).to.have.length(0);
});
});
it("should allow deleting derived resources by transformations", function () {
this.timeout(TIMEOUT.LARGE);
return Q.all([
uploadImage({
public_id: PUBLIC_ID_1,
tags: UPLOAD_TAGS,
eager: [EXPLICIT_TRANSFORMATION]
}),
uploadImage({
public_id: PUBLIC_ID_2,
tags: UPLOAD_TAGS,
eager: [EXPLICIT_TRANSFORMATION2]
}),
uploadImage({
public_id: PUBLIC_ID_3,
tags: UPLOAD_TAGS,
eager: [EXPLICIT_TRANSFORMATION, EXPLICIT_TRANSFORMATION2]
})
]).then(wait(4000)).then(() => cloudinary.v2.api.delete_derived_by_transformation(
[PUBLIC_ID_1, PUBLIC_ID_3], [EXPLICIT_TRANSFORMATION, EXPLICIT_TRANSFORMATION2]
)).then(
() => cloudinary.v2.api.resource(PUBLIC_ID_1)
).then((result) => {
expect(result.derived.length).to.eql(0);
return cloudinary.v2.api.resource(PUBLIC_ID_2);
}).then((result) => {
expect(result.derived.find(d => d.transformation === EXPLICIT_TRANSFORMATION_NAME2))
.to.not.be.empty();
return cloudinary.v2.api.resource(PUBLIC_ID_3);
}).then(function (result) {
expect(result.derived.length).to.eql(0);
});
});
it("should allow deleting resources", function () {
this.timeout(TIMEOUT.MEDIUM);
return uploadImage({
public_id: PUBLIC_ID_3,
tags: UPLOAD_TAGS
}).then(
() => cloudinary.v2.api.resource(PUBLIC_ID_3)
).then(function (resource) {
expect(resource).not.to.eql(void 0);
return cloudinary.v2.api.delete_resources(["apit_test", PUBLIC_ID_2, PUBLIC_ID_3]);
}).then(
() => cloudinary.v2.api.resource(PUBLIC_ID_3)
).then(() => {
expect().fail();
}).catch(function ({error}) {
expect(error).to.be.an(Object);
expect(error.http_code).to.eql(404);
});
});
describe("delete_resources_by_prefix", function () {
callReusableTest("accepts next_cursor", cloudinary.v2.api.delete_resources_by_prefix, "prefix_foobar");
return it("should allow deleting resources by prefix", function () {
this.timeout(TIMEOUT.MEDIUM);
return uploadImage({
public_id: "api_test_by_prefix",
tags: UPLOAD_TAGS
}).then(
() => cloudinary.v2.api.resource("api_test_by_prefix")
).then(function (resource) {
expect(resource).not.to.eql(void 0);
return cloudinary.v2.api.delete_resources_by_prefix("api_test_by");
}).then(
() => cloudinary.v2.api.resource("api_test_by_prefix")
).then(
() => expect().fail()
).catch(function ({error}) {
expect(error).to.be.an(Object);
expect(error.http_code).to.eql(404);
});
});
});
describe("delete_resources_by_tag", function () {
let deleteTestTag = TEST_TAG + "_delete";
callReusableTest("accepts next_cursor", cloudinary.v2.api.delete_resources_by_prefix, deleteTestTag);
it("should allow deleting resources by tags", function () {
this.timeout(TIMEOUT.MEDIUM);
return uploadImage({
public_id: PUBLIC_ID_4,
tags: UPLOAD_TAGS.concat([deleteTestTag])
}).then(
() => cloudinary.v2.api.resource(PUBLIC_ID_4)
).then(function (resource) {
expect(resource).to.be.ok();
return cloudinary.v2.api.delete_resources_by_tag(deleteTestTag);
}).then(
() => cloudinary.v2.api.resource(PUBLIC_ID_4)
).then(
() => expect().fail()
).catch(({error}) => {
expect(error).to.be.an(Object);
expect(error.http_code).to.eql(404);
});
});
});
});
describe("tags", function () {
callReusableTest("a list with a cursor", cloudinary.v2.api.tags);
it("should allow listing tags", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.tags({
max_results: 500
}).then(result => expect(result.tags).not.to.be.empty());
});
it("should allow listing tag by prefix ", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.tags({
prefix: TEST_TAG.slice(0, -1),
max_results: 500
}).then(result => expect(result.tags).to.contain(TEST_TAG));
});
it("should allow listing tag by prefix if not found", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.tags({
prefix: "api_test_no_such_tag"
}).then(result => expect(result.tags).to.be.empty());
});
});
describe("headers", function () {
it("should include rate limits", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.resources().then(function (result) {
expect(result.rate_limit_allowed).to.be.a("number");
expect(result.rate_limit_reset_at).to.be.an("object");
expect(result.rate_limit_reset_at).to.have.property("getDate");
expect(result.rate_limit_remaining).to.be.a("number");
expect(result.rate_limit_allowed).to.be.above(0);
expect(result.rate_limit_remaining).to.be.above(0);
expect(result.rate_limit_reset_at).to.be.above(0);
});
});
});
describe("transformations", function () {
var transformationName;
callReusableTest("a list with a cursor", cloudinary.v2.api.transformation, EXPLICIT_TRANSFORMATION_NAME);
callReusableTest("a list with a cursor", cloudinary.v2.api.transformations);
transformationName = "api_test_transformation3" + UNIQUE_JOB_SUFFIX_ID;
after(function () {
return Q.allSettled(
[
cloudinary.v2.api.delete_transformation(transformationName),
cloudinary.v2.api.delete_transformation(NAMED_TRANSFORMATION),
cloudinary.v2.api.delete_transformation(NAMED_TRANSFORMATION2)
]
).finally(function () {
});
});
it("should allow listing transformations", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.transformations().then(function (result) {
expect(result).to.have.key("transformations");
expect(result.transformations).not.to.be.empty();
expect(result.transformations[0]).to.have.key('used');
});
});
it("should allow getting transformation metadata", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.transformation(EXPLICIT_TRANSFORMATION_NAME).then(function (transformation) {
expect(transformation).not.to.eql(void 0);
expect(transformation.info).to.eql([EXPLICIT_TRANSFORMATION]);
});
});
it("should allow getting transformation metadata by info", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.transformation(EXPLICIT_TRANSFORMATION).then(function (transformation) {
expect(transformation).not.to.eql(void 0);
expect(transformation.info).to.eql([EXPLICIT_TRANSFORMATION]);
});
});
it("should allow updating transformation allowed_for_strict", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.update_transformation(EXPLICIT_TRANSFORMATION_NAME, {
allowed_for_strict: true
}).then(
() => cloudinary.v2.api.transformation(EXPLICIT_TRANSFORMATION_NAME)
).then(function (transformation) {
expect(transformation).not.to.eql(void 0);
expect(transformation.allowed_for_strict).to.be.ok();
return cloudinary.v2.api.update_transformation(EXPLICIT_TRANSFORMATION_NAME, {
allowed_for_strict: false
});
}).then(() => cloudinary.v2.api.transformation(EXPLICIT_TRANSFORMATION_NAME)).then(function (transformation) {
expect(transformation).not.to.eql(void 0);
expect(transformation.allowed_for_strict).not.to.be.ok();
});
});
describe("Named Transformations", function () {
it("should allow creating named transformation", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.create_transformation(NAMED_TRANSFORMATION, {
crop: "scale",
width: 102
}).then(
() => cloudinary.v2.api.transformation(NAMED_TRANSFORMATION)
).then(function (transformation) {
expect(transformation).not.to.eql(void 0);
expect(transformation.allowed_for_strict).to.be.ok();
expect(transformation.info).to.eql([
{
crop: "scale",
width: 102
}
]);
expect(transformation.used).not.to.be.ok();
});
});
it("should allow creating named transformation with an empty format", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.create_transformation(NAMED_TRANSFORMATION2, {
crop: "scale",
width: 102,
format: ''
}).then(
() => cloudinary.v2.api.transformation(NAMED_TRANSFORMATION2)
).then(function (transformation) {
expect(transformation).not.to.eql(void 0);
expect(transformation.allowed_for_strict).to.be.ok();
expect(transformation.info).to.eql([
{
crop: "scale",
width: 102,
extension: 'none'
}
]);
expect(transformation.used).not.to.be.ok();
});
});
it("should allow listing of named transformations", function () {
return helper.provideMockObjects(function (mockXHR, writeSpy, requestSpy) {
cloudinary.v2.api.transformations({
named: true
});
return sinon.assert.calledWith(requestSpy, sinon.match({
query: sinon.match('named=true')
}, "named=true"));
});
});
it("should allow unsafe update of named transformation", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.create_transformation(transformationName, {
crop: "scale",
width: 102
}).then(() => cloudinary.v2.api.update_transformation(transformationName, {
unsafe_update: {
crop: "scale",
width: 103
}
})).then(
() => cloudinary.v2.api.transformation(transformationName)
).then((transformation) => {
expect(transformation).not.to.eql(void 0);
expect(transformation.info).to.eql([
{
crop: "scale",
width: 103
}
]);
expect(transformation.used).not.to.be.ok();
});
});
it("should allow deleting named transformation", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.delete_transformation(NAMED_TRANSFORMATION)
.then(() => cloudinary.v2.api.transformation(NAMED_TRANSFORMATION))
.then(() => expect().fail())
.catch(({error}) => expect(error.http_code).to.eql(404));
});
});
it("should allow deleting implicit transformation", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.transformation(EXPLICIT_TRANSFORMATION_NAME).then(function (transformation) {
expect(transformation).to.be.an(Object);
return cloudinary.v2.api.delete_transformation(EXPLICIT_TRANSFORMATION_NAME);
}).then(
() => cloudinary.v2.api.transformation(EXPLICIT_TRANSFORMATION_NAME)
).then(
() => expect().fail()
).catch(({error}) => expect(error.http_code).to.eql(404));
});
});
describe("upload_preset", function () {
callReusableTest("a list with a cursor", cloudinary.v2.api.upload_presets);
it("should allow listing upload_presets", function () {
return helper.provideMockObjects(function (mockXHR, writeSpy, requestSpy) {
cloudinary.v2.api.upload_presets();
return sinon.assert.calledWith(requestSpy, sinon.match({
pathname: sinon.match(/.*\/upload_presets$/)
}, "upload_presets"));
});
});
it("should allow getting a single upload_preset", function () {
return helper.provideMockObjects(function (mockXHR, writeSpy, requestSpy) {
cloudinary.v2.api.upload_preset(API_TEST_UPLOAD_PRESET1);
var expectedPath = "/.*\/upload_presets/" + API_TEST_UPLOAD_PRESET1 + "$";
return sinon.assert.calledWith(requestSpy, sinon.match({
pathname: sinon.match(new RegExp(expectedPath)),
method: sinon.match("GET")
}));
});
});
it("should allow deleting upload_presets", function () {
return helper.provideMockObjects(function (mockXHR, writeSpy, requestSpy) {
cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET2);
var expectedPath = "/.*\/upload_presets/" + API_TEST_UPLOAD_PRESET2 + "$";
return sinon.assert.calledWith(requestSpy, sinon.match({
pathname: sinon.match(new RegExp(expectedPath)),
method: sinon.match("DELETE")
}))
});
});
it("should allow updating upload_presets", function () {
return helper.provideMockObjects(function (mockXHR, writeSpy, requestSpy) {
cloudinary.v2.api.update_upload_preset(API_TEST_UPLOAD_PRESET3,
{
colors: true,
unsigned: true,
disallow_public_id: true,
live: true,
eval: TEST_EVAL_STR
});
var expectedPath = "/.*\/upload_presets/" + API_TEST_UPLOAD_PRESET3 + "$";
sinon.assert.calledWith(requestSpy, sinon.match({
pathname: sinon.match(new RegExp(expectedPath)),
method: sinon.match("PUT")
}));
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('colors', 1, "colors=1")));
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('unsigned', true, "unsigned=true")));
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('disallow_public_id', true, "disallow_public_id=true")));
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('live', true, "live=true")));
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('eval', TEST_EVAL_STR, `eval=${TEST_EVAL_STR}`)));
});
});
it("should allow creating upload_presets", function () {
return helper.provideMockObjects(function (mockXHR, writeSpy, requestSpy) {
cloudinary.v2.api.create_upload_preset({
folder: "upload_folder",
unsigned: true,
tags: UPLOAD_TAGS,
live: true,
eval: TEST_EVAL_STR
}).then((preset) => {
cloudinary.v2.api.delete_upload_preset(preset.name).catch((err) => {
console.log(err);
// we don't fail the test if the delete fails
});
});
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('unsigned', true, "unsigned=true")));
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('live', true, "live=true")));
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('eval', TEST_EVAL_STR, `eval=${TEST_EVAL_STR}`)));
});
});
});
it("should support the usage API call", function () {
this.timeout(TIMEOUT.MEDIUM);
return cloudinary.v2.api.usage()
.then(usage => {
expect(usage).to.be.an("object");
expect(usage).to.have.keys("plan", "last_updated", "transformations", "objects", "bandwidth", "storage", "requests", "resources", "derived_resources", "media_limits");
});
});
it("should return usage values for a specific date", function () {
const yesterday = formatDate(subDate(new Date(), {days: 1}), "dd-MM-yyyy");
return cloudinary.v2.api.usage({date: yesterday})
.then(usage => {
expect(usage).to.be.an("object");
expect(usage).to.have.keys("plan", "last_updated", "transformations", "objects", "bandwidth", "storage", "requests", "resources", "derived_resources", "media_limits");
expect(usage.bandwidth).to.be.an("object");
expect(usage.bandwidth).to.not.have.keys("limit", "used_percent");
});
});
describe("delete_all_resources", function () {
callReusableTest("accepts next_cursor", cloudinary.v2.api.delete_all_resources);
describe("keep_original: yes", function () {
it("should allow deleting all derived resources", function () {
return helper.provideMockObjects(function (mockXHR, writeSpy, requestSpy) {
let options = {
keep_original: true
};
cloudinary.v2.api.delete_all_resources(options);
sinon.assert.calledWith(requestSpy, sinon.match(arg => new RegExp("/resources/image/upload$").test(arg.pathname), "/resources/image/upload"));
sinon.assert.calledWith(requestSpy, sinon.match(arg => arg.method === "DELETE", "DELETE"));
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('keep_original', 'true'), "keep_original=true"));
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('all', 'true'), "all=true"));
});
});
});
});
describe("update", function () {
describe("notification url", function () {
var writeSpy, xhr;
before(function () {
xhr = sinon.useFakeXMLHttpRequest();
writeSpy = sinon.spy(ClientRequest.prototype, 'write');
});
after(function () {
writeSpy.restore();
xhr.restore();
});
it("should support changing moderation status with notification-url", function () {
this.timeout(TIMEOUT.LONG);
return uploadImage({
moderation: "manual"
}).then(upload_result => cloudinary.v2.api.update(upload_result.public_id, {
moderation_status: "approved",
notification_url: "https://example.com"
})).then(function () {
if (writeSpy.called) {
sinon.assert.calledWith(writeSpy, sinon.match(/notification_url=https%3A%2F%2Fexample.com/));
sinon.assert.calledWith(writeSpy, sinon.match(/moderation_status=approved/));
}
});
});
it("should support updating metadata with clear_invalid", () => {
this.timeout(TIMEOUT.LONG);
return uploadImage()
.then(upload_result => {
return cloudinary.v2.api.update(upload_result.public_id, {
clear_invalid: true
});
}).then(() => {
if (writeSpy.called) {
sinon.assert.calledWith(writeSpy, sinon.match(/clear_invalid=true/));
}
});
});
it('should support visual_search parameter', () => {
this.timeout(TIMEOUT.LONG);
return uploadImage()
.then(upload_result => {
return cloudinary.v2.api.update(upload_result.public_id, {
visual_search: true
});
}).then(() => {
sinon.assert.calledWith(writeSpy, sinon.match(/visual_search=true/));
});
});
});
describe("quality override", function () {
const mocked = helper.mockTest();
const qualityValues = ["auto:advanced", "auto:best", "80:420", "none"];
qualityValues.forEach(quality => {
it("should support '" + quality + "' in update", function () {
cloudinary.v2.api.update("sample", {quality_override: quality});
sinon.assert.calledWith(mocked.write, sinon.match(helper.apiParamMatcher("quality_override", quality)));
});
});
});
describe(":ocr", function () {
before(async function () {
this.timeout(TIMEOUT.MEDIUM);
await retry(async function () {
await uploadImage({
public_id: PUBLIC_ID_OCR_1,
tags: [TEST_TAG]
});
});
});
it("should support requesting ocr when updating", async function () {
if (!shouldTestAddOn(ADDON_OCR)) {
this.skip();
}
// Update an image with ocr parameter
const ocrType = "adv_ocr";
const updateResult = await API_V2.update(PUBLIC_ID_OCR_1, {ocr: ocrType});