From 76af1c92e6946a7dfe2df31aee1266503fedf384 Mon Sep 17 00:00:00 2001 From: Abhinav Adduri Date: Mon, 19 Jun 2017 13:37:56 -0700 Subject: [PATCH] finished storage tests --- server/portal_server.js | 4 +- server/storage.js | 4 +- test/aws.storage.test.js | 140 +++++++++++++++++++++++++++++++++++++ test/local.storage.test.js | 2 + 4 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 test/aws.storage.test.js diff --git a/server/portal_server.js b/server/portal_server.js index 8173a6eb..836d28b1 100644 --- a/server/portal_server.js +++ b/server/portal_server.js @@ -101,7 +101,7 @@ app.post('/delete/:id', (req, res) => { storage .delete(id, delete_token) - .then(err => { + .then(() => { if (!err) { log.info('Deleted:', id); res.sendStatus(200); @@ -112,7 +112,7 @@ app.post('/delete/:id', (req, res) => { app.post('/upload/:id', (req, res, next) => { if (!validateID(req.params.id)) { - res.send(404); + res.sendStatus(404); return; } diff --git a/server/storage.js b/server/storage.js index c6327d25..e9388290 100644 --- a/server/storage.js +++ b/server/storage.js @@ -206,7 +206,7 @@ function awsDelete(id, delete_token) { }; s3.deleteObject(params, function(err, _data) { - resolve(err); + err ? reject(err) : resolve(err); }); } }); @@ -222,7 +222,7 @@ function awsForceDelete(id) { }; s3.deleteObject(params, function(err, _data) { - resolve(err); + err ? reject(err) : resolve(err); }); }); } diff --git a/test/aws.storage.test.js b/test/aws.storage.test.js new file mode 100644 index 00000000..03ae8a6e --- /dev/null +++ b/test/aws.storage.test.js @@ -0,0 +1,140 @@ +const assert = require('assert'); +const sinon = require('sinon'); +const proxyquire = require('proxyquire'); + +const conf = require('../server/config.js'); +conf.notLocalHost = true; + +let redisStub = {}; +let exists = sinon.stub(); +let hget = sinon.stub(); +let hmset = sinon.stub(); +let expire = sinon.stub(); +let del = sinon.stub(); + +redisStub.createClient = function() { + return { + on: sinon.spy(), + exists: exists, + hget: hget, + hmset: hmset, + expire: expire, + del: del + } +} + +let fsStub = {}; +fsStub.statSync = sinon.stub(); +fsStub.createReadStream = sinon.stub(); +fsStub.createWriteStream = sinon.stub(); +fsStub.unlinkSync = sinon.stub(); + +let logStub = {}; +logStub.info = sinon.stub(); +logStub.error = sinon.stub(); + +let s3Stub = {}; +s3Stub.headObject = sinon.stub(); +s3Stub.getObject = sinon.stub(); +s3Stub.upload = sinon.stub(); +s3Stub.deleteObject = sinon.stub(); + +let awsStub = { + S3: function() { + return s3Stub; + } +} + +const storage = proxyquire('../server/storage', { + 'redis': redisStub, + 'fs': fsStub, + './log.js': function() { return logStub }, + 'aws-sdk': awsStub +}); + +describe('Testing Length using aws', function() { + it('Filesize returns properly if id exists', function() { + s3Stub.headObject.callsArgWith(1, null, {ContentLength: 1}); + return storage.length('123') + .then(reply => assert(reply === 1)) + .catch(err => assert.fail()) + }) + + it('Filesize fails if the id does not exist', function() { + s3Stub.headObject.callsArgWith(1, new Error(), null); + return storage.length('123') + .then(reply => assert.fail()) + .catch(err => assert(1)) + }) +}); + +describe('Testing Get using aws', function() { + it('Should not error out when the file exists', function() { + s3Stub.getObject.returns({ + createReadStream: function() { return 1; } + }); + assert(storage.get('123') === 1); + }) + + it('Should error when the file does not exist', function() { + s3Stub.getObject.returns({ + createReadStream: function() { return null; } + }); + assert(storage.get('123') === null); + }) +}); + +describe('Testing Set using aws', function() { + it('Should pass when the file is successfully uploaded and no bitly key', function() { + conf.bitly_key = null; + s3Stub.upload.callsArgWith(1, null, {}); + return storage.set('123', {}, 'Filename.moz', 'url.com') + .then(reply => assert(reply.url === 'url.com' && reply.uuid !== null)) + .catch(err => assert.fail()); + }) + + it('Should fail if there was an error during uploading', function() { + s3Stub.upload.callsArgWith(1, new Error(), null); + return storage.set('123', {}, 'Filename.moz', 'url.com') + .then(reply => assert.fail()) + .catch(err => assert(1)); + }) +}); + +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, {}); + return storage.delete('file_id', 'delete_token') + .then(reply => assert(1), err => assert.fail()) + }) + + 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(), {}); + return storage.delete('file_id', 'delete_token') + .then(reply => assert.fail(), err => assert(1)) + }) + + it('Delete fails if the delete token does not match', function() { + hget.callsArgWith(2, null, {}); + return storage.delete('Filename.moz', 'delete_token') + .then(reply => assert.fail()) + .catch(reply => assert(1)) + }) +}); + +describe('Testing Forced Delete from aws', function() { + it('Deletes properly if id exists', function() { + s3Stub.deleteObject.callsArgWith(1, null, {}); + return storage.forceDelete('file_id', 'delete_token') + .then(reply => assert(1), err => assert.fail()); + }) + + it('Deletes fails if id does not exist', function() { + s3Stub.deleteObject.callsArgWith(1, new Error(), {}); + return storage.forceDelete('file_id') + .then(reply => assert.fail(), err => assert(1)) + }) + +}) \ No newline at end of file diff --git a/test/local.storage.test.js b/test/local.storage.test.js index 5df6f2eb..c4c6c9c5 100644 --- a/test/local.storage.test.js +++ b/test/local.storage.test.js @@ -2,6 +2,8 @@ const assert = require('assert'); const sinon = require('sinon'); const proxyquire = require('proxyquire'); +const conf = require('../server/config.js') +conf.notLocalHost = false; let redisStub = {}; let exists = sinon.stub();