From 547d19aaf35214b235b262362526f29df8d3569b Mon Sep 17 00:00:00 2001 From: Florian ENOH Date: Wed, 19 Apr 2017 17:58:41 +0200 Subject: [PATCH] On expose un nouvel endpoint avec les infos api sur /api (#375) [#375] [FEATURE] On expose un nouvel endpoint avec les infos api sur /api (US-453). --- .../healthcheck/healthcheck-controller.js | 15 ++++++++ api/lib/application/healthcheck/index.js | 18 +++++++++ api/server.js | 1 + .../healthcheck-controller_test.js | 26 +++++++++++++ .../application/healthcheck/index_test.js | 37 +++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 api/lib/application/healthcheck/healthcheck-controller.js create mode 100644 api/lib/application/healthcheck/index.js create mode 100644 api/tests/unit/application/healthcheck/healthcheck-controller_test.js create mode 100644 api/tests/unit/application/healthcheck/index_test.js diff --git a/api/lib/application/healthcheck/healthcheck-controller.js b/api/lib/application/healthcheck/healthcheck-controller.js new file mode 100644 index 000000000..5025d14f0 --- /dev/null +++ b/api/lib/application/healthcheck/healthcheck-controller.js @@ -0,0 +1,15 @@ +const packageJSON = require('../../../package.json'); +module.exports = { + + get(request, reply) { + + reply({ + 'name': packageJSON.name, + 'version': packageJSON.version, + 'description': packageJSON.description + }); + } + + +}; + diff --git a/api/lib/application/healthcheck/index.js b/api/lib/application/healthcheck/index.js new file mode 100644 index 000000000..7efaa69b0 --- /dev/null +++ b/api/lib/application/healthcheck/index.js @@ -0,0 +1,18 @@ +const healthcheckController = require('./healthcheck-controller'); +exports.register = function (server, options, next) { + + server.route([ + { + method: 'GET', + path: '/api', + config: {handler: healthcheckController.get, tags: ['api']} + }, + ]); + + return next(); +}; + +exports.register.attributes = { + name: 'healthcheck-api', + version: '1.0.0' +}; diff --git a/api/server.js b/api/server.js index c5cd7e494..d1fc251bc 100644 --- a/api/server.js +++ b/api/server.js @@ -29,6 +29,7 @@ server.register([ require('./lib/application/users'), require('./lib/application/followers'), require('./lib/application/feedbacks'), + require('./lib/application/healthcheck'), /* Hapi plugins */ require('inert'), diff --git a/api/tests/unit/application/healthcheck/healthcheck-controller_test.js b/api/tests/unit/application/healthcheck/healthcheck-controller_test.js new file mode 100644 index 000000000..5e1f63dd7 --- /dev/null +++ b/api/tests/unit/application/healthcheck/healthcheck-controller_test.js @@ -0,0 +1,26 @@ +const {describe, it, expect, sinon} = require('../../../test-helper'); + +const healthcheckController = require('../../../../lib/application/healthcheck/healthcheck-controller'); + +describe('Unit | Controller | healthcheckController', () => { + + describe('#get', () => { + it('should provide get method', () => { + expect(healthcheckController.get).to.exist; + }); + + it('should reply with the API description', function () { + // given + const replySpy = sinon.spy(); + + // when + healthcheckController.get(null, replySpy); + + // then + const arguments = replySpy.firstCall.args[0]; + expect(arguments).to.include.keys('name', 'version', 'description'); + expect(arguments['name']).to.equal('pix-api'); + expect(arguments['description']).to.equal('Plateforme d\'évaluation et de certification des compétences numériques à l\'usage de tous les citoyens francophones'); + }); + }); +}); diff --git a/api/tests/unit/application/healthcheck/index_test.js b/api/tests/unit/application/healthcheck/index_test.js new file mode 100644 index 000000000..7d60cd820 --- /dev/null +++ b/api/tests/unit/application/healthcheck/index_test.js @@ -0,0 +1,37 @@ +const {describe, it, expect, before, beforeEach, after, sinon} = require('../../../test-helper'); +const Hapi = require('hapi'); +const healthcheckController = require('../../../../lib/application/healthcheck/healthcheck-controller'); + +describe('Unit | Router | HealthcheckRouter', function () { + + let server; + + beforeEach(function () { + server = this.server = new Hapi.Server(); + server.connection({port: null}); + server.register({register: require('../../../../lib/application/healthcheck')}); + }); + + function expectRouteToExist(routeOptions, done) { + server.inject(routeOptions, (res) => { + expect(res.statusCode).to.equal(200); + done(); + }); + } + + describe('GET /api', function () { + + before(function () { + sinon.stub(healthcheckController, 'get', (request, reply) => reply('ok')); + }); + + after(function () { + healthcheckController.get.restore(); + }); + + it('should exist', function (done) { + return expectRouteToExist({method: 'GET', url: '/api'}, done); + }); + }); + +});