diff --git a/mocks/libs/io.js b/mocks/libs/io.js index 2765c37..5aaf20b 100644 --- a/mocks/libs/io.js +++ b/mocks/libs/io.js @@ -1,6 +1,12 @@ /* global Buffer */ var fs = require('fs'); +function getFileStats(path, callback) { + callback(null, { + size: 1024 + }); +} + function createFile(path, buffer, offset, length, callback) { callback(null); } @@ -22,6 +28,7 @@ function renameFile(path, newPath, callback) { } module.exports = { + GetFileStats: getFileStats, CreateFile: createFile, WriteFileChunk: writeFileChunk, DeleteFile: deleteFile, diff --git a/routes/chunked-download-routes.js b/routes/chunked-download-routes.js index 99e5239..e394dc8 100644 --- a/routes/chunked-download-routes.js +++ b/routes/chunked-download-routes.js @@ -49,16 +49,17 @@ var routes = { }); }), "post": new apiModels.RouteHandler(routePrefix, function (req, res) { - var valid = validators.validateUploadRequest(req.body); + var valid = validators.validateDownloadRequest(req.body); if(valid !== validators.valid) { throw errorModels.ValidationError(valid); } - io.getFileStats(req.body.path, function(error, stats) { + io.GetFileStats(req.body.path, function(error, stats) { + errorHelper.genericErrorHandler(error); var download = new apiModels.Download(req.body, stats.size, chunkSize); download.configure(guidHelper.newGuid()); - dataCache.create(download.id, download, defaultTtl, function (error, success) { - errorHelper.genericErrorHandler(error); + dataCache.create(download.id, download, defaultTtl, function (createError, success) { + errorHelper.genericErrorHandler(createError); if(success) { res.json(new apiModels.ApiResponse(routePrefix, {}, download)); } else { @@ -103,7 +104,7 @@ function configure(cache, storage, options) { routePrefix = stringHelper.stripTrailingSlashes(options.routePrefix); } if(typeHelper.isNumber(options.chunkSize)) { - chunkSize = options.chunkSize + chunkSize = options.chunkSize; } } diff --git a/test/routes/chunked-download-routes-tests.js b/test/routes/chunked-download-routes-tests.js new file mode 100644 index 0000000..b084c43 --- /dev/null +++ b/test/routes/chunked-download-routes-tests.js @@ -0,0 +1,152 @@ +/* global describe, it, afterEach */ +var should = require('should'), + errorModels = require.main.require('libs/models/errorModels'), + guidHelper = require.main.require('libs/helpers/guidHelper'); + +describe("chunked-upload-routes.js", function() { + var io_mock = require.main.require('mocks/libs/io'), + cache_mock = require.main.require('mocks/libs/caching/localCache'), + routes = require.main.require('routes/chunked-download-routes')(cache_mock, io_mock, {debug:true, routePrefix:"/chunked/download"}), + downloadId = null; + + afterEach('reset the cache_mock', function() { + cache_mock.setReturnValue(true); + }); + + it('should return a route object', function() { + should.exist(routes); + routes.should.be.a.Object; + }); + + describe("#POST", function() { + it('should have a uri and handler', function() { + should.exist(routes.post); + + should.exist(routes.post.uri); + routes.post.uri.should.be.a.String; + + should.exist(routes.post.handler); + routes.post.handler.should.be.a.Function; + }); + + it('should create a new download if the request object is considered valid', function(done) { + routes.post.handler({ + body: { + path: "/i/am/a/path/to/a/destination" + } + }, { + json: function(data) { + should.exist(data); + should.exist(data.data); + data.data.should.be.a.Object; + data.data.count.should.be.a.Number; + data.data.chunkSize.should.be.a.Number; + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[8,9,a,b][0-9a-f]{3}-[0-9a-f]{12}$/ig.test(data.data.id).should.be.true; + downloadId = data.data.id; + done(); + } + }); + }); + + it('should throw a ServerError if the cache fails to store the upload data', function() { + cache_mock.setReturnValue(false); + (function() { + routes.post.handler({ + body: { + path: "/i/am/a/path/to/a/destination" + } + }, { + json: function(data) { + } + }); + }).should.throw(errorModels.GenericError);; + }); + + it('should throw a ValidationError if the request object is considered invalid', function() { + (function() { + routes.post.handler({ + body: { + } + }, { + json: function(data) { + } + }); + }).should.throw(errorModels.GenericError);; + }); + }); + + describe("#GET", function() { + it('should have a uri and handler', function() { + should.exist(routes.get); + + should.exist(routes.get.uri); + routes.get.uri.should.be.a.String; + + should.exist(routes.get.handler); + routes.get.handler.should.be.a.Function; + }); + }); + + describe("#DELETE", function() { + it('should have a uri and handler', function() { + should.exist(routes.delete); + + should.exist(routes.delete.uri); + routes.delete.uri.should.be.a.String; + + should.exist(routes.delete.handler); + routes.delete.handler.should.be.a.Function; + }); + + it('should remove the specified download from the cache', function(done) { + routes.delete.handler({ + params: { + downloadId: downloadId + } + }, { + json: function(data) { + should.exist(data); + should.exist(data.data); + data.data.should.be.an.String; + data.data.should.equal("Download: " + downloadId + ", deleted successfuly."); + done(); + } + }); + }); + + it('should throw a ValidationError if the supplied downloadId is considered invalid', function() { + (function() { + routes.delete.handler({ + params: { + downloadId: "downloadId" + } + }, { + json: function(data) { + } + }); + }).should.throw(errorModels.GenericError);; + }); + + it('should throw a UploadMissing error if the supplied downloadId does not exist', function() { + (function() { + routes.delete.handler({ + params: { + downloadId: "downloadId" + } + }, { + json: function(data) { + } + }); + }).should.throw(errorModels.GenericError);; + }); + }); + + describe("#ERROR", function() { + it('should have a handler', function() { + should.exist(routes.error); + + should.exist(routes.error.handler); + routes.error.handler.should.be.a.Function; + }); + }); +}); \ No newline at end of file diff --git a/test/routes/chunked-upload-routes-tests.js b/test/routes/chunked-upload-routes-tests.js index a763f02..b63c185 100644 --- a/test/routes/chunked-upload-routes-tests.js +++ b/test/routes/chunked-upload-routes-tests.js @@ -4,7 +4,6 @@ var should = require('should'), guidHelper = require.main.require('libs/helpers/guidHelper'); describe("chunked-upload-routes.js", function() { - var io_mock = require.main.require('mocks/libs/io'), cache_mock = require.main.require('mocks/libs/caching/localCache'), routes = require.main.require('routes/chunked-upload-routes')(cache_mock, io_mock, {debug:true, routePrefix:"/chunked/upload"}), @@ -164,7 +163,7 @@ describe("chunked-upload-routes.js", function() { } }); }).should.throw(errorModels.GenericError); - }) + }); it('should upload a file chunk and associate it with the specified upload', function(done) { routes.put.handler({