-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'new-api' into new-api-auth
- Loading branch information
Showing
42 changed files
with
1,571 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.