Skip to content

Commit

Permalink
Fixed up the last of the unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
supernomad committed Jun 28, 2015
1 parent 40bc02e commit c1c9015
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
11 changes: 9 additions & 2 deletions mocks/libs/io.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/* global Buffer */
var returnErrorOnGetFileStats = false;

function setReturnErrorOnGetFileStats(value) {
returnErrorOnGetFileStats = value;
}

function getFileStats(path, callback) {
if(path.indexOf('chunksize') !== -1) {
callback(null, {
callback(returnErrorOnGetFileStats ? new Error('RandomError') : null, {
size: 1025
});
} else {
callback(null, {
callback(returnErrorOnGetFileStats ? new Error('RandomError') : null, {
size: 1024
});
}
Expand Down Expand Up @@ -36,6 +42,7 @@ function readFileChunk(path, buffer, offset, length, position, callback) {
}

module.exports = {
setReturnErrorOnGetFileStats: setReturnErrorOnGetFileStats,
GetFileStats: getFileStats,
CreateFile: createFile,
WriteFileChunk: writeFileChunk,
Expand Down
56 changes: 55 additions & 1 deletion test/routes/chunked-download-routes-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('chunked-download-routes.js', function() {

afterEach('reset the cache_mock', function() {
cache_mock.setReturnValue(true);
cache_mock.setReturnErrorOnRestore(false);
io_mock.setReturnErrorOnGetFileStats(false);
});

it('should return a route object', function() {
Expand Down Expand Up @@ -89,6 +91,23 @@ describe('chunked-download-routes.js', function() {
});
});

it('should throw a ServerError if the file stats cannot be found', function(done) {
io_mock.setReturnErrorOnGetFileStats(true);
routes.post.handler({
body: {
path: '/i/am/a/path/to/a/destination'
}
}, {
json: function() {
should.fail();
}
}, function(error) {
should.exist(error);
error.should.be.an.Error();
done();
});
});

it('should throw a ValidationError if the request object is considered invalid', function(done) {
routes.post.handler({
body: {
Expand Down Expand Up @@ -151,6 +170,24 @@ describe('chunked-download-routes.js', function() {
});
});

it('should throw a ServerError if the cache fails to retrieve the download data', function(done) {
cache_mock.setReturnErrorOnRestore(true);
routes.get.handler({
params: {
downloadId: downloadId,
index: 0
}
}, {
send: function() {
should.fail();
}
}, function(error) {
should.exist(error);
error.should.be.an.Error();
done();
});
});

it('should throw a DownloadMissing error if the supplied downloadId does not exist', function(done) {
routes.get.handler({
params: {
Expand Down Expand Up @@ -215,6 +252,23 @@ describe('chunked-download-routes.js', function() {
routes.delete.handler.should.be.a.Function();
});

it('should throw a ServerError if the cache fails to restore the download data', function(done) {
cache_mock.setReturnErrorOnRestore(true);
routes.delete.handler({
params: {
downloadId: downloadId
}
}, {
json: function() {
should.fail();
}
}, function(error) {
should.exist(error);
error.should.be.an.Error();
done();
});
});

it('should remove the specified download from the cache', function(done) {
routes.delete.handler({
params: {
Expand Down Expand Up @@ -252,7 +306,7 @@ describe('chunked-download-routes.js', function() {
it('should throw a UploadMissing error if the supplied downloadId does not exist', function(done) {
routes.delete.handler({
params: {
downloadId: 'downloadId'
downloadId: guidHelper.newGuid()
}
}, {
json: function() {
Expand Down

0 comments on commit c1c9015

Please sign in to comment.