2013-09-08 00:55:01 +04:00
|
|
|
/*globals describe, beforeEach, it*/
|
2013-09-13 18:12:38 +04:00
|
|
|
var testUtils = require('./testUtils'),
|
2013-09-28 17:54:26 +04:00
|
|
|
fs = require('fs-extra'),
|
2013-09-08 00:55:01 +04:00
|
|
|
should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
2013-09-13 18:12:38 +04:00
|
|
|
when = require('when'),
|
2013-09-08 00:55:01 +04:00
|
|
|
|
2013-09-13 18:12:38 +04:00
|
|
|
// Stuff we are testing
|
|
|
|
admin = require('../../server/controllers/admin');
|
2013-09-19 10:26:26 +04:00
|
|
|
|
2013-09-08 00:55:01 +04:00
|
|
|
describe('Admin Controller', function() {
|
|
|
|
describe('uploader', function() {
|
|
|
|
|
|
|
|
var req;
|
|
|
|
var res;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
req = {
|
|
|
|
files: {
|
|
|
|
uploadimage: {
|
|
|
|
path: "/tmp/TMPFILEID"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
res = {
|
|
|
|
send: function(){}
|
|
|
|
};
|
2013-09-28 17:54:26 +04:00
|
|
|
|
|
|
|
// localfilesystem.save = sinon.stub().returns(when('URL'));
|
2013-09-08 00:55:01 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('can not upload invalid file', function() {
|
2013-10-21 00:25:00 +04:00
|
|
|
it('should return 415 for invalid file type', function() {
|
2013-09-08 00:55:01 +04:00
|
|
|
res.send = sinon.stub();
|
2013-09-19 10:26:26 +04:00
|
|
|
req.files.uploadimage.name = 'INVALID.FILE';
|
|
|
|
req.files.uploadimage.type = 'application/octet-stream'
|
2013-09-08 00:55:01 +04:00
|
|
|
admin.uploader(req, res);
|
|
|
|
res.send.calledOnce.should.be.true;
|
2013-10-21 00:25:00 +04:00
|
|
|
res.send.args[0][0].should.equal(415);
|
|
|
|
res.send.args[0][1].should.equal('Unsupported Media Type');
|
2013-09-08 00:55:01 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-09-19 10:26:26 +04:00
|
|
|
describe('can not upload file with valid extension but invalid type', function() {
|
2013-10-21 00:25:00 +04:00
|
|
|
it('should return 415 for invalid file type', function() {
|
2013-09-19 10:26:26 +04:00
|
|
|
res.send = sinon.stub();
|
|
|
|
req.files.uploadimage.name = 'INVALID.jpg';
|
|
|
|
req.files.uploadimage.type = 'application/octet-stream'
|
|
|
|
admin.uploader(req, res);
|
|
|
|
res.send.calledOnce.should.be.true;
|
2013-10-21 00:25:00 +04:00
|
|
|
res.send.args[0][0].should.equal(415);
|
|
|
|
res.send.args[0][1].should.equal('Unsupported Media Type');
|
2013-09-19 10:26:26 +04:00
|
|
|
});
|
|
|
|
});
|
2013-09-13 11:24:28 +04:00
|
|
|
|
2013-09-08 00:55:01 +04:00
|
|
|
describe('valid file', function() {
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2013-09-19 10:26:26 +04:00
|
|
|
req.files.uploadimage.name = 'IMAGE.jpg';
|
|
|
|
req.files.uploadimage.type = 'image/jpeg';
|
2013-09-18 12:59:42 +04:00
|
|
|
sinon.stub(fs, 'unlink').yields();
|
2013-09-28 17:54:26 +04:00
|
|
|
var storage = sinon.stub();
|
|
|
|
storage.save = sinon.stub().returns(when('URL'));
|
|
|
|
sinon.stub(admin, 'get_storage').returns(storage);
|
2013-09-08 00:55:01 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
2013-09-18 12:59:42 +04:00
|
|
|
fs.unlink.restore();
|
2013-09-28 17:54:26 +04:00
|
|
|
admin.get_storage.restore();
|
2013-09-08 00:55:01 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can upload jpg', function(done) {
|
|
|
|
sinon.stub(res, 'send', function(data) {
|
2013-09-28 17:54:26 +04:00
|
|
|
data.should.not.equal(415);
|
2013-09-08 00:55:01 +04:00
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
admin.uploader(req, res);
|
|
|
|
});
|
|
|
|
|
2013-09-28 17:54:26 +04:00
|
|
|
it('cannot upload jpg with incorrect extension', function(done) {
|
2013-09-19 10:26:26 +04:00
|
|
|
req.files.uploadimage.name = 'IMAGE.xjpg';
|
|
|
|
sinon.stub(res, 'send', function(data) {
|
2013-09-28 17:54:26 +04:00
|
|
|
data.should.equal(415);
|
2013-09-19 10:26:26 +04:00
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
admin.uploader(req, res);
|
|
|
|
});
|
|
|
|
|
2013-09-08 00:55:01 +04:00
|
|
|
it('can upload png', function(done) {
|
2013-09-19 10:26:26 +04:00
|
|
|
req.files.uploadimage.name = 'IMAGE.png';
|
|
|
|
req.files.uploadimage.type = 'image/png';
|
2013-09-08 00:55:01 +04:00
|
|
|
sinon.stub(res, 'send', function(data) {
|
2013-09-28 17:54:26 +04:00
|
|
|
data.should.not.equal(415);
|
2013-09-08 00:55:01 +04:00
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
admin.uploader(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can upload gif', function(done) {
|
2013-09-19 10:26:26 +04:00
|
|
|
req.files.uploadimage.name = 'IMAGE.gif';
|
|
|
|
req.files.uploadimage.type = 'image/gif';
|
2013-09-08 00:55:01 +04:00
|
|
|
sinon.stub(res, 'send', function(data) {
|
2013-09-28 17:54:26 +04:00
|
|
|
data.should.not.equal(415);
|
2013-09-08 00:55:01 +04:00
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
admin.uploader(req, res);
|
|
|
|
});
|
|
|
|
|
2013-09-18 12:59:42 +04:00
|
|
|
it('should not leave temporary file when uploading', function(done) {
|
|
|
|
sinon.stub(res, 'send', function(data) {
|
|
|
|
fs.unlink.calledOnce.should.be.true;
|
|
|
|
fs.unlink.args[0][0].should.equal('/tmp/TMPFILEID');
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
admin.uploader(req, res);
|
|
|
|
});
|
2013-09-08 00:55:01 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|