Skip to content

Commit

Permalink
Made a few minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
supernomad committed Jun 21, 2015
1 parent bba7741 commit 311306b
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 7 deletions.
7 changes: 7 additions & 0 deletions mocks/libs/io.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand All @@ -22,6 +28,7 @@ function renameFile(path, newPath, callback) {
}

module.exports = {
GetFileStats: getFileStats,
CreateFile: createFile,
WriteFileChunk: writeFileChunk,
DeleteFile: deleteFile,
Expand Down
11 changes: 6 additions & 5 deletions routes/chunked-download-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}

Expand Down
152 changes: 152 additions & 0 deletions test/routes/chunked-download-routes-tests.js
Original file line number Diff line number Diff line change
@@ -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;
});
});
});
3 changes: 1 addition & 2 deletions test/routes/chunked-upload-routes-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}),
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 311306b

Please sign in to comment.