2017-06-10 01:31:34 +02:00
|
|
|
const assert = require('assert');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const proxyquire = require('proxyquire');
|
|
|
|
|
2017-06-24 05:01:32 +02:00
|
|
|
// const conf = require('../server/config.js');
|
2017-06-10 01:31:34 +02:00
|
|
|
|
2017-06-20 21:18:14 +02:00
|
|
|
const redisStub = {};
|
|
|
|
const exists = sinon.stub();
|
|
|
|
const hget = sinon.stub();
|
|
|
|
const hmset = sinon.stub();
|
|
|
|
const expire = sinon.stub();
|
|
|
|
const del = sinon.stub();
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
redisStub.createClient = function() {
|
|
|
|
return {
|
|
|
|
on: sinon.spy(),
|
|
|
|
exists: exists,
|
|
|
|
hget: hget,
|
|
|
|
hmset: hmset,
|
|
|
|
expire: expire,
|
|
|
|
del: del
|
2017-06-20 21:18:14 +02:00
|
|
|
};
|
|
|
|
};
|
2017-06-10 01:31:34 +02:00
|
|
|
|
2017-06-20 21:18:14 +02:00
|
|
|
const fsStub = {};
|
2017-06-10 01:31:34 +02:00
|
|
|
fsStub.statSync = sinon.stub();
|
|
|
|
fsStub.createReadStream = sinon.stub();
|
|
|
|
fsStub.createWriteStream = sinon.stub();
|
|
|
|
fsStub.unlinkSync = sinon.stub();
|
|
|
|
|
2017-06-20 21:18:14 +02:00
|
|
|
const logStub = {};
|
2017-06-10 01:31:34 +02:00
|
|
|
logStub.info = sinon.stub();
|
|
|
|
logStub.error = sinon.stub();
|
|
|
|
|
2017-07-11 21:47:40 +02:00
|
|
|
const storage = proxyquire('../../server/storage', {
|
2017-06-20 21:18:14 +02:00
|
|
|
redis: redisStub,
|
|
|
|
fs: fsStub,
|
|
|
|
'./log.js': function() {
|
|
|
|
return logStub;
|
|
|
|
}
|
2017-06-10 01:31:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Testing Exists from local filesystem', function() {
|
|
|
|
it('Exists returns true when file exists', function() {
|
|
|
|
exists.callsArgWith(1, null, 1);
|
2017-06-24 02:06:08 +02:00
|
|
|
return storage
|
|
|
|
.exists('test')
|
|
|
|
.then(() => assert(1))
|
|
|
|
.catch(err => assert.fail());
|
2017-06-20 21:18:14 +02:00
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
it('Exists returns false when file does not exist', function() {
|
|
|
|
exists.callsArgWith(1, null, 0);
|
2017-06-24 02:06:08 +02:00
|
|
|
return storage
|
|
|
|
.exists('test')
|
|
|
|
.then(() => assert.fail())
|
|
|
|
.catch(err => assert(1));
|
2017-06-20 21:18:14 +02:00
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Testing Filename from local filesystem', function() {
|
|
|
|
it('Filename returns properly if id exists', function() {
|
|
|
|
hget.callsArgWith(2, null, 'Filename.moz');
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.filename('test')
|
|
|
|
.then(_reply => assert(1))
|
|
|
|
.catch(err => assert.fail());
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
it('Filename fails if id does not exist', function() {
|
|
|
|
hget.callsArgWith(2, null, 'Filename.moz');
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.filename('test')
|
|
|
|
.then(_reply => assert.fail())
|
|
|
|
.catch(err => assert(1));
|
|
|
|
});
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
describe('Testing Length from local filesystem', function() {
|
|
|
|
it('Filesize returns properly if id exists', function() {
|
2017-06-20 21:18:14 +02:00
|
|
|
fsStub.statSync.returns({ size: 10 });
|
|
|
|
return storage
|
|
|
|
.length('Filename.moz')
|
|
|
|
.then(_reply => assert(1))
|
|
|
|
.catch(err => assert.fail());
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
it('Filesize fails if the id does not exist', function() {
|
|
|
|
fsStub.statSync.returns(null);
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.length('Filename.moz')
|
|
|
|
.then(_reply => assert.fail())
|
|
|
|
.catch(err => assert(1));
|
|
|
|
});
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
describe('Testing Get from local filesystem', function() {
|
|
|
|
it('Get returns properly if id exists', function() {
|
|
|
|
fsStub.createReadStream.returns(1);
|
|
|
|
if (storage.get('Filename.moz')) {
|
2017-06-20 21:18:14 +02:00
|
|
|
assert(1);
|
2017-06-10 01:31:34 +02:00
|
|
|
} else {
|
|
|
|
assert.fail();
|
|
|
|
}
|
2017-06-20 21:18:14 +02:00
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
it('Get fails if the id does not exist', function() {
|
|
|
|
fsStub.createReadStream.returns(null);
|
|
|
|
if (storage.get('Filename.moz')) {
|
|
|
|
assert.fail();
|
|
|
|
} else {
|
|
|
|
assert(1);
|
|
|
|
}
|
2017-06-20 21:18:14 +02:00
|
|
|
});
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
describe('Testing Set to local filesystem', function() {
|
|
|
|
it('Successfully writes the file to the local filesystem', function() {
|
2017-06-20 21:18:14 +02:00
|
|
|
const stub = sinon.stub();
|
2017-07-20 21:50:20 +02:00
|
|
|
stub.withArgs('finish', sinon.match.any).callsArgWithAsync(1);
|
2017-06-10 01:31:34 +02:00
|
|
|
stub.withArgs('error', sinon.match.any).returns(1);
|
2017-06-20 21:18:14 +02:00
|
|
|
fsStub.createWriteStream.returns({ on: stub });
|
2017-06-10 01:31:34 +02:00
|
|
|
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
2017-07-20 21:50:20 +02:00
|
|
|
.set('test', { pipe: sinon.stub(), on: sinon.stub() }, 'Filename.moz', {})
|
2017-07-10 21:30:17 +02:00
|
|
|
.then(() => {
|
|
|
|
assert(1);
|
2017-06-20 21:18:14 +02:00
|
|
|
})
|
|
|
|
.catch(err => assert.fail());
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
it('Fails when the file is not properly written to the local filesystem', function() {
|
2017-06-20 21:18:14 +02:00
|
|
|
const stub = sinon.stub();
|
|
|
|
stub.withArgs('error', sinon.match.any).callsArgWithAsync(1);
|
2017-06-10 01:31:34 +02:00
|
|
|
stub.withArgs('close', sinon.match.any).returns(1);
|
2017-06-20 21:18:14 +02:00
|
|
|
fsStub.createWriteStream.returns({ on: stub });
|
2017-06-10 01:31:34 +02:00
|
|
|
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.set('test', { pipe: sinon.stub() }, 'Filename.moz', 'moz.la')
|
|
|
|
.then(_reply => assert.fail())
|
|
|
|
.catch(err => assert(1));
|
|
|
|
});
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
describe('Testing Delete from local filesystem', function() {
|
|
|
|
it('Deletes properly if id exists', function() {
|
|
|
|
hget.callsArgWith(2, null, '123');
|
|
|
|
fsStub.unlinkSync.returns(1);
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.delete('Filename.moz', '123')
|
|
|
|
.then(reply => assert(reply))
|
|
|
|
.catch(err => assert.fail());
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
it('Delete fails if id does not exist', function() {
|
|
|
|
hget.callsArgWith(2, null, null);
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.delete('Filename.moz', '123')
|
|
|
|
.then(_reply => assert.fail())
|
|
|
|
.catch(err => assert(1));
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
it('Delete fails if the delete token does not match', function() {
|
|
|
|
hget.callsArgWith(2, null, null);
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.delete('Filename.moz', '123')
|
|
|
|
.then(_reply => assert.fail())
|
|
|
|
.catch(err => assert(1));
|
|
|
|
});
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
describe('Testing Forced Delete from local filesystem', function() {
|
|
|
|
it('Deletes properly if id exists', function() {
|
|
|
|
fsStub.unlinkSync.returns(1);
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage.forceDelete('Filename.moz').then(reply => assert(reply));
|
|
|
|
});
|
2017-06-10 01:31:34 +02:00
|
|
|
|
|
|
|
it('Deletes fails if id does not exist, but no reject is called', function() {
|
|
|
|
fsStub.unlinkSync.returns(0);
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage.forceDelete('Filename.moz').then(reply => assert(!reply));
|
|
|
|
});
|
|
|
|
});
|