Skip to content

Commit b938041

Browse files
igystrausr
authored andcommitted
Mock upload preset listing test
1 parent f1d9ebd commit b938041

File tree

1 file changed

+37
-76
lines changed

1 file changed

+37
-76
lines changed

test/api_spec.js

Lines changed: 37 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -608,89 +608,50 @@ describe("api", function () {
608608
});
609609
describe("upload_preset", function () {
610610
itBehavesLike("a list with a cursor", cloudinary.v2.api.upload_presets);
611-
it("should allow creating and listing upload_presets", function () {
612-
this.timeout(helper.TIMEOUT_MEDIUM);
613-
const PRESET_NAMES = [API_TEST_UPLOAD_PRESET3, API_TEST_UPLOAD_PRESET2, API_TEST_UPLOAD_PRESET1];
614-
return Q.all(
615-
PRESET_NAMES.map(name => cloudinary.v2.api.create_upload_preset({
616-
name: name,
617-
folder: "folder",
618-
})),
619-
).then(
620-
() => cloudinary.v2.api.upload_presets(),
621-
).then(
622-
({ presets }) => presets.map(p => p.name),
623-
).then(
624-
presetList => PRESET_NAMES.forEach(p => expect(presetList).to.contain(p)),
625-
).finally(() => Q.allSettled(
626-
PRESET_NAMES.map(name => cloudinary.v2.api.delete_upload_preset(name)),
627-
));
611+
it("should allow listing upload_presets", function () {
612+
return helper.mockPromise(function (xhr, write, request) {
613+
cloudinary.v2.api.upload_presets();
614+
return sinon.assert.calledWith(request, sinon.match({
615+
pathname: sinon.match(/.*\/upload_presets$/),
616+
}, "upload_presets"));
617+
});
628618
});
629619
it("should allow getting a single upload_preset", function () {
630-
this.timeout(helper.TIMEOUT_MEDIUM);
631-
return cloudinary.v2.api.create_upload_preset({
632-
unsigned: true,
633-
folder: "folder",
634-
transformation: EXPLICIT_TRANSFORMATION,
635-
tags: ["a", "b", "c"],
636-
context: {
637-
a: "b",
638-
c: "d",
639-
},
640-
}).then(
641-
newPreset => cloudinary.v2.api.upload_preset(newPreset.name)
642-
.then(preset => [newPreset.name, preset]),
643-
).then(([name, preset]) => {
644-
expect(preset.name).to.eql(name);
645-
expect(preset.unsigned).to.eql(true);
646-
expect(preset.settings.folder).to.eql("folder");
647-
expect(preset.settings.transformation).to.eql([EXPLICIT_TRANSFORMATION]);
648-
expect(preset.settings.context).to.eql({
649-
a: "b",
650-
c: "d",
651-
});
652-
expect(preset.settings.tags).to.eql(["a", "b", "c"]);
653-
return cloudinary.v2.api.delete_upload_preset(name);
620+
return helper.mockPromise(function (xhr, write, request) {
621+
cloudinary.v2.api.upload_preset(API_TEST_UPLOAD_PRESET1);
622+
var expectedPath="/.*\/upload_presets/"+API_TEST_UPLOAD_PRESET1+"$";
623+
return sinon.assert.calledWith(request, sinon.match({
624+
pathname: sinon.match(new RegExp(expectedPath)),
625+
method: sinon.match("GET")
626+
}));
654627
});
655628
});
656629
it("should allow deleting upload_presets", function () {
657-
this.timeout(helper.TIMEOUT_MEDIUM);
658-
return cloudinary.v2.api.create_upload_preset({
659-
name: API_TEST_UPLOAD_PRESET4,
660-
folder: "folder",
661-
}).then(
662-
() => cloudinary.v2.api.upload_preset(API_TEST_UPLOAD_PRESET4),
663-
).then(
664-
() => cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET4),
665-
).then(
666-
() => cloudinary.v2.api.upload_preset(API_TEST_UPLOAD_PRESET4),
667-
).then(
668-
() => expect().fail(),
669-
).catch(({ error }) => expect(error.message).to.contain("Can't find"));
630+
return helper.mockPromise(function (xhr, write, request) {
631+
cloudinary.v2.api.delete_upload_preset(API_TEST_UPLOAD_PRESET2);
632+
var expectedPath="/.*\/upload_presets/"+API_TEST_UPLOAD_PRESET2+"$";
633+
return sinon.assert.calledWith(request, sinon.match({
634+
pathname: sinon.match(new RegExp(expectedPath)),
635+
method: sinon.match("DELETE")
636+
}))
637+
});
670638
});
671639
it("should allow updating upload_presets", function () {
672-
var name = '';
673-
this.timeout(helper.TIMEOUT_MEDIUM);
674-
return cloudinary.v2.api.create_upload_preset({
675-
folder: "folder",
676-
}).then((preset) => {
677-
name = preset.name;
678-
return cloudinary.v2.api.upload_preset(name);
679-
}).then(preset => cloudinary.v2.api.update_upload_preset(name, merge(preset.settings, {
680-
colors: true,
681-
unsigned: true,
682-
disallow_public_id: true,
683-
}))).then(
684-
() => cloudinary.v2.api.upload_preset(name),
685-
).then((preset) => {
686-
expect(preset.name).to.eql(name);
687-
expect(preset.unsigned).to.eql(true);
688-
expect(preset.settings).to.eql({
689-
folder: "folder",
690-
colors: true,
691-
disallow_public_id: true,
692-
});
693-
return cloudinary.v2.api.delete_upload_preset(name);
640+
return helper.mockPromise(function (xhr, write, request) {
641+
cloudinary.v2.api.update_upload_preset(API_TEST_UPLOAD_PRESET3,
642+
{
643+
colors: true,
644+
unsigned: true,
645+
disallow_public_id: true,
646+
});
647+
var expectedPath="/.*\/upload_presets/"+API_TEST_UPLOAD_PRESET3+"$";
648+
sinon.assert.calledWith(request, sinon.match({
649+
pathname: sinon.match(new RegExp(expectedPath)),
650+
method: sinon.match("PUT")
651+
}));
652+
sinon.assert.calledWith(write, sinon.match(helper.apiParamMatcher('colors', 1 , "colors=1")));
653+
sinon.assert.calledWith(write, sinon.match(helper.apiParamMatcher('unsigned', true , "unsigned=true")));
654+
sinon.assert.calledWith(write, sinon.match(helper.apiParamMatcher('disallow_public_id', true , "disallow_public_id=true")));
694655
});
695656
});
696657
});

0 commit comments

Comments
 (0)