UNPKG

docparse-upload-process

Version:

process upload api request for the docparse server

243 lines (220 loc) 7.97 kB
var async = require('async'); var assert = require('should'); var fs = require('fs'); var path = require('path'); var mongoose = require('mongoose'); var Upload = require('docparse-upload'); var Supplier = require('docparse-supplier'); var should = require('should'); var ce = require('cloneextend'); var createUser = require('./createUser'); var createSupplier = require('./createSupplier'); var createUpload = require('./createUpload'); var create = require('../index').create; var fetch = require('../index').fetch; var inspect = require('eyespect').inspector(); var configFilePath = path.join(__dirname, 'local_config.json'); var configFileExists = fs.existsSync(configFilePath); var config = require('docparse-config')(configFilePath); var logger = require('docparse-logger')(config); assert.ok(configFileExists, 'config file not found at path, note that you must supply this file manually to run the test suite. It is not included with the public module since it contains sensitive user and passwords for the remote pdfer service'); var pathhash = require('pathhash'); describe('fetch test', function(done) { var user; var filename = 'raw-single-page.pdf'; var filePath = path.join(__dirname, 'data', filename); var userData1 = { username: 'api_test_user', password:'123455', email: 'api_tester@example.com' }; var userData2 = { username: 'api_test_user2', password:'22222', email: 'api_tester2@example.com', remove: false }; var user1, user2; var data = { supplierCode: 'FGS', filePath: filePath, filename: filename, config: config }; var hash; before(function (done) { this.timeout(20*1000); this.slow(10*1000); mongoose.connect('mongodb://localhost/test', function (err, reply) { Upload.remove({}, function (err, reply) { should.not.exist(err, 'error removing existing bills: ' + err); createUser(userData1, function (err, user1Reply) { should.not.exist(err, 'error creating test user: ' + err); should.exist(user1Reply); data.userID = user1Reply._id.toString(); user1 = user1Reply; createUser(userData2, function (err, user2Reply) { should.not.exist(err, 'error creating test user: ' + err); should.exist(user2Reply); user2 = user2Reply; createSupplier(function (err, reply) { var exists = fs.existsSync(filePath); assert.ok(exists, 'test file not found at path: ' + filePath); data.filePath = filePath; pathhash(filePath, function (err, reply) { should.not.exist(err); should.exist(reply); hash = reply; data.hash = hash; data.logger = logger; data.config = config; createUpload(data, function (err, reply) { should.not.exist(err); should.exist(reply); done(); }); }); }); }); }); }); }); }); it('should reject fetch requests missing a hash field', function(done) { var testData = ce.clone(data); testData.config = config; delete testData.hash; fetch(testData, function (err, reply) { should.exist(err, 'create should return error'); err.should.match(/key does not exist in data: hash/); done(); }); }); it('should reject fetch requests with invalid hash field', function(done) { var testData = ce.clone(data); testData.hash = 'dummy hash value'; testData.config = config; fetch(testData, function (err, reply) { err.should.match(/upload not found with hash:/); should.exist(err, 'create should return error'); done(); }); }); it('should reject fetch requests missing a userID field', function(done) { var testData = ce.clone(data); testData.config = config; delete testData.userID; fetch(testData, function (err, reply) { should.exist(err, 'create should return error'); err.should.match(/key does not exist in data: userID/); done(); }); }); it('should reject fetch requests with invalid userID field', function(done) { var testData = ce.clone(data); testData.config = config; testData.userID = 'dummy userID value'; fetch(testData, function (err, reply) { var pattern = /user not found with id/; err.should.match(pattern, 'wrong error message'); should.exist(err, 'create should return error'); done(); }); }); it('should reject fetch requests where user does not own the upload', function(done) { var userData = { username: 'username2', password: 'password2', email: 'test2@example.com' }; createUser(userData, function (err, user) { var testData = ce.clone(data); testData.config = config; testData.userID = user._id; fetch(testData, function (err, reply) { err.should.match(/you are not permitted to fetch this upload/); should.exist(err, 'fetch should return error'); done(); }); }); }); it('should proces valid fetch requests correctly', function(done) { this.timeout(100*1000); this.slow(10*1000); var testData = ce.clone(data); testData.config = config; Upload.findOne({hash: data.hash}, function (err, reply) { should.not.exist(err); should.exist(reply, 'upload should exist in database before we try to fetch it'); var upload; var maxAttempts = 100; async.until( function() { if (!upload) { return false; } if (upload.ocr_status.complete) { return true; } return false; }, function (cb) { setTimeout(function() { fetch(testData, function (err, reply) { if (err) { inspect(err, 'error in fetch'); return; } should.not.exist(err, 'fetch request should not return error: ' + err); should.exist(reply, 'upload was not actually fetchd like it should have been'); reply.should.have.property('ocr_status'); upload = reply; cb(); }); }, 5000); // setTimeout(function() { // inspect('fetching now'); // fetch(testData, function (err, reply) { // inspect(reply.toObject(), 'upload fetch done'); // should.not.exist(err, 'fetch request should not return error: ' + err); // should.exist(reply, 'upload was not actually fetchd like it should have been'); // reply.should.have.property('ocr_status'); // upload = reply; // cb(); // }, 500000); // }); }, function(err) { should.not.exist(err); assert.ok(upload.ocr_status.complete, 'upload.ocr_status.complete should be true'); done(); }); }); }); it('should return 401 Unauthorized for fetch requests where user is not an owner', function (done) { this.timeout(100*1000); this.slow(10*1000); var testData = ce.clone(data); delete testData.config; var oldID = testData.userID; var userID = user2._id.toString(); userID.should.not.eql(oldID); testData.userID = user2._id.toString(); testData.config = config; Upload.findOne({hash: data.hash}, function (err, reply) { should.not.exist(err); should.exist(reply, 'upload should exist in database before we try to fetch it'); fetch(testData, function (err, reply) { should.exist(err); should.not.exist(reply); done(); }); }); }); // cleanup after(function(done) { Upload.remove({}, function(err, reply) { mongoose.disconnect(done); }); }); });