box-view-api
Version:
Box View API, node.js client library
595 lines (486 loc) • 18.1 kB
JavaScript
// Tests for the Box View document API
var API = require('index').DocumentAPI;
// setup aliases from the framework to save typing
var statusCodeChecks = framework.statusCodeChecks;
var jsonContentTypeChecks = framework.jsonContentTypeChecks;
var contentTypeChecks = framework.contentTypeChecks;
var pollTimeout = framework.timeouts.poll;
var responseTimeout = framework.timeouts.response;
var checkProps = framework.checkProps;
var responses = framework.boxViewResponses;
var fsUtils = framework.fsUtils;
// make sure we clean temporarily created uuids
after(function(done) {
this.timeout(responseTimeout * (framework.tempUuid.uuids.length+1));
framework.tempUuid.purge(done);
});
describe('Box View API: REST: document', function() {
var instance = new API(framework.boxView.validApiKey);
var preloadedUuid;
it('should preload a file without timing out', function(done) {
var url = 'http://web.crocodoc.com/files/test-simple.pdf';
var name = url;
this.timeout(pollTimeout);
framework.tempUuid.fromUrl(url, name, function(e, r, b) {
framework.assert(typeof b === 'object', 'Could not pre-load a document for the tests: e: '+ e +', typeof body: '+ typeof b + ' body: '+ framework.inspect(b, true, 1) );
preloadedUuid = b.id;
framework.tempUuid.waitFor(preloadedUuid, function(e, r, b) {
done();
});
});
});
describe('.list()', function() {
var error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.list(function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 200)(getError, getResponse, getBody);
jsonContentTypeChecks(getError, getResponse, getBody);
describe('the response body', function() {
checkProps(getBody, responses.document.list);
});
});
describe('.info()', function() {
describe('with a non-existent document id', function() {
var error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.info('non-existent docId', function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 404)(getError, getResponse, getBody);
jsonContentTypeChecks(getError, getResponse, getBody);
describe('the response body', function() {
checkProps(function () {
return body;
}, responses.document.notFound, true);
});
});
describe('with the id of a preloaded file', function() {
var error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.info(preloadedUuid, function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 200)(getError, getResponse, getBody);
jsonContentTypeChecks(getError, getResponse, getBody);
describe('the response body', function() {
checkProps(getBody, responses.document.done, true );
});
});
});
describe('.uploadUrl()', function() {
describe('with a malformed url', function() {
var uuid, error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.uploadUrl('not a url', 'not a url', function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 400)(getError, getResponse, getBody);
jsonContentTypeChecks(getError, getResponse, getBody);
describe('the response body', function() {
checkProps(getBody, responses.document.badUrl, true);
});
});
describe('with a good url', function() {
var url = 'http://web.crocodoc.com/files/test-simple.pdf';
var name = url;
var uuid, error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
// wait for the conversion to finish, to avoid exceeding the rate limiter
after(function(done) {
this.timeout(pollTimeout);
instance.poll(uuid, done);
});
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.uploadUrl(url, name, function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 202)(getError, getResponse, getBody);
jsonContentTypeChecks(getError, getResponse, getBody);
describe('the response body', function() {
checkProps(getBody, responses.document.generic, true);
it('.id should be non-blank', function() {
framework.assert(typeof body.id === 'string' && body.id.length > 0, 'Expected body.id to be a non-empty string, not: ('+typeof body.id+'): '+body.id);
uuid = body.id;
framework.tempUuid.add(uuid);
});
describe('the final retrieved status', function() {
var _fe, _fr, _fb;
function getError() {
return _fe;
}
function getResponse() {
return _fr;
}
function getBody() {
return _fb;
}
it('should poll for a final response without timing out', function(done) {
this.timeout(pollTimeout);
instance.poll(uuid, function(e, r, b) {
_fe = e;
_fr = r;
_fb = b;
done();
});
});
statusCodeChecks(it, 200)(getError, getResponse, getBody);
jsonContentTypeChecks(getError, getResponse, getBody);
checkProps(getBody, responses.document.done, true);
});
});
});
});
/*
* Not sure this is supported by the Box View API, so commenting out for now
*
describe('.uploadFile()', function() {
describe('with an unsupported file type', function() {
var error, response, body;
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.uploadFile('test/support/files/unsupported.txt', function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 400)(function(){return error;}, function(){return response;}, function(){return body;});
jsonContentTypeChecks(function(){return error;}, function(){return response;}, function(){return body;});
describe('the response body', function() {
it('should have an undefined uuid field', function() {
framework.assert(body.uuid === undefined, 'Expected body.uuid to be undefined, not: ('+typeof body.uuid+'): '+body.uuid);
});
it('should have a truthy error field', function() {
framework.assert(body.error, 'Expected body.error to truthy, not: ('+typeof body.error+'): '+body.error);
});
});
});
describe('with a supported file type', function() {
var uuid, error, response, body;
// wait for the conversion to finish, to avoid exceeding the rate limiter
after(function(done) {
this.timeout(pollTimeout);
instance.poll(uuid, done);
});
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.uploadFile('test/support/files/simple-presentation.ppt', function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 200)(function(){return error;}, function(){return response;}, function(){return body;});
jsonContentTypeChecks(function(){return error;}, function(){return response;}, function(){return body;});
describe('the response body', function() {
it('should have a non-empty uuid string field', function() {
framework.assert(typeof body.uuid === 'string' && body.uuid.length > 0, 'Expected body.uuid to be a non-empty string, not: ('+typeof body.uuid+'): '+body.uuid);
uuid = body.uuid;
framework.tempUuid.add(uuid);
});
it('should have an undefined error field', function() {
framework.assert(body.error === undefined, 'Expected body.error to be undefined, not: ('+typeof body.error+'): '+body.error);
});
describe('the final retrieved status', function() {
var _fe, _fr, _fb;
it('should poll for a final response without timing out', function(done) {
this.timeout(pollTimeout);
instance.poll(uuid, function(e, r, b) {
_fe = e;
_fr = r;
_fb = b;
done();
});
});
statusCodeChecks(it, 200)(function(){return _fe;}, function(){return _fr;}, function(){return _fb;});
jsonContentTypeChecks(function(){return _fe;}, function(){return _fr;}, function(){return _fb;});
it('should have a body.status field of "DONE"', function() {
framework.assert(_fb.status === 'DONE', 'Expected body.status to be "DONE", not: '+typeof _fb.status+'): '+_fb.status);
});
it('should have a body.viewable field of true', function() {
framework.assert(_fb.viewable === true, 'Expected body.viewable to be true, not: '+typeof _fb.viewable+'): '+_fb.viewable);
});
it('should have an undefined body.error field', function() {
framework.assert(_fb.error === undefined, 'Expected body.error to be undefined, not: '+typeof _fb.error+'): '+_fb.error);
});
});
});
});
});
*/
describe('.download() as a zip', function() {
describe('with a non-existent document id', function() {
var error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
var outputFilename = './test/results/test-output.noDocId.zip';
var outputStream = fsUtils.writeStream(outputFilename);
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.download('non-existent docId', API.downloadTypes.zip, outputStream, function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 404)(getError, getResponse, getBody);
contentTypeChecks(getError, getResponse, getBody, 'application/zip');
describe('the response body', function() {
checkProps(getBody, responses.document.downloadNotFound, true);
});
describe('the downloaded file', function() {
it ('should exist', function() {
framework.assert(fsUtils.isFile(outputFilename), 'Expected '+outputFilename+' to be a regular file');
});
it ('should have zero length', function() {
framework.assert(fsUtils.fileSize(outputFilename) === 0, 'Expected '+outputFilename+' to be zero length');
});
});
});
describe('with the id of a preloaded file', function() {
var error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
var outputFilename = './test/results/test-output.goodDocId.zip';
var outputStream = fsUtils.writeStream(outputFilename);
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.download(preloadedUuid, API.downloadTypes.zip, outputStream, function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 200)(getError, getResponse, getBody);
contentTypeChecks(getError, getResponse, getBody, 'application/zip');
describe('the response body', function() {
checkProps(getBody, responses.document.downloadOK, true);
});
describe('the downloaded file', function() {
it ('should exist', function() {
framework.assert(fsUtils.isFile(outputFilename), 'Expected '+outputFilename+' to be a regular file');
});
it ('should not be zero length', function() {
framework.assert(fsUtils.fileSize(outputFilename) > 0, 'Expected '+outputFilename+' not to be zero length');
});
});
});
});
describe('.download() as a pdf', function() {
describe('with a non-existent document id', function() {
var error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
var outputFilename = './test/results/test-output.noDocId.pdf';
var outputStream = fsUtils.writeStream(outputFilename);
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.download('non-existent docId', API.downloadTypes.pdf, outputStream, function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 404)(getError, getResponse, getBody);
contentTypeChecks(getError, getResponse, getBody, 'application/pdf');
describe('the response body', function() {
checkProps(getBody, responses.document.downloadNotFound, true);
});
describe('the downloaded file', function() {
it ('should exist', function() {
framework.assert(fsUtils.isFile(outputFilename), 'Expected '+outputFilename+' to be a regular file');
});
it ('should have zero length', function() {
framework.assert(fsUtils.fileSize(outputFilename) === 0, 'Expected '+outputFilename+' to be zero length');
});
});
});
describe('with the id of a preloaded file', function() {
var error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
var outputFilename = './test/results/test-output.goodDocId.pdf';
var outputStream = fsUtils.writeStream(outputFilename);
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.download(preloadedUuid, API.downloadTypes.pdf, outputStream, function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 200)(getError, getResponse, getBody);
contentTypeChecks(getError, getResponse, getBody, 'application/pdf');
describe('the response body', function() {
checkProps(getBody, responses.document.downloadOK, true);
});
describe('the downloaded file', function() {
it ('should exist', function() {
framework.assert(fsUtils.isFile(outputFilename), 'Expected '+outputFilename+' to be a regular file');
});
it ('should not be zero length', function() {
framework.assert(fsUtils.fileSize(outputFilename) > 0, 'Expected '+outputFilename+' not to be zero length');
});
});
});
});
describe('.remove()', function() {
describe('with a non-existent id', function() {
var error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.remove('non-existent docId', function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 404)(getError, getResponse, getBody);
jsonContentTypeChecks(getError, getResponse, getBody);
describe('the response body', function() {
checkProps(getBody, responses.document.notFound, true);
});
});
describe('with a docId of a pre-loaded document', function() {
var error, response, body;
function getError() {
return error;
}
function getResponse() {
return response;
}
function getBody() {
return body;
}
it('should respond without timing out', function(done) {
this.timeout(responseTimeout);
instance.remove(preloadedUuid, function(e, r, b) {
error = e;
response = r;
body = b;
done();
});
});
statusCodeChecks(it, 204)(getError, getResponse, getBody);
jsonContentTypeChecks(getError, getResponse, getBody);
describe('the response body', function() {
checkProps(getBody, responses.document.deleted, true);
});
});
});
});