Fixed travis

- Tests against Node 4, 8, 10, 12 and newest

Modernized:
- Use arrow functions
- Use string templates in examples and some code
- Use single quote for strings
This commit is contained in:
Fabian Arndt
2019-10-14 01:46:30 +02:00
parent 93e6b59710
commit 8f2b55a7aa
12 changed files with 159 additions and 166 deletions

View File

@ -3,109 +3,109 @@ const fixture = require('./fixture');
const request = require('request');
const io = require('socket.io-client');
describe('authorizer', function() {
describe('authorizer', () => {
//start and stop the server
before(function(done) { fixture.start({ }, done) });
before((done) => { fixture.start({ }, done) });
after(fixture.stop);
describe('when the user is not logged in', function () {
it('should emit error with unauthorized handshake', function (done) {
describe('when the user is not logged in', () => {
it('should emit error with unauthorized handshake', (done) => {
const socket = io.connect('http://localhost:9000?token=boooooo', {
forceNew: true
});
socket.on('error', function(err) {
err.message.should.eql("jwt malformed");
err.code.should.eql("invalid_token");
socket.on('error', (err) => {
err.message.should.eql('jwt malformed');
err.code.should.eql('invalid_token');
socket.close();
done();
});
});
});
describe('when the user is logged in', function() {
before(function (done) {
describe('when the user is logged in', () => {
before((done) => {
request.post({
url: 'http://localhost:9000/login',
form: { username: 'jose', password: 'Pa123' },
json: true
}, function (err, resp, body) {
}, (err, resp, body) => {
this.token = body.token;
done();
}.bind(this));
});
});
describe('authorizer disallows query string token when specified in startup options', function() {
before(function(done) {
describe('authorizer disallows query string token when specified in startup options', () => {
before((done) => {
Q.ninvoke(fixture, 'stop')
.then(function() { return Q.ninvoke(fixture, 'start', { auth_header_required: true })})
.then(() => Q.ninvoke(fixture, 'start', { auth_header_required: true }))
.done(done);
});
after(function(done) {
after((done) => {
Q.ninvoke(fixture, 'stop')
.then(function() { return Q.ninvoke(fixture, 'start', { })})
.then(() => Q.ninvoke(fixture, 'start', { }))
.done(done);
});
it('auth headers are supported', function (done) {
it('auth headers are supported', (done) => {
const socket = io.connect('http://localhost:9000', {
forceNew: true,
extraHeaders: { Authorization: 'Bearer ' + this.token}
});
socket
.on('connect', function() {
.on('connect', () => {
socket.close();
done();
})
.on('error', done);
});
it('auth token in query string is disallowed', function (done) {
it('auth token in query string is disallowed', (done) => {
const socket = io.connect('http://localhost:9000', {
forceNew: true,
query: 'token=' + this.token
});
socket.on('error', function(err) {
err.message.should.eql("Server requires Authorization Header");
err.code.should.eql("missing_authorization_header");
socket.on('error', (err) => {
err.message.should.eql('Server requires Authorization Header');
err.code.should.eql('missing_authorization_header');
socket.close();
done();
});
});
})
describe('authorizer all auth types allowed', function() {
before(function(done) {
describe('authorizer all auth types allowed', () => {
before((done) => {
Q.ninvoke(fixture, 'stop')
.then(function() { return Q.ninvoke(fixture, 'start', {})})
.then(() => Q.ninvoke(fixture, 'start', {}))
.done(done);
})
it('auth headers are supported', function (done) {
it('auth headers are supported', (done) => {
const socket = io.connect('http://localhost:9000', {
forceNew: true,
extraHeaders: { Authorization: 'Bearer ' + this.token }
});
socket
.on('connect', function() {
.on('connect', () => {
socket.close();
done();
})
.on('error', done);
});
it('should do the handshake and connect', function (done) {
it('should do the handshake and connect', (done) => {
const socket = io.connect('http://localhost:9000', {
forceNew: true,
query: 'token=' + this.token
});
socket
.on('connect', function() {
.on('connect', () => {
socket.close();
done();
})
@ -115,25 +115,25 @@ describe('authorizer', function() {
});
});
describe('unsigned token', function() {
beforeEach(function () {
describe('unsigned token', () => {
beforeEach(() => {
this.token = 'eyJhbGciOiJub25lIiwiY3R5IjoiSldUIn0.eyJuYW1lIjoiSm9obiBGb28ifQ.';
});
it('should not do the handshake and connect', function (done) {
it('should not do the handshake and connect', (done) => {
const socket = io.connect('http://localhost:9000', {
forceNew: true,
query: 'token=' + this.token
});
socket
.on('connect', function () {
.on('connect', () => {
socket.close();
done(new Error('this shouldnt happen'));
})
.on('error', function (err) {
.on('error', (err) => {
socket.close();
err.message.should.eql("jwt signature is required");
err.message.should.eql('jwt signature is required');
done();
});
});

View File

@ -2,46 +2,43 @@ const fixture = require('./fixture/namespace');
const request = require('request');
const io = require('socket.io-client');
describe('authorizer with namespaces', function () {
describe('authorizer with namespaces', () => {
//start and stop the server
before(fixture.start);
after(fixture.stop);
describe('when the user is not logged in', function () {
describe('when the user is not logged in', () => {
it('should be able to connect to the default namespace', function (done) {
it('should be able to connect to the default namespace', (done) => {
io.connect('http://localhost:9000')
.once('hi', done);
.once('hi', () => done());
});
it('should not be able to connect to the admin namespace', function (done) {
it('should not be able to connect to the admin namespace', (done) => {
io.connect('http://localhost:9000/admin')
.once('disconnect', function() { done(); })
.once('hi admin', function() {
done(new Error('unauthenticated client was able to connect to the admin namespace'));
});
.once('disconnect', () => done())
.once('hi admin', () => done(new Error('unauthenticated client was able to connect to the admin namespace')));
});
});
describe('when the user is logged in', function() {
describe('when the user is logged in', () => {
beforeEach(function (done) {
beforeEach((done) => {
request.post({
url: 'http://localhost:9000/login',
form: { username: 'jose', password: 'Pa123' },
json: true
}, function (err, resp, body) {
}, (err, resp, body) => {
this.token = body.token;
done();
}.bind(this));
});
});
it('should do the handshake and connect', function (done) {
it('should do the handshake and connect', (done) => {
io.connect('http://localhost:9000/admin', { forceNew: true })
.on('authenticated', done)
.on('authenticated', () => done())
.emit('authenticate', { token: this.token });
});
});

View File

@ -2,29 +2,25 @@ const fixture = require('./fixture');
const request = require('request');
const io = require('socket.io-client');
describe('authorizer without querystring', function () {
describe('authorizer without querystring', () => {
//start and stop the server
before(function (done) {
fixture.start({
handshake: false
} , done);
before((done) => {
fixture.start({ handshake: false }, done);
});
after(fixture.stop);
describe('when the user is not logged in', function () {
describe('when the user is not logged in', () => {
it('should close the connection after a timeout if no auth message is received', function (done) {
it('should close the connection after a timeout if no auth message is received', (done) => {
io.connect('http://localhost:9000', { forceNew: true })
.once('disconnect', function () { done(); });
.once('disconnect', () => done());
});
it('should not respond echo', function (done) {
it('should not respond echo', (done) => {
io.connect('http://localhost:9000', { forceNew: true })
.on('echo-response', function () {
done(new Error('this should not happen'));
})
.on('echo-response', () => done(new Error('this should not happen')))
.emit('echo', { hi: 123 });
setTimeout(done, 1200);
@ -32,30 +28,28 @@ describe('authorizer without querystring', function () {
});
describe('when the user is logged in', function() {
describe('when the user is logged in', () => {
beforeEach(function (done) {
beforeEach((done) => {
request.post({
url: 'http://localhost:9000/login',
form: { username: 'jose', password: 'Pa123' },
json: true
}, function (err, resp, body) {
}, (err, resp, body) => {
this.token = body.token;
done();
}.bind(this));
});
});
it('should do the handshake and connect', function (done) {
it('should do the handshake and connect', (done) => {
const socket = io.connect('http://localhost:9000', { forceNew: true });
socket
.on('echo-response', function () {
.on('echo-response', () => {
socket.close();
done();
})
.on('authenticated', function () {
socket.emit('echo');
})
.on('authenticated', () => { socket.emit('echo'); })
.emit('authenticate', { token: this.token });
});
});

View File

@ -2,10 +2,10 @@ const fixture = require('./fixture/secret_function');
const request = require('request');
const io = require('socket.io-client');
describe('authorizer with secret function', function () {
describe('authorizer with secret function', () => {
//start and stop the server
before(function (done) {
before((done) => {
fixture.start({
handshake: false
}, done);
@ -13,51 +13,49 @@ describe('authorizer with secret function', function () {
after(fixture.stop);
describe('when the user is not logged in', function () {
describe('when the user is not logged in', () => {
describe('and when token is not valid', function() {
beforeEach(function (done) {
describe('and when token is not valid', () => {
beforeEach((done) => {
request.post({
url: 'http://localhost:9000/login',
json: { username: 'invalid_signature', password: 'Pa123' }
}, function (err, resp, body) {
}, (err, resp, body) => {
this.invalidToken = body.token;
done();
}.bind(this));
});
});
it('should emit unauthorized', function (done) {
it('should emit unauthorized', (done) => {
io.connect('http://localhost:9000', { forceNew: true })
.on('unauthorized', function() { done(); })
.on('unauthorized', () => done())
.emit('authenticate', { token: this.invalidToken + 'ass' })
});
});
});
describe('when the user is logged in', function() {
describe('when the user is logged in', () => {
beforeEach(function (done) {
beforeEach((done) => {
request.post({
url: 'http://localhost:9000/login',
json: { username: 'valid_signature', password: 'Pa123' }
}, function (err, resp, body) {
}, (err, resp, body) => {
this.token = body.token;
done();
}.bind(this));
});
});
it('should do the handshake and connect', function (done) {
it('should do the handshake and connect', (done) => {
const socket = io.connect('http://localhost:9000', { forceNew: true });
socket
.on('echo-response', function () {
.on('echo-response', () => {
socket.close();
done();
})
.on('authenticated', function () {
socket.emit('echo');
})
.on('authenticated', () => { socket.emit('echo'); })
.emit('authenticate', { token: this.token });
});
});

View File

@ -2,20 +2,20 @@ const fixture = require('./fixture/secret_function');
const request = require('request');
const io = require('socket.io-client');
describe('authorizer with secret function', function () {
describe('authorizer with secret function', () => {
//start and stop the server
before(fixture.start);
after(fixture.stop);
describe('when the user is not logged in', function () {
describe('when the user is not logged in', () => {
it('should emit error with unauthorized handshake', function (done) {
it('should emit error with unauthorized handshake', (done) => {
const socket = io.connect('http://localhost:9000?token=boooooo', { forceNew: true });
socket.on('error', function(err) {
err.message.should.eql("jwt malformed");
err.code.should.eql("invalid_token");
socket.on('error', (err) => {
err.message.should.eql('jwt malformed');
err.code.should.eql('invalid_token');
socket.close();
done();
});
@ -23,26 +23,26 @@ describe('authorizer with secret function', function () {
});
describe('when the user is logged in', function() {
describe('when the user is logged in', () => {
beforeEach(function (done) {
beforeEach((done) => {
request.post({
url: 'http://localhost:9000/login',
json: { username: 'valid_signature', password: 'Pa123' }
}, function (err, resp, body) {
}, (err, resp, body) => {
this.token = body.token;
done();
}.bind(this));
});
});
it('should do the handshake and connect', function (done) {
it('should do the handshake and connect', (done) => {
const socket = io.connect('http://localhost:9000', {
forceNew: true,
query: 'token=' + this.token
});
socket
.on('connect', function() {
.on('connect', () => {
socket.close();
done();
})
@ -50,25 +50,25 @@ describe('authorizer with secret function', function () {
});
});
describe('unsigned token', function() {
beforeEach(function () {
describe('unsigned token', () => {
beforeEach(() => {
this.token = 'eyJhbGciOiJub25lIiwiY3R5IjoiSldUIn0.eyJuYW1lIjoiSm9obiBGb28ifQ.';
});
it('should not do the handshake and connect', function (done) {
it('should not do the handshake and connect', (done) => {
const socket = io.connect('http://localhost:9000', {
forceNew: true,
query: 'token=' + this.token
});
socket
.on('connect', function () {
.on('connect', () => {
socket.close();
done(new Error('this shouldnt happen'));
})
.on('error', function (err) {
.on('error', (err) => {
socket.close();
err.message.should.eql("jwt signature is required");
err.message.should.eql('jwt signature is required');
done();
});
});

View File

@ -11,9 +11,9 @@ const enableDestroy = require('server-destroy');
let sio;
exports.start = function (options, callback) {
exports.start = (options, callback) => {
if(typeof options == 'function'){
if (typeof options == 'function') {
callback = options;
options = {};
}
@ -29,7 +29,7 @@ exports.start = function (options, callback) {
sio = socketIo.listen(server);
app.use(bodyParser.json());
app.post('/login', function (req, res) {
app.post('/login', (req, res) => {
const profile = {
first_name: 'John',
last_name: 'Doe',
@ -46,28 +46,28 @@ exports.start = function (options, callback) {
if (options.handshake) {
sio.use(socketio_jwt.authorize(options));
sio.sockets.on('echo', function (m) {
sio.sockets.on('echo', (m) => {
sio.sockets.emit('echo-response', m);
});
} else {
sio.sockets
.on('connection', socketio_jwt.authorize(options))
.on('authenticated', function (socket) {
socket.on('echo', function (m) {
.on('authenticated', (socket) => {
socket.on('echo', (m) => {
socket.emit('echo-response', m);
});
});
}
server.__sockets = [];
server.on('connection', function (c) {
server.on('connection', (c) => {
server.__sockets.push(c);
});
server.listen(9000, callback);
enableDestroy(server);
};
exports.stop = function (callback) {
exports.stop = (callback) => {
sio.close();
try {
server.destroy();

View File

@ -15,7 +15,7 @@ let sio;
*
* The /admin namespace is protected by JWTs while the global namespace is public.
*/
exports.start = function (callback) {
exports.start = (callback) => {
const options = {
secret: 'aaafoo super sercret',
@ -28,7 +28,7 @@ exports.start = function (callback) {
sio = socketIo.listen(server);
app.use(bodyParser.json());
app.post('/login', function (req, res) {
app.post('/login', (req, res) => {
const profile = {
first_name: 'John',
last_name: 'Doe',
@ -43,14 +43,14 @@ exports.start = function (callback) {
sio.on('connection', function (socket) {
sio.on('connection', (socket) => {
socket.emit('hi');
});
const admin_nsp = sio.of('/admin');
admin_nsp.on('connection', socketio_jwt.authorize(options))
.on('authenticated', function (socket) {
.on('authenticated', (socket) => {
socket.emit('hi admin');
});
@ -59,7 +59,7 @@ exports.start = function (callback) {
enableDestroy(server);
};
exports.stop = function (callback) {
exports.stop = (callback) => {
sio.close();
try {
server.destroy();

View File

@ -11,19 +11,19 @@ const enableDestroy = require('server-destroy');
let sio;
exports.start = function (options, callback) {
exports.start = (options, callback) => {
const SECRETS = {
123: 'aaafoo super sercret',
555: 'other'
};
if(typeof options == 'function'){
if (typeof options == 'function') {
callback = options;
options = {};
}
options = xtend({
secret: function(request, decodedToken, callback) {
secret: (request, decodedToken, callback) => {
callback(null, SECRETS[decodedToken.id]);
},
timeout: 1000,
@ -35,7 +35,7 @@ exports.start = function (options, callback) {
sio = socketIo.listen(server);
app.use(bodyParser.json());
app.post('/login', function (req, res) {
app.post('/login', (req, res) => {
const profile = {
first_name: 'John',
last_name: 'Doe',
@ -51,21 +51,21 @@ exports.start = function (options, callback) {
if (options.handshake) {
sio.use(socketio_jwt.authorize(options));
sio.sockets.on('echo', function (m) {
sio.sockets.on('echo', (m) => {
sio.sockets.emit('echo-response', m);
});
} else {
sio.sockets
.on('connection', socketio_jwt.authorize(options))
.on('authenticated', function (socket) {
socket.on('echo', function (m) {
.on('authenticated', (socket) => {
socket.on('echo', (m) => {
socket.emit('echo-response', m);
});
});
}
server.__sockets = [];
server.on('connection', function (c) {
server.on('connection', (c) => {
server.__sockets.push(c);
});
@ -73,7 +73,7 @@ exports.start = function (options, callback) {
enableDestroy(server);
};
exports.stop = function (callback) {
exports.stop = (callback) => {
sio.close();
try {
server.destroy();