Skip to content

Commit 8ec55b3

Browse files
author
Amir Tocker
committed
Use random suffix in api tests
1 parent e3c53ee commit 8ec55b3

File tree

1 file changed

+62
-44
lines changed

1 file changed

+62
-44
lines changed

test/api_spec.coffee

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,29 @@ describe "api", ->
7474
if(!(config.api_key && config.api_secret))
7575
expect().fail("Missing key and secret. Please set CLOUDINARY_URL.")
7676

77-
cloudinary.v2.api.delete_resources_by_tag helper.TEST_TAG, (error, result) ->
77+
cloudinary.v2.api.delete_resources_by_tag TEST_TAG, (error, result) ->
7878
if error?
7979
done(new Error error.message)
8080
else
8181
done()
8282

8383
SUFFIX = Math.floor(Math.random() * 99999)
84-
PUBLIC_ID = "npm_api_test" + SUFFIX
85-
PUBLIC_ID_1 = PUBLIC_ID + "_1" + SUFFIX
86-
PUBLIC_ID_2 = PUBLIC_ID + "_2" + SUFFIX
87-
PUBLIC_ID_3 = PUBLIC_ID + "_3" + SUFFIX
88-
PUBLIC_ID_4 = PUBLIC_ID + "_4" + SUFFIX
84+
PUBLIC_ID_PREFIX = "npm_api_test"
85+
PUBLIC_ID = PUBLIC_ID_PREFIX + SUFFIX
86+
PUBLIC_ID_1 = PUBLIC_ID_PREFIX + "_1_" + SUFFIX
87+
PUBLIC_ID_2 = PUBLIC_ID_PREFIX + "_2_" + SUFFIX
88+
PUBLIC_ID_3 = PUBLIC_ID_PREFIX + "_3_" + SUFFIX
89+
PUBLIC_ID_4 = PUBLIC_ID_PREFIX + "_4_" + SUFFIX
90+
91+
NAMED_TRANSFORMATION = "api_test_transformation" + SUFFIX
92+
API_TEST_UPLOAD_PRESET1 = "api_test_upload_preset_1_" + SUFFIX
93+
API_TEST_UPLOAD_PRESET2 = "api_test_upload_preset_2_" + SUFFIX
94+
API_TEST_UPLOAD_PRESET3 = "api_test_upload_preset_3_" + SUFFIX
95+
API_TEST_UPLOAD_PRESET4 = "api_test_upload_preset_4_" + SUFFIX
96+
97+
random_width = Math.floor(Math.random() * 9999)
98+
EXPLICIT_TRANSFORMATION_NAME = "c_scale,w_" + random_width
99+
EXPLICIT_TRANSFORMATION = {width: random_width, crop: "scale"}
89100

90101
find_by_attr = (elements, attr, value) ->
91102
for element in elements
@@ -106,17 +117,24 @@ describe "api", ->
106117
@timeout 0
107118
@timestamp_tag = "#{TEST_TAG}_#{cloudinary.utils.timestamp()}"
108119

109-
cloudinary.v2.api.delete_resources [PUBLIC_ID, PUBLIC_ID_1, PUBLIC_ID_2], (error, result)->
110-
Q.all [
111-
cloudinary.v2.uploader.upload(IMAGE_FILE, public_id: PUBLIC_ID, tags: [TEST_TAG, @timestamp_tag], context: "key=value", eager: [width: 100, crop: "scale"])
112-
cloudinary.v2.uploader.upload(IMAGE_FILE, public_id: PUBLIC_ID_2, tags: [TEST_TAG, @timestamp_tag], context: "key=value", eager: [width: 100, crop: "scale"])
113-
cloudinary.v2.api.delete_transformation("api_test_transformation")
114-
cloudinary.v2.api.delete_upload_preset("api_test_upload_preset1")
115-
cloudinary.v2.api.delete_upload_preset("api_test_upload_preset2")
116-
cloudinary.v2.api.delete_upload_preset("api_test_upload_preset3")
117-
cloudinary.v2.api.delete_upload_preset("api_test_upload_preset4")]
118-
.finally ->
119-
done()
120+
Q.all [
121+
cloudinary.v2.uploader.upload(IMAGE_FILE, public_id: PUBLIC_ID, tags: [TEST_TAG, @timestamp_tag], context: "key=value", eager: [EXPLICIT_TRANSFORMATION])
122+
cloudinary.v2.uploader.upload(IMAGE_FILE, public_id: PUBLIC_ID_2, tags: [TEST_TAG, @timestamp_tag], context: "key=value", eager: [EXPLICIT_TRANSFORMATION])]
123+
.finally ->
124+
done()
125+
126+
after (done) ->
127+
@timeout 0
128+
@timestamp_tag = "#{TEST_TAG}_#{cloudinary.utils.timestamp()}"
129+
130+
Q.all [
131+
cloudinary.v2.api.delete_transformation(NAMED_TRANSFORMATION)
132+
cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET1)
133+
cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET2)
134+
cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET3)
135+
cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET4)]
136+
.finally ->
137+
done()
120138

121139
describe "resources", ()->
122140
itBehavesLike "a list with a cursor", cloudinary.v2.api.resources
@@ -153,7 +171,7 @@ describe "api", ->
153171

154172
it "should allow listing resources by prefix", (done) ->
155173
@timeout helper.TIMEOUT_MEDIUM
156-
cloudinary.v2.api.resources type: "upload", prefix: PUBLIC_ID, max_results: 500, (error, result) ->
174+
cloudinary.v2.api.resources type: "upload", prefix: PUBLIC_ID_PREFIX, max_results: 500, (error, result) ->
157175
return done(new Error error.message) if error?
158176
public_ids = (resource.public_id for resource in result.resources)
159177
expect(public_ids).to.contain(PUBLIC_ID)
@@ -215,7 +233,7 @@ describe "api", ->
215233

216234
it "should allow get resource metadata", (done) ->
217235
@timeout helper.TIMEOUT_MEDIUM
218-
cloudinary.v2.uploader.upload IMAGE_FILE, tags: [TEST_TAG, @timestamp_tag], eager: [width: 100, crop: "scale"], (error, result)->
236+
cloudinary.v2.uploader.upload IMAGE_FILE, tags: [TEST_TAG, @timestamp_tag], eager: [EXPLICIT_TRANSFORMATION], (error, result)->
219237
done(new Error error.message) if error?
220238
public_id = result.public_id
221239
cloudinary.v2.api.resource public_id, (error, resource) ->
@@ -313,48 +331,48 @@ describe "api", ->
313331
done()
314332

315333
describe "transformations", ()->
316-
itBehavesLike "a list with a cursor", cloudinary.v2.api.transformation, "c_scale,w_100"
334+
itBehavesLike "a list with a cursor", cloudinary.v2.api.transformation, EXPLICIT_TRANSFORMATION_NAME
317335
itBehavesLike "a list with a cursor", cloudinary.v2.api.transformations
318336

319337
it "should allow listing transformations", (done) ->
320338
@timeout helper.TIMEOUT_MEDIUM
321339
cloudinary.v2.api.transformations (error, result) ->
322340
return done(new Error error.message) if error?
323-
transformation = find_by_attr(result.transformations, "name", "c_scale,w_100")
341+
transformation = find_by_attr(result.transformations, "name", EXPLICIT_TRANSFORMATION_NAME)
324342
expect(transformation).not.to.eql(undefined)
325343
expect(transformation.used).to.be.ok
326344
done()
327345

328346
it "should allow getting transformation metadata", (done) ->
329347
@timeout helper.TIMEOUT_MEDIUM
330-
cloudinary.v2.api.transformation "c_scale,w_100", (error, transformation) ->
348+
cloudinary.v2.api.transformation EXPLICIT_TRANSFORMATION_NAME, (error, transformation) ->
331349
expect(transformation).not.to.eql(undefined)
332-
expect(transformation.info).to.eql([crop: "scale", width: 100])
350+
expect(transformation.info).to.eql([EXPLICIT_TRANSFORMATION])
333351
done()
334352

335353
it "should allow getting transformation metadata by info", (done) ->
336354
@timeout helper.TIMEOUT_MEDIUM
337-
cloudinary.v2.api.transformation {crop: "scale", width: 100}, (error, transformation) ->
355+
cloudinary.v2.api.transformation EXPLICIT_TRANSFORMATION, (error, transformation) ->
338356
expect(transformation).not.to.eql(undefined)
339-
expect(transformation.info).to.eql([crop: "scale", width: 100])
357+
expect(transformation.info).to.eql([EXPLICIT_TRANSFORMATION])
340358
done()
341359

342360
it "should allow updating transformation allowed_for_strict", (done) ->
343361
@timeout helper.TIMEOUT_MEDIUM
344-
cloudinary.v2.api.update_transformation "c_scale,w_100", {allowed_for_strict: true}, () ->
345-
cloudinary.v2.api.transformation "c_scale,w_100", (error, transformation) ->
362+
cloudinary.v2.api.update_transformation EXPLICIT_TRANSFORMATION_NAME, {allowed_for_strict: true}, () ->
363+
cloudinary.v2.api.transformation EXPLICIT_TRANSFORMATION_NAME, (error, transformation) ->
346364
expect(transformation).not.to.eql(undefined)
347365
expect(transformation.allowed_for_strict).to.be.ok
348-
cloudinary.v2.api.update_transformation "c_scale,w_100", {allowed_for_strict: false}, () ->
349-
cloudinary.v2.api.transformation "c_scale,w_100", (error, transformation) ->
366+
cloudinary.v2.api.update_transformation EXPLICIT_TRANSFORMATION_NAME, {allowed_for_strict: false}, () ->
367+
cloudinary.v2.api.transformation EXPLICIT_TRANSFORMATION_NAME, (error, transformation) ->
350368
expect(transformation).not.to.eql(undefined)
351369
expect(transformation.allowed_for_strict).not.to.be.ok
352370
done()
353371

354372
it "should allow creating named transformation", (done) ->
355373
@timeout helper.TIMEOUT_MEDIUM
356-
cloudinary.v2.api.create_transformation "api_test_transformation", {crop: "scale", width: 102}, () ->
357-
cloudinary.v2.api.transformation "api_test_transformation", (error, transformation) ->
374+
cloudinary.v2.api.create_transformation NAMED_TRANSFORMATION, {crop: "scale", width: 102}, () ->
375+
cloudinary.v2.api.transformation NAMED_TRANSFORMATION, (error, transformation) ->
358376
expect(transformation).not.to.eql(undefined)
359377
expect(transformation.allowed_for_strict).to.be.ok
360378
expect(transformation.info).to.eql([crop: "scale", width: 102])
@@ -373,25 +391,25 @@ describe "api", ->
373391

374392
it "should allow deleting named transformation", (done) ->
375393
@timeout helper.TIMEOUT_MEDIUM
376-
cloudinary.v2.api.delete_transformation "api_test_transformation", () ->
377-
cloudinary.v2.api.transformation "api_test_transformation", (error, transformation) ->
394+
cloudinary.v2.api.delete_transformation NAMED_TRANSFORMATION, () ->
395+
cloudinary.v2.api.transformation NAMED_TRANSFORMATION, (error, transformation) ->
378396
expect(error.http_code).to.eql 404
379397
done()
380398

381399
it "should allow deleting implicit transformation", (done) ->
382400
@timeout helper.TIMEOUT_MEDIUM
383-
cloudinary.v2.api.transformation "c_scale,w_100", (error, transformation) ->
401+
cloudinary.v2.api.transformation EXPLICIT_TRANSFORMATION_NAME, (error, transformation) ->
384402
expect(transformation).to.be.an(Object)
385-
cloudinary.v2.api.delete_transformation "c_scale,w_100", () ->
386-
cloudinary.v2.api.transformation "c_scale,w_100", (error, transformation) ->
403+
cloudinary.v2.api.delete_transformation EXPLICIT_TRANSFORMATION_NAME, () ->
404+
cloudinary.v2.api.transformation EXPLICIT_TRANSFORMATION_NAME, (error, transformation) ->
387405
expect(error.http_code).to.eql 404
388406
done()
389407

390408
describe "upload_preset", ()->
391409
itBehavesLike "a list with a cursor", cloudinary.v2.api.upload_presets
392410
it "should allow creating and listing upload_presets", (done) ->
393411
@timeout helper.TIMEOUT_MEDIUM
394-
create_names = ["api_test_upload_preset3", "api_test_upload_preset2", "api_test_upload_preset1"]
412+
create_names = [API_TEST_UPLOAD_PRESET3, API_TEST_UPLOAD_PRESET2, API_TEST_UPLOAD_PRESET1]
395413
delete_names = []
396414
after_delete = ->
397415
delete_names.pop()
@@ -414,24 +432,24 @@ describe "api", ->
414432

415433
it "should allow getting a single upload_preset", (done) ->
416434
@timeout helper.TIMEOUT_MEDIUM
417-
cloudinary.v2.api.create_upload_preset unsigned: true, folder: "folder", transformation: {width: 100, crop: "scale"}, tags: ["a","b","c"], context: {a: "b", c: "d"}, (error, preset) ->
435+
cloudinary.v2.api.create_upload_preset unsigned: true, folder: "folder", transformation: EXPLICIT_TRANSFORMATION, tags: ["a","b","c"], context: {a: "b", c: "d"}, (error, preset) ->
418436
name = preset.name
419437
cloudinary.v2.api.upload_preset name, (error, preset) ->
420438
expect(preset.name).to.eql(name)
421439
expect(preset.unsigned).to.eql(true)
422440
expect(preset.settings.folder).to.eql("folder")
423-
expect(preset.settings.transformation).to.eql([{width: 100, crop: "scale"}])
441+
expect(preset.settings.transformation).to.eql([EXPLICIT_TRANSFORMATION])
424442
expect(preset.settings.context).to.eql({a: "b", c: "d"})
425443
expect(preset.settings.tags).to.eql(["a","b","c"])
426444
cloudinary.v2.api.delete_upload_preset name, ->
427445
done()
428446

429447
it "should allow deleting upload_presets", (done) ->
430448
@timeout helper.TIMEOUT_MEDIUM
431-
cloudinary.v2.api.create_upload_preset name: "api_test_upload_preset4", folder: "folder", (error, preset) ->
432-
cloudinary.v2.api.upload_preset "api_test_upload_preset4", ->
433-
cloudinary.v2.api.delete_upload_preset "api_test_upload_preset4", ->
434-
cloudinary.v2.api.upload_preset "api_test_upload_preset4", (error, result) ->
449+
cloudinary.v2.api.create_upload_preset name: API_TEST_UPLOAD_PRESET4, folder: "folder", (error, preset) ->
450+
cloudinary.v2.api.upload_preset API_TEST_UPLOAD_PRESET4, ->
451+
cloudinary.v2.api.delete_upload_preset API_TEST_UPLOAD_PRESET4, ->
452+
cloudinary.v2.api.upload_preset API_TEST_UPLOAD_PRESET4, (error, result) ->
435453
expect(error.message).to.contain "Can't find"
436454
done()
437455

@@ -687,7 +705,7 @@ describe "api", ->
687705
publishTestId = result.public_id
688706
done()
689707
afterEach (done)->
690-
cloudinary.v2.uploader.destroy publishTestId, (error, result)->
708+
cloudinary.v2.uploader.destroy publishTestId, type: "authenticated", (error, result)->
691709
return done(new Error error.message) if error?
692710
done()
693711
it "by public id", (done)->

0 commit comments

Comments
 (0)