mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
Remove method stubs from upload API tests
This commit is contained in:
parent
f0e364b42e
commit
bdcb25a0cd
2 changed files with 135 additions and 78 deletions
|
@ -1,117 +1,163 @@
|
||||||
/*globals describe, beforeEach, afterEach, it*/
|
/*globals describe, afterEach, it*/
|
||||||
var fs = require('fs-extra'),
|
var tmp = require('tmp'),
|
||||||
should = require('should'),
|
should = require('should'),
|
||||||
sinon = require('sinon'),
|
|
||||||
Promise = require('bluebird'),
|
Promise = require('bluebird'),
|
||||||
storage = require('../../../server/storage'),
|
configUtils = require('../../utils/configUtils'),
|
||||||
|
extname = require('path').extname,
|
||||||
|
|
||||||
// Stuff we are testing
|
// Stuff we are testing
|
||||||
UploadAPI = require('../../../server/api/upload'),
|
UploadAPI = require('../../../server/api/upload'),
|
||||||
store;
|
uploadimage = {
|
||||||
|
name: '',
|
||||||
|
type: '',
|
||||||
|
path: ''
|
||||||
|
};
|
||||||
|
|
||||||
describe('Upload API', function () {
|
function setupAndTestUpload(filename, mimeType) {
|
||||||
// Doesn't test the DB
|
return new Promise(function (resolve, reject) {
|
||||||
|
// create a temp file (the uploaded file)
|
||||||
|
tmp.file({keep: true}, function (err, path/*, fd, cleanupFile*/) {
|
||||||
|
if (err) {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
|
||||||
afterEach(function () {
|
uploadimage.path = path;
|
||||||
storage.getStorage.restore();
|
uploadimage.name = filename;
|
||||||
fs.unlink.restore();
|
uploadimage.type = mimeType;
|
||||||
|
|
||||||
|
// create a temp directory (the directory that the API saves the file to)
|
||||||
|
tmp.dir({keep: true, unsafeCleanup: true}, function (err, path, cleanupDir) {
|
||||||
|
if (err) {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
configUtils.set({
|
||||||
|
paths: {
|
||||||
|
contentPath: path
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(function () {
|
UploadAPI.add({uploadimage: uploadimage})
|
||||||
store = sinon.stub();
|
.then(resolve)
|
||||||
store.save = sinon.stub().returns(Promise.resolve('URL'));
|
.catch(reject)
|
||||||
store.exists = sinon.stub().returns(Promise.resolve(true));
|
.finally(function () {
|
||||||
store.destroy = sinon.stub().returns(Promise.resolve());
|
// remove the temporary directory (file is unlinked by the API)
|
||||||
sinon.stub(storage, 'getStorage').returns(store);
|
cleanupDir();
|
||||||
sinon.stub(fs, 'unlink').yields();
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function testResult(filename, result) {
|
||||||
|
var base = filename,
|
||||||
|
ext = extname(base),
|
||||||
|
regex;
|
||||||
|
|
||||||
|
if (ext) {
|
||||||
|
base = base.split(ext)[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
regex = new RegExp('^/content/images/[0-9]{4}/[0-9]{2}/' + base + '.*' + ext + '$');
|
||||||
|
regex.test(result).should.be.true();
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('Upload API', function () {
|
||||||
|
afterEach(function () {
|
||||||
|
uploadimage = {
|
||||||
|
name: '',
|
||||||
|
type: 'application/octet-stream',
|
||||||
|
path: ''
|
||||||
|
};
|
||||||
|
|
||||||
|
configUtils.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
should.exist(UploadAPI);
|
should.exist(UploadAPI);
|
||||||
|
|
||||||
describe('invalid file', function () {
|
describe('invalid file extension and mime-type', function () {
|
||||||
it('should return 415 for invalid file type', function (done) {
|
it('should return 415 for invalid file extension', function (done) {
|
||||||
var uploadimage = {
|
setupAndTestUpload('test.invalid', 'application/octet-stream').then(function () {
|
||||||
name: 'INVALID.FILE',
|
done(new Error('Upload succeeded with invalid extension and mime-type'));
|
||||||
type: 'application/octet-stream',
|
}).catch(function (err) {
|
||||||
path: '/tmp/TMPFILEID'
|
should.exist(err);
|
||||||
};
|
err.statusCode.should.equal(415);
|
||||||
UploadAPI.add({uploadimage: uploadimage}).then(function () {
|
err.errorType.should.equal('UnsupportedMediaTypeError');
|
||||||
done(new Error('Upload suceeded with invalid file.'));
|
|
||||||
}, function (result) {
|
|
||||||
result.statusCode.should.equal(415);
|
|
||||||
result.errorType.should.equal('UnsupportedMediaTypeError');
|
|
||||||
done();
|
done();
|
||||||
});
|
}).catch(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('valid extension but invalid type', function () {
|
describe('valid extension but invalid type', function () {
|
||||||
it('should return 415 for invalid file type', function (done) {
|
it('should return 415 for mime-type', function (done) {
|
||||||
var uploadimage = {
|
setupAndTestUpload('test.jpg', 'application/octet-stream').then(function () {
|
||||||
name: 'INVALID.jpg',
|
done(new Error('Upload succeeded with invalid mime-type'));
|
||||||
type: 'application/octet-stream',
|
}).catch(function (err) {
|
||||||
path: '/tmp/TMPFILEID'
|
should.exist(err);
|
||||||
};
|
err.statusCode.should.equal(415);
|
||||||
UploadAPI.add({uploadimage: uploadimage}).then(function () {
|
err.errorType.should.equal('UnsupportedMediaTypeError');
|
||||||
done(new Error('Upload suceeded with invalid file.'));
|
|
||||||
}, function (result) {
|
|
||||||
result.statusCode.should.equal(415);
|
|
||||||
result.errorType.should.equal('UnsupportedMediaTypeError');
|
|
||||||
done();
|
done();
|
||||||
});
|
}).catch(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('valid file', function () {
|
describe('valid file', function () {
|
||||||
it('can upload jpg', function (done) {
|
it('can upload jpg', function (done) {
|
||||||
var uploadimage = {
|
var filename = 'test.jpg';
|
||||||
name: 'INVALID.jpg',
|
|
||||||
type: 'image/jpeg',
|
setupAndTestUpload(filename, 'image/jpeg').then(function (url) {
|
||||||
path: '/tmp/TMPFILEID'
|
testResult(filename, url);
|
||||||
};
|
|
||||||
UploadAPI.add({uploadimage: uploadimage}).then(function (result) {
|
|
||||||
result.should.equal('URL');
|
|
||||||
done();
|
done();
|
||||||
});
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('cannot upload jpg with incorrect extension', function (done) {
|
it('cannot upload jpg with incorrect extension', function (done) {
|
||||||
var uploadimage = {
|
setupAndTestUpload('invalid.xjpg', 'image/jpeg').then(function () {
|
||||||
name: 'INVALID.xjpg',
|
done(new Error('Upload succeeded with invalid extension'));
|
||||||
type: 'image/jpeg',
|
}).catch(function (err) {
|
||||||
path: '/tmp/TMPFILEID'
|
should.exist(err);
|
||||||
};
|
err.statusCode.should.equal(415);
|
||||||
UploadAPI.add({uploadimage: uploadimage}).then(function () {
|
err.errorType.should.equal('UnsupportedMediaTypeError');
|
||||||
done(new Error('Upload suceeded with invalid file.'));
|
|
||||||
}, function (result) {
|
|
||||||
result.statusCode.should.equal(415);
|
|
||||||
result.errorType.should.equal('UnsupportedMediaTypeError');
|
|
||||||
done();
|
done();
|
||||||
});
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can upload png', function (done) {
|
it('can upload png', function (done) {
|
||||||
var uploadimage = {
|
var filename = 'test.png';
|
||||||
name: 'INVALID.png',
|
|
||||||
type: 'image/png',
|
setupAndTestUpload(filename, 'image/png').then(function (url) {
|
||||||
path: '/tmp/TMPFILEID'
|
testResult(filename, url);
|
||||||
};
|
|
||||||
UploadAPI.add({uploadimage: uploadimage}).then(function (result) {
|
|
||||||
result.should.equal('URL');
|
|
||||||
done();
|
done();
|
||||||
});
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can upload gif', function (done) {
|
it('can upload gif', function (done) {
|
||||||
var uploadimage = {
|
var filename = 'test.gif';
|
||||||
name: 'INVALID.gif',
|
|
||||||
type: 'image/gif',
|
setupAndTestUpload(filename, 'image/gif').then(function (url) {
|
||||||
path: '/tmp/TMPFILEID'
|
testResult(filename, url);
|
||||||
};
|
|
||||||
UploadAPI.add({uploadimage: uploadimage}).then(function (result) {
|
|
||||||
result.should.equal('URL');
|
|
||||||
done();
|
done();
|
||||||
|
}).catch(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('missing file', function () {
|
||||||
|
it('throws an error if no file is provided', function (done) {
|
||||||
|
UploadAPI.add({}).then(function () {
|
||||||
|
done(new Error('Upload succeeded with invalid extension'));
|
||||||
|
}).catch(function (err) {
|
||||||
|
should.exist(err);
|
||||||
|
err.statusCode.should.equal(403);
|
||||||
|
err.errorType.should.equal('NoPermissionError');
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
13
package.json
13
package.json
|
@ -100,9 +100,20 @@
|
||||||
"should-http": "0.0.4",
|
"should-http": "0.0.4",
|
||||||
"sinon": "1.17.3",
|
"sinon": "1.17.3",
|
||||||
"supertest": "1.1.0",
|
"supertest": "1.1.0",
|
||||||
|
"tmp": "0.0.28",
|
||||||
"top-gh-contribs": "2.0.2"
|
"top-gh-contribs": "2.0.2"
|
||||||
},
|
},
|
||||||
"greenkeeper": {
|
"greenkeeper": {
|
||||||
"ignore": ["bower", "bluebird", "glob", "lodash", "mysql", "nodemailer", "pg", "showdown-ghost", "validator"]
|
"ignore": [
|
||||||
|
"bower",
|
||||||
|
"bluebird",
|
||||||
|
"glob",
|
||||||
|
"lodash",
|
||||||
|
"mysql",
|
||||||
|
"nodemailer",
|
||||||
|
"pg",
|
||||||
|
"showdown-ghost",
|
||||||
|
"validator"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue