add basic integration tests
This commit is contained in:
parent
d8c7c0ed7d
commit
3dc3096b15
@ -50,6 +50,13 @@ Usage
|
||||
|
||||
```
|
||||
|
||||
Develop
|
||||
=======
|
||||
|
||||
npm install
|
||||
npm test
|
||||
|
||||
|
||||
License
|
||||
========
|
||||
|
||||
|
@ -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 = {
|
||||
|
16
package.json
16
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"
|
||||
}
|
||||
}
|
||||
|
51
test/authorizer.test.js
Normal file
51
test/authorizer.test.js
Normal file
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
71
test/fixture/index.js
Normal file
71
test/fixture/index.js
Normal file
@ -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();
|
||||
};
|
20
test/fixture/setSocketIOHandshakeCookies.js
Normal file
20
test/fixture/setSocketIOHandshakeCookies.js
Normal file
@ -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);
|
||||
};
|
||||
};
|
||||
};
|
23
test/fixture/setupPassport.js
Normal file
23
test/fixture/setupPassport.js
Normal file
@ -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);
|
||||
});
|
2
test/mocha.opts
Normal file
2
test/mocha.opts
Normal file
@ -0,0 +1,2 @@
|
||||
--require should
|
||||
--reporter spec
|
Reference in New Issue
Block a user