2014-07-17 09:34:26 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2014-07-17 09:34:26 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
2014-07-24 15:47:45 +02:00
|
|
|
|
2014-07-17 09:34:26 +02:00
|
|
|
var should = require("should");
|
2015-01-28 23:41:13 +01:00
|
|
|
var sinon = require("sinon");
|
2018-04-23 13:37:26 +02:00
|
|
|
const stoppable = require('stoppable');
|
2015-01-28 23:41:13 +01:00
|
|
|
|
|
|
|
var when = require("when");
|
2014-07-24 15:47:45 +02:00
|
|
|
var http = require('http');
|
|
|
|
var express = require('express');
|
|
|
|
var app = express();
|
|
|
|
var WebSocket = require('ws');
|
|
|
|
|
2017-08-22 23:26:29 +02:00
|
|
|
var comms = require("../../../../red/api/editor/comms");
|
|
|
|
var Users = require("../../../../red/api/auth/users");
|
|
|
|
var Tokens = require("../../../../red/api/auth/tokens");
|
2015-01-28 23:41:13 +01:00
|
|
|
|
2014-07-24 15:47:45 +02:00
|
|
|
var address = '127.0.0.1';
|
|
|
|
var listenPort = 0; // use ephemeral port
|
|
|
|
|
2018-01-13 17:14:03 +01:00
|
|
|
|
2018-04-23 13:37:26 +02:00
|
|
|
describe("api/editor/comms", function() {
|
2018-01-13 17:14:03 +01:00
|
|
|
|
2014-08-01 22:55:05 +02:00
|
|
|
describe("with default keepalive", function() {
|
|
|
|
var server;
|
|
|
|
var url;
|
|
|
|
var port;
|
|
|
|
before(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
2018-04-23 13:37:26 +02:00
|
|
|
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
2015-11-12 10:03:03 +01:00
|
|
|
comms.init(server, {
|
|
|
|
settings:{},
|
|
|
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
|
|
|
events:{on:function(){},removeListener:function(){}}
|
|
|
|
});
|
2014-08-01 22:55:05 +02:00
|
|
|
server.listen(listenPort, address);
|
|
|
|
server.on('listening', function() {
|
|
|
|
port = server.address().port;
|
|
|
|
url = 'http://' + address + ':' + port + '/comms';
|
|
|
|
comms.start();
|
|
|
|
done();
|
|
|
|
});
|
2014-07-24 15:47:45 +02:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2018-04-23 13:37:26 +02:00
|
|
|
after(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
Users.default.restore();
|
2014-08-01 22:55:05 +02:00
|
|
|
comms.stop();
|
2018-04-23 13:37:26 +02:00
|
|
|
server.stop(done);
|
2014-07-24 15:47:45 +02:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-08-01 22:55:05 +02:00
|
|
|
it('accepts connection', function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
});
|
2014-07-24 15:47:45 +02:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-08-01 22:55:05 +02:00
|
|
|
it('publishes message after subscription', function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.send('{"subscribe":"topic1"}');
|
|
|
|
comms.publish('topic1', 'foo');
|
|
|
|
});
|
|
|
|
ws.on('message', function(msg) {
|
2018-01-13 17:14:03 +01:00
|
|
|
msg.should.equal('[{"topic":"topic1","data":"foo"}]');
|
2014-08-01 22:55:05 +02:00
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
});
|
2014-07-24 15:47:45 +02:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-08-01 22:55:05 +02:00
|
|
|
it('publishes retained message for subscription', function(done) {
|
|
|
|
comms.publish('topic2', 'bar', true);
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.send('{"subscribe":"topic2"}');
|
|
|
|
});
|
|
|
|
ws.on('message', function(msg) {
|
2018-01-13 17:14:03 +01:00
|
|
|
console.log(msg);
|
|
|
|
msg.should.equal('[{"topic":"topic2","data":"bar"}]');
|
2014-08-01 22:55:05 +02:00
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
});
|
2014-07-24 15:47:45 +02:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-08-01 22:55:05 +02:00
|
|
|
it('retained message is deleted by non-retained message', function(done) {
|
|
|
|
comms.publish('topic3', 'retained', true);
|
|
|
|
comms.publish('topic3', 'non-retained');
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.send('{"subscribe":"topic3"}');
|
|
|
|
comms.publish('topic3', 'new');
|
|
|
|
});
|
|
|
|
ws.on('message', function(msg) {
|
2018-01-13 17:14:03 +01:00
|
|
|
console.log(msg);
|
|
|
|
msg.should.equal('[{"topic":"topic3","data":"new"}]');
|
2014-08-01 22:55:05 +02:00
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
});
|
2014-07-24 15:47:45 +02:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-08-01 22:55:05 +02:00
|
|
|
it('malformed messages are ignored',function(done) {
|
2014-07-24 15:47:45 +02:00
|
|
|
var ws = new WebSocket(url);
|
|
|
|
ws.on('open', function() {
|
2014-08-01 22:55:05 +02:00
|
|
|
ws.send('not json');
|
|
|
|
ws.send('[]');
|
|
|
|
ws.send('{"subscribe":"topic3"}');
|
|
|
|
comms.publish('topic3', 'correct');
|
2014-07-24 15:47:45 +02:00
|
|
|
});
|
|
|
|
ws.on('message', function(msg) {
|
2018-01-13 17:14:03 +01:00
|
|
|
console.log(msg);
|
|
|
|
msg.should.equal('[{"topic":"topic3","data":"correct"}]');
|
2014-07-24 15:47:45 +02:00
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-08-01 22:55:05 +02:00
|
|
|
// The following test currently fails due to minimum viable
|
|
|
|
// implementation. More test should be written to test topic
|
|
|
|
// matching once this one is passing
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-11-12 10:03:03 +01:00
|
|
|
it.skip('receives message on correct topic', function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.send('{"subscribe":"topic4"}');
|
|
|
|
comms.publish('topic5', 'foo');
|
|
|
|
comms.publish('topic4', 'bar');
|
|
|
|
});
|
|
|
|
ws.on('message', function(msg) {
|
2018-01-13 17:14:03 +01:00
|
|
|
console.log(msg);
|
|
|
|
msg.should.equal('[{"topic":"topic4","data":"bar"}]');
|
2015-11-12 10:03:03 +01:00
|
|
|
ws.close();
|
|
|
|
done();
|
2014-08-01 22:55:05 +02:00
|
|
|
});
|
2015-11-12 10:03:03 +01:00
|
|
|
});
|
|
|
|
|
2018-04-23 13:37:26 +02:00
|
|
|
it('listens for node/status events');
|
2014-08-01 22:55:05 +02:00
|
|
|
});
|
2015-03-21 18:42:06 +01:00
|
|
|
describe("disabled editor", function() {
|
|
|
|
var server;
|
|
|
|
var url;
|
|
|
|
var port;
|
|
|
|
before(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
2018-04-23 13:37:26 +02:00
|
|
|
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
2015-11-12 10:03:03 +01:00
|
|
|
comms.init(server, {
|
|
|
|
settings:{disableEditor:true},
|
|
|
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
|
|
|
events:{on:function(){},removeListener:function(){}}
|
|
|
|
});
|
2015-03-21 18:42:06 +01:00
|
|
|
server.listen(listenPort, address);
|
|
|
|
server.on('listening', function() {
|
|
|
|
port = server.address().port;
|
|
|
|
url = 'http://' + address + ':' + port + '/comms';
|
|
|
|
comms.start();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2018-04-23 13:37:26 +02:00
|
|
|
after(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
Users.default.restore();
|
2015-03-21 18:42:06 +01:00
|
|
|
comms.stop();
|
2018-04-23 13:37:26 +02:00
|
|
|
server.stop(done);
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
it('rejects websocket connections',function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
ws.on('open', function() {
|
|
|
|
done(new Error("Socket connection unexpectedly accepted"));
|
|
|
|
ws.close();
|
|
|
|
});
|
|
|
|
ws.on('error', function() {
|
|
|
|
done();
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
describe("non-default httpAdminRoot set: /adminPath", function() {
|
|
|
|
var server;
|
|
|
|
var url;
|
|
|
|
var port;
|
|
|
|
before(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
2018-04-23 13:37:26 +02:00
|
|
|
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
2015-11-12 10:03:03 +01:00
|
|
|
comms.init(server, {
|
|
|
|
settings:{httpAdminRoot:"/adminPath"},
|
|
|
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
|
|
|
events:{on:function(){},removeListener:function(){}}
|
|
|
|
});
|
2015-03-21 18:42:06 +01:00
|
|
|
server.listen(listenPort, address);
|
|
|
|
server.on('listening', function() {
|
|
|
|
port = server.address().port;
|
|
|
|
url = 'http://' + address + ':' + port + '/adminPath/comms';
|
|
|
|
comms.start();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2018-04-23 13:37:26 +02:00
|
|
|
after(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
Users.default.restore();
|
2015-03-21 18:42:06 +01:00
|
|
|
comms.stop();
|
2018-04-23 13:37:26 +02:00
|
|
|
server.stop(done);
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
it('accepts connections',function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
ws.on('error', function() {
|
|
|
|
done(new Error("Socket connection failed"));
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
describe("non-default httpAdminRoot set: /adminPath/", function() {
|
|
|
|
var server;
|
|
|
|
var url;
|
|
|
|
var port;
|
|
|
|
before(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
2018-04-23 13:37:26 +02:00
|
|
|
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
2015-11-12 10:03:03 +01:00
|
|
|
comms.init(server,{
|
|
|
|
settings:{httpAdminRoot:"/adminPath"},
|
|
|
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
|
|
|
events:{on:function(){},removeListener:function(){}}
|
|
|
|
});
|
2015-03-21 18:42:06 +01:00
|
|
|
server.listen(listenPort, address);
|
|
|
|
server.on('listening', function() {
|
|
|
|
port = server.address().port;
|
|
|
|
url = 'http://' + address + ':' + port + '/adminPath/comms';
|
|
|
|
comms.start();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2018-04-23 13:37:26 +02:00
|
|
|
after(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
Users.default.restore();
|
2015-03-21 18:42:06 +01:00
|
|
|
comms.stop();
|
2018-04-23 13:37:26 +02:00
|
|
|
server.stop(done);
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
it('accepts connections',function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
ws.on('error', function() {
|
|
|
|
done(new Error("Socket connection failed"));
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
describe("non-default httpAdminRoot set: adminPath", function() {
|
|
|
|
var server;
|
|
|
|
var url;
|
|
|
|
var port;
|
|
|
|
before(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
2018-04-23 13:37:26 +02:00
|
|
|
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
2015-11-12 10:03:03 +01:00
|
|
|
comms.init(server, {
|
|
|
|
settings:{httpAdminRoot:"adminPath"},
|
|
|
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
|
|
|
events:{on:function(){},removeListener:function(){}}
|
|
|
|
});
|
2015-03-21 18:42:06 +01:00
|
|
|
server.listen(listenPort, address);
|
|
|
|
server.on('listening', function() {
|
|
|
|
port = server.address().port;
|
|
|
|
url = 'http://' + address + ':' + port + '/adminPath/comms';
|
|
|
|
comms.start();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2018-04-23 13:37:26 +02:00
|
|
|
after(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
Users.default.restore();
|
2015-03-21 18:42:06 +01:00
|
|
|
comms.stop();
|
2018-04-23 13:37:26 +02:00
|
|
|
server.stop(done);
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
it('accepts connections',function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
ws.on('error', function() {
|
|
|
|
done(new Error("Socket connection failed"));
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-07-30 14:58:11 +02:00
|
|
|
describe("keep alives", function() {
|
2014-08-01 22:55:05 +02:00
|
|
|
var server;
|
|
|
|
var url;
|
|
|
|
var port;
|
2014-07-30 14:58:11 +02:00
|
|
|
before(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
2018-04-23 13:37:26 +02:00
|
|
|
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
2015-11-12 10:03:03 +01:00
|
|
|
comms.init(server, {
|
|
|
|
settings:{webSocketKeepAliveTime: 100},
|
|
|
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
|
|
|
events:{on:function(){},removeListener:function(){}}
|
|
|
|
});
|
2014-07-30 14:58:11 +02:00
|
|
|
server.listen(listenPort, address);
|
|
|
|
server.on('listening', function() {
|
|
|
|
port = server.address().port;
|
|
|
|
url = 'http://' + address + ':' + port + '/comms';
|
|
|
|
comms.start();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-04-23 13:37:26 +02:00
|
|
|
after(function(done) {
|
2015-11-12 10:03:03 +01:00
|
|
|
Users.default.restore();
|
2014-08-01 22:55:05 +02:00
|
|
|
comms.stop();
|
2018-04-23 13:37:26 +02:00
|
|
|
server.stop(done);
|
2014-08-01 22:55:05 +02:00
|
|
|
});
|
2014-07-30 14:58:11 +02:00
|
|
|
it('are sent', function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
var count = 0;
|
|
|
|
ws.on('message', function(data) {
|
2018-01-13 17:14:03 +01:00
|
|
|
var msg = JSON.parse(data)[0];
|
2014-07-30 14:58:11 +02:00
|
|
|
msg.should.have.property('topic','hb');
|
2017-08-22 23:26:29 +02:00
|
|
|
msg.should.have.property('data').be.a.Number();
|
2014-07-30 14:58:11 +02:00
|
|
|
count++;
|
|
|
|
if (count == 3) {
|
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('are not sent if other messages are sent', function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
var count = 0;
|
|
|
|
var interval;
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.send('{"subscribe":"foo"}');
|
|
|
|
interval = setInterval(function() {
|
|
|
|
comms.publish('foo', 'bar');
|
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
ws.on('message', function(data) {
|
2018-01-13 17:14:03 +01:00
|
|
|
var msg = JSON.parse(data)[0];
|
2014-10-31 14:06:08 +01:00
|
|
|
// It is possible a heartbeat message may arrive - so ignore them
|
|
|
|
if (msg.topic != "hb") {
|
|
|
|
msg.should.have.property('topic', 'foo');
|
|
|
|
msg.should.have.property('data', 'bar');
|
|
|
|
count++;
|
|
|
|
if (count == 5) {
|
|
|
|
clearInterval(interval);
|
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
}
|
2014-07-30 14:58:11 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
describe('authentication required, no anonymous',function() {
|
|
|
|
var server;
|
|
|
|
var url;
|
|
|
|
var port;
|
|
|
|
var getDefaultUser;
|
|
|
|
var getUser;
|
|
|
|
var getToken;
|
|
|
|
before(function(done) {
|
|
|
|
getDefaultUser = sinon.stub(Users,"default",function() { return when.resolve(null);});
|
|
|
|
getUser = sinon.stub(Users,"get", function(username) {
|
|
|
|
if (username == "fred") {
|
|
|
|
return when.resolve({permissions:"read"});
|
|
|
|
} else {
|
|
|
|
return when.resolve(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
getToken = sinon.stub(Tokens,"get",function(token) {
|
|
|
|
if (token == "1234") {
|
2015-03-29 23:27:07 +02:00
|
|
|
return when.resolve({user:"fred",scope:["*"]});
|
2015-01-28 23:41:13 +01:00
|
|
|
} else if (token == "5678") {
|
2015-03-29 23:27:07 +02:00
|
|
|
return when.resolve({user:"barney",scope:["*"]});
|
2015-01-28 23:41:13 +01:00
|
|
|
} else {
|
|
|
|
return when.resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
|
|
|
|
2018-04-23 13:37:26 +02:00
|
|
|
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
2015-11-12 10:03:03 +01:00
|
|
|
comms.init(server,{
|
|
|
|
settings:{adminAuth:{}},
|
|
|
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
|
|
|
events:{on:function(){},removeListener:function(){}}
|
|
|
|
});
|
2015-01-28 23:41:13 +01:00
|
|
|
server.listen(listenPort, address);
|
|
|
|
server.on('listening', function() {
|
|
|
|
port = server.address().port;
|
|
|
|
url = 'http://' + address + ':' + port + '/comms';
|
|
|
|
comms.start();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-04-23 13:37:26 +02:00
|
|
|
after(function(done) {
|
2015-01-28 23:41:13 +01:00
|
|
|
getDefaultUser.restore();
|
|
|
|
getUser.restore();
|
|
|
|
getToken.restore();
|
|
|
|
comms.stop();
|
2018-04-23 13:37:26 +02:00
|
|
|
server.stop(done);
|
2015-01-28 23:41:13 +01:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
it('prevents connections that do not authenticate',function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
var count = 0;
|
|
|
|
var interval;
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.send('{"subscribe":"foo"}');
|
|
|
|
});
|
|
|
|
ws.on('close', function() {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
it('allows connections that do authenticate',function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
var received = 0;
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.send('{"auth":"1234"}');
|
|
|
|
});
|
|
|
|
ws.on('message', function(msg) {
|
|
|
|
received++;
|
|
|
|
if (received == 1) {
|
|
|
|
msg.should.equal('{"auth":"ok"}');
|
|
|
|
ws.send('{"subscribe":"foo"}');
|
|
|
|
comms.publish('foo', 'correct');
|
|
|
|
} else {
|
2018-01-13 17:14:03 +01:00
|
|
|
msg.should.equal('[{"topic":"foo","data":"correct"}]');
|
2015-01-28 23:41:13 +01:00
|
|
|
ws.close();
|
|
|
|
}
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
ws.on('close', function() {
|
2015-03-29 23:27:07 +02:00
|
|
|
try {
|
|
|
|
received.should.equal(2);
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
2015-01-28 23:41:13 +01:00
|
|
|
});
|
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
it('rejects connections for non-existant token',function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
var received = 0;
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.send('{"auth":"2345"}');
|
|
|
|
});
|
|
|
|
ws.on('close', function() {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('rejects connections for invalid token',function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
var received = 0;
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.send('{"auth":"5678"}');
|
|
|
|
});
|
|
|
|
ws.on('close', function() {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-30 14:58:11 +02:00
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
describe('authentication required, anonymous enabled',function() {
|
|
|
|
var server;
|
|
|
|
var url;
|
|
|
|
var port;
|
|
|
|
var getDefaultUser;
|
|
|
|
before(function(done) {
|
|
|
|
getDefaultUser = sinon.stub(Users,"default",function() { return when.resolve({permissions:"read"});});
|
2018-04-23 13:37:26 +02:00
|
|
|
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
2015-11-12 10:03:03 +01:00
|
|
|
comms.init(server, {
|
|
|
|
settings:{adminAuth:{}},
|
|
|
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
|
|
|
events:{on:function(){},removeListener:function(){}}
|
|
|
|
});
|
2015-01-28 23:41:13 +01:00
|
|
|
server.listen(listenPort, address);
|
|
|
|
server.on('listening', function() {
|
|
|
|
port = server.address().port;
|
|
|
|
url = 'http://' + address + ':' + port + '/comms';
|
|
|
|
comms.start();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-04-23 13:37:26 +02:00
|
|
|
after(function(done) {
|
2015-01-28 23:41:13 +01:00
|
|
|
getDefaultUser.restore();
|
|
|
|
comms.stop();
|
2018-04-23 13:37:26 +02:00
|
|
|
server.stop(done);
|
2015-01-28 23:41:13 +01:00
|
|
|
});
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2015-01-28 23:41:13 +01:00
|
|
|
it('allows anonymous connections that do not authenticate',function(done) {
|
|
|
|
var ws = new WebSocket(url);
|
|
|
|
var count = 0;
|
|
|
|
var interval;
|
|
|
|
ws.on('open', function() {
|
|
|
|
ws.send('{"subscribe":"foo"}');
|
|
|
|
setTimeout(function() {
|
|
|
|
comms.publish('foo', 'correct');
|
|
|
|
},200);
|
|
|
|
});
|
|
|
|
ws.on('message', function(msg) {
|
2018-01-13 17:14:03 +01:00
|
|
|
msg.should.equal('[{"topic":"foo","data":"correct"}]');
|
2015-01-28 23:41:13 +01:00
|
|
|
count++;
|
|
|
|
ws.close();
|
|
|
|
});
|
|
|
|
ws.on('close', function() {
|
2015-03-29 23:27:07 +02:00
|
|
|
try {
|
|
|
|
count.should.equal(1);
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
2015-01-28 23:41:13 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-11-12 08:56:23 +01:00
|
|
|
|
2014-07-17 09:34:26 +02:00
|
|
|
});
|