Skip to content

Commit

Permalink
Merge branch 'new-api' into new-api-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinND committed Jun 23, 2014
2 parents 784adae + 38f520b commit 16872d0
Show file tree
Hide file tree
Showing 42 changed files with 1,571 additions and 10 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ node_modules/*
npm-debug.log
libpeerconnection.log

# Docco artifacts
# Foreman Artifacts
Procfile
.env

# Docco Artifacts
docs/*

# IDE Artifacts
.idea/*
*.iml
*.sublime-*

# Jekyll artifacts
# Jekyll Artifacts
_site/*

keys.sh
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"es5" : false,
"esnext" : false,
"evil" : false,
"expr" : false,
"expr" : true,
"forin" : false,
"funcscope" : false,
"globalstrict" : false,
Expand Down
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
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="
before_script:
- npm install -g istanbul
- npm install coveralls
Expand Down
37 changes: 37 additions & 0 deletions bin/clear-subcompanies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';
/**
* @file Delete all the subcompanies of this account
* @see http://developers.anyfetch.com/endpoints/
*/

var async = require('async');

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

if(!configuration.test.login || !configuration.test.password) {
throw new Error('This script requires valid LOGIN and PASSWORD to be set in your env');
}
var anyfetch = new Anyfetch(configuration.test.login, configuration.test.password);

async.waterfall([
function(cb) {
anyfetch.getSubcompanies(function(err, res) {
cb(err, res.body);
});
},
function(subcompanies, cb) {
async.map(subcompanies, function(subcompany, cb) {
console.log('Deleting subcompany ' + subcompany.id);
anyfetch.deleteSubcompanyById(subcompany.id, {}, cb);
}, cb);
}

],
function(err) {
if(err) {
throw err;
}
console.log('All subcompanies deleted.');
}
);
41 changes: 41 additions & 0 deletions bin/clear-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
/**
* @file Delete all the users (except the one that's logged in)
* @see http://developers.anyfetch.com/endpoints/
*/

var async = require('async');

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

if(!configuration.test.login || !configuration.test.password) {
throw new Error('This script requires valid LOGIN and PASSWORD to be set in your env');
}
var anyfetch = new Anyfetch(configuration.test.login, configuration.test.password);

async.waterfall([
function(cb) {
anyfetch.getUsers(function(err, res) {
cb(err, res.body);
});
},
function(users, cb) {
async.map(users, function(user, cb) {
if(user.email === configuration.test.login) {
return cb(null);
}

console.log('Deleting user ' + user.email + ' (' + user.id + ')');
anyfetch.deleteUserById(user.id, cb);
}, cb);
}

],
function(err) {
if(err) {
throw err;
}
console.log('All users deleted.');
}
);
216 changes: 216 additions & 0 deletions bin/make-mocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
'use strict';
/**
* @file Make a call to each endpoint and record the response (code, body, ...)
* in JSON files (one file per request).
* It is useful to create the mock responses used in testing.
* @see http://developers.anyfetch.com/endpoints/
*/

var fs = require('fs');
var mkdirp = require('mkdirp');
var async = require('async');
var util = require('util');

var filename = require('../lib/helpers/endpoint-filename.js');

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

if(!configuration.test.login || !configuration.test.password) {
throw new Error('This script requires valid LOGIN and PASSWORD to be set in your env');
}
var anyfetch = new Anyfetch(configuration.test.login, configuration.test.password);
var mocksDirectory = __dirname + '/../lib/test-server/mocks/';

/**
* Save the response's body, at least if it's not empty
*/
var saveMock = function(endpointConfig, body) {
if (endpointConfig.expectedStatus !== 204 && Object.keys(body).length > 0) {
// We'll write pretty JSON
var json = JSON.stringify(body, null, 2);
var target = filename(endpointConfig) + '.json';
fs.writeFile(mocksDirectory + target, json, function(err) {
if(err) {
throw err;
}
console.log(target + ' saved.');
});
}
};

var mockEndpoint = function(name, args, cb) {
if (!configuration.apiDescriptors[name]) {
throw new Error('The endpoint ' + name + ' is not specified.');
}

// Add callback
args.push(function(err, res){
var body = res.body || null;
saveMock(configuration.apiDescriptors[name], body);
cb(err);
});

anyfetch[name].apply(anyfetch, args);
};

// ----- Make sure the output directory exists
mkdirp(mocksDirectory, function(err) {
if(err) {
throw new Error(err);
}

// ----- Fill with fake content
var userId;
var anyChuck;
var subcompanyId;
var documentId;
var documentIdentifier = configuration.test.fakeDocument.identifier;

anyfetch.getToken(function(err, res) {
if(err) {
throw err;
}
saveMock(configuration.apiDescriptors.getToken, res.body);

anyfetch = new Anyfetch(res.body.token);

async.auto({

getMyUserId: function(cb) {
anyfetch.getIndex(function(err, res) {
if(res.body && res.body.current_user_url) {
// TODO: user the helper `getUserInfo` instead
var userUrl = res.body.current_user_url;
userId = userUrl.substring(userUrl.lastIndexOf('/') + 1);
}
cb(err);
});
},

postUsers: function(cb) {
anyfetch.postUsers(configuration.test.fakeUser, function(err, res) {
if(res.body && res.body.id) {
saveMock(configuration.apiDescriptors.postUsers, res.body);
}
cb(err);
});
},

/**
* We need to create the subcompany from an admin user,
* who will be moved into the subcompany.
*/
postSubcompanies: ['postUsers', function(cb) {
anyChuck = new Anyfetch(configuration.test.fakeUser.email, configuration.test.fakeUser.password);

anyChuck.postSubcompanies(configuration.test.fakeCompany, function(err, res) {
// The fake user is now the first admin of the new company
if(res.body && res.body.id) {
subcompanyId = res.body.id;
saveMock(configuration.apiDescriptors.postSubcompanies, res.body);
}
cb(err);
});
}],

postDocuments: function(cb) {
anyfetch.postDocuments(configuration.test.fakeDocument, function(err, res) {
if(res.body && res.body.id) {
documentId = res.body.id;
saveMock(configuration.apiDescriptors.postDocuments, res.body);
}
cb(err);
});
},

postDocumentsFile: ['postDocuments', function(cb) {
var hash = configuration.test.fakeFile;
hash.file = fs.createReadStream(hash.path);
anyfetch.getDocumentById(documentId).postFile(hash, function(err, res) {
if(res.body) {
saveMock({ expectedStatus: 204 }, res.body);
}
cb(err);
});
}],

// Now the fake content is setup, we can test all the gets in parallel
endpoints: ['getMyUserId', 'postSubcompanies', 'postDocumentsFile', function(cb) {
var endpoints = [
'getDocuments',
'getStatus',
'getIndex',
'getCompany',
'getSubcompanies',
'postCompanyUpdate',
'getUsers',
'getDocumentTypes',
'getProviders',
['getSubcompaniesById', subcompanyId],
['getDocumentsById', documentId],
['getDocumentsByIdentifier', documentIdentifier],
['getUsersById', userId],
['getBatch', { pages: ['/document_types', '/providers'] }]
];

// Only proceed when all of them are done
async.map(endpoints, function(args, cb) {
if(util.isArray(args)) {
var endpoint = args.shift();
mockEndpoint(endpoint, args, cb);
}
else {
mockEndpoint(args, [], cb);
}
}, cb);
}],

// Subfunctions of getDocumentById
subFunctions: ['postDocumentsFile', function(cb) {
var subs = [
'getSimilar',
'getRelated',
'getRaw',
// TODO: re-enable when API is fixed
//'getFile'
];
var pre = anyfetch.getDocumentsById(documentId);
var c = configuration.apiDescriptors.getDocumentsById.subFunctions;

async.map(subs, function(name, cb){
pre[name](function(err, res) {
saveMock(c[name], res.body);
cb(err);
});
}, cb);
}]

}, function(err) {
// ----- Clean up in parallel
async.parallel({

deleteSubcompanyById: function(cb) {
// The fake user, who's inside this subcompany, will get deleted as well
anyfetch.deleteSubcompanyById(subcompanyId, {}, cb);
},

deleteDocumentByIdentifier: function(cb) {
anyfetch.deleteDocumentsByIdentifier(documentIdentifier, cb);
}

}, function(cleanupErr) {
// A bit weird, but we'd like to try and clean-up even if there's
// been an error
if(err) {
throw err;
}
if(cleanupErr) {
throw cleanupErr;
}
});
});

});

});
11 changes: 11 additions & 0 deletions bin/run-test-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

var createTestServer = require('../lib/test-server/index.js');
var configuration = require('../config/configuration.js');

var port = configuration.test.port;

var testServer = createTestServer();
testServer.listen(port, function() {
console.log('Test server listening on port ' + port);
});

0 comments on commit 16872d0

Please sign in to comment.