Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ describe('scheme', () => {
expect(res2.result).to.equal('resource');
});

it('errors in validation function', async () => {
it('returns unauthorized on errors in validation function', async () => {

const server = Hapi.server();
await server.register(require('../'));
Expand Down Expand Up @@ -411,6 +411,8 @@ describe('scheme', () => {

expect(res2.statusCode).to.equal(401);
expect(error).to.be.an.error('boom');
expect(res2.headers['set-cookie']).to.exist().and.have.length(1);
expect(res2.headers['set-cookie'][0]).to.include('special=;');
});

it('uauthorized error in validation function fails over to subsequent authentication scheme', async () => {
Expand Down Expand Up @@ -489,6 +491,49 @@ describe('scheme', () => {
expect(res2.request.auth.credentials.user).to.equal('bogus-user');
});

it('returns system errors in validation function', async () => {

const server = Hapi.server({ debug: false });
await server.register(require('../'));

server.auth.strategy('default', 'cookie', {
cookie: {
password: 'password-should-be-32-characters',
clearInvalid: true,
ttl: 60 * 1000,
name: 'special'
},
validate: function (request, session) {

throw new TypeError('huh?');
}
});
server.auth.default('default');

Helpers.loginWithResourceEndpoint(server);

const res = await server.inject('/login/steve');

expect(res.result).to.equal('steve');
const header = res.headers['set-cookie'];
expect(header.length).to.equal(1);
expect(header[0]).to.contain('Max-Age=60');
const cookie = header[0].match(internals.cookieRx);

let error;
server.ext('onPreResponse', (request, h) => {

error = request.response;
return h.continue;
});

const res2 = await server.inject({ method: 'GET', url: '/resource', headers: { cookie: 'special=' + cookie[1] } });

expect(res2.statusCode).to.equal(500);
expect(error).to.be.an.error('huh?');
expect(res2.headers['set-cookie']).to.not.exist();
});

it('authenticates a request (no ttl)', async () => {

const server = Hapi.server();
Expand Down
Loading