From d23debdd269a774f7ad5abfe1b94358029668176 Mon Sep 17 00:00:00 2001 From: oveddan Date: Tue, 25 Sep 2012 00:49:31 -0400 Subject: [PATCH 1/9] addresses #3 started creating unit tests for emitting events. created test describing how provider inherits from eventemitter --- makefile | 4 +++ package.json | 9 +++++-- test/OAuth2Provider_tests.js | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 makefile create mode 100644 test/OAuth2Provider_tests.js 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..5ed7d99 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": { @@ -9,7 +9,12 @@ }, "main": "index", "dependencies": { - "serializer": ">=0.0.2 <0.1.0" + "serializer": ">=0.0.2 <0.1.0", + }, + "devDependencies": { + "mocha" : "1.0.3" + ,"sinon" : "1.3.4" + ,"chai" : "1.0.3" }, "licenses": [ { diff --git a/test/OAuth2Provider_tests.js b/test/OAuth2Provider_tests.js new file mode 100644 index 0000000..3905168 --- /dev/null +++ b/test/OAuth2Provider_tests.js @@ -0,0 +1,49 @@ +var sinon = require('sinon'), + 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(); + + oAuth2Provider.should.be.a('EventEmitter'); + }); + + describe('login', function(){ + beforeEach(function(){ + // stub returned serializer so that can mock it + + 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(); + }); + it('should write error to response if cannot parse access token', function(done){ + + }); + }); + + // utility methods + var createOauth2Provider = function(crypt_key, sign_key){ + 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); + return oAuth2Provider; + }; +}); \ No newline at end of file From 23392ab1b453d2c1877c5b2eb9f87c35309fc2b4 Mon Sep 17 00:00:00 2001 From: oveddan Date: Tue, 25 Sep 2012 00:58:57 -0400 Subject: [PATCH 2/9] addresses #21 added requirement for connect.js, not sure if allowed versions are correct --- .gitignore | 1 + package.json | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/package.json b/package.json index b67e104..4227cd9 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ }, "main": "index", "dependencies": { - "serializer": ">=0.0.2 <0.1.0" + "serializer": ">=0.0.2 <0.1.0", + "connect": "2.3.x" }, "licenses": [ { From 251109f1e5d097b30df96003df1ea03be1bf5d3a Mon Sep 17 00:00:00 2001 From: oveddan Date: Tue, 25 Sep 2012 02:01:36 -0400 Subject: [PATCH 3/9] addresses #19 added test instructions to readme --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) 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: From 4ddb93639fe315b01f97ed136a8b1a4e6c30ddc3 Mon Sep 17 00:00:00 2001 From: oveddan Date: Tue, 25 Sep 2012 02:11:48 -0400 Subject: [PATCH 4/9] #19 made better test names --- test/OAuth2Provider_tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/OAuth2Provider_tests.js b/test/OAuth2Provider_tests.js index 4917d67..b496bca 100644 --- a/test/OAuth2Provider_tests.js +++ b/test/OAuth2Provider_tests.js @@ -31,7 +31,7 @@ describe('OAuth2Provider', function(){ var accessTokenKey = 'access_token'; // for backwards compatibility - it('should return function that emits access_token if it can be parsed from request', function(){ + 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'; From 393b95cc6d6125c637e833631d3f6cd3eb9de497 Mon Sep 17 00:00:00 2001 From: oveddan Date: Tue, 25 Sep 2012 02:31:28 -0400 Subject: [PATCH 5/9] fixes '#23 removed requiring connect from index since it's never used. --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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); From 263da8f4b45a4a2506f92b47d7a680e03b0884cd Mon Sep 17 00:00:00 2001 From: oveddan Date: Tue, 25 Sep 2012 02:32:51 -0400 Subject: [PATCH 6/9] 'closes #21 connect should have never been required, so removing from dependencies' Revert "addresses #21 added requirement for connect.js, not sure if allowed versions are correct" This reverts commit 23392ab1b453d2c1877c5b2eb9f87c35309fc2b4. --- .gitignore | 1 - package.json | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index b512c09..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -node_modules \ No newline at end of file diff --git a/package.json b/package.json index 4227cd9..b67e104 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,7 @@ }, "main": "index", "dependencies": { - "serializer": ">=0.0.2 <0.1.0", - "connect": "2.3.x" + "serializer": ">=0.0.2 <0.1.0" }, "licenses": [ { From 393ad823cf7743d23128342f6677a3df7bfef63b Mon Sep 17 00:00:00 2001 From: oveddan Date: Tue, 25 Sep 2012 02:36:17 -0400 Subject: [PATCH 7/9] added .gitignore for node_modules --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules From d4ff785e061266518f6ac64d6010e46d62979918 Mon Sep 17 00:00:00 2001 From: oveddan Date: Tue, 25 Sep 2012 10:27:43 -0400 Subject: [PATCH 8/9] #19 change to go directly to parse stub --- test/OAuth2Provider_tests.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/OAuth2Provider_tests.js b/test/OAuth2Provider_tests.js index b496bca..6c9e3d6 100644 --- a/test/OAuth2Provider_tests.js +++ b/test/OAuth2Provider_tests.js @@ -15,13 +15,14 @@ describe('OAuth2Provider', function(){ var crypt_key = '123131', sign_key = 'asdfasdfas'; - // stub returned serializer so that can mock it - this.stubSerializer = { - parse : sinon.stub() - }; + // 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(this.stubSerializer); + this.createSerializerStub.withArgs(crypt_key, sign_key).returns({ + parse : this.parseStub + }); this.oAuth2Provider = createOauth2Provider(); }); @@ -40,9 +41,10 @@ describe('OAuth2Provider', function(){ 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.stubSerializer.parse.withArgs(access_token).returns(expectedParsedData); + this.parseStub.withArgs(access_token).returns(expectedParsedData); this.oAuth2Provider.emit = sinon.spy(); @@ -77,7 +79,7 @@ describe('OAuth2Provider', function(){ 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}); + this.parseStub.withArgs(access_token).throws({ message : errorMessage}); var req = { query : { From ea7fafbdc43690419d284ec87ba828c7edf7c40b Mon Sep 17 00:00:00 2001 From: oveddan Date: Tue, 25 Sep 2012 10:31:19 -0400 Subject: [PATCH 9/9] addressed #19 cleanup unneeded code --- test/OAuth2Provider_tests.js | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/test/OAuth2Provider_tests.js b/test/OAuth2Provider_tests.js index 6c9e3d6..96887d6 100644 --- a/test/OAuth2Provider_tests.js +++ b/test/OAuth2Provider_tests.js @@ -2,13 +2,9 @@ var sinon = require('sinon'), should = require('chai').should(), serializer = require('serializer'); -describe('OAuth2Provider', function(){ - // it('should inherit from EventEmitter', function(){ - // // secure serializer - // var oAuth2Provider = createOauth2Provider(); +var module = require('../index'); - // oAuth2Provider.should.be.an('EventEmitter'); - // }); +describe('OAuth2Provider', function(){ describe('login', function(){ beforeEach(function(){ @@ -49,7 +45,7 @@ describe('OAuth2Provider', function(){ this.oAuth2Provider.emit = sinon.spy(); // TEST - // build arguments that are verified + // build arguments that are passed to middleware function var req = { query : { 'access_token' : access_token @@ -92,7 +88,7 @@ describe('OAuth2Provider', function(){ }; // TEST - // get login middle ware function, and invoke it with above arguments + // get login middleware function, and invoke it with above arguments var middlewareFunction = this.oAuth2Provider.login(); middlewareFunction(req, res); @@ -101,16 +97,14 @@ describe('OAuth2Provider', function(){ 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'; - // utility methods - var createOauth2Provider = function(crypt_key, sign_key){ - var crypt_key = crypt_key || '123131', - sign_key = sign_key || 'asdfasdfas'; - - // 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 module = require('../index'), - oAuth2Provider = new module.OAuth2Provider(crypt_key, sign_key); - return oAuth2Provider; - }; -}); \ No newline at end of file + return new module.OAuth2Provider(crypt_key, sign_key); +}; \ No newline at end of file