Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for login, and remove require of connect from index.js #22

Merged
merged 11 commits into from
Sep 28, 2012
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ support both cookie-authenticated and OAuth access to protected URLs, you
could populate `req.session.user` so that individual URLs don't need to
care about which type of authentication was used.

## Running tests

Install dev dependencies:

$ npm install -d

Run the tests:

$ make test

## Example

In the root directory, run `npm install express` and then run:
Expand Down
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

var EventEmitter = require('events').EventEmitter,
querystring = require('querystring'),
serializer = require('serializer'),
connect = require('connect');
serializer = require('serializer');

function OAuth2Provider(crypt_key, sign_key) {
this.serializer = serializer.createSecureSerializer(crypt_key, sign_key);
Expand Down
4 changes: 4 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
@./node_modules/.bin/mocha -u bdd -R spec

.PHONY: test testintegration
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oauth2-provider",
"version": "1.1.0",
"version": "1.1.1",
"description": "A simple customizable OAuth 2.0 provider (server) for node.js.",
"homepage": "https://github.com/ammmir/node-oauth2-provider",
"author": {
Expand All @@ -11,6 +11,11 @@
"dependencies": {
"serializer": ">=0.0.2 <0.1.0"
},
"devDependencies": {
"mocha" : "1.0.3"
,"sinon" : "1.3.4"
,"chai" : "1.0.3"
},
"licenses": [
{
"type": "MIT",
Expand Down
110 changes: 110 additions & 0 deletions test/OAuth2Provider_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
var sinon = require('sinon'),
should = require('chai').should(),
serializer = require('serializer');

var module = require('../index');

describe('OAuth2Provider', function(){

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

// create parse stub that will be used to parse incoming requests
this.parseStub = sinon.stub();

// stub method to return object that has parseStub for parser
this.createSerializerStub = sinon.stub(serializer, 'createSecureSerializer');
this.createSerializerStub.withArgs(crypt_key, sign_key).returns({
parse : this.parseStub
});

this.oAuth2Provider = createOauth2Provider();
});
afterEach(function(){
this.createSerializerStub.restore();
});
var accessTokenKey = 'access_token';
// for backwards compatibility

it('should return function that emits access_token event with parsed user data if token 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';

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

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

// TEST
// build arguments that are passed to middleware function
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(){
// SETUP
var errorMessage = 'could not parse data',
access_token = '123412341234124312341234';
// change serializer to throw an error with the access token
this.parseStub.withArgs(access_token).throws({ message : errorMessage});

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

// TEST
// get login middleware 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;
});
});
});



// utility methods
var createOauth2Provider = function(crypt_key, sign_key){
var crypt_key = crypt_key || '123131',
sign_key = sign_key || 'asdfasdfas';

return new module.OAuth2Provider(crypt_key, sign_key);
};