diff --git a/bin/make-mocks.js b/bin/make-mocks.js index 6fa65d6..f86b4f3 100644 --- a/bin/make-mocks.js +++ b/bin/make-mocks.js @@ -76,7 +76,7 @@ mkdirp(mocksDirectory, function(err) { anyfetch = new Anyfetch(res.body.token); async.auto({ - getUserId: function(cb) { + getCurrentUser: function(cb) { anyfetch.getCurrentUser(function(err, user) { userId = user.id; cb(err); @@ -119,19 +119,8 @@ mkdirp(mocksDirectory, function(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) { + endpoints: ['getCurrentUser', 'postSubcompanies', 'postDocuments', function(cb) { var endpoints = [ 'getDocuments', 'getStatus', @@ -146,7 +135,6 @@ mkdirp(mocksDirectory, function(err) { ['getDocumentsById', documentId], ['getDocumentsByIdentifier', documentIdentifier], ['getUsersById', userId], - ['getBatch', { pages: ['/document_types', '/providers'] }] ]; // Only proceed when all of them are done @@ -162,13 +150,11 @@ mkdirp(mocksDirectory, function(err) { }], // Subfunctions of getDocumentById - subFunctions: ['postDocumentsFile', function(cb) { + subFunctions: ['endpoints', 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; diff --git a/config/configuration.js b/config/configuration.js index fec7ace..e2f9fbf 100644 --- a/config/configuration.js +++ b/config/configuration.js @@ -41,6 +41,7 @@ module.exports = { test: { 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, diff --git a/lib/test-server/index.js b/lib/test-server/index.js index 426f04d..3988475 100644 --- a/lib/test-server/index.js +++ b/lib/test-server/index.js @@ -5,8 +5,6 @@ var restify = require('restify'); var filename = require('../helpers/endpoint-filename.js'); var configuration = require('../../config/configuration.js'); -// TODO: create `GET /batch` response from the various mock files - module.exports = function createTestServer() { var server = restify.createServer(); server.use(restify.queryParser()); @@ -56,7 +54,10 @@ module.exports = function createTestServer() { Object.keys(configuration.apiDescriptors).forEach(function(name) { var config = configuration.apiDescriptors[name]; - respondTo(config); + // We'll override `GET /batch` + if(config.endpoint !== '/batch') { + respondTo(config); + } if(config.subFunctions) { Object.keys(config.subFunctions).forEach(function(name) { @@ -66,5 +67,24 @@ module.exports = function createTestServer() { } }); + /** + * Custom response to GET /batch + * We craft the response from each asked endpoint. + */ + server.get('/batch', function(req, res) { + var pages = req.params.pages; + var response = {}; + + pages.forEach(function(page) { + var config = { + verb: 'GET', + endpoint: page + }; + response[page] = require('./mocks/' + filename(config) + '.json'); + }); + + res.send(response); + }); + return server; }; \ No newline at end of file diff --git a/lib/test-server/mocks/get-batch.json b/lib/test-server/mocks/get-batch.json deleted file mode 100644 index f7e0133..0000000 --- a/lib/test-server/mocks/get-batch.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "/providers": { - "5396f9a148f9f76e6b197957": { - "client": null, - "name": null, - "updated": "2014-06-20T15:17:40.156Z", - "document_count": 1 - }, - "53a14dadaa19645626f38b4a": { - "client": "52bff1eec8318cb228000001", - "name": "Google Contacts", - "updated": "2014-06-18T08:32:25.418Z", - "document_count": 378 - } - }, - "/document_types": { - "5252ce4ce4cfcd16f55cfa3b": { - "_type": "DocumentType", - "id": "5252ce4ce4cfcd16f55cfa3b", - "name": "file", - "templates": { - "snippet": "
\n

{{{ title }}}

\n {{{ path }}}\n
\n", - "full": "
\n

{{{ title }}}

\n {{{ path }}}\n
\n", - "title": "{{ title }}" - }, - "updated": "2014-06-20T15:17:40.156Z", - "documents": 1 - }, - "5252ce4ce4cfcd16f55cfa3a": { - "_type": "DocumentType", - "id": "5252ce4ce4cfcd16f55cfa3a", - "name": "contact", - "templates": { - "snippet": "
\n \n
\n

{{{ name }}}

\n {{{ job }}}\n
\n
\n", - "full": "
\n\n
\n \n
\n

{{{ name }}}

\n {{{ job }}} \n
\n
\n\n
\n {{#phone.length}}\n

Phones:

\n \n {{/phone.length}}\n\n {{#email.length}}\n

Emails:

\n \n {{/email.length}}\n\n {{#address.length}}\n

Address:

\n \n {{/address.length}}\n\n {{#website.length}}\n

Website:

\n \n {{/website.length}}\n\n\n {{#birthday}}\n

Birthday:

\n {{birthday}}\n {{/birthday}}\n\n
\n\n
\n", - "title": "{{ name }}" - }, - "updated": "2014-06-18T08:32:25.418Z", - "documents": 378 - } - } -} \ No newline at end of file diff --git a/lib/test-server/mocks/get-document_types.json b/lib/test-server/mocks/get-document_types.json index fae9629..0b62ab1 100644 --- a/lib/test-server/mocks/get-document_types.json +++ b/lib/test-server/mocks/get-document_types.json @@ -8,7 +8,19 @@ "full": "
\n

{{{ title }}}

\n {{{ path }}}\n
\n", "title": "{{ title }}" }, - "updated": "2014-06-16T14:11:13.144Z", + "updated": "2014-06-25T14:06:24.833Z", "documents": 1 + }, + "5252ce4ce4cfcd16f55cfa3a": { + "_type": "DocumentType", + "id": "5252ce4ce4cfcd16f55cfa3a", + "name": "contact", + "templates": { + "snippet": "
\n \n
\n

{{{ name }}}

\n {{{ job }}}\n
\n
\n", + "full": "
\n\n
\n \n
\n

{{{ name }}}

\n {{{ job }}} \n
\n
\n\n
\n {{#phone.length}}\n

Phones:

\n \n {{/phone.length}}\n\n {{#email.length}}\n

Emails:

\n \n {{/email.length}}\n\n {{#address.length}}\n

Address:

\n \n {{/address.length}}\n\n {{#website.length}}\n

Website:

\n \n {{/website.length}}\n\n\n {{#birthday}}\n

Birthday:

\n {{birthday}}\n {{/birthday}}\n\n
\n\n
\n", + "title": "{{ name }}" + }, + "updated": "2014-06-25T07:59:21.566Z", + "documents": 381 } } \ No newline at end of file diff --git a/lib/test-server/mocks/get-documents-id-raw.json b/lib/test-server/mocks/get-documents-id-raw.json index 74f3a75..6ca8744 100644 --- a/lib/test-server/mocks/get-documents-id-raw.json +++ b/lib/test-server/mocks/get-documents-id-raw.json @@ -1,13 +1,13 @@ { "_type": "Document", - "id": "539efb0019cf08ec1bc40c31", + "id": "53aad75f45419da341fc5c02", "identifier": "the \"unique\" document identifier (éüà)", - "creation_date": "2014-06-16T14:11:12.671Z", - "token": "539afa9270e5c5e1387fc2c3", + "creation_date": "2014-06-25T14:06:23.948Z", + "provider": "5396f9a148f9f76e6b197957", "company": "5391b692c8318c0ec7000026", "document_type": "5252ce4ce4cfcd16f55cfa3b", "actions": {}, - "document_url": "/documents/539efb0019cf08ec1bc40c31", + "document_url": "/documents/53aad75f45419da341fc5c02", "projection_type": "raw", "data": { "foo": "some_string" @@ -16,7 +16,7 @@ "path": "/hello", "some_key": "some random sentence" }, - "last_hydration": "2014-06-16T14:11:13.120Z", + "last_hydration": "2014-06-25T14:06:24.653Z", "hydrating": [ "http://plaintext.hydrater.anyfetch.com/hydrate" ], diff --git a/lib/test-server/mocks/get-documents-id-related.json b/lib/test-server/mocks/get-documents-id-related.json index 342fc10..ac87d92 100644 --- a/lib/test-server/mocks/get-documents-id-related.json +++ b/lib/test-server/mocks/get-documents-id-related.json @@ -5,7 +5,7 @@ "document_type": "5252ce4ce4cfcd16f55cfa3b", "facets": { "document_types": {}, - "tokens": {}, + "providers": {}, "creation_dates": {} }, "data": [], diff --git a/lib/test-server/mocks/get-documents-id-similar.json b/lib/test-server/mocks/get-documents-id-similar.json index 0efff46..83cb24a 100644 --- a/lib/test-server/mocks/get-documents-id-similar.json +++ b/lib/test-server/mocks/get-documents-id-similar.json @@ -9,11 +9,39 @@ "random" ], "facets": { - "document_types": {}, - "tokens": {}, - "creation_dates": {} + "document_types": { + "5252ce4ce4cfcd16f55cfa3a": 1 + }, + "providers": { + "53a14dadaa19645626f38b4a": 1 + }, + "creation_dates": { + "1401580800000": 1 + } }, - "data": [], - "count": 0, - "max_score": null + "data": [ + { + "_type": "Document", + "id": "53a14e37d9d493b510e41996", + "identifier": "https://mail.google.com/mail/b/merlin.nimier@gmail.com/#contact/5865599e0d5e0f2a", + "creation_date": "2014-06-18T08:30:47.638Z", + "provider": "53a14dadaa19645626f38b4a", + "company": "5391b692c8318c0ec7000026", + "document_type": "5252ce4ce4cfcd16f55cfa3a", + "actions": { + "show": "https://mail.google.com/mail/b/merlin.nimier@gmail.com/#contact/5865599e0d5e0f2a" + }, + "document_url": "/documents/53a14e37d9d493b510e41996", + "projection_type": "snippet", + "data": { + "name": "Anonymous", + "image": "https://www.gravatar.com/avatar/5348ec1f92ed3a6be5accc2d04566d64?d=mm&s=96", + "job": " " + }, + "related_count": 0, + "score": 0.1973946 + } + ], + "count": 1, + "max_score": 0.1973946 } \ No newline at end of file diff --git a/lib/test-server/mocks/get-documents-id.json b/lib/test-server/mocks/get-documents-id.json index b6590ca..c60805b 100644 --- a/lib/test-server/mocks/get-documents-id.json +++ b/lib/test-server/mocks/get-documents-id.json @@ -1,13 +1,13 @@ { "_type": "Document", - "id": "539efb0019cf08ec1bc40c31", + "id": "53aad75f45419da341fc5c02", "identifier": "the \"unique\" document identifier (éüà)", - "creation_date": "2014-06-16T14:11:12.671Z", - "token": "539afa9270e5c5e1387fc2c3", + "creation_date": "2014-06-25T14:06:23.948Z", + "provider": "5396f9a148f9f76e6b197957", "company": "5391b692c8318c0ec7000026", "document_type": "5252ce4ce4cfcd16f55cfa3b", "actions": {}, - "document_url": "/documents/539efb0019cf08ec1bc40c31", + "document_url": "/documents/53aad75f45419da341fc5c02", "projection_type": "full", "data": { "title": "Hello", diff --git a/lib/test-server/mocks/get-documents-identifier-identifier.json b/lib/test-server/mocks/get-documents-identifier-identifier.json index b6590ca..c60805b 100644 --- a/lib/test-server/mocks/get-documents-identifier-identifier.json +++ b/lib/test-server/mocks/get-documents-identifier-identifier.json @@ -1,13 +1,13 @@ { "_type": "Document", - "id": "539efb0019cf08ec1bc40c31", + "id": "53aad75f45419da341fc5c02", "identifier": "the \"unique\" document identifier (éüà)", - "creation_date": "2014-06-16T14:11:12.671Z", - "token": "539afa9270e5c5e1387fc2c3", + "creation_date": "2014-06-25T14:06:23.948Z", + "provider": "5396f9a148f9f76e6b197957", "company": "5391b692c8318c0ec7000026", "document_type": "5252ce4ce4cfcd16f55cfa3b", "actions": {}, - "document_url": "/documents/539efb0019cf08ec1bc40c31", + "document_url": "/documents/53aad75f45419da341fc5c02", "projection_type": "full", "data": { "title": "Hello", diff --git a/lib/test-server/mocks/get-index.json b/lib/test-server/mocks/get-index.json index 5a9c146..c41041c 100644 --- a/lib/test-server/mocks/get-index.json +++ b/lib/test-server/mocks/get-index.json @@ -8,5 +8,5 @@ "update_url": "/company/update", "reset_url": "/company/reset", "token_url": "/token", - "server_time": "2014-06-16T14:11:13.319Z" + "server_time": "2014-06-25T14:06:25.102Z" } \ No newline at end of file diff --git a/lib/test-server/mocks/get-providers.json b/lib/test-server/mocks/get-providers.json index 5ba1d11..e94be86 100644 --- a/lib/test-server/mocks/get-providers.json +++ b/lib/test-server/mocks/get-providers.json @@ -1,8 +1,14 @@ { - "539afa9270e5c5e1387fc2c3": { + "5396f9a148f9f76e6b197957": { "client": null, "name": null, - "updated": "2014-06-16T14:11:13.144Z", + "updated": "2014-06-25T14:06:24.833Z", "document_count": 1 + }, + "53a14dadaa19645626f38b4a": { + "client": "52bff1eec8318cb228000001", + "name": "Google Contacts", + "updated": "2014-06-25T07:59:21.566Z", + "document_count": 381 } } \ No newline at end of file diff --git a/lib/test-server/mocks/get-subcompanies-id.json b/lib/test-server/mocks/get-subcompanies-id.json index 7504d82..b8faf94 100644 --- a/lib/test-server/mocks/get-subcompanies-id.json +++ b/lib/test-server/mocks/get-subcompanies-id.json @@ -1,9 +1,9 @@ { "_type": "Company", - "id": "539efb0019cf08ec1bc40c32", + "id": "53aad76045419da341fc5c03", "name": "the_fake_subcompany", "hydraters": [ - "http://localhost:5000/plaintext/hydrate", - "http://localhost:5000/pdf/hydrate" + "http://plaintext.hydrater.anyfetch.com/hydrate", + "http://pdf.hydrater.anyfetch.com/hydrate" ] } \ No newline at end of file diff --git a/lib/test-server/mocks/get-subcompanies.json b/lib/test-server/mocks/get-subcompanies.json index e1ae105..652d51b 100644 --- a/lib/test-server/mocks/get-subcompanies.json +++ b/lib/test-server/mocks/get-subcompanies.json @@ -1,11 +1,11 @@ [ { "_type": "Company", - "id": "539efb0019cf08ec1bc40c32", + "id": "53aad76045419da341fc5c03", "name": "the_fake_subcompany", "hydraters": [ - "http://localhost:5000/plaintext/hydrate", - "http://localhost:5000/pdf/hydrate" + "http://plaintext.hydrater.anyfetch.com/hydrate", + "http://pdf.hydrater.anyfetch.com/hydrate" ] } ] \ No newline at end of file diff --git a/lib/test-server/mocks/get-token.json b/lib/test-server/mocks/get-token.json index 6fb454e..c410350 100644 --- a/lib/test-server/mocks/get-token.json +++ b/lib/test-server/mocks/get-token.json @@ -1,3 +1,3 @@ { - "token": "599e754ef7313c2e2975ce8ac610430a7707b66711fae1b33e109e088ff868a9" + "token": "ce48a8d00155140901aba760d9e2844d723e1d8e65b415cc3512b31dfdb6b7af" } \ No newline at end of file diff --git a/lib/test-server/mocks/get-users-id.json b/lib/test-server/mocks/get-users-id.json index e70acff..9063e85 100644 --- a/lib/test-server/mocks/get-users-id.json +++ b/lib/test-server/mocks/get-users-id.json @@ -2,7 +2,7 @@ "_type": "User", "id": "5391b692c8318c0ec7000027", "email": "merlin@anyfetch.com", - "name": "", + "name": "User", "is_admin": true, "user_url": "/users/5391b692c8318c0ec7000027" } \ No newline at end of file diff --git a/lib/test-server/mocks/get-users.json b/lib/test-server/mocks/get-users.json index 034d7e8..0c75c38 100644 --- a/lib/test-server/mocks/get-users.json +++ b/lib/test-server/mocks/get-users.json @@ -3,8 +3,16 @@ "_type": "User", "id": "5391b692c8318c0ec7000027", "email": "merlin@anyfetch.com", - "name": "", + "name": "User", "is_admin": true, "user_url": "/users/5391b692c8318c0ec7000027" + }, + { + "_type": "User", + "id": "53aad42245419da341fc5bfc", + "email": "thechuck1049@norris.com", + "name": "Chuck Norris", + "is_admin": true, + "user_url": "/users/53aad42245419da341fc5bfc" } ] \ No newline at end of file diff --git a/lib/test-server/mocks/post-documents.json b/lib/test-server/mocks/post-documents.json index 9c84b66..9b3b3b3 100644 --- a/lib/test-server/mocks/post-documents.json +++ b/lib/test-server/mocks/post-documents.json @@ -1,13 +1,13 @@ { "_type": "Document", - "id": "539efb0019cf08ec1bc40c31", + "id": "53aad75f45419da341fc5c02", "identifier": "the \"unique\" document identifier (éüà)", - "creation_date": "2014-06-16T14:11:12.671Z", - "token": "539afa9270e5c5e1387fc2c3", + "creation_date": "2014-06-25T14:06:23.948Z", + "provider": "5396f9a148f9f76e6b197957", "company": "5391b692c8318c0ec7000026", "document_type": "5252ce4ce4cfcd16f55cfa3b", "actions": {}, - "document_url": "/documents/539efb0019cf08ec1bc40c31", + "document_url": "/documents/53aad75f45419da341fc5c02", "projection_type": "raw", "data": { "foo": "some_string" diff --git a/lib/test-server/mocks/post-subcompanies.json b/lib/test-server/mocks/post-subcompanies.json index 7504d82..b8faf94 100644 --- a/lib/test-server/mocks/post-subcompanies.json +++ b/lib/test-server/mocks/post-subcompanies.json @@ -1,9 +1,9 @@ { "_type": "Company", - "id": "539efb0019cf08ec1bc40c32", + "id": "53aad76045419da341fc5c03", "name": "the_fake_subcompany", "hydraters": [ - "http://localhost:5000/plaintext/hydrate", - "http://localhost:5000/pdf/hydrate" + "http://plaintext.hydrater.anyfetch.com/hydrate", + "http://pdf.hydrater.anyfetch.com/hydrate" ] } \ No newline at end of file diff --git a/lib/test-server/mocks/post-users.json b/lib/test-server/mocks/post-users.json index ab30d97..eec422e 100644 --- a/lib/test-server/mocks/post-users.json +++ b/lib/test-server/mocks/post-users.json @@ -1,8 +1,8 @@ { "_type": "User", - "id": "539efb00051c96021c261cdc", - "email": "thechuck21@norris.com", + "id": "53aad75ffdf51cb5419e9f21", + "email": "thechuck677@norris.com", "name": "Chuck Norris", "is_admin": true, - "user_url": "/users/539efb00051c96021c261cdc" + "user_url": "/users/53aad75ffdf51cb5419e9f21" } \ No newline at end of file diff --git a/package.json b/package.json index b0431e9..16919b1 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ } ], "scripts": { - "test": "NODE_ENV=test mocha -R spec test/ -t 5000", + "test": "NODE_ENV=test mocha -R spec test/ -t 10000", "lint": "jshint lib/ test/" }, "main": "./lib/", diff --git a/test/auth.js b/test/auth.js index b4827ff..43e2aa8 100644 --- a/test/auth.js +++ b/test/auth.js @@ -12,9 +12,10 @@ describe('', function() { var appId = configuration.test.fakeAppId; var appSecret = configuration.test.fakeAppSecret; var code = configuration.test.fakeOAuthCode; + var fakeManagerServer; before(function(done) { - var fakeManagerServer = createFakeManagerServer(); + fakeManagerServer = createFakeManagerServer(); var port = configuration.test.managerPort; fakeManagerServer.listen(port, function() { @@ -53,6 +54,8 @@ describe('', function() { }); }); + after(function() { + fakeManagerServer.close(); + }); }); - }); \ No newline at end of file diff --git a/test/helpers.js b/test/helpers.js index c9d9536..c14f1c8 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -6,7 +6,7 @@ var async = require('async'); var Anyfetch = require('../lib/index.js'); var configuration = require('../config/configuration.js'); -// TODO: use mock server (improve it to handle batch requests) +// TODO: use mock server describe('', function() { var anyfetch; diff --git a/test/mock-server.js b/test/mock-server.js new file mode 100644 index 0000000..66b031a --- /dev/null +++ b/test/mock-server.js @@ -0,0 +1,38 @@ +'use strict'; + +var should = require('should'); + +var Anyfetch = require('../lib/index.js'); +var createMockServer = require('../lib/test-server/index.js'); +var configuration = require('../config/configuration.js'); + +describe('', function() { + var anyfetch = new Anyfetch(configuration.test.login, configuration.test.password); + var server; + + before(function(done) { + server = createMockServer(); + var port = configuration.test.mockPort; + server.listen(port, function() { + var apiHost = 'http://localhost:' + port; + console.log('Mock server running on ' + apiHost); + anyfetch.setApiHost(apiHost); + + done(); + }); + }); + + it('should respond with all the mocks we asked for', function(done) { + var pages = ['/document_types', '/providers', '/users', '/company']; + anyfetch.getBatch({ pages: pages }, function(err, res) { + should(err).not.be.ok; + should(res.body).be.ok; + res.body.should.have.keys(pages); + done(); + }); + }); + + after(function() { + server.close(); + }); +}); \ No newline at end of file