From 4a39a287b2b28b20729e8da96889b6f7f472a973 Mon Sep 17 00:00:00 2001 From: Patrick Tolosa Date: Tue, 10 Mar 2020 18:23:09 +0200 Subject: [PATCH 1/5] Refactor in a wait period for eager uploads The current tests are failing randomally, as the even though we've receieved a response from the server, the back-end operation is not yet done. any request to delete the derived asset when it doesn't exist will fail, thus fail the test. This fix adds a small timeout to the request, we might want to consider a more long-term solution. However, this usecase is not really common (upload and immediately delete) --- test/api_spec.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/api_spec.js b/test/api_spec.js index 4b951005..a36d7708 100644 --- a/test/api_spec.js +++ b/test/api_spec.js @@ -51,6 +51,14 @@ const LABEL_INT_1 = 'metadata_label_1_' + TEST_ID; const LABEL_INT_2 = 'metadata_label_2_' + TEST_ID; const LABEL_INT_3 = 'metadata_label_3_' + TEST_ID; +function wait(ms = 0) { + return new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, ms); + }); +} + sharedExamples("a list with a cursor", function (testFunc, ...args) { specify(":max_results", function () { return helper.mockPromise(function (xhr, writeSpy, requestSpy) { @@ -365,7 +373,7 @@ describe("api", function () { tags: UPLOAD_TAGS, eager: [EXPLICIT_TRANSFORMATION, EXPLICIT_TRANSFORMATION2], }), - ]).then(() => cloudinary.v2.api.delete_derived_by_transformation( + ]).then(wait(2000)).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) From 3b6f4e9582d7a021a2377176a9082cacfc73fcde Mon Sep 17 00:00:00 2001 From: Patrick Tolosa Date: Wed, 11 Mar 2020 11:11:49 +0200 Subject: [PATCH 2/5] Add wait for another function --- test/api_spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/api_spec.js b/test/api_spec.js index a36d7708..27833378 100644 --- a/test/api_spec.js +++ b/test/api_spec.js @@ -338,7 +338,7 @@ describe("api", function () { crop: "scale", }, ], - }).then( + }).then(wait(2000)).then( ({ public_id }) => cloudinary.v2.api.resource(public_id) .then(resource => [public_id, resource]) ).then(([public_id, resource]) => { From 805470450a843d73a373662dd1b4d6ed5a4d3405 Mon Sep 17 00:00:00 2001 From: Patrick Tolosa Date: Wed, 11 Mar 2020 11:50:13 +0200 Subject: [PATCH 3/5] delete created upload preset in api-spec --- test/api_spec.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/api_spec.js b/test/api_spec.js index 27833378..c488bc9f 100644 --- a/test/api_spec.js +++ b/test/api_spec.js @@ -673,13 +673,19 @@ describe("api", function () { }); }); it("should allow creating upload_presets", function () { - return helper.mockPromise(function (xhr, write) { - cloudinary.v2.api.create_upload_preset({ + return helper.mockPromise(async function (xhr, write) { + let preset = await cloudinary.v2.api.create_upload_preset({ folder: "upload_folder", unsigned: true, tags: UPLOAD_TAGS, live: true }); + + await 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(write, sinon.match(helper.apiParamMatcher('unsigned', true, "unsigned=true"))); sinon.assert.calledWith(write, sinon.match(helper.apiParamMatcher('live', true, "live=true"))); }); From 32dc395c8b9958486295230c30edd4ac5f1668d7 Mon Sep 17 00:00:00 2001 From: Patrick Tolosa Date: Wed, 11 Mar 2020 11:57:17 +0200 Subject: [PATCH 4/5] fix tests --- test/api_spec.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/api_spec.js b/test/api_spec.js index c488bc9f..f3fca4bd 100644 --- a/test/api_spec.js +++ b/test/api_spec.js @@ -673,17 +673,17 @@ describe("api", function () { }); }); it("should allow creating upload_presets", function () { - return helper.mockPromise(async function (xhr, write) { - let preset = await cloudinary.v2.api.create_upload_preset({ + return helper.mockPromise(function (xhr, write) { + cloudinary.v2.api.create_upload_preset({ folder: "upload_folder", unsigned: true, - tags: UPLOAD_TAGS, + tags: ['TESTRAYA', ...UPLOAD_TAGS], live: true - }); - - await cloudinary.v2.api.delete_upload_preset(preset.name).catch((err) => { - console.log(err); - // we don't fail the test if the delete fails + }).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(write, sinon.match(helper.apiParamMatcher('unsigned', true, "unsigned=true"))); From fbc878d671a04091e3de6e0a7b7812b2f3b2c68f Mon Sep 17 00:00:00 2001 From: Patrick Tolosa Date: Wed, 11 Mar 2020 12:06:51 +0200 Subject: [PATCH 5/5] Increase timeout for a test --- test/api_spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/api_spec.js b/test/api_spec.js index f3fca4bd..eec6a639 100644 --- a/test/api_spec.js +++ b/test/api_spec.js @@ -373,7 +373,7 @@ describe("api", function () { tags: UPLOAD_TAGS, eager: [EXPLICIT_TRANSFORMATION, EXPLICIT_TRANSFORMATION2], }), - ]).then(wait(2000)).then(() => cloudinary.v2.api.delete_derived_by_transformation( + ]).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) @@ -677,7 +677,7 @@ describe("api", function () { cloudinary.v2.api.create_upload_preset({ folder: "upload_folder", unsigned: true, - tags: ['TESTRAYA', ...UPLOAD_TAGS], + tags: UPLOAD_TAGS, live: true }).then((preset) => { cloudinary.v2.api.delete_upload_preset(preset.name).catch((err) => {