forked from outmoded/university
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
851656f
commit 36f62ce
Showing
5 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
"username": "Bar", | ||
"password": "mysupersecuredpassword2" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('<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) { | ||
|
||
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'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters