From 078d9d7ed77301b6e3ca594587379144abfe4309 Mon Sep 17 00:00:00 2001 From: AdriVanHoudt Date: Mon, 20 Apr 2015 09:53:40 +0200 Subject: [PATCH 1/2] Assingment 4 closes #118 --- Makefile | 2 +- lib/index.js | 3 +- lib/private.js | 49 +++++++++++++++++++++++++ lib/users.json | 10 +++++ package.json | 3 +- test/index.js | 3 +- test/private.js | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ test/version.js | 4 +- 8 files changed, 165 insertions(+), 7 deletions(-) create mode 100644 lib/private.js create mode 100644 lib/users.json create mode 100644 test/private.js diff --git a/Makefile b/Makefile index 83aaf0c..ab2cc70 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ test: @node node_modules/lab/bin/lab -a code test-cov: - @node node_modules/lab/bin/lab -a code -t 100 -L + @node node_modules/lab/bin/lab -a code -t 100 -Lv test-cov-html: @node node_modules/lab/bin/lab -a code -r html -o coverage.html diff --git a/lib/index.js b/lib/index.js index 74a77b4..261c1f9 100755 --- a/lib/index.js +++ b/lib/index.js @@ -2,6 +2,7 @@ var Hapi = require('hapi'); var Version = require('./version'); +var Private = require('./private'); // Declare internals @@ -14,7 +15,7 @@ exports.init = function (port, next) { var server = new Hapi.Server(); server.connection({ port: port }); - server.register(Version, function (err) { + server.register([Version, Private], function (err) { if (err) { return next(err); diff --git a/lib/private.js b/lib/private.js new file mode 100644 index 0000000..409d34a --- /dev/null +++ b/lib/private.js @@ -0,0 +1,49 @@ +// Load modules + +var Basic = require('hapi-auth-basic'); +var Users = require('./users.json'); + +// Declare internals + +var internals = {}; + +internals.validateFunc = function (username, password, callback) { + + var user = Users[username]; + if (!user || user.password !== password) { + return callback(null, false); + } + + return callback(null, true, user); +}; + +exports.register = function (server, options, next) { + + server.register(Basic, function (err) { + + if (err) { + return next(err); + } + + server.auth.strategy('simple', 'basic', { validateFunc: internals.validateFunc }); + server.route({ + method: 'GET', + path: '/private', + config: { + auth: 'simple', + description: 'Returns a greeting message to the authenticated user', + handler: function (request, reply) { + + var html = '
Hello ' + request.auth.credentials.username + '
'; + return reply(html); + } + } + }); + + return next(); + }); +}; + +exports.register.attributes = { + name: 'Private' +}; diff --git a/lib/users.json b/lib/users.json new file mode 100644 index 0000000..2c06092 --- /dev/null +++ b/lib/users.json @@ -0,0 +1,10 @@ +{ + "Foo": { + "username": "Foo", + "password": "mysupersecuredpassword" + }, + "Bar": { + "username": "Bar", + "password": "mysupersecuredpassword2" + } +} diff --git a/package.json b/package.json index e45972e..48c2eca 100755 --- a/package.json +++ b/package.json @@ -19,10 +19,11 @@ "homepage": "https://github.com/hueniverse/hueniversity", "dependencies": { "hapi": "8.x.x", + "hapi-auth-basic": "2.x.x", "hoek": "2.x.x" }, "scripts": { - "test": "node node_modules/lab/bin/lab -a code -t 100 -L", + "test": "node node_modules/lab/bin/lab -a code -t 100 -Lv", "start": "node lib/start.js" }, "devDependencies": { diff --git a/test/index.js b/test/index.js index 2d7900f..eabe42e 100755 --- a/test/index.js +++ b/test/index.js @@ -10,7 +10,6 @@ var Version = require('../lib/version'); // Test shortcuts var lab = exports.lab = Lab.script(); -var describe = lab.experiment; var expect = Code.expect; var it = lab.test; @@ -50,7 +49,7 @@ it('handles register plugin errors', { parallel: false }, function (done) { name: 'fake version' }; - Hueniversity.init(0, function (err, server) { + Hueniversity.init(0, function (err) { expect(err).to.exist(); expect(err.message).to.equal('register version failed'); diff --git a/test/private.js b/test/private.js new file mode 100644 index 0000000..5f7d5ab --- /dev/null +++ b/test/private.js @@ -0,0 +1,98 @@ +// Load modules + +var Code = require('code'); +var Lab = require('lab'); +var Hueniversity = require('../lib'); +var Users = require('../lib/users.json'); +var Basic = require('hapi-auth-basic'); + +// Declare internals + +var internals = {}; + + +// Test shortcuts + +var lab = exports.lab = Lab.script(); +var describe = lab.experiment; +var expect = Code.expect; +var it = lab.test; + + +describe('/private', function () { + + it('returns a greeting for the authenticated user', function (done) { + + Hueniversity.init(0, function (err, server) { + + expect(err).to.not.exist(); + + var request = { method: 'GET', url: '/private', headers: { authorization: internals.header(Users.Foo.username, Users.Foo.password) } }; + server.inject(request, function (res) { + + expect(res.statusCode, 'Status code').to.equal(200); + expect(res.result, 'result').to.equal('
Hello Foo
'); + + server.stop(done); + }); + }); + }); + + it('returns error on wrong password', function (done) { + + Hueniversity.init(0, function (err, server) { + + expect(err).to.not.exist(); + + var request = { method: 'GET', url: '/private', headers: { authorization: internals.header(Users.Foo.username, '') } }; + server.inject(request, function (res) { + + expect(res.statusCode, 'Status code').to.equal(401); + + server.stop(done); + }); + }); + }); + + it('returns error on failed auth', function (done) { + + Hueniversity.init(0, function (err, server) { + + expect(err).to.not.exist(); + + var request = { method: 'GET', url: '/private', headers: { authorization: internals.header('I do not exist', '') } }; + server.inject(request, function (res) { + + expect(res.statusCode, 'Status code').to.equal(401); + + server.stop(done); + }); + }); + }); + + it('returns error on failed registering of auth', { parallel: false }, function (done) { + + var orig = Basic.register; + Basic.register = function (plugin, options, next) { + + return next(new Error('fail')); + }; + Basic.register.attributes = { + name: 'fake hapi-auth-basic' + }; + + Hueniversity.init(0, function (err) { + + Basic.register = orig; + + expect(err).to.exist(); + + done(); + }); + }); +}); + +internals.header = function (username, password) { + + return 'Basic ' + (new Buffer(username + ':' + password, 'utf8')).toString('base64'); +}; diff --git a/test/version.js b/test/version.js index dfd2601..daf8bd6 100755 --- a/test/version.js +++ b/test/version.js @@ -3,7 +3,7 @@ var Code = require('code'); var Lab = require('lab'); var Pkg = require('../package.json'); -var Server = require('../lib'); +var Hueniversity = require('../lib'); // Test shortcuts @@ -18,7 +18,7 @@ describe('/version', function () { it('returns the version from package.json', function (done) { - Server.init(0, function (err, server) { + Hueniversity.init(0, function (err, server) { expect(err).to.not.exist(); From 094c8355992006d716af2cb4610d6290cadd3e50 Mon Sep 17 00:00:00 2001 From: AdriVanHoudt Date: Tue, 28 Apr 2015 14:14:06 +0200 Subject: [PATCH 2/2] added new lint tresholds and moved monkey patch to de-monkey patch quicker --- Makefile | 2 +- package.json | 2 +- test/private.js | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index ab2cc70..36760e3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ test: @node node_modules/lab/bin/lab -a code test-cov: - @node node_modules/lab/bin/lab -a code -t 100 -Lv + @node node_modules/lab/bin/lab -a code -t 100 -Lv --lint-errors-threshold 0 --lint-warnings-threshold 0 test-cov-html: @node node_modules/lab/bin/lab -a code -r html -o coverage.html diff --git a/package.json b/package.json index 48c2eca..7c39d4b 100755 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "hoek": "2.x.x" }, "scripts": { - "test": "node node_modules/lab/bin/lab -a code -t 100 -Lv", + "test": "node node_modules/lab/bin/lab -a code -t 100 -Lv --lint-errors-threshold 0 --lint-warnings-threshold 0", "start": "node lib/start.js" }, "devDependencies": { diff --git a/test/private.js b/test/private.js index 5f7d5ab..a50f691 100644 --- a/test/private.js +++ b/test/private.js @@ -75,6 +75,7 @@ describe('/private', function () { var orig = Basic.register; Basic.register = function (plugin, options, next) { + Basic.register = orig; return next(new Error('fail')); }; Basic.register.attributes = { @@ -83,8 +84,6 @@ describe('/private', function () { Hueniversity.init(0, function (err) { - Basic.register = orig; - expect(err).to.exist(); done();