diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md index b7f17c1..de4edc1 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/index.js b/index.js index 9f079db..9f30653 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/makefile b/makefile new file mode 100644 index 0000000..2239768 --- /dev/null +++ b/makefile @@ -0,0 +1,4 @@ +test: + @./node_modules/.bin/mocha -u bdd -R spec + +.PHONY: test testintegration diff --git a/package.json b/package.json index b67e104..e1c5a94 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/test/OAuth2Provider_tests.js b/test/OAuth2Provider_tests.js new file mode 100644 index 0000000..96887d6 --- /dev/null +++ b/test/OAuth2Provider_tests.js @@ -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); +}; \ No newline at end of file