Something went wrong. Try again.
:rocket: A fully-featured and deeply tested Cloudinary Ghost storage adapter ghost.org/integrations/cloudinary
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278'use strict';
const chai = require('chai'), expect = chai.expect, sinon = require('sinon'), cloudinary = require('cloudinary').v2, path = require('path'), moment = require('moment'), CloudinaryAdapter = require(path.join(__dirname, '../../')), CloudinaryAdapterError = require(path.join(__dirname, '../../errors')).CloudinaryAdapterError, fixtures = require(path.join(__dirname, 'fixtures'));
let cloudinaryAdapter = null;
describe('save', function () { before(function () { cloudinaryAdapter = new CloudinaryAdapter(fixtures.sampleConfig()); });
it('should upload successfully', function (done) { const expectedUploadConfig = { "use_filename": true, "unique_filename": false, "phash": true, "overwrite": false, "invalidate": true, "folder": "", "tags": [], "public_id": "favicon" };
sinon.stub(cloudinary.uploader, 'upload'); cloudinary.uploader.upload .withArgs(fixtures.mockImage.path, expectedUploadConfig, sinon.match.any) .callsArgWith(2, null, fixtures.sampleApiResult());
sinon.stub(cloudinary, 'url').callsFake(function urlStub() { return 'http://res.cloudinary.com/blog-mornati-net/image/upload/q_auto/favicon.png'; });
cloudinaryAdapter.save(fixtures.mockImage).then(function (url) { expect(url).to.equals('http://res.cloudinary.com/blog-mornati-net/image/upload/q_auto/favicon.png'); done(); }); });
it('should upload successfully and generate random public id', function (done) { /* eslint one-var: ["error", "consecutive"] */ const configForRandomPublicId = fixtures.sampleConfig(), apiResult = fixtures.sampleApiResult();
configForRandomPublicId.upload.use_filename = false; apiResult.public_id = '06e288276dbab60c5fe48a51b03735b2';
const adapter = new CloudinaryAdapter(configForRandomPublicId), expectedUploadConfig = { "use_filename": false, "unique_filename": false, "phash": true, "overwrite": false, "invalidate": true, "folder": "", "tags": [] };
sinon.stub(cloudinary.uploader, 'upload'); cloudinary.uploader.upload .withArgs(fixtures.mockImage.path, expectedUploadConfig, sinon.match.any) .callsArgWith(2, null, apiResult);
sinon.stub(cloudinary, 'url').callsFake(function urlStub() { return 'http://res.cloudinary.com/blog-mornati-net/image/upload/q_auto/06e288276dbab60c5fe48a51b03735b2.png'; });
adapter.save(fixtures.mockImage).then(function (url) { expect(url).to.equals('http://res.cloudinary.com/blog-mornati-net/image/upload/q_auto/06e288276dbab60c5fe48a51b03735b2.png'); done(); }); });
it('should upload successfully with legacy config', function (done) { const adapter = new CloudinaryAdapter(fixtures.sampleLegacyConfig()), expectedUploadConfig = { "use_filename": true, "unique_filename": true, "phash": true, "overwrite": false, "invalidate": true, "public_id": "favicon" };
sinon.stub(cloudinary.uploader, 'upload'); cloudinary.uploader.upload .withArgs(fixtures.mockImage.path, expectedUploadConfig, sinon.match.any) .callsArgWith(2, null, fixtures.sampleApiResult());
sinon.stub(cloudinary, 'url').callsFake(function urlStub() { return 'https://res.cloudinary.com/blog-mornati-net/image/upload/q_auto:good/favicon.png'; });
adapter.save(fixtures.mockImage).then(function (url) { expect(url).to.equals('https://res.cloudinary.com/blog-mornati-net/image/upload/q_auto:good/favicon.png'); done(); }); });
it('should normalize image name', function (done) { const expectedUploadConfig = { "use_filename": true, "unique_filename": false, "phash": true, "overwrite": false, "invalidate": true, "folder": "", "tags": [], "public_id": "favicon-with-spaces" };
sinon.stub(cloudinary.uploader, 'upload') .withArgs(fixtures.mockImageWithSpacesInName.path, expectedUploadConfig, sinon.match.any) .callsArgWith(2, null, fixtures.sampleApiResult());
sinon.stub(cloudinary, 'url').callsFake(function urlStub() { return 'http://res.cloudinary.com/blog-mornati-net/image/upload/q_auto/favicon-with-spaces.png'; });
cloudinaryAdapter.save(fixtures.mockImageWithSpacesInName).then(function (url) { expect(url).equals('http://res.cloudinary.com/blog-mornati-net/image/upload/q_auto/favicon-with-spaces.png'); done(); }); });
it('should upload successfully with tags and folder', function (done) { let config = fixtures.sampleConfig(); config.upload.folder = 'blog.eexit.net/v3'; config.upload.tags = ['foo', 'bar'];
cloudinaryAdapter = new CloudinaryAdapter(config);
const expectedUploadConfig = { "use_filename": true, "unique_filename": false, "phash": true, "overwrite": false, "invalidate": true, "folder": "blog.eexit.net/v3", "tags": ["foo", "bar"], "public_id": "favicon" }, apiResult = Object.assign(fixtures.sampleApiResult(), { public_id: 'blog.eexit.net/v3/favicon', tags: ['foo', 'bar'], url: 'http://res.cloudinary.com/blog-mornati-net/image/upload/v1505580646/blog.eexit.net/v3/favicon.png', secure_url: 'https://res.cloudinary.com/blog-mornati-net/image/upload/v1505580646/blog.eexit.net/v3/favicon.png' });
sinon.stub(cloudinary.uploader, 'upload') .withArgs(fixtures.mockImage.path, expectedUploadConfig, sinon.match.any) .callsArgWith(2, null, apiResult);
sinon.stub(cloudinary, 'url').callsFake(function urlStub() { return 'http://res.cloudinary.com/blog-mornati-net/image/upload/q_auto/blog.eexit.net/v3/favicon.png'; });
cloudinaryAdapter.save(fixtures.mockImage).then(function (url) { expect(url).equals('http://res.cloudinary.com/blog-mornati-net/image/upload/q_auto/blog.eexit.net/v3/favicon.png'); done(); }); });
it('should upload successfully in a dated folder at root', function (done) { let config = fixtures.sampleConfig(); config.useDatedFolder = true; cloudinaryAdapter = new CloudinaryAdapter(config);
const date = moment(), year = date.format('YYYY'), month = date.format('MM'), expectedUploadConfig = { "folder": `${year}/${month}`, "public_id": "favicon" }, apiResult = Object.assign(fixtures.sampleApiResult(), { public_id: `${year}/${month}/favicon`, url: `http://res.cloudinary.com/foo/image/upload/v1/${year}/${month}/favicon.png` });
sinon.stub(cloudinary.uploader, 'upload') .withArgs(fixtures.mockImage.path, sinon.match(expectedUploadConfig), sinon.match.any) .callsArgWith(2, null, apiResult);
sinon.stub(cloudinary, 'url').callsFake(function urlStub() { return `http://res.cloudinary.com/foo/image/upload/v1/${year}/${month}/favicon.png`; });
cloudinaryAdapter.save(fixtures.mockImage).then(function (url) { expect(url).equals(`http://res.cloudinary.com/foo/image/upload/v1/${year}/${month}/favicon.png`); done(); }); });
it('should upload successfully in a dated folder in subfolder', function (done) { let config = fixtures.sampleConfig(); config.useDatedFolder = true; config.upload.folder = 'blog/eexit.net'; cloudinaryAdapter = new CloudinaryAdapter(config);
const date = moment(), year = date.format('YYYY'), month = date.format('MM'), expectedUploadConfig = { "folder": `blog/eexit.net/${year}/${month}`, "public_id": "favicon" }, apiResult = Object.assign(fixtures.sampleApiResult(), { public_id: `blog/eexit.net/${year}/${month}/favicon`, url: `http://res.cloudinary.com/foo/image/upload/v1/blog/eexit.net/${year}/${month}/favicon.png` });
sinon.stub(cloudinary.uploader, 'upload') .withArgs(fixtures.mockImage.path, sinon.match(expectedUploadConfig), sinon.match.any) .callsArgWith(2, null, apiResult);
sinon.stub(cloudinary, 'url').callsFake(function urlStub() { return `http://res.cloudinary.com/foo/image/upload/v1/blog/eexit.net/${year}/${month}/favicon.png`; });
cloudinaryAdapter.save(fixtures.mockImage).then(function (url) { expect(url).equals(`http://res.cloudinary.com/foo/image/upload/v1/blog/eexit.net/${year}/${month}/favicon.png`); done(); }); });
it('should return an error on api upload failure', function (done) { sinon.stub(cloudinary.uploader, 'upload'); sinon.stub(cloudinary, 'url'); cloudinary.uploader.upload.callsArgWith(2, { error: "some error" });
cloudinaryAdapter.save(fixtures.mockImage) .then(function () { done('expected error'); }) .catch(function (ex) { expect(ex).to.be.an.instanceOf(CloudinaryAdapterError); expect(ex.message).to.equal(`Could not upload image ${fixtures.mockImage.path}`); done(); }); });
it('should upload successfully with RetinaJS plugin', function (done) { const config = fixtures.sampleConfig(); config.plugins = { retinajs: { baseWidth: 48 } };
cloudinaryAdapter = new CloudinaryAdapter(config);
sinon.stub(cloudinary.uploader, 'upload').callsArgWith(2, null, fixtures.sampleApiResult()); sinon.stub(cloudinary, 'url').callsFake(function urlStub() { return 'https://res.cloudinary.com/blog-mornati-net/image/upload/q_auto:good/favicon.png'; });
cloudinaryAdapter.save(fixtures.mockImage) .then(function (url) { expect(url).equals('https://res.cloudinary.com/blog-mornati-net/image/upload/q_auto:good/favicon.png'); expect(cloudinary.uploader.upload.callCount).to.equal(2, 'cloudinary.uploader.upload should have been called 2 times'); expect(cloudinary.url.callCount).to.equal(1, 'cloudinary.url should have been called 1 time'); done(); }); });
afterEach(function () { // Unwraps the spy cloudinary.uploader.upload.restore(); cloudinary.url.restore(); });});