From 3dc3096b151546f4222a4d6e4525cade6c2c6a83 Mon Sep 17 00:00:00 2001 From: "Jose F. Romaniello" Date: Fri, 16 Nov 2012 12:43:12 -0300 Subject: [PATCH] add basic integration tests --- README.md | 7 ++ lib/index.js | 4 +- package.json | 16 ++++- test/authorizer.test.js | 51 +++++++++++++++ test/fixture/index.js | 71 +++++++++++++++++++++ test/fixture/setSocketIOHandshakeCookies.js | 20 ++++++ test/fixture/setupPassport.js | 23 +++++++ test/mocha.opts | 2 + 8 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 test/authorizer.test.js create mode 100644 test/fixture/index.js create mode 100644 test/fixture/setSocketIOHandshakeCookies.js create mode 100644 test/fixture/setupPassport.js create mode 100644 test/mocha.opts diff --git a/README.md b/README.md index 660d3e3..e7a2ad9 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,13 @@ Usage ``` +Develop +======= + + npm install + npm test + + License ======== diff --git a/lib/index.js b/lib/index.js index 85e2ae9..f1ce77c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,7 +4,7 @@ var connectUtils = require('connect').utils, var overwrite = function(overwritten) { return (function() { if( arguments.length > 1 ) { - for( objects in arguments ) { + for(var objects in arguments ) { overwrite( this, arguments[objects] ); } } @@ -17,7 +17,7 @@ var overwrite = function(overwritten) { return this; }).apply(overwritten, Array.prototype.slice.call(arguments, 1)); -} +}; function authorize(options) { var auth = { diff --git a/package.json b/package.json index 59e80dc..334a11a 100644 --- a/package.json +++ b/package.json @@ -16,11 +16,23 @@ "type": "git", "url": "https://github.com/jfromaniello/passport.socketio.git" }, - "scripts": {}, + "scripts": { + "test": "mocha" + }, "license": "MIT", "dependencies": { "passport": "~0.1.12", "connect": "~2.4.5", - "cookie": "0.0.4" + "cookie": "0.0.4", + "request": "~2.12.0" + }, + "devDependencies": { + "should": "~1.2.1", + "mocha": "~1.7.0", + "express": "~3.0.3", + "socket.io": "~0.9.11", + "connect": "~2.4.6", + "passport-local": "~0.1.6", + "xmlhttprequest": "~1.5.0" } } diff --git a/test/authorizer.test.js b/test/authorizer.test.js new file mode 100644 index 0000000..bd1cedc --- /dev/null +++ b/test/authorizer.test.js @@ -0,0 +1,51 @@ +var fixture = require('./fixture'), + request = require('request'), + setSocketIOHandshakeCookies = require('./fixture/setSocketIOHandshakeCookies'); + +var io = require('socket.io-client'); + +describe('authorizer', function () { + + //start and stop the server + before(fixture.start); + after(fixture.stop); + + //create a new session for every test + beforeEach(function(){ + this.cookies = request.jar(); + setSocketIOHandshakeCookies(this.cookies); + }); + + + describe('when the user is not logged in', function () { + + it('should emit error with unauthorized handshake', function (done){ + var socket = io.connect('http://localhost:9000', {'force new connection':true}); + socket.on('error', function(err){ + err.should.eql('handshake unauthorized'); + done(); + }); + }); + + }); + + describe('when the user is logged in', function() { + + beforeEach(function (done) { + request.post({ + jar: this.cookies, + url: 'http://localhost:9000/login', + form: {username: 'jose', password: 'Pa123'} + }, done); + }); + + it('should do the handshake and connect', function (done){ + var socket = io.connect('http://localhost:9000'); + socket.on('connect', function(){ + done(); + }).on('error', done); + }); + + }); + +}); \ No newline at end of file diff --git a/test/fixture/index.js b/test/fixture/index.js new file mode 100644 index 0000000..9071dd4 --- /dev/null +++ b/test/fixture/index.js @@ -0,0 +1,71 @@ +var express = require('express'), + connect = require('connect'), + passport = require('passport'), + http = require('http'); + +var socketIo = require('socket.io'), + passportSocketIo = require('../../lib'); + +var sessionStore = new connect.session.MemoryStore(), + cookieSecret = 'asdasdsdas1312312', + sessionKey = 'test-session-key'; + +var server; + +require('./setupPassport'); + +exports.start = function (callback) { + var app = express(); + app.configure(function(){ + app.use(express.cookieParser(cookieSecret)); + + app.use(express.bodyParser()); + app.use(express.methodOverride()); + + app.use(express.session({ + store: sessionStore, + key: sessionKey + })); + + app.use(passport.initialize()); + app.use(passport.session()); + + }); + + app.post('/login', passport.authenticate('local', { successRedirect: '/', + failureRedirect: '/login', + failureFlash: true })); + + app.get('/', function(req, res){ + if(!req.user){ + res.send(401); + }else{ + res.json(req.user); + } + }); + + server = http.createServer(app); + + var sio = socketIo.listen(server); + sio.configure(function(){ + this.set('authorization', passportSocketIo.authorize({ + sessionKey: sessionKey, + sessionStore: sessionStore, + sessionSecret: cookieSecret + })); + + this.set('log level', 0); + + }); + + sio.sockets.on('echo', function (m) { + sio.sockets.emit('echo-response', m); + }); + + server.listen(9000, callback); +}; + +exports.stop = function (callback) { + server.close(); + callback(); +}; \ No newline at end of file diff --git a/test/fixture/setSocketIOHandshakeCookies.js b/test/fixture/setSocketIOHandshakeCookies.js new file mode 100644 index 0000000..3ac8dcb --- /dev/null +++ b/test/fixture/setSocketIOHandshakeCookies.js @@ -0,0 +1,20 @@ +var xmlhttprequest = require('xmlhttprequest'); +var originalRequest = xmlhttprequest.XMLHttpRequest; + +module.exports = function (jar) { + xmlhttprequest.XMLHttpRequest = function(){ + originalRequest.apply(this, arguments); + this.setDisableHeaderCheck(true); + + var stdOpen = this.open; + + this.open = function() { + stdOpen.apply(this, arguments); + var header = jar.get({ url: 'http://localhost:9000' }) + .map(function (c) { + return c.name + "=" + c.value; + }).join("; "); + this.setRequestHeader('cookie', header); + }; + }; +}; \ No newline at end of file diff --git a/test/fixture/setupPassport.js b/test/fixture/setupPassport.js new file mode 100644 index 0000000..ebc7da8 --- /dev/null +++ b/test/fixture/setupPassport.js @@ -0,0 +1,23 @@ +var passport = require('passport'), + LocalStrategy = require('passport-local').Strategy; + +passport.use(new LocalStrategy( + function(username, password, done) { + if(username === 'jose' && password === 'Pa123'){ + return done(null, { + name: 'jose', + mail: 'j@f.r' + }); + } else { + return done(null, false, {message: 'wrong user name or password'}); + } + } +)); + +passport.serializeUser(function(user, done) { + done(null, user); +}); + +passport.deserializeUser(function(user, done) { + done(null, user); +}); \ No newline at end of file diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..35a00c1 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,2 @@ +--require should +--reporter spec \ No newline at end of file