Ghost/core/test/unit/admin_spec.js

128 lines
4.2 KiB
JavaScript
Raw Normal View History

2013-11-12 01:35:17 +04:00
/*globals describe, beforeEach, afterEach, it*/
var fs = require('fs-extra'),
should = require('should'),
sinon = require('sinon'),
when = require('when'),
storage = require('../../server/storage'),
// Stuff we are testing
admin = require('../../server/controllers/admin');
2013-11-11 14:37:09 +04:00
describe('Admin Controller', function () {
describe('uploader', function () {
var req, res, store;
2013-11-11 14:37:09 +04:00
beforeEach(function () {
req = {
2013-11-11 14:37:09 +04:00
files: {
uploadimage: {
path: "/tmp/TMPFILEID"
}
2013-11-11 14:37:09 +04:00
}
};
res = {
2013-11-11 14:37:09 +04:00
send: function () {
}
};
store = sinon.stub();
store.save = sinon.stub().returns(when('URL'));
store.exists = sinon.stub().returns(when(true));
store.destroy = sinon.stub().returns(when());
sinon.stub(storage, 'get_storage').returns(store);
});
afterEach(function () {
storage.get_storage.restore();
});
2013-11-11 14:37:09 +04:00
describe('can not upload invalid file', function () {
it('should return 415 for invalid file type', function () {
res.send = sinon.stub();
req.files.uploadimage.name = 'INVALID.FILE';
req.files.uploadimage.type = 'application/octet-stream';
admin.uploader(req, res);
res.send.calledOnce.should.be.true;
res.send.args[0][0].should.equal(415);
res.send.args[0][1].should.equal('Unsupported Media Type');
});
});
2013-11-11 14:37:09 +04:00
describe('can not upload file with valid extension but invalid type', function () {
it('should return 415 for invalid file type', function () {
res.send = sinon.stub();
req.files.uploadimage.name = 'INVALID.jpg';
2013-11-12 01:35:17 +04:00
req.files.uploadimage.type = 'application/octet-stream';
admin.uploader(req, res);
res.send.calledOnce.should.be.true;
res.send.args[0][0].should.equal(415);
res.send.args[0][1].should.equal('Unsupported Media Type');
});
});
2013-11-11 14:37:09 +04:00
describe('valid file', function () {
2013-11-11 14:37:09 +04:00
beforeEach(function () {
req.files.uploadimage.name = 'IMAGE.jpg';
req.files.uploadimage.type = 'image/jpeg';
sinon.stub(fs, 'unlink').yields();
});
2013-11-11 14:37:09 +04:00
afterEach(function () {
fs.unlink.restore();
});
2013-11-11 14:37:09 +04:00
it('can upload jpg', function (done) {
sinon.stub(res, 'send', function (data) {
data.should.not.equal(415);
return done();
});
admin.uploader(req, res);
});
2013-11-11 14:37:09 +04:00
it('cannot upload jpg with incorrect extension', function (done) {
req.files.uploadimage.name = 'IMAGE.xjpg';
2013-11-11 14:37:09 +04:00
sinon.stub(res, 'send', function (data) {
data.should.equal(415);
return done();
});
admin.uploader(req, res);
});
2013-11-11 14:37:09 +04:00
it('can upload png', function (done) {
req.files.uploadimage.name = 'IMAGE.png';
req.files.uploadimage.type = 'image/png';
2013-11-11 14:37:09 +04:00
sinon.stub(res, 'send', function (data) {
data.should.not.equal(415);
return done();
});
admin.uploader(req, res);
});
2013-11-11 14:37:09 +04:00
it('can upload gif', function (done) {
req.files.uploadimage.name = 'IMAGE.gif';
req.files.uploadimage.type = 'image/gif';
2013-11-11 14:37:09 +04:00
sinon.stub(res, 'send', function (data) {
data.should.not.equal(415);
return done();
});
admin.uploader(req, res);
});
2013-11-11 14:37:09 +04:00
it('should send correct url', function (done) {
sinon.stub(res, 'send', function (data) {
data.should.equal('URL');
return done();
});
admin.uploader(req, res);
});
});
});
2013-11-11 14:37:09 +04:00
});