-
Notifications
You must be signed in to change notification settings - Fork 192
Assingment 4 #119
Assingment 4 #119
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = '<div>Hello ' + request.auth.credentials.username + '</div>'; | ||
return reply(html); | ||
} | ||
} | ||
}); | ||
|
||
return next(); | ||
}); | ||
}; | ||
|
||
exports.register.attributes = { | ||
name: 'Private' | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"Foo": { | ||
"username": "Foo", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Safer to add the username programatically than repeat information. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you mean not have the username and assume the key is? |
||
"password": "mysupersecuredpassword" | ||
}, | ||
"Bar": { | ||
"username": "Bar", | ||
"password": "mysupersecuredpassword2" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to keep the callback function with all its actual arguments. Kind of self documenting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like a waste to pass around unused objects, the callback from the init function is self documenting enough imo, but you have a point |
||
expect(err).to.exist(); | ||
expect(err.message).to.equal('register version failed'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// 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('<div>Hello Foo</div>'); | ||
|
||
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) { | ||
|
||
Basic.register = orig; | ||
return next(new Error('fail')); | ||
}; | ||
Basic.register.attributes = { | ||
name: 'fake hapi-auth-basic' | ||
}; | ||
|
||
Hueniversity.init(0, function (err) { | ||
|
||
expect(err).to.exist(); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
internals.header = function (username, password) { | ||
|
||
return 'Basic ' + (new Buffer(username + ':' + password, 'utf8')).toString('base64'); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these extra flags really needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debatable but Imo they will help prevent that the majority of the comments
on pr's are about following the hapi style.
On Fri, 1 May 2015 18:13 Eran Hammer [email protected] wrote: