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

Commit

Permalink
Merge pull request #10 from hapijs/test-cleanup
Browse files Browse the repository at this point in the history
Removed outer describe.
  • Loading branch information
arb committed Sep 29, 2014
2 parents 66272e7 + ed5e9d4 commit d436bb6
Show file tree
Hide file tree
Showing 2 changed files with 591 additions and 599 deletions.
266 changes: 131 additions & 135 deletions test/bewit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,202 +14,198 @@ var internals = {};

var expect = Lab.expect;
var before = Lab.before;
var describe = Lab.experiment;
var it = Lab.test;


describe('Bewit', function () {

var credentials = {
'john': {
cred: {
id: 'john',
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256'
}
},
'jane': {
err: Hapi.error.internal('boom')
var credentials = {
'john': {
cred: {
id: 'john',
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256'
}
};
},
'jane': {
err: Hapi.error.internal('boom')
}
};

var getCredentials = function (id, callback) {
var getCredentials = function (id, callback) {

if (credentials[id]) {
return callback(credentials[id].err, credentials[id].cred);
}
else {
return callback(null, null);
}
};
if (credentials[id]) {
return callback(credentials[id].err, credentials[id].cred);
}
else {
return callback(null, null);
}
};

var getBewit = function (id, path) {
var getBewit = function (id, path) {

if (credentials[id] && credentials[id].cred) {
return Hawk.uri.getBewit('http://example.com:8080' + path, { credentials: credentials[id].cred, ttlSec: 60 });
}
else {
return '';
}
};
if (credentials[id] && credentials[id].cred) {
return Hawk.uri.getBewit('http://example.com:8080' + path, { credentials: credentials[id].cred, ttlSec: 60 });
}
else {
return '';
}
};

var bewitHandler = function (request, reply) {
var bewitHandler = function (request, reply) {

reply('Success');
};
reply('Success');
};

var server = new Hapi.Server();
before(function (done) {
var server = new Hapi.Server();
before(function (done) {

server.pack.register(require('../'), function (err) {
server.pack.register(require('../'), function (err) {

expect(err).to.not.exist;
expect(err).to.not.exist;

server.auth.strategy('default', 'bewit', true, { getCredentialsFunc: getCredentials });
server.auth.strategy('default', 'bewit', true, { getCredentialsFunc: getCredentials });

server.route([
{ method: 'GET', path: '/bewit', handler: bewitHandler, config: { auth: 'default' } },
{ method: 'GET', path: '/bewitOptional', handler: bewitHandler, config: { auth: { mode: 'optional', strategy: 'default' } } },
{ method: 'GET', path: '/bewitScope', handler: bewitHandler, config: { auth: { scope: 'x', strategy: 'default' } } },
{ method: 'GET', path: '/bewitTos', handler: bewitHandler, config: { auth: { tos: '2.0.0', strategy: 'default' } } }
]);
server.route([
{ method: 'GET', path: '/bewit', handler: bewitHandler, config: { auth: 'default' } },
{ method: 'GET', path: '/bewitOptional', handler: bewitHandler, config: { auth: { mode: 'optional', strategy: 'default' } } },
{ method: 'GET', path: '/bewitScope', handler: bewitHandler, config: { auth: { scope: 'x', strategy: 'default' } } },
{ method: 'GET', path: '/bewitTos', handler: bewitHandler, config: { auth: { tos: '2.0.0', strategy: 'default' } } }
]);

done();
});
done();
});
});

it('returns a reply on successful auth', function (done) {
it('returns a reply on successful auth', function (done) {

var bewit = getBewit('john', '/bewit');
server.inject('http://example.com:8080/bewit?bewit=' + bewit, function (res) {
var bewit = getBewit('john', '/bewit');
server.inject('http://example.com:8080/bewit?bewit=' + bewit, function (res) {

expect(res.result).to.equal('Success');
done();
});
expect(res.result).to.equal('Success');
done();
});
});

it('returns an error reply on failed optional auth', function (done) {
it('returns an error reply on failed optional auth', function (done) {

var bewit = getBewit('john', '/abc');
server.inject('http://example.com:8080/bewitOptional?bewit=' + bewit, function (res) {
var bewit = getBewit('john', '/abc');
server.inject('http://example.com:8080/bewitOptional?bewit=' + bewit, function (res) {

expect(res.statusCode).to.equal(401);
done();
});
expect(res.statusCode).to.equal(401);
done();
});
});

it('returns an error on bad bewit', function (done) {
it('returns an error on bad bewit', function (done) {

var bewit = getBewit('john', '/abc');
server.inject('http://example.com:8080/bewit?bewit=' + bewit, function (res) {
var bewit = getBewit('john', '/abc');
server.inject('http://example.com:8080/bewit?bewit=' + bewit, function (res) {

expect(res.statusCode).to.equal(401);
done();
});
expect(res.statusCode).to.equal(401);
done();
});
});

it('returns an error on bad bewit format', function (done) {
it('returns an error on bad bewit format', function (done) {

server.inject('http://example.com:8080/bewit?bewit=junk', function (res) {
server.inject('http://example.com:8080/bewit?bewit=junk', function (res) {

expect(res.statusCode).to.equal(400);
done();
});
expect(res.statusCode).to.equal(400);
done();
});
});

it('returns an error on insufficient tos', function (done) {
it('returns an error on insufficient tos', function (done) {

var bewit = getBewit('john', '/bewitTos');
server.inject('http://example.com:8080/bewitTos?bewit=' + bewit, function (res) {
var bewit = getBewit('john', '/bewitTos');
server.inject('http://example.com:8080/bewitTos?bewit=' + bewit, function (res) {

expect(res.statusCode).to.equal(403);
done();
});
expect(res.statusCode).to.equal(403);
done();
});
});

it('returns an error on insufficient scope', function (done) {
it('returns an error on insufficient scope', function (done) {

var bewit = getBewit('john', '/bewitScope');
server.inject('http://example.com:8080/bewitScope?bewit=' + bewit, function (res) {
var bewit = getBewit('john', '/bewitScope');
server.inject('http://example.com:8080/bewitScope?bewit=' + bewit, function (res) {

expect(res.statusCode).to.equal(403);
done();
});
expect(res.statusCode).to.equal(403);
done();
});
});

it('returns a reply on successful auth when using a custom host header key', function (done) {
it('returns a reply on successful auth when using a custom host header key', function (done) {

var bewit = getBewit('john', '/bewit');
var request = { method: 'GET', url: '/bewit?bewit=' + bewit, headers: { custom: 'example.com:8080' } };
var bewit = getBewit('john', '/bewit');
var request = { method: 'GET', url: '/bewit?bewit=' + bewit, headers: { custom: 'example.com:8080' } };

var server = new Hapi.Server();
server.pack.register(require('../'), function (err) {
var server = new Hapi.Server();
server.pack.register(require('../'), function (err) {

expect(err).to.not.exist;
expect(err).to.not.exist;

server.auth.strategy('default', 'bewit', {
getCredentialsFunc: getCredentials,
hawk: {
hostHeaderName: 'custom'
}
});
server.auth.strategy('default', 'bewit', {
getCredentialsFunc: getCredentials,
hawk: {
hostHeaderName: 'custom'
}
});

server.route({ method: 'GET', path: '/bewit', handler: bewitHandler, config: { auth: 'default' } });
server.route({ method: 'GET', path: '/bewit', handler: bewitHandler, config: { auth: 'default' } });

server.inject(request, function (res) {
server.inject(request, function (res) {

expect(res.statusCode).to.equal(200);
expect(res.result).to.equal('Success');
done();
});
expect(res.statusCode).to.equal(200);
expect(res.result).to.equal('Success');
done();
});
});
});

it('cannot add a route that has payload validation required', function (done) {
it('cannot add a route that has payload validation required', function (done) {

var fn = function () {
var fn = function () {

server.route({ method: 'POST',
path: '/bewitPayload',
handler: bewitHandler,
config: { auth: { mode: 'required', strategy: 'default', payload: 'required' },
payload: { output: 'stream', parse: false } }
});
};
server.route({ method: 'POST',
path: '/bewitPayload',
handler: bewitHandler,
config: { auth: { mode: 'required', strategy: 'default', payload: 'required' },
payload: { output: 'stream', parse: false } }
});
};

expect(fn).to.throw(Error);
done();
});
expect(fn).to.throw(Error);
done();
});

it('cannot add a route that has payload validation as optional', function (done) {
it('cannot add a route that has payload validation as optional', function (done) {

var fn = function () {
var fn = function () {

server.route({ method: 'POST',
path: '/bewitPayload',
handler: bewitHandler,
config: { auth: { mode: 'required', strategy: 'default', payload: 'optional' },
payload: { output: 'stream', parse: false } }
});
};
server.route({ method: 'POST',
path: '/bewitPayload',
handler: bewitHandler,
config: { auth: { mode: 'required', strategy: 'default', payload: 'optional' },
payload: { output: 'stream', parse: false } }
});
};

expect(fn).to.throw(Error);
done();
});
expect(fn).to.throw(Error);
done();
});

it('can add a route that has payload validation as none', function (done) {
it('can add a route that has payload validation as none', function (done) {

var fn = function () {
var fn = function () {

server.route({ method: 'POST',
path: '/bewitPayload',
handler: bewitHandler,
config: { auth: { mode: 'required', strategy: 'default', payload: false },
payload: { output: 'stream', parse: false } }
});
};
server.route({ method: 'POST',
path: '/bewitPayload',
handler: bewitHandler,
config: { auth: { mode: 'required', strategy: 'default', payload: false },
payload: { output: 'stream', parse: false } }
});
};

expect(fn).to.not.throw(Error);
done();
});
expect(fn).to.not.throw(Error);
done();
});
Loading

0 comments on commit d436bb6

Please sign in to comment.