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-08 00:55:01 +04:00
|
|
|
should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
2013-09-13 18:12:38 +04:00
|
|
|
when = require('when'),
|
|
|
|
fs = require('fs-extra'),
|
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-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(){}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('can not upload invalid file', function() {
|
|
|
|
it('should return 404 for invalid file type', function() {
|
|
|
|
res.send = sinon.stub();
|
|
|
|
req.files.uploadimage.name = "INVALID.FILE";
|
|
|
|
admin.uploader(req, res);
|
|
|
|
res.send.calledOnce.should.be.true;
|
|
|
|
res.send.args[0][0].should.equal(404);
|
|
|
|
res.send.args[0][1].should.equal('Invalid filetype');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-09-13 11:24:28 +04:00
|
|
|
|
2013-09-08 00:55:01 +04:00
|
|
|
describe('valid file', function() {
|
|
|
|
|
|
|
|
var clock;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
req.files.uploadimage.name = "IMAGE.jpg";
|
|
|
|
sinon.stub(fs, 'mkdirs').yields();
|
|
|
|
sinon.stub(fs, 'copy').yields();
|
2013-09-18 12:59:42 +04:00
|
|
|
sinon.stub(fs, 'unlink').yields();
|
2013-09-08 00:55:01 +04:00
|
|
|
sinon.stub(fs, 'exists').yields(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
fs.mkdirs.restore();
|
|
|
|
fs.copy.restore();
|
2013-09-18 12:59:42 +04:00
|
|
|
fs.unlink.restore();
|
2013-09-08 00:55:01 +04:00
|
|
|
fs.exists.restore();
|
|
|
|
clock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can upload jpg', function(done) {
|
2013-09-13 18:12:38 +04:00
|
|
|
clock = sinon.useFakeTimers(42);
|
2013-09-08 00:55:01 +04:00
|
|
|
sinon.stub(res, 'send', function(data) {
|
|
|
|
data.should.not.equal(404);
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
admin.uploader(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can upload png', function(done) {
|
|
|
|
req.files.uploadimage.name = "IMAGE.png";
|
2013-09-13 18:12:38 +04:00
|
|
|
clock = sinon.useFakeTimers(42);
|
2013-09-08 00:55:01 +04:00
|
|
|
sinon.stub(res, 'send', function(data) {
|
|
|
|
data.should.not.equal(404);
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
admin.uploader(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can upload gif', function(done) {
|
|
|
|
req.files.uploadimage.name = "IMAGE.gif";
|
2013-09-13 18:12:38 +04:00
|
|
|
clock = sinon.useFakeTimers(42);
|
2013-09-08 00:55:01 +04:00
|
|
|
sinon.stub(res, 'send', function(data) {
|
|
|
|
data.should.not.equal(404);
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
admin.uploader(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should send correct path to image when today is in Sep 2013', function(done) {
|
2013-09-14 06:15:30 +04:00
|
|
|
// Sat Sep 07 2013 21:24
|
|
|
|
clock = sinon.useFakeTimers(new Date(2013, 8, 7, 21, 24).getTime());
|
2013-09-08 00:55:01 +04:00
|
|
|
sinon.stub(res, 'send', function(data) {
|
|
|
|
data.should.equal('/content/images/2013/Sep/IMAGE.jpg');
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
return admin.uploader(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should send correct path to image when today is in Jan 2014', function(done) {
|
2013-09-14 06:15:30 +04:00
|
|
|
// Jan 1 2014 12:00
|
|
|
|
clock = sinon.useFakeTimers(new Date(2014, 0, 1, 12).getTime());
|
2013-09-08 00:55:01 +04:00
|
|
|
sinon.stub(res, 'send', function(data) {
|
|
|
|
data.should.equal('/content/images/2014/Jan/IMAGE.jpg');
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
admin.uploader(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can upload two different images with the same name without overwriting the first', function(done) {
|
2013-09-14 06:15:30 +04:00
|
|
|
// Sun Sep 08 2013 10:57
|
|
|
|
clock = sinon.useFakeTimers(new Date(2013, 8, 8, 10, 57).getTime());
|
2013-09-08 00:55:01 +04:00
|
|
|
fs.exists.withArgs('content/images/2013/Sep/IMAGE.jpg').yields(true);
|
|
|
|
fs.exists.withArgs('content/images/2013/Sep/IMAGE-1.jpg').yields(false);
|
|
|
|
|
2013-09-21 18:35:15 +04:00
|
|
|
// if on windows need to setup with back slashes
|
|
|
|
// doesn't hurt for the test to cope with both
|
|
|
|
fs.exists.withArgs('content\\images\\2013\\Sep\\IMAGE.jpg').yields(true);
|
|
|
|
fs.exists.withArgs('content\\images\\2013\\Sep\\IMAGE-1.jpg').yields(false);
|
|
|
|
|
2013-09-08 00:55:01 +04:00
|
|
|
sinon.stub(res, 'send', function(data) {
|
|
|
|
data.should.equal('/content/images/2013/Sep/IMAGE-1.jpg');
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
return admin.uploader(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can upload five different images with the same name without overwriting the first', function(done) {
|
2013-09-14 06:15:30 +04:00
|
|
|
// Sun Sep 08 2013 10:57
|
|
|
|
clock = sinon.useFakeTimers(new Date(2013, 8, 8, 10, 57).getTime());
|
2013-09-08 00:55:01 +04:00
|
|
|
fs.exists.withArgs('content/images/2013/Sep/IMAGE.jpg').yields(true);
|
|
|
|
fs.exists.withArgs('content/images/2013/Sep/IMAGE-1.jpg').yields(true);
|
|
|
|
fs.exists.withArgs('content/images/2013/Sep/IMAGE-2.jpg').yields(true);
|
|
|
|
fs.exists.withArgs('content/images/2013/Sep/IMAGE-3.jpg').yields(true);
|
|
|
|
fs.exists.withArgs('content/images/2013/Sep/IMAGE-4.jpg').yields(false);
|
|
|
|
|
2013-09-21 18:35:15 +04:00
|
|
|
// windows setup
|
|
|
|
fs.exists.withArgs('content\\images\\2013\\Sep\\IMAGE.jpg').yields(true);
|
|
|
|
fs.exists.withArgs('content\\images\\2013\\Sep\\IMAGE-1.jpg').yields(true);
|
|
|
|
fs.exists.withArgs('content\\images\\2013\\Sep\\IMAGE-2.jpg').yields(true);
|
|
|
|
fs.exists.withArgs('content\\images\\2013\\Sep\\IMAGE-3.jpg').yields(true);
|
|
|
|
fs.exists.withArgs('content\\images\\2013\\Sep\\IMAGE-4.jpg').yields(false);
|
|
|
|
|
2013-09-08 00:55:01 +04:00
|
|
|
sinon.stub(res, 'send', function(data) {
|
|
|
|
data.should.equal('/content/images/2013/Sep/IMAGE-4.jpg');
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
|
|
|
|
return admin.uploader(req, res);
|
|
|
|
});
|
2013-09-18 12:59:42 +04:00
|
|
|
|
|
|
|
it('should not leave temporary file when uploading', function(done) {
|
|
|
|
clock = sinon.useFakeTimers(new Date(2013, 8, 8, 10, 57).getTime());
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|