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