Skip to content

Commit

Permalink
Add unit and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Basim Hennawi committed Jan 28, 2018
1 parent b9cd14d commit cb7a559
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
3 changes: 2 additions & 1 deletion test/integration/handlers/authenticate-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe('AuthenticateHandler integration', function() {
});
});

it('should return an access token', function() {
it('should return an access token with extend model obj with request', function() {
var accessToken = {
user: {},
accessTokenExpiresAt: new Date(new Date().getTime() + 10000)
Expand All @@ -192,6 +192,7 @@ describe('AuthenticateHandler integration', function() {

return handler.handle(request, response)
.then(function(data) {
model.request.should.equal(request);
data.should.equal(accessToken);
})
.catch(should.fail);
Expand Down
3 changes: 2 additions & 1 deletion test/integration/handlers/authorize-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ describe('AuthorizeHandler integration', function() {
});
});

it('should return the `code` if successful', function() {
it('should return the `code` if successful with extend model obj with request', function() {
var client = { grants: ['authorization_code'], redirectUris: ['http://example.com/cb'] };
var model = {
getAccessToken: function() {
Expand Down Expand Up @@ -479,6 +479,7 @@ describe('AuthorizeHandler integration', function() {

return handler.handle(request, response)
.then(function(data) {
model.request.should.equal(request);
data.should.eql({
authorizationCode: 12345,
client: client
Expand Down
3 changes: 2 additions & 1 deletion test/integration/handlers/token-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('TokenHandler integration', function() {
});
});

it('should return a bearer token if successful', function() {
it('should return a bearer token if successful with extend model obj with request', function() {
var token = { accessToken: 'foo', client: {}, refreshToken: 'bar', scope: 'foobar', user: {} };
var model = {
getClient: function() { return { grants: ['password'] }; },
Expand All @@ -323,6 +323,7 @@ describe('TokenHandler integration', function() {

return handler.handle(request, response)
.then(function(data) {
model.request.should.equal(request);
data.should.eql(token);
})
.catch(should.fail);
Expand Down
34 changes: 34 additions & 0 deletions test/unit/handlers/authenticate-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

var AuthenticateHandler = require('../../../lib/handlers/authenticate-handler');
var Request = require('../../../lib/request');
var Response = require('../../../lib/response');
var sinon = require('sinon');
var should = require('should');
var ServerError = require('../../../lib/errors/server-error');
Expand All @@ -15,6 +16,39 @@ var ServerError = require('../../../lib/errors/server-error');
*/

describe('AuthenticateHandler', function() {
describe('handle()', function() {
it('should extend model object with request context', function() {
var model = {
getAccessToken: sinon.stub().returns({
user: 'foo',
accessTokenExpiresAt: new Date(new Date().getTime() + 10000)
}),
verifyScope: sinon.stub().returns(true)
};

var handler = new AuthenticateHandler({
addAcceptedScopesHeader: true,
addAuthorizedScopesHeader: true,
model: model,
scope: 'bar'
});

var request = new Request({
body: {},
headers: { 'Authorization': 'Bearer foo' },
method: {},
query: {}
});
var response = new Response({});

return handler.handle(request, response)
.then(function() {
model.request.should.equal(request);
})
.catch(should.fail);
});
});

describe('getTokenFromRequest()', function() {
describe('with bearer token in the request authorization header', function() {
it('should call `getTokenFromRequestHeader()`', function() {
Expand Down
34 changes: 34 additions & 0 deletions test/unit/handlers/authorize-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,40 @@ var should = require('should');
*/

describe('AuthorizeHandler', function() {
describe('handle()', function() {
it('should extend model object with request context', function() {
var model = {
getClient: sinon.stub().returns({
grants: ['authorization_code'],
redirectUris: ['/abc']
}),
saveAuthorizationCode: sinon.stub().returns({ authorizationCode: 'code_abc' })
};
var handler = new AuthorizeHandler({
authenticateHandler: {
handle: sinon.stub().returns({ name: 'xyz' })
},
authorizationCodeLifetime: 123,
allowEmptyState: true,
model: model
});

var request = new Request({
body: { client_id: '123', response_type: 'code' },
headers: {},
method: {},
query: {}
});
var response = new Response({});

return handler.handle(request, response)
.then(function() {
model.request.should.equal(request);
})
.catch(should.fail);
});
});

describe('generateAuthorizationCode()', function() {
it('should call `model.generateAuthorizationCode()`', function() {
var model = {
Expand Down
37 changes: 37 additions & 0 deletions test/unit/handlers/token-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

var Request = require('../../../lib/request');
var Response = require('../../../lib/response');
var TokenHandler = require('../../../lib/handlers/token-handler');
var sinon = require('sinon');
var should = require('should');
Expand All @@ -14,6 +15,42 @@ var should = require('should');
*/

describe('TokenHandler', function() {
describe('handle()', function() {
it('should extend model object with request context', function() {
var model = {
getClient: sinon.stub().returns({ grants: ['client_credentials'] }),
getUserFromClient: sinon.stub().returns({}),
saveToken: sinon.stub().returns({
accessToken: '123',
client: {},
user: {},
accessTokenExpiresAt: new Date(new Date().getTime() + 10000),
refreshTokenExpiresAt: new Date(new Date().getTime() + 10000)
}),
};

var handler = new TokenHandler({
accessTokenLifetime: 123,
refreshTokenLifetime: 123,
model: model,
});

var request = new Request({
method: 'POST',
body: { 'grant_type': 'client_credentials', 'client_id': 'abc', 'client_secret': 'xyz' },
headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' },
query: {}
});
var response = new Response({});

return handler.handle(request, response)
.then(function() {
model.request.should.equal(request);
})
.catch(should.fail);
});
});

describe('getClient()', function() {
it('should call `model.getClient()`', function() {
var model = {
Expand Down

0 comments on commit cb7a559

Please sign in to comment.