Skip to content

Commit

Permalink
Merge pull request #37 from AnyFetch/new-api-cleaner-tests
Browse files Browse the repository at this point in the history
Cleaner tests (reset is key)
  • Loading branch information
Neamar committed Jun 27, 2014
2 parents be3506e + b28aaf0 commit ba75137
Show file tree
Hide file tree
Showing 9 changed files with 683 additions and 281 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ language: node_js
node_js:
- '0.10'
env:
global:
- API_HOST: "http://staging.api.anyfetch.com"
- secure: "JZD117XvW5EFVFcjIlJFpFfUYujXy0r4tiBeWbolKznrfLFSvg9A+IVPd9PC/LR/wFksnd6wCyZ9lzBsavR+lrHzASfM5UBAYdlnC+ehOglba18Y3HdkU7t+vYYeFKREF0rfi0JQ21TwFyXpKv+xFTGp+h8Jc8aHn7BpLxqcirU="
- secure: "fde4hIF0xkPRo7qv4OQY4Elg88A9G1qQewWRKNmS36UwtN8PskXfl3b88YOhF1lfMcaie/u7BC1bOWvLFtB2Deq1HlbGSW7ThIkI+uYfrC2SEkUR4v1LQ42nM7chGccG2jVzdvdMpA6mknhzp7iueFAGZkBdR1GveHOYuKCe1Ns="
- API_URL=https://api.anyfetch.com LOGIN=test@anyfetch.com PASSWORD=password
- API_URL=http://staging.api.anyfetch.com LOGIN=staging.test@anyfetch.com PASSWORD=password
before_script:
- npm install -g istanbul
- npm install coveralls
Expand Down
9 changes: 5 additions & 4 deletions config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ var extendAllDescriptors = function(descriptors, defaultDescriptor) {
// The sub-functions extend this descriptor
for (var subName in descriptors[name].subFunctions) {
var subConfig = descriptors[name].subFunctions[subName];
extendDefault(subConfig, descriptors[name]);
subConfig.endpoint = descriptors[name].endpoint + subConfig.endpoint;
delete subConfig.subFunctions;
extendDefault(subConfig, descriptors[name]);
subConfig.endpoint = descriptors[name].endpoint + subConfig.endpoint;
delete subConfig.subFunctions;
}
}

Expand All @@ -33,7 +33,7 @@ module.exports = {
aliases: require('../config/json/aliases.json'),
apiDescriptors: extendAllDescriptors(apiDescriptors, defaultDescriptor),

apiHost: process.env.API_HOST || 'https://api.anyfetch.com',
apiHost: process.env.API_URL || 'https://api.anyfetch.com',
managerHost: process.env.MANAGER_HOST || 'https://manager.anyfetch.com',
oAuthEndpoint: '/oauth/access_token',

Expand All @@ -42,6 +42,7 @@ module.exports = {
port: process.env.PORT || '50000',
managerPort: process.env.MANAGER_TEST_PORT || '50001',
mockPort: process.env.MOCK_TEST_PORT || '50002',

login: process.env.LOGIN,
password: process.env.PASSWORD,

Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var Anyfetch = function(accessToken, password) {
};

/**
* Override the default API_HOST (useful for testing).
* Override the default API_URL (useful for testing).
*/
Anyfetch.prototype.setApiHost = function(host) {
this.apiHost = host;
Expand All @@ -45,4 +45,4 @@ addMappings(Anyfetch);
// High-level functions
addHelpers(Anyfetch);

module.exports = Anyfetch;
module.exports = Anyfetch;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"scripts": {
"test": "NODE_ENV=test mocha -R spec test/ -t 10000",
"test": "NODE_ENV=test mocha -R spec test/ -t 10000 -s 1000",
"lint": "jshint lib/ test/"
},
"main": "./lib/",
Expand Down
100 changes: 36 additions & 64 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,20 @@ var async = require('async');

var Anyfetch = require('../lib/index.js');
var configuration = require('../config/configuration.js');

// TODO: use mock server
var extendDefaults = require('../lib/helpers/extend-defaults.js');
var makeResetFunction = require('./helpers/reset.js');

describe('<High-level helper functions>', function() {
var anyfetch;
var anyfetchBasic = new Anyfetch(configuration.test.login, configuration.test.password);

// Retrieve token from credentials
before(function(done) {
anyfetchBasic.getToken(function(err, res) {
anyfetch = new Anyfetch(res.body.token);
done(err);
});
});
var anyfetch = new Anyfetch(configuration.test.login, configuration.test.password);
var cleaner = makeResetFunction(anyfetch);

describe('getDocumentWithInfo', function() {
var documentId;

before(cleaner);
// Prepare a fake document
before(function(done) {
var anyfetch = new Anyfetch(this.token);
anyfetch.postDocument(configuration.test.fakeDocument, function(err, res) {
if(res.body && res.body.id) {
documentId = res.body.id;
Expand Down Expand Up @@ -56,16 +51,13 @@ describe('<High-level helper functions>', function() {
done();
});
});

after(function(done) {
anyfetch.deleteDocumentById(documentId, done);
});
});

describe('getDocumentsWithInfo', function() {

// Prepare two documents
before(cleaner);
// Prepare two fake documents
before(function(done) {
var anyfetch = new Anyfetch(this.token);
async.parallel([
function(cb) {
anyfetch.postDocument(configuration.test.fakeDocument, cb);
Expand All @@ -77,70 +69,42 @@ describe('<High-level helper functions>', function() {
});

it('should get all documents and populate their `document_type` and `provider`', function(done) {
anyfetch.getDocumentsWithInfo(function(err, docs) {
docs.data.forEach(function(doc) {
//TODO : remove dat shit (but solve ES shard indexing before)
setTimeout(function() {
anyfetch.getDocumentsWithInfo(function(err, docs) {
should(err).not.be.ok;
should(doc).be.ok;
doc.should.have.properties('id', 'identifier', 'provider', 'document_type');
doc.provider.should.have.properties('client', 'name', 'document_count');
doc.document_type.should.have.properties('id', 'name', 'templates');
});

done();
});
});
docs.data.forEach(function(doc) {
should(err).not.be.ok;
should(doc).be.ok;
doc.should.have.properties('id', 'identifier', 'provider', 'document_type');
doc.provider.should.have.properties('client', 'name', 'document_count');
doc.document_type.should.have.properties('id', 'name', 'templates');
});

after(function(done) {
async.parallel([
function(cb) {
var identifier = configuration.test.fakeDocument.identifier;
anyfetch.deleteDocumentByIdentifier(identifier, cb);
},
function(cb) {
var identifier = configuration.test.fakeDocument2.identifier;
anyfetch.deleteDocumentByIdentifier(identifier, cb);
}
], done);
done();
});
}, 1500);
});
});

describe('sendDocumentAndFile', function() {
var doc = configuration.test.fakeDocument;
var documentId;
before(cleaner);

var hash = configuration.test.fakeImageFile;
var doc = configuration.test.fakeDocument;
var hash = extendDefaults({}, configuration.test.fakeImageFile);
// Simply upload from filename
hash.file = hash.path;

it('should create the document', function(done) {
it('should create the document and post the file without error', function(done) {
var anyfetch = new Anyfetch(this.token);
anyfetch.sendDocumentAndFile(doc, hash, function(err, doc) {
should(err).not.be.ok;
should(doc).be.ok;
doc.should.have.properties('id', 'identifier', 'provider', 'document_type');
documentId = doc.id;

done();
});
});

it('should have updated the document with info from the file', function(done) {
should(documentId).be.ok;

// TODO: introduce delay to let the file be hydrated?
anyfetch.getDocumentById(documentId, function(err, res) {
var doc = res.body;
should(err).not.be.ok;
should(doc).be.ok;
doc.should.have.properties('data');
doc.data.should.have.properties({ 'extension': 'jpg' });

done();
});

});

after(function(done) {
anyfetch.deleteDocumentById(documentId, done);
});
});

Expand All @@ -158,12 +122,19 @@ describe('<High-level helper functions>', function() {
});

describe('createSubcompanyWithAdmin', function() {
before(cleaner);
var anyfetch;
before(function() {
anyfetch = new Anyfetch(this.token);
});

var admin = configuration.test.fakeUser;
var subcompany = configuration.test.fakeCompany;
var subcompanyId;

it('should run smoothly', function(done) {
anyfetch.createSubcompanyWithAdmin(subcompany, admin, function(err, company) {
should(err).not.be.ok;
subcompanyId = company.id;
done(err);
});
Expand Down Expand Up @@ -195,6 +166,7 @@ describe('<High-level helper functions>', function() {
});
});

// Subcompanies are not cleared by a simple reset, we must still delete it by hand
after(function(done) {
anyfetch.deleteSubcompanyById(subcompanyId, done);
});
Expand Down
28 changes: 28 additions & 0 deletions test/helpers/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

var async = require('async');
var rarity = require('rarity');

module.exports = function makeResetFunction(anyfetchBasic) {
/**
* Reset the company and obtain the new Bearer token
*/
return function(done) {
var self = this;

async.waterfall([
function resetCompany(cb) {
anyfetchBasic.deleteCompanyReset(rarity.slice(1, cb));
},
function obtainNewToken(cb) {
anyfetchBasic.getToken(cb);
}
], function(err, res) {
if(err) {
throw err;
}
self.token = res.body.token;
done();
});
};
};

0 comments on commit ba75137

Please sign in to comment.