Skip to content

Commit

Permalink
addresses #19 added unit tests for success and failed token parsing w…
Browse files Browse the repository at this point in the history
…hen logging in
  • Loading branch information
oveddan committed Sep 25, 2012
2 parents d23debd + 23392ab commit a5d3997
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"main": "index",
"dependencies": {
"serializer": ">=0.0.2 <0.1.0",
"connect": "2.3.x"
},
"devDependencies": {
"mocha" : "1.0.3"
Expand Down
101 changes: 83 additions & 18 deletions test/OAuth2Provider_tests.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,102 @@
var sinon = require('sinon'),
should = require('chai').should,
should = require('chai').should(),
serializer = require('serializer');

describe('OAuth2Provider', function(){
it('should inherit from EventEmitter', function(){
// rneeds to be in here since static constructor depends on creating
// secure serializer
var oAuth2Provider = createOauth2Provider();
// it('should inherit from EventEmitter', function(){
// // secure serializer
// var oAuth2Provider = createOauth2Provider();

oAuth2Provider.should.be.a('EventEmitter');
});
// oAuth2Provider.should.be.an('EventEmitter');
// });

describe('login', function(){
beforeEach(function(){
var crypt_key = '123131',
sign_key = 'asdfasdfas';

// stub returned serializer so that can mock it
this.stubSerializer = {
parse : sinon.stub()
};

this.createSerializerStub = sinon.stub(serializer, 'createSecureSerializer');
this.createSerializerStub.withArgs(crypt_key, sign_key).returns(this.stubSerializer);

this.createSerializerStub = sinon.stub(serializer, 'createSecureSerializer');
this.emitterStub = sinon.stub('EventEmitter'),
this.oAuth2Provider = createOauth2Provider();
});
afterEach(function(){
this.createSerializerStub.restore();
this.emitterStub.restore();
});
var accessTokenKey = 'access_token';
// for backwards compatibility
it('should emit access_token if it can be parsed from request', function(done){
oAuth2Provider.login();
// for backwards compatibility

it('should return function that emits access_token if it can be parsed from request', function(){
// SETUP
var access_token = '123412341234124312341234';

var user_id = 'james',
client_id = '1231231',
dateString = '01/05/2012',
extra_data = 'wadfasdfasfasdfas';

var expectedParsedData = [user_id, client_id, dateString, extra_data];
// setup serializer so that returns above data for that access token
this.stubSerializer.parse.withArgs(access_token).returns(expectedParsedData);

this.oAuth2Provider.emit = sinon.spy();

// TEST
// build arguments that are verified
var req = {
query : {
'access_token' : access_token
}
},
nextFunction = function(){};
// get login middle ware function, and invoke it with above arguments
var middlewareFunction = this.oAuth2Provider.login();
middlewareFunction(req, {}, nextFunction);

// SHOULD
// make sure emit was called with correct arguments
this.oAuth2Provider.emit.calledOnce.should.equal(true);
var callArgs = this.oAuth2Provider.emit.firstCall.args;
callArgs[0].should.eql('access_token');
callArgs[1].should.eql(req);
callArgs[2].should.eql({
user_id: user_id,
client_id: client_id,
extra_data: extra_data,
grant_date: new Date(dateString)
});
callArgs[3].should.equal(nextFunction);
});
it('should write error to response if cannot parse access token', function(done){
it('should write error to response if cannot parse access token', function(){
// SETUP
var errorMessage = 'could not parse data',
access_token = '123412341234124312341234';
// change serializer to throw an error with the access token
this.stubSerializer.parse.withArgs(access_token).throws({ message : errorMessage});

var req = {
query : {
'access_token' : access_token
}
},
res = {
writeHead : sinon.spy(),
end : sinon.stub()
};

// TEST
// get login middle ware function, and invoke it with above arguments
var middlewareFunction = this.oAuth2Provider.login();
middlewareFunction(req, res);

// SHOULD
res.writeHead.calledWith(400).should.be.ok;
res.end.calledWith(errorMessage).should.be.ok;
});
});

Expand All @@ -38,12 +105,10 @@ describe('OAuth2Provider', function(){
var crypt_key = crypt_key || '123131',
sign_key = sign_key || 'asdfasdfas';

sinon.stub()

// requiring this needs module needs to be done repeatedly, since it depends on a static serializer
// factory in its static constructuro, which needs to be stubbed by many of the methods
var OAuth2Provider = require('../index'),
oAuth2Provider = new OAuth2Provider(crypt_key, sign_key);
var module = require('../index'),
oAuth2Provider = new module.OAuth2Provider(crypt_key, sign_key);
return oAuth2Provider;
};
});

0 comments on commit a5d3997

Please sign in to comment.