Permalink
Browse files

Merge pull request #22 from oveddan/addUnitTests

Add unit tests for login, and remove require of connect from index.js
  • Loading branch information...
2 parents 7acc5ed + ea7fafb commit 8b18eaf04c63c8c86c8eab7538f2c39350b95643 @ammmir committed Sep 28, 2012
Showing with 132 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +10 −0 README.md
  3. +1 −2 index.js
  4. +4 −0 makefile
  5. +6 −1 package.json
  6. +110 −0 test/OAuth2Provider_tests.js
View
1 .gitignore
@@ -0,0 +1 @@
+node_modules
View
10 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:
View
3 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);
View
4 makefile
@@ -0,0 +1,4 @@
+test:
+ @./node_modules/.bin/mocha -u bdd -R spec
+
+.PHONY: test testintegration
View
7 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",
View
110 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);
+};

0 comments on commit 8b18eaf

Please sign in to comment.