Skip to content
This repository was archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
Cleanup for #119
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed May 12, 2015
1 parent c3a0fec commit 51cec7c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
10 changes: 8 additions & 2 deletions lib/private.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
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);
}

user.username = username;

This comment has been minimized.

Copy link
@hueniverse

hueniverse May 12, 2015

Better to set duplicate values in code than repeat them in the JSON file.


return callback(null, true, user);
};


exports.register = function (server, options, next) {

server.register(Basic, function (err) {
Expand All @@ -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 });

This comment has been minimized.

Copy link
@hueniverse

hueniverse May 12, 2015

When possible, name the strategy the same as the scheme. Keeps things easier to follow.

server.route({
method: 'GET',
path: '/private',
config: {
auth: 'simple',
auth: 'basic',
description: 'Returns a greeting message to the authenticated user',
handler: function (request, reply) {

Expand All @@ -44,6 +49,7 @@ exports.register = function (server, options, next) {
});
};


exports.register.attributes = {
name: 'Private'
};
10 changes: 4 additions & 6 deletions lib/users.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"Foo": {
"username": "Foo",
"password": "mysupersecuredpassword"
"foo": {
"password": "foo"

This comment has been minimized.

Copy link
@hueniverse

hueniverse May 12, 2015

Don't make life complicated. Someone will need to type this to test it.

},
"Bar": {
"username": "Bar",
"password": "mysupersecuredpassword2"
"bar": {
"password": "bar"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hueniversity",
"version": "0.0.3",
"version": "0.0.4",
"description": "Community learning experiment",
"main": "lib/index.js",
"repository": {
Expand All @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

This comment has been minimized.

Copy link
@hueniverse

hueniverse May 12, 2015

Keep the callback signature consistent even when not using the arguments.


expect(err).to.exist();
expect(err.message).to.equal('register version failed');
Expand Down
16 changes: 10 additions & 6 deletions test/private.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Hueniversity = require('../lib');
var Users = require('../lib/users.json');
var Basic = require('hapi-auth-basic');


// Declare internals

var internals = {};
Expand All @@ -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('<div>Hello Foo</div>');
expect(res.result, 'result').to.equal('<div>Hello foo</div>');

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);
Expand All @@ -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) {

Expand All @@ -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'
};
Expand All @@ -91,6 +94,7 @@ describe('/private', function () {
});
});


internals.header = function (username, password) {

return 'Basic ' + (new Buffer(username + ':' + password, 'utf8')).toString('base64');
Expand Down

0 comments on commit 51cec7c

Please sign in to comment.