From 241dae508bb39c19ea45765f65cd039641419131 Mon Sep 17 00:00:00 2001 From: Yulia Tenincheva Date: Mon, 13 Nov 2017 19:31:45 +0200 Subject: [PATCH] Updated dependencies, tests are re-written with lab v15 and hapi v17 Library linted --- lib/index.js | 79 ++++++------- package.json | 14 +-- test/index.js | 315 ++++++++++++++++++++------------------------------ 3 files changed, 171 insertions(+), 237 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1f7b619..42dcc6b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,7 +7,7 @@ const Joi = require('joi'); const Pkg = require('../package.json'); // Declare internals -var internals = { +const internals = { schema: { showAuth: Joi.boolean().default(false), showStart: Joi.boolean().default(true) @@ -16,25 +16,25 @@ var internals = { exports.register = function (server, options) { - var result = Joi.validate(options, internals.schema); + const result = Joi.validate(options, internals.schema); Hoek.assert(!result.error, result.error && result.error.annotate()); options = result.value; server.expose('text', function () { - - var info = this.info(); + + const info = this.info(); return internals.printableInfo(info, options); }); - server.expose('info', function () { - + server.expose('info', () => { + return internals.getRouteInfo(server, options); }); if (options.showStart) { - server.events.on('start', function () { - - var out = server.plugins[Pkg.name].text(); + server.events.on('start', () => { + + const out = server.plugins[Pkg.name].text(); console.log(out); }); } @@ -42,9 +42,9 @@ exports.register = function (server, options) { internals.printableInfo = function (connections, options) { - var out = ''; - for (var i = 0, il = connections.length; i < il; ++i) { - var connection = connections[i]; + let out = ''; + for (let i = 0; i < connections.length; ++i) { + const connection = connections[i]; out += internals.printableConnection(connection, options); } @@ -54,7 +54,7 @@ internals.printableInfo = function (connections, options) { internals.printableConnection = function (connection, options) { - var out = internals.printableTitle(connection, options) + '\n'; + let out = internals.printableTitle(connection, options) + '\n'; out += internals.printableRoutes(connection.routes, options); return out; }; @@ -62,18 +62,19 @@ internals.printableConnection = function (connection, options) { internals.printableRoutes = function (routes, options) { - var out = ''; - for (var i = 0, il = routes.length; i < il; ++i) { - var show = routes[i]; + let out = ''; + for (let i = 0; i < routes.length; ++i) { + const show = routes[i]; - var method = internals.formatMethod(show.method); - var description = internals.formatDescription(show.description); - var auth = internals.formatAuth(show.auth); - var path = internals.formatPath(show.path); + const method = internals.formatMethod(show.method); + const description = internals.formatDescription(show.description); + const auth = internals.formatAuth(show.auth); + const path = internals.formatPath(show.path); if (options.showAuth) { out += [method, path, auth, description].join(' ') + '\n'; - } else { + } + else { out += [method, path, description].join(' ') + '\n'; } } @@ -83,42 +84,39 @@ internals.printableRoutes = function (routes, options) { internals.printableTitle = function (connection) { - - var title = Chalk.underline(connection.uri); + + const title = Chalk.underline(connection.uri); return Chalk.cyan(title); }; internals.getRouteInfo = function (server, options) { - var connections = []; + const routingTable = server.table(); - var routingTable = server.table(); - - var connectionInfo = { + const connectionInfo = { uri: server.info.uri, routes: [] }; internals.connectionInfo(routingTable, options, connectionInfo); - connections.push(connectionInfo); - return connections; + return [connectionInfo]; }; internals.connectionInfo = function (routes, options, connectionInfo) { - for (var i = 0, il = routes.length; i < il; ++i) { - var route = routes[i]; + for (let i = 0; i < routes.length; ++i) { + const route = routes[i]; - var defaultStrategy = Hoek.reach(route, 'connection.auth.settings.default.strategies'); - var authStrategy = route.settings.auth ? route.settings.auth.strategies.toString() : false; + const defaultStrategy = Hoek.reach(route, 'server.auth.settings.default.strategies'); + let authStrategy = route.settings.auth ? route.settings.auth.strategies.toString() : false; if (route.settings.auth === undefined) { authStrategy = defaultStrategy ? String(defaultStrategy) : false; } - var show = { + const show = { method: route.method.toUpperCase(), path: route.path, description: route.settings.description || '' @@ -131,10 +129,7 @@ internals.connectionInfo = function (routes, options, connectionInfo) { connectionInfo.routes.push(show); } - connectionInfo.routes.sort(function (a, b) { - - return a.path.localeCompare(b.path); - }); + connectionInfo.routes.sort((a, b) => a.path.localeCompare(b.path)); }; @@ -148,8 +143,7 @@ internals.formatPath = function (path) { internals.ljust = function (string, amount) { - var padding = ' '; - var currentLength = string.length; + const padding = ' '; while (string.length < amount) { string = string + padding; @@ -171,7 +165,8 @@ internals.formatAuth = function (auth) { if (auth === false) { auth = Chalk.red('none'); - } else { + } + else { auth = Chalk.green(auth); } auth = internals.ljust(auth, 20); @@ -182,7 +177,7 @@ internals.formatDescription = function (description) { description = description || ''; description = Chalk.yellow(description); - + return description; }; diff --git a/package.json b/package.json index a391016..7a9a8fc 100644 --- a/package.json +++ b/package.json @@ -18,14 +18,14 @@ "author": "Daniel Bretoi", "license": "BSD", "dependencies": { - "chalk": "0.5.x", - "hoek": "3.x.x", - "joi": "7.x.x" + "chalk": "^2.3.0", + "hoek": "^5.0.2", + "joi": "^13.0.1" }, "devDependencies": { - "code": "1.x.x", - "hapi": "8.x.x", - "hapi-auth-basic": "2.x.x", - "lab": "5.x.x" + "code": "^5.1.2", + "hapi": "^17.0.1", + "hapi-auth-basic": "4.2.x", + "lab": "^15.1.2" } } diff --git a/test/index.js b/test/index.js index 8675854..00618d6 100644 --- a/test/index.js +++ b/test/index.js @@ -1,158 +1,108 @@ -// Load modules +'use strict'; -var Hapi = require('hapi'); -var Code = require('code'); -var Lab = require('lab'); +// Load modules -var Blipp = require('../'); -var Pkg = require('../package.json'); +const Hapi = require('hapi'); +const Code = require('code'); +const Lab = require('lab'); +const Blipp = require('../lib/'); +const Pkg = require('../package.json'); // Test shortcuts +const { expect, it, describe } = exports.lab = require('lab').script(); -var lab = exports.lab = Lab.script(); -var expect = Code.expect; -var describe = lab.describe; -var it = lab.test; - - +// only one connection; results are in alphabetical order var internals = { - validateFunc: function (username, password, callback) { + validateFunc: async function (request, username, password, h) { - callback(err, true, {}); + return {isValid: true, credentials: null}; }, result: [{ - uri: 'http://nero.local', - labels: ['first'], routes: [ { method: 'GET', path: '/', description: 'main index' }, { method: 'GET', path: '/all', description: 'a route on all connections' }, + { method: 'GET', path: '/api', description: 'api routes' }, { method: 'POST', path: '/apost/{foo}/comment/{another}', description: '' }, { method: 'GET', path: '/hi', description: '' }, { method: 'DELETE', path: '/post/{id}', description: '' } ] - }, { - uri: 'http://nero.local', - labels: ['second'], - routes: [ - { method: 'GET', path: '/all', description: 'a route on all connections' }, - { method: 'GET', path: '/api', description: 'api routes' } - ] - }, { - uri: 'http://nero.local', - labels: [], - routes: [{ - method: 'GET', - path: '/all', - description: 'a route on all connections' - }] }], authResult: [{ - uri: 'http://nero.local', - labels: ['first'], routes: [ { method: 'GET', path: '/', description: 'main index', auth: false }, - { method: 'GET', path: '/all', description: 'a route on all connections', auth: false }, - { method: 'POST', path: '/apost/{foo}/comment/{another}', description: '', auth: false }, + { method: 'GET', path: '/all', description: 'a route on all connections', auth: 'findme' }, + { method: 'GET', path: '/api', description: 'api routes', auth: 'findme' }, + { method: 'POST', path: '/apost/{foo}/comment/{another}', description: '', auth: 'findme' }, { method: 'GET', path: '/hi', description: '', auth: 'findme' }, - { method: 'DELETE', path: '/post/{id}', description: '', auth: false } - ] - }, { - uri: 'http://nero.local', - labels: ['second'], - routes: [ - { method: 'GET', path: '/all', description: 'a route on all connections', auth: false }, - { method: 'GET', path: '/api', description: 'api routes', auth: false } - ] - }, { - uri: 'http://nero.local', - labels: [], - routes: [ - { method: 'GET', path: '/all', description: 'a route on all connections', auth: false } + { method: 'DELETE', path: '/post/{id}', description: '', auth: 'findme' } ] }], defaultAuthResult: [{ - uri: 'http://nero.local', - labels: ['first'], routes: [ { method: 'GET', path: '/', description: 'main index', auth: false }, { method: 'GET', path: '/all', description: 'a route on all connections', auth: 'findme' }, + { method: 'GET', path: '/api', description: 'api routes', auth: 'findme' }, { method: 'POST', path: '/apost/{foo}/comment/{another}', description: '', auth: 'findme' }, { method: 'GET', path: '/hi', description: '', auth: 'findme' }, { method: 'DELETE', path: '/post/{id}', description: '', auth: 'findme' } ] - }, { - uri: 'http://nero.local', - labels: ['second'], - routes: [ - { method: 'GET', path: '/all', description: 'a route on all connections', auth: 'findme' }, - { method: 'GET', path: '/api', description: 'api routes', auth: 'findme' } - ] - }, { - uri: 'http://nero.local', - labels: [], - routes: [ - { method: 'GET', path: '/all', description: 'a route on all connections', auth: 'findme' } - ] }] }; -internals.prepareServer = function (options, callback) { +internals.prepareServer = async function (options) { var server = new Hapi.Server(); - server.connection({ labels: ['first'] }); - server.connection({ labels: ['second'] }); - server.connection(); - - server.register(require('hapi-auth-basic'), function (err) { - + try { + await server.register(require('hapi-auth-basic')); + if (options.authType === 'findme') { - server.auth.strategy('findme', 'basic', { validateFunc: internals.validateFunc }); + server.auth.strategy('findme', 'basic', { validate: internals.validateFunc }); } if (options.authType === 'default') { - server.auth.strategy('findme', 'basic', { validateFunc: internals.validateFunc }); + server.auth.strategy('findme', 'basic', { validate: internals.validateFunc }); server.auth.default('findme'); } - }); + + } catch(err) { + console.log('Error: Couldn\'t register hapi-auth-basic', err); + } var api = { - register: function (plugin, pluginOptions, next) { - + register: function (plugin, pluginOptions) { plugin.route({ method: 'GET', path: '/api', - config: { + options: { description: 'api routes', - handler: function (request, reply) { + auth: options.authType ? 'findme' : null, + handler: function (request, h) { - return reply('index!'); + return 'index!'; } } }); - return next(); } }; - api.register.attributes = { - name: 'an api plugin', - version: '0.1.1' - }; + api.name = 'an api plugin'; + api.version = '1.0.0'; var main = { - register: function (plugin, pluginOptions, next) { + register: function (plugin, pluginOptions) { plugin.route({ method: 'GET', path: '/', - config: { + options: { auth: false, description: 'main index', - handler: function (request, reply) { + handler: function (request, h) { - return reply('index!'); + return 'index!'; } } }); @@ -160,11 +110,11 @@ internals.prepareServer = function (options, callback) { plugin.route({ method: 'GET', path: '/hi', - config: { + options: { auth: options.authType ? 'findme' : null, - handler: function (request, reply) { + handler: function (request, h) { - return reply('Hello!'); + return 'Hello!'; } } }); @@ -172,143 +122,132 @@ internals.prepareServer = function (options, callback) { plugin.route({ method: 'POST', path: '/apost/{foo}/comment/{another}', - handler: function (request, reply) { + options: { + auth: options.authType ? 'findme' : null, + handler: function (request, h) { - return reply(''); + return ''; + } } }); plugin.route({ method: 'DELETE', path: '/post/{id}', - handler: function (request, reply) { + options: { + auth: options.authType ? 'findme' : null, + handler: function (request, h) { - return reply(''); + return ''; + } } }); - - - return next(); } }; - main.register.attributes = { - name: 'main', - version: '0.1.1' - }; + main.name = 'main'; + main.version = '0.1.1'; server.route({ method: 'GET', path: '/all', - config: { + options: { description: 'a route on all connections', - handler: function (request, reply) { + auth: options.authType ? 'findme' : null, + handler: function (request, h) { - return reply('index!'); + return 'index!'; } } }); + + try { + await server.register([ + { plugin: Blipp, options: options.blippOptions }, + { plugin: main, options: {}}, + { plugin: api, options: {}} + ]); - server.register([{ register: Blipp, options: options.blippOptions }], function (err) { - - server.register([main], { select: 'first' }, function (err) { - - server.register([api], { select: 'second' }, function (err) { - - expect(err).to.not.exist(); - return callback(server); - }); - }); - }); - - server.start(); -}; - + await server.start(); -internals.fixUri = function (server, connections) { + expect(server).to.exist(); - for (var i = 0, il = connections.length; i < il; ++i) { - var connection = connections[i]; - connection.uri = server.info.uri; + return server; + } catch(err) { + expect(err).to.not.exist(); } }; - describe('routes', function () { - it('print route information', function (done) { - + it('print route information', async () => { + var saved = console.log; var out = ''; - console.log = function (str) { - - out += str; - }; - - internals.prepareServer(false, function (server) { - - setTimeout(function () { - - console.log = saved; - expect(out).to.not.match(/none.*main index/); - expect(out).to.match(/DELETE.*post/); - done(); - }, 20); - }); + console.log = (str) => out += str; + + const server = await internals.prepareServer(false); + + console.log = saved; + expect(out).to.not.match(/none.*main index/); + expect(out).to.match(/DELETE.*post/); + }); - - it('gets route information', function (done) { - - internals.prepareServer({ blippOptions: { showAuth: false, showStart: false } }, function (server) { - - var info = server.plugins[Pkg.name].info(); - internals.fixUri(server, internals.result); - expect(info).to.deep.equal(internals.result); - var text = server.plugins[Pkg.name].text(); - expect(text).to.not.match(/none.*main index/); - done(); - }); + it('gets route information', async () => { + let blippOptions = { + showAuth: false, + showStart: false + }; + + const server = await internals.prepareServer({blippOptions}); + + var info = server.plugins[Pkg.name].info(); + delete info[0].uri; + expect(info).to.equal(internals.result); + var text = server.plugins[Pkg.name].text(); + expect(text).to.not.match(/none.*main index/); }); - it('gets route information with auth', function (done) { - - internals.prepareServer({ blippOptions: { showAuth: true, showStart: false }, authType: 'findme' }, function (server) { - - var info = server.plugins[Pkg.name].info(); - internals.fixUri(server, internals.authResult); - expect(info).to.deep.equal(internals.authResult); - var text = server.plugins[Pkg.name].text(); - expect(text).to.match(/none.*main index/); - expect(text).to.match(/none.*api routes/); - expect(text).to.match(/hi.*findme/); - done(); - }); + it('gets route information with auth', async () => { + let blippOptions = { + showAuth: true, + showStart: false + }; + + const server = await internals.prepareServer({ blippOptions, authType: 'findme' }); + + var info = server.plugins[Pkg.name].info(); + delete info[0].uri; + expect(info).to.equal(internals.authResult); + + var text = server.plugins[Pkg.name].text(); + expect(text).to.match(/none.*main index/); + expect(text).to.match(/findme.*api routes/); + expect(text).to.match(/hi.*findme/); }); - it('gets route information with default', function (done) { - - internals.prepareServer({ blippOptions: { showAuth: true, showStart: false }, authType: 'default' }, function (server) { - - var info = server.plugins[Pkg.name].info(); - internals.fixUri(server, internals.defaultAuthResult); - expect(info).to.deep.equal(internals.defaultAuthResult); - var text = server.plugins[Pkg.name].text(); - expect(text).to.match(/none.*main index/); - expect(text).to.match(/findme.*api routes/); - done(); - }); + it('gets route information with default', async () => { + let blippOptions = { + showAuth: true, + showStart: false + }; + + const server = await internals.prepareServer({ blippOptions, authType: 'default' }); + + var info = server.plugins[Pkg.name].info(); + delete info[0].uri; + expect(info).to.equal(internals.defaultAuthResult); + var text = server.plugins[Pkg.name].text(); + expect(text).to.match(/none.*main index/); + expect(text).to.match(/findme.*api routes/); }); - it('fails with invalid options', function (done) { - - var invalidOptions = function () { - - internals.prepareServer({ blippOptions: { derp: true } }, function (server) { - - }); + it('fails with invalid options', async () => { + try { + await internals.prepareServer({ blippOptions: { derp: true } }) + } catch(err) { + expect(err).to.exist(); }; - expect(invalidOptions).to.throw(); - done(); }); });