2017-06-19 22:37:56 +02:00
|
|
|
const assert = require('assert');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const proxyquire = require('proxyquire');
|
2017-06-20 19:21:11 +02:00
|
|
|
const crypto = require('crypto');
|
2017-06-19 22:37:56 +02:00
|
|
|
|
|
|
|
const conf = require('../server/config.js');
|
|
|
|
conf.notLocalHost = true;
|
|
|
|
|
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.spy();
|
|
|
|
const del = sinon.stub();
|
2017-06-19 22:37:56 +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-19 22:37:56 +02:00
|
|
|
|
2017-06-20 21:18:14 +02:00
|
|
|
const fsStub = {};
|
2017-06-19 22:37:56 +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-19 22:37:56 +02:00
|
|
|
logStub.info = sinon.stub();
|
|
|
|
logStub.error = sinon.stub();
|
|
|
|
|
2017-06-20 21:18:14 +02:00
|
|
|
const s3Stub = {};
|
2017-06-19 22:37:56 +02:00
|
|
|
s3Stub.headObject = sinon.stub();
|
|
|
|
s3Stub.getObject = sinon.stub();
|
|
|
|
s3Stub.upload = sinon.stub();
|
|
|
|
s3Stub.deleteObject = sinon.stub();
|
|
|
|
|
2017-06-20 21:18:14 +02:00
|
|
|
const awsStub = {
|
|
|
|
S3: function() {
|
|
|
|
return s3Stub;
|
|
|
|
}
|
|
|
|
};
|
2017-06-19 22:37:56 +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-19 22:37:56 +02:00
|
|
|
'aws-sdk': awsStub
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Testing Length using aws', function() {
|
|
|
|
it('Filesize returns properly if id exists', function() {
|
2017-06-20 21:18:14 +02:00
|
|
|
s3Stub.headObject.callsArgWith(1, null, { ContentLength: 1 });
|
|
|
|
return storage
|
|
|
|
.length('123')
|
|
|
|
.then(reply => assert.equal(reply, 1))
|
|
|
|
.catch(err => assert.fail());
|
|
|
|
});
|
2017-06-19 22:37:56 +02:00
|
|
|
|
|
|
|
it('Filesize fails if the id does not exist', function() {
|
|
|
|
s3Stub.headObject.callsArgWith(1, new Error(), null);
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.length('123')
|
|
|
|
.then(_reply => assert.fail())
|
|
|
|
.catch(err => assert(1));
|
|
|
|
});
|
2017-06-19 22:37:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Testing Get using aws', function() {
|
|
|
|
it('Should not error out when the file exists', function() {
|
2017-06-20 21:18:14 +02:00
|
|
|
const spy = sinon.spy();
|
2017-06-19 22:37:56 +02:00
|
|
|
s3Stub.getObject.returns({
|
2017-06-20 19:21:11 +02:00
|
|
|
createReadStream: spy
|
2017-06-19 22:37:56 +02:00
|
|
|
});
|
2017-06-20 19:21:11 +02:00
|
|
|
|
|
|
|
storage.get('123');
|
|
|
|
assert(spy.calledOnce);
|
2017-06-20 21:18:14 +02:00
|
|
|
});
|
2017-06-19 22:37:56 +02:00
|
|
|
|
|
|
|
it('Should error when the file does not exist', function() {
|
2017-06-20 21:18:14 +02:00
|
|
|
const err = function() {
|
|
|
|
throw new Error();
|
|
|
|
};
|
|
|
|
const spy = sinon.spy(err);
|
2017-06-19 22:37:56 +02:00
|
|
|
s3Stub.getObject.returns({
|
2017-06-20 19:21:11 +02:00
|
|
|
createReadStream: spy
|
2017-06-19 22:37:56 +02:00
|
|
|
});
|
2017-06-20 19:21:11 +02:00
|
|
|
|
|
|
|
assert.equal(storage.get('123'), null);
|
|
|
|
assert(spy.threw());
|
2017-06-20 21:18:14 +02:00
|
|
|
});
|
2017-06-19 22:37:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Testing Set using aws', function() {
|
2017-06-20 19:21:11 +02:00
|
|
|
beforeEach(function() {
|
|
|
|
expire.reset();
|
2017-06-20 21:18:14 +02:00
|
|
|
});
|
2017-06-20 19:21:11 +02:00
|
|
|
|
|
|
|
after(function() {
|
|
|
|
crypto.randomBytes.restore();
|
2017-06-20 21:18:14 +02:00
|
|
|
});
|
2017-06-20 19:21:11 +02:00
|
|
|
|
2017-06-19 22:37:56 +02:00
|
|
|
it('Should pass when the file is successfully uploaded and no bitly key', function() {
|
|
|
|
conf.bitly_key = null;
|
2017-06-20 21:18:14 +02:00
|
|
|
const buf = Buffer.alloc(10);
|
2017-06-20 19:21:11 +02:00
|
|
|
sinon.stub(crypto, 'randomBytes').returns(buf);
|
2017-06-19 22:37:56 +02:00
|
|
|
s3Stub.upload.callsArgWith(1, null, {});
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.set('123', {}, 'Filename.moz', 'url.com')
|
|
|
|
.then(reply => {
|
|
|
|
assert.equal(reply.uuid, buf.toString('hex'));
|
|
|
|
assert.equal(reply.url, 'url.com');
|
|
|
|
assert.notEqual(reply.uuid, null);
|
|
|
|
assert(expire.calledOnce);
|
|
|
|
assert(expire.calledWith('123', 86400000));
|
|
|
|
})
|
|
|
|
.catch(err => assert.fail());
|
|
|
|
});
|
2017-06-19 22:37:56 +02:00
|
|
|
|
|
|
|
it('Should fail if there was an error during uploading', function() {
|
|
|
|
s3Stub.upload.callsArgWith(1, new Error(), null);
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.set('123', {}, 'Filename.moz', 'url.com')
|
|
|
|
.then(_reply => assert.fail())
|
|
|
|
.catch(err => assert(1));
|
|
|
|
});
|
2017-06-19 22:37:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Testing Delete from aws', function() {
|
|
|
|
it('Returns successfully if the id is deleted off aws', function() {
|
|
|
|
hget.callsArgWith(2, null, 'delete_token');
|
|
|
|
s3Stub.deleteObject.callsArgWith(1, null, {});
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.delete('file_id', 'delete_token')
|
|
|
|
.then(_reply => assert(1), err => assert.fail());
|
|
|
|
});
|
2017-06-19 22:37:56 +02:00
|
|
|
|
|
|
|
it('Delete fails if id exists locally but does not in aws', function() {
|
|
|
|
hget.callsArgWith(2, null, 'delete_token');
|
|
|
|
s3Stub.deleteObject.callsArgWith(1, new Error(), {});
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.delete('file_id', 'delete_token')
|
|
|
|
.then(_reply => assert.fail(), err => assert(1));
|
|
|
|
});
|
2017-06-19 22:37:56 +02:00
|
|
|
|
|
|
|
it('Delete fails if the delete token does not match', function() {
|
|
|
|
hget.callsArgWith(2, null, {});
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.delete('Filename.moz', 'delete_token')
|
|
|
|
.then(_reply => assert.fail())
|
|
|
|
.catch(err => assert(1));
|
|
|
|
});
|
2017-06-19 22:37:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Testing Forced Delete from aws', function() {
|
|
|
|
it('Deletes properly if id exists', function() {
|
|
|
|
s3Stub.deleteObject.callsArgWith(1, null, {});
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.forceDelete('file_id', 'delete_token')
|
|
|
|
.then(_reply => assert(1), err => assert.fail());
|
|
|
|
});
|
2017-06-19 22:37:56 +02:00
|
|
|
|
|
|
|
it('Deletes fails if id does not exist', function() {
|
|
|
|
s3Stub.deleteObject.callsArgWith(1, new Error(), {});
|
2017-06-20 21:18:14 +02:00
|
|
|
return storage
|
|
|
|
.forceDelete('file_id')
|
|
|
|
.then(_reply => assert.fail(), err => assert(1));
|
|
|
|
});
|
|
|
|
});
|