Skip to content

Commit 0fd7ee8

Browse files
Enabled skipped tests caused by running the suites on a fresh cloud (#387)
- Add timeouts to ensure files have a chance to update - Fix an implementation bug on the wait() function - Move wait() to a utility file - Create a sample.jpg file when creating a new cloud
1 parent e2d21e3 commit 0fd7ee8

File tree

9 files changed

+197
-127
lines changed

9 files changed

+197
-127
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"devDependencies": {
1818
"@types/mocha": "^7.0.2",
1919
"@types/node": "^13.5.0",
20+
"@types/expect.js": "^0.3.29",
2021
"babel-cli": "^6.26.0",
2122
"babel-core": "^6.26.3",
2223
"babel-plugin-transform-runtime": "^6.23.0",

test/.resources/sample.jpg

8.02 KB
Loading

test/api_spec.js

Lines changed: 109 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
require('dotenv').load({
2+
silent: true,
3+
});
4+
15
const expect = require("expect.js");
26
const sinon = require('sinon');
37
const ClientRequest = require('_http_client').ClientRequest;
48
const http = require('http');
59
const Q = require('q');
610
const cloudinary = require("../cloudinary");
711
const helper = require("./spechelper");
12+
const wait = require('./testUtils/helpers/wait');
813
const sharedExamples = helper.sharedExamples;
914
const itBehavesLike = helper.itBehavesLike;
1015
const uploadImage = helper.uploadImage;
1116

1217
const testConstants = require('./testUtils/testConstants');
18+
const API_V2 = cloudinary.v2.api;
1319

1420
const {
1521
TIMEOUT,
@@ -65,13 +71,7 @@ const EXPLICIT_TRANSFORMATION2 = {
6571
overlay: `text:Arial_60:${TEST_TAG}`,
6672
};
6773

68-
function wait(ms = 0) {
69-
return new Promise((resolve) => {
70-
setTimeout(() => {
71-
resolve();
72-
}, ms);
73-
});
74-
}
74+
7575

7676
sharedExamples("a list with a cursor", function (testFunc, ...args) {
7777
specify(":max_results", function () {
@@ -795,7 +795,7 @@ describe("api", function () {
795795
});
796796
});
797797
});
798-
describe.skip("quality override", function() {
798+
describe("quality override", function() {
799799
const mocked = helper.mockTest();
800800
const qualityValues = ["auto:advanced", "auto:best", "80:420", "none"];
801801
qualityValues.forEach(quality => {
@@ -805,13 +805,16 @@ describe("api", function () {
805805
});
806806
});
807807
});
808-
it.skip("should support setting manual moderation status", () => {
808+
it("should support setting manual moderation status", function() {
809809
this.timeout(TIMEOUT.LONG);
810810
return uploadImage({
811811
moderation: "manual",
812812
}).then(upload_result => cloudinary.v2.api.update(upload_result.public_id, {
813813
moderation_status: "approved",
814-
})).then(api_result => expect(api_result.moderation[0].status).to.eql("approved"));
814+
})).then(api_result => expect(api_result.moderation[0].status).to.eql("approved"))
815+
.catch((err) => {
816+
console.og(err);
817+
});
815818
});
816819
it("should support requesting ocr info", function () {
817820
this.timeout(TIMEOUT.MEDIUM);
@@ -904,7 +907,7 @@ describe("api", function () {
904907
});
905908
// For this test to work, "Auto-create folders" should be enabled in the Upload Settings.
906909
// Replace `it` with `it.skip` below if you want to disable it.
907-
it.skip("should list folders in cloudinary", function () {
910+
it("should list folders in cloudinary", function () {
908911
this.timeout(TIMEOUT.LONG);
909912
return Q.all([
910913
uploadImage({
@@ -927,7 +930,7 @@ describe("api", function () {
927930
public_id: 'test_folder1/test_subfolder2/item',
928931
tags: UPLOAD_TAGS,
929932
}),
930-
]).then(function (results) {
933+
]).then(wait(3000)).then(function (results) {
931934
return Q.all([cloudinary.v2.api.root_folders(), cloudinary.v2.api.sub_folders('test_folder1')]);
932935
}).then(function (results) {
933936
var folder, root, root_folders, sub_1;
@@ -951,7 +954,10 @@ describe("api", function () {
951954
}).then((result) => {
952955
console.log('error test_folder_not_exists should not pass to "then" handler but "catch"');
953956
expect().fail('error test_folder_not_exists should not pass to "then" handler but "catch"');
954-
}).catch(({ error }) => expect(error.message).to.eql('Can\'t find folder with path test_folder_not_exists'));
957+
}).catch((err) => {
958+
let error = err.error;
959+
return expect(error.message).to.eql('Can\'t find folder with path test_folder_not_exists');
960+
});
955961
});
956962
describe("delete folders", function() {
957963
this.timeout(TIMEOUT.MEDIUM);
@@ -986,7 +992,7 @@ describe("api", function () {
986992
public_id: publicId,
987993
backup: true,
988994
tags: UPLOAD_TAGS,
989-
}).then(() => cloudinary.v2.api.resource(publicId)).then((resource) => {
995+
}).then(wait(2000)).then(() => cloudinary.v2.api.resource(publicId)).then((resource) => {
990996
expect(resource).not.to.be(null);
991997
expect(resource.bytes).to.eql(3381);
992998
return cloudinary.v2.api.delete_resources(publicId);
@@ -1004,67 +1010,88 @@ describe("api", function () {
10041010
expect(resource).not.to.be(null);
10051011
expect(resource.bytes).to.eql(3381);
10061012
}));
1007-
it('should restore a deleted resource by versions', function () {
1008-
return uploadImage({
1009-
public_id: PUBLIC_ID_BACKUP_1,
1010-
backup: true,
1011-
}).then(() => uploadImage({
1012-
public_id: PUBLIC_ID_BACKUP_2,
1013-
backup: true,
1014-
}).then(wait(TIMEOUT.SHORT))).then((uploadResponse) => {
1015-
expect(uploadResponse).not.to.be(null);
1016-
}).then(wait(TIMEOUT.SHORT))
1017-
.then(() => cloudinary.v2.api.delete_resources([PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2]))
1018-
.then((deleteResponse) => {
1019-
expect(deleteResponse).to.have.property("deleted");
1020-
})
1021-
.then(wait(TIMEOUT.SHORT))
1022-
.then(() => Q.all([
1023-
cloudinary.v2.api.resource(PUBLIC_ID_BACKUP_1, { versions: true }),
1024-
cloudinary.v2.api.resource(PUBLIC_ID_BACKUP_2, { versions: true })]))
1025-
.then((resources) => {
1026-
expect(resources.length).to.be(2);
10271013

1028-
const version_1 = resources[0].versions[0].version_id;
1029-
const version_2 = resources[1].versions[0].version_id;
1030-
return cloudinary.v2.api.restore([PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2], { versions: [version_1, version_2] });
1031-
}).then((restoreResponse) => {
1032-
expect(restoreResponse[PUBLIC_ID_BACKUP_1].bytes).to.eql(3381);
1033-
expect(restoreResponse[PUBLIC_ID_BACKUP_2].bytes).to.eql(3381);
1034-
}).then(() => cloudinary.v2.api.delete_resources([PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2]))
1035-
.then((deleteResponse) => {
1036-
expect(deleteResponse).to.have.property("deleted");
1037-
});
1014+
it('should restore different versions of a deleted asset', async function () {
1015+
this.timeout(TIMEOUT.LARGE);
1016+
// Upload the same file twice (upload->delete->upload->delete)
1017+
1018+
// Upload and delete a file
1019+
const firstUpload = await uploadImage({ public_id: PUBLIC_ID_BACKUP_1, backup: true });
1020+
await wait(1000)();
1021+
1022+
const firstDelete = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1]);
1023+
1024+
1025+
// Upload and delete it again, this time add angle to create a different 'version'
1026+
const secondUpload = await uploadImage({ public_id: PUBLIC_ID_BACKUP_1, backup: true, angle: '0'});
1027+
await wait(1000)();
1028+
1029+
const secondDelete = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1]);
1030+
1031+
1032+
// Sanity, ensure these uploads are different before we continue
1033+
expect(firstUpload.bytes).not.to.equal(secondUpload.bytes);
1034+
1035+
// Ensure all files were uploaded correctly
1036+
expect(firstUpload).not.to.be(null);
1037+
expect(secondUpload).not.to.be(null);
1038+
1039+
// Ensure all files were deleted correctly
1040+
expect(firstDelete).to.have.property("deleted");
1041+
expect(secondDelete).to.have.property("deleted");
1042+
1043+
// Get the versions versions of the deleted asset
1044+
const getVersionsResp = await API_V2.resource(PUBLIC_ID_BACKUP_1, { versions: true });
1045+
1046+
const firstAssetVersion = getVersionsResp.versions[0].version_id;
1047+
const secondAssetVersion = getVersionsResp.versions[1].version_id;
1048+
1049+
// Restore first version, ensure it's equal to the upload size
1050+
await wait(1000)();
1051+
const firstVerRestore = await API_V2.restore([PUBLIC_ID_BACKUP_1], { versions: [firstAssetVersion] });
1052+
expect(firstVerRestore[PUBLIC_ID_BACKUP_1].bytes).to.eql(firstUpload.bytes);
1053+
1054+
// Restore second version, ensure it's equal to the upload size
1055+
const secondVerRestore = await API_V2.restore([PUBLIC_ID_BACKUP_1], { versions: [secondAssetVersion] });
1056+
expect(secondVerRestore[PUBLIC_ID_BACKUP_1].bytes).to.eql(secondUpload.bytes);
1057+
1058+
// Cleanup,
1059+
const finalDeleteResp = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1]);
1060+
expect(finalDeleteResp).to.have.property("deleted");
10381061
});
1039-
it('should restore an old deleted resource by versions', function () {
1040-
return uploadImage({
1041-
public_id: PUBLIC_ID_BACKUP_3,
1042-
backup: true,
1043-
}).then(() => uploadImage({
1044-
public_id: PUBLIC_ID_BACKUP_3,
1045-
angle: '0',
1046-
backup: true,
1047-
}).then(wait(TIMEOUT.SHORT))).then((uploadResponse) => {
1048-
expect(uploadResponse).not.to.be(null);
1049-
}).then(wait(TIMEOUT.SHORT))
1050-
.then(() => cloudinary.v2.api.delete_resources([PUBLIC_ID_BACKUP_3]))
1051-
.then((deleteResponse) => {
1052-
expect(deleteResponse).to.have.property("deleted");
1053-
})
1054-
.then(wait(TIMEOUT.SHORT))
1055-
.then(() => cloudinary.v2.api.resource(PUBLIC_ID_BACKUP_3, { versions: true }))
1056-
.then((resources) => {
1057-
expect(resources.versions.length).to.be(2);
10581062

1059-
const old_version = resources.versions[0].version_id;
1060-
return cloudinary.v2.api.restore(PUBLIC_ID_BACKUP_3, { versions: old_version });
1061-
}).then((restoreResponse) => {
1062-
expect(restoreResponse[PUBLIC_ID_BACKUP_3].bytes).to.eql(2353);
1063-
})
1064-
.then(() => cloudinary.v2.api.delete_resources([PUBLIC_ID_BACKUP_3]))
1065-
.then((deleteResponse) => {
1066-
expect(deleteResponse).to.have.property("deleted");
1067-
});
1063+
it('should restore two different deleted assets', async () => {
1064+
// Upload two different files
1065+
const firstUpload = await uploadImage({ public_id: PUBLIC_ID_BACKUP_1, backup: true });
1066+
const secondUpload = await uploadImage({ public_id: PUBLIC_ID_BACKUP_2, backup: true, angle: '0' });
1067+
1068+
// delete both resources
1069+
const deleteAll = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2]);
1070+
1071+
// Expect correct deletion of the assets
1072+
expect(deleteAll.deleted[PUBLIC_ID_BACKUP_1]).to.be("deleted");
1073+
expect(deleteAll.deleted[PUBLIC_ID_BACKUP_2]).to.be("deleted");
1074+
1075+
const getFirstAssetVersion = await API_V2.resource(PUBLIC_ID_BACKUP_1, { versions: true });
1076+
const getSecondAssetVersion = await API_V2.resource(PUBLIC_ID_BACKUP_2, { versions: true });
1077+
1078+
const firstAssetVersion = getFirstAssetVersion.versions[0].version_id;
1079+
const secondAssetVersion = getSecondAssetVersion.versions[0].version_id;
1080+
1081+
const IDS_TO_RESTORE = [PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2];
1082+
const VERSIONS_TO_RESTORE = [firstAssetVersion, secondAssetVersion];
1083+
1084+
const restore = await API_V2.restore(IDS_TO_RESTORE, { versions: VERSIONS_TO_RESTORE });
1085+
1086+
// Expect correct restorations
1087+
expect(restore[PUBLIC_ID_BACKUP_1].bytes).to.equal(firstUpload.bytes);
1088+
expect(restore[PUBLIC_ID_BACKUP_2].bytes).to.equal(secondUpload.bytes);
1089+
1090+
// Cleanup
1091+
const finalDelete = await API_V2.delete_resources([PUBLIC_ID_BACKUP_1, PUBLIC_ID_BACKUP_2]);
1092+
// Expect correct deletion of the assets
1093+
expect(finalDelete.deleted[PUBLIC_ID_BACKUP_1]).to.be("deleted");
1094+
expect(finalDelete.deleted[PUBLIC_ID_BACKUP_2]).to.be("deleted");
10681095
});
10691096
});
10701097
describe('mapping', function () {
@@ -1172,22 +1199,25 @@ describe("api", function () {
11721199
});
11731200
});
11741201
});
1175-
describe.skip("access_mode", function () {
1176-
var access_mode_tag, i, publicId;
1202+
describe("access_mode", function () {
1203+
let access_mode_tag, i, publicId;
11771204
i = 0;
11781205
this.timeout(TIMEOUT.LONG);
11791206
publicId = "";
11801207
access_mode_tag = '';
1181-
beforeEach(function () {
1208+
1209+
beforeEach(async function () {
11821210
access_mode_tag = TEST_TAG + "access_mode" + i++;
1183-
return uploadImage({
1211+
const result = await uploadImage({
11841212
access_mode: "authenticated",
11851213
tags: UPLOAD_TAGS.concat([access_mode_tag]),
1186-
}).then((result) => {
1187-
publicId = result.public_id;
1188-
expect(result.access_mode).to.be("authenticated");
11891214
});
1215+
1216+
await wait(5000)();
1217+
publicId = result.public_id;
1218+
expect(result.access_mode).to.be("authenticated");
11901219
});
1220+
11911221
it("should update access mode by ids", () => cloudinary.v2.api.update_resources_access_mode_by_ids("public", [publicId]).then((result) => {
11921222
var resource;
11931223
expect(result.updated).to.be.an('array');

test/archivespec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ describe("archive", function () {
176176
sinon.assert.calledWith(write, sinon.match(helper.uploadParamMatcher("target_format", "zip")));
177177
});
178178
});
179-
it.skip('should create archive with "zip" format and include multiple resource types', function () {
179+
it('should create archive with "zip" format and include multiple resource types', function () {
180180
return uploader.create_zip({
181181
fully_qualified_public_ids: [FULLY_QUALIFIED_IMAGE, FULLY_QUALIFIED_VIDEO],
182182
resource_type: "auto",

test/testUtils/helpers/wait.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @description Used when chained to promises, new Promise().then(wait(1000)).then(wait(2500)).then(log)
3+
* @param {number }ms
4+
* @return {function(...item): Promise<...item>}
5+
*/
6+
function wait(ms = 0) {
7+
return (...rest) => {
8+
return new Promise((resolve) => {
9+
setTimeout(() => {
10+
resolve(...rest);
11+
}, ms);
12+
});
13+
}
14+
}
15+
16+
module.exports = wait;

test/uploader_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ describe("uploader", function () {
229229
expect(result.public_id).to.eql('sample');
230230
});
231231
});
232-
it.skip("should successfully call explicit api", function () {
232+
it("should successfully call explicit api", function () {
233233
return cloudinary.v2.uploader.explicit("sample", {
234234
type: "upload",
235235
eager: [
@@ -560,7 +560,7 @@ describe("uploader", function () {
560560
expect(result.moderation[0].kind).to.eql("manual");
561561
});
562562
});
563-
it.skip("should support requesting ocr analysis", function () {
563+
it("should support requesting ocr analysis", function () {
564564
return cloudinary.v2.uploader.upload(IMAGE_FILE, {
565565
ocr: "adv_ocr",
566566
tags: UPLOAD_TAGS,

0 commit comments

Comments
 (0)