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