From 51cec7cf6922cf6fec2dfa116ab8b30495362b69 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 11 May 2015 23:27:00 -0700 Subject: [PATCH] Cleanup for #119 --- lib/private.js | 10 ++++++++-- lib/users.json | 10 ++++------ package.json | 4 ++-- test/index.js | 2 +- test/private.js | 16 ++++++++++------ 5 files changed, 25 insertions(+), 17 deletions(-) mode change 100644 => 100755 lib/private.js mode change 100644 => 100755 lib/users.json mode change 100644 => 100755 test/private.js diff --git a/lib/private.js b/lib/private.js old mode 100644 new mode 100755 index 409d34a..cb04f04 --- a/lib/private.js +++ b/lib/private.js @@ -3,10 +3,12 @@ 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]; @@ -14,9 +16,12 @@ internals.validateFunc = function (username, password, callback) { return callback(null, false); } + user.username = username; + return callback(null, true, user); }; + exports.register = function (server, options, next) { server.register(Basic, function (err) { @@ -25,12 +30,12 @@ exports.register = function (server, options, next) { return next(err); } - server.auth.strategy('simple', 'basic', { validateFunc: internals.validateFunc }); + server.auth.strategy('basic', 'basic', { validateFunc: internals.validateFunc }); server.route({ method: 'GET', path: '/private', config: { - auth: 'simple', + auth: 'basic', description: 'Returns a greeting message to the authenticated user', handler: function (request, reply) { @@ -44,6 +49,7 @@ exports.register = function (server, options, next) { }); }; + exports.register.attributes = { name: 'Private' }; diff --git a/lib/users.json b/lib/users.json old mode 100644 new mode 100755 index 2c06092..ac4476c --- a/lib/users.json +++ b/lib/users.json @@ -1,10 +1,8 @@ { - "Foo": { - "username": "Foo", - "password": "mysupersecuredpassword" + "foo": { + "password": "foo" }, - "Bar": { - "username": "Bar", - "password": "mysupersecuredpassword2" + "bar": { + "password": "bar" } } diff --git a/package.json b/package.json index 7c39d4b..7646f3d 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hueniversity", - "version": "0.0.3", + "version": "0.0.4", "description": "Community learning experiment", "main": "lib/index.js", "repository": { @@ -23,7 +23,7 @@ "hoek": "2.x.x" }, "scripts": { - "test": "node node_modules/lab/bin/lab -a code -t 100 -Lv --lint-errors-threshold 0 --lint-warnings-threshold 0", + "test": "node node_modules/lab/bin/lab -a code -t 100 -L -v", "start": "node lib/start.js" }, "devDependencies": { diff --git a/test/index.js b/test/index.js index eabe42e..b7591d5 100755 --- a/test/index.js +++ b/test/index.js @@ -49,7 +49,7 @@ it('handles register plugin errors', { parallel: false }, function (done) { name: 'fake version' }; - Hueniversity.init(0, function (err) { + Hueniversity.init(0, function (err, server) { expect(err).to.exist(); expect(err.message).to.equal('register version failed'); diff --git a/test/private.js b/test/private.js old mode 100644 new mode 100755 index a50f691..8b0e86c --- a/test/private.js +++ b/test/private.js @@ -6,6 +6,7 @@ var Hueniversity = require('../lib'); var Users = require('../lib/users.json'); var Basic = require('hapi-auth-basic'); + // Declare internals var internals = {}; @@ -27,24 +28,24 @@ describe('/private', function () { expect(err).to.not.exist(); - var request = { method: 'GET', url: '/private', headers: { authorization: internals.header(Users.Foo.username, Users.Foo.password) } }; + var request = { method: 'GET', url: '/private', headers: { authorization: internals.header('foo', Users.foo.password) } }; server.inject(request, function (res) { expect(res.statusCode, 'Status code').to.equal(200); - expect(res.result, 'result').to.equal('
Hello Foo
'); + expect(res.result, 'result').to.equal('
Hello foo
'); server.stop(done); }); }); }); - it('returns error on wrong password', function (done) { + it('errors 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, '') } }; + var request = { method: 'GET', url: '/private', headers: { authorization: internals.header('foo', '') } }; server.inject(request, function (res) { expect(res.statusCode, 'Status code').to.equal(401); @@ -54,7 +55,7 @@ describe('/private', function () { }); }); - it('returns error on failed auth', function (done) { + it('errors on failed auth', function (done) { Hueniversity.init(0, function (err, server) { @@ -70,14 +71,16 @@ describe('/private', function () { }); }); - it('returns error on failed registering of auth', { parallel: false }, function (done) { + it('errors on failed registering of auth', { parallel: false }, function (done) { var orig = Basic.register; + Basic.register = function (plugin, options, next) { Basic.register = orig; return next(new Error('fail')); }; + Basic.register.attributes = { name: 'fake hapi-auth-basic' }; @@ -91,6 +94,7 @@ describe('/private', function () { }); }); + internals.header = function (username, password) { return 'Basic ' + (new Buffer(username + ':' + password, 'utf8')).toString('base64');