Working gateway
Change-Id: I8ca690fe9d1b7f8e20b438df1ddd48d6b2f99326
diff --git a/spec/.eslintrc b/spec/.eslintrc
new file mode 100644
index 0000000..30a6ba1
--- /dev/null
+++ b/spec/.eslintrc
@@ -0,0 +1,10 @@
+{
+ "globals" :{
+ "describe": true,
+ "it": true,
+ "before": true,
+ "beforeEach": true,
+ "after": true,
+ "afterEach": true
+ }
+}
\ No newline at end of file
diff --git a/spec/core_proxy.spec.js b/spec/core_proxy.spec.js
new file mode 100644
index 0000000..c5bb9ad
--- /dev/null
+++ b/spec/core_proxy.spec.js
@@ -0,0 +1,183 @@
+(function () {
+ 'use strict';
+
+ const chai = require('chai');
+ const expect = chai.expect;
+ const sinonChai = require('sinon-chai');
+ const supertest = require('supertest');
+ const mockery = require('mockery');
+ chai.use(sinonChai);
+
+ const request = require('superagent');
+ const stub = require('./spec_helper.js');
+
+ const configMock = {
+ xos: {
+ host: 'http://test-xos-url',
+ port: '80'
+ }
+ };
+
+ let app;
+
+ describe('The core proxy routes', () => {
+
+ // stub for GET method
+ stub.makeStub('getSuccess', request, 'get', cb => {
+ cb(null, {
+ status: 200,
+ body: {msg: 'successfully proxied'}
+ });
+ });
+
+ // stub for POST method
+ stub.makeStub('postSuccess', request, 'post', cb => {
+ cb(null, {
+ status: 201,
+ body: {msg: 'successfully proxied'}
+ });
+ });
+
+ // stub for PUT method
+ stub.makeStub('putSuccess', request, 'put', cb => {
+ cb(null, {
+ status: 200,
+ body: {msg: 'successfully proxied'}
+ });
+ });
+
+ // stub for DELETE method
+ stub.makeStub('deleteSuccess', request, 'delete', cb => {
+ cb(null, {
+ status: 204
+ });
+ });
+
+ // mocking the config.rest module
+ before(() => {
+ mockery.enable({
+ warnOnReplace: true,
+ warnOnUnregistered: false
+ });
+ mockery.registerMock('../config/config.js', configMock);
+
+ app = require('../src/server.js').app;
+ });
+
+ after(() => {
+ mockery.deregisterMock('../config/config.js');
+ mockery.disable();
+ });
+
+ it('should read XOS address from config.rest module', (done) => {
+ const myStub = stub.getStub('getSuccess');
+
+ supertest(app)
+ .get('/api/core/')
+ .end((err) => {
+ if (err) return done(err);
+ expect(myStub.get.called).to.be.true;
+ expect(myStub.get).to.have.been.calledWith('http://test-xos-url:80/api/core/');
+ done();
+ });
+ });
+
+ it('should pass token and cookies along with the request', (done) => {
+ const myStub = stub.getStub('getSuccess');
+
+ supertest(app)
+ .get('/api/core/')
+ .set('Accept', 'application/json')
+ .set('x-csrftoken', 'testToken')
+ .set('cookie', 'testCookie')
+ .end(function(err) {
+ if (err) return done(err);
+ expect(myStub.set.getCall(0)).to.have.been.calledWith('x-csrftoken', 'testToken');
+ expect(myStub.set.getCall(1)).to.have.been.calledWith('cookie', 'testCookie');
+ done();
+ });
+ });
+
+ it('should pass query paramenters along with the request', (done) => {
+ const myStub = stub.getStub('getSuccess');
+
+ supertest(app)
+ .get('/api/core/instances/?no_hyperlink=1&node=1')
+ .end((err) => {
+ if (err) return done(err);
+ expect(myStub.get.called).to.be.true;
+ expect(myStub.get).to.have.been.calledWith('http://test-xos-url:80/api/core/instances/?no_hyperlink=1&node=1');
+ done();
+ });
+ });
+
+ it('should proxy GET request to XOS', (done) => {
+ supertest(app)
+ .get('/api/core/')
+ .set('Accept', 'application/json')
+ .set('x-csrftoken', 'testToken')
+ .set('cookie', 'testCookie')
+ .end(function(err, res) {
+ if (err) return done(err);
+ expect(res.status).to.equal(200);
+ expect(res.body).to.deep.equal({msg: 'successfully proxied'});
+ done();
+ });
+ });
+
+ it('should proxy POST request to XOS', (done) => {
+
+ const myStub = stub.getStub('postSuccess');
+
+ supertest(app)
+ .post('/api/core/')
+ .send({foo: 'bar'})
+ .set('Accept', 'application/json')
+ .set('x-csrftoken', 'testToken')
+ .set('cookie', 'testCookie')
+ .end(function(err, res) {
+ if (err) return done(err);
+ expect(myStub.send.getCall(0)).to.have.been.calledWith({foo: 'bar'});
+ expect(res.status).to.equal(201);
+ expect(res.body).to.deep.equal({msg: 'successfully proxied'});
+ done();
+ });
+ });
+
+ it('should proxy PUT request to XOS', (done) => {
+
+ const myStub = stub.getStub('putSuccess');
+
+ supertest(app)
+ .put('/api/core/')
+ .send({foo: 'bar'})
+ .set('Accept', 'application/json')
+ .set('x-csrftoken', 'testToken')
+ .set('cookie', 'testCookie')
+ .end(function(err, res) {
+ if (err) return done(err);
+ expect(myStub.send.getCall(0)).to.have.been.calledWith({foo: 'bar'});
+ expect(res.status).to.equal(200);
+ expect(res.body).to.deep.equal({msg: 'successfully proxied'});
+ done();
+ });
+ });
+
+ it('should proxy DELETE request to XOS', (done) => {
+
+ const myStub = stub.getStub('deleteSuccess');
+
+ supertest(app)
+ .delete('/api/core/')
+ .set('Accept', 'application/json')
+ .set('x-csrftoken', 'testToken')
+ .set('cookie', 'testCookie')
+ .end(function(err, res) {
+ if (err) return done(err);
+ expect(myStub.send).not.to.have.been.called;
+ expect(res.status).to.equal(204);
+ done();
+ });
+ });
+ });
+})();
\ No newline at end of file
diff --git a/spec/index.html b/spec/index.html
new file mode 100644
index 0000000..4f97749
--- /dev/null
+++ b/spec/index.html
@@ -0,0 +1,60 @@
+<!doctype html>
+<html>
+ <head>
+ <title>Socket.IO chat</title>
+ <script src="http://10.1.8.44:3000/socket.io/socket.io.js"></script>
+ <script
+ src="https://code.jquery.com/jquery-1.12.4.min.js"
+ integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
+ crossorigin="anonymous"></script>
+ <script src="https://rawgit.com/notifyjs/notifyjs/master/dist/notify.js"></script>
+
+ <style>
+ body {
+ max-width: 100%;
+ }
+ </style>
+<script>
+ /* global io $ alert*/
+ function enableSocket(auth) {
+
+ auth.user = JSON.parse(auth.user);
+
+ var socket = io.connect('http://10.1.8.44:3000', {
+ query: 'name=' + auth.user.username + '&token=' + auth.xoscsrftoken + '&sessionId=' + auth.xossessionid + '&id=' + auth.user.id
+ });
+ socket.on('event', function(data) {
+ var p = $('body').append('<p></p>');
+ p.append('<b>' + data.model + '</b><br/>');
+ p.append('<i>' + JSON.stringify(data.msg.changed_fields) + '</i>');
+ p.append('<pre>' + JSON.stringify(data.msg.object) + '</pre>');
+ console.log(data.object);
+ $.notify(data.model + ': ' + JSON.stringify(data.msg.object.name) + 'updated', 'success');
+ });
+ }
+
+ function login() {
+ var username = $('#username').val();
+ var password = $('#password').val();
+
+ $.get('http://10.1.8.44:3000/api/utility/login?username='+username+'&password='+password,
+ function(res) {
+ enableSocket(res);
+ $('#login').hide();
+ }
+ )
+ .fail(function(e) {
+ alert( 'error' );
+ console.log(e);
+ });
+ }
+</script>
+ </head>
+ <body>
+ <form onsubmit="login()" id="login">
+ <input type="text" id="username">
+ <input type="password" id="password">
+ <input type="button" value="Login" onclick="login()">
+ </form>
+ </body>
+</html>
\ No newline at end of file
diff --git a/spec/redis.spec.js b/spec/redis.spec.js
new file mode 100644
index 0000000..e607e5c
--- /dev/null
+++ b/spec/redis.spec.js
@@ -0,0 +1,86 @@
+(function () {
+ 'use strict';
+
+ const chai = require('chai');
+ const expect = chai.expect;
+ const sinon = require('sinon');
+ const sinonChai = require('sinon-chai');
+ const mockery = require('mockery');
+ chai.use(sinonChai);
+ const fakeredis = require('fakeredis');
+
+ const client = fakeredis.createClient('test-client');
+ const publisher = fakeredis.createClient('test-client');
+
+ const socketSpy = sinon.spy();
+ const mockSocket = {
+ get: () => {
+ return {
+ emit: socketSpy
+ }
+ }
+ };
+ const channelName = 'Site';
+
+ describe('The event system', () => {
+
+ before((done) => {
+
+ // Enable mockery to mock objects
+ mockery.enable({
+ warnOnReplace: false,
+ warnOnUnregistered: false
+ });
+
+ // Stub the createClient method to *always* return the client created above
+ sinon.stub(fakeredis, 'createClient', () => client);
+
+ // Override the redis module with our fakeredis instance
+ mockery.registerMock('redis', fakeredis);
+
+ // mock the socketIo client to have a spy
+ mockery.registerMock('./websocket.js', mockSocket);
+
+ require('../src/controllers/redis.js');
+ setTimeout(() => {
+ done();
+ }, 1000);
+ });
+
+ after(() => {
+ mockery.disable();
+ fakeredis.createClient.restore();
+ });
+
+ // run after each test
+ beforeEach(() => {
+ client.unsubscribe(channelName);
+ client.subscribe(channelName);
+ publisher.flushdb();
+ });
+
+ it('should send a websocket event when it receive a redis event that is not JSON', (done) => {
+ publisher.publish(channelName, 'I am sending a message.');
+ setTimeout(() => {
+ expect(socketSpy).to.have.been.called;
+ expect(socketSpy).to.have.been.calledWith('event', {
+ model: channelName,
+ msg: 'I am sending a message.'
+ });
+ done();
+ }, 500)
+ });
+
+ it('should send a websocket event when it receive a redis event that is JSON', (done) => {
+ publisher.publish(channelName, JSON.stringify({msg: 'Json Message'}));
+ setTimeout(() => {
+ expect(socketSpy).to.have.been.called;
+ expect(socketSpy).to.have.been.calledWith('event', {
+ model: channelName,
+ msg: {msg: 'Json Message'}
+ });
+ done();
+ }, 1000)
+ });
+ });
+})();
\ No newline at end of file
diff --git a/spec/spec_helper.js b/spec/spec_helper.js
new file mode 100644
index 0000000..abedb29
--- /dev/null
+++ b/spec/spec_helper.js
@@ -0,0 +1,42 @@
+(function () {
+ 'use strict';
+
+ const sinon = require('sinon');
+
+ let stubCache = {};
+
+ exports.makeStub = (name, target, method, cb) => {
+
+ let methodStub, prototypeStub;
+
+ function SuperAgentStub(end) {
+ this.end = end;
+ this.set = sinon.stub().returns(this);
+ this.send = sinon.stub().returns(this);
+ return this;
+ }
+
+ beforeEach(() => {
+ methodStub = sinon.stub(target, method);
+
+ prototypeStub = new SuperAgentStub(cb);
+
+ methodStub.returns(prototypeStub);
+
+ // cache stub (for use in tests)
+ stubCache[name] = {
+ set: prototypeStub.set,
+ send: prototypeStub.send
+ };
+ stubCache[name][method] = methodStub
+ });
+
+ afterEach(() => {
+ target[method].restore();
+ });
+
+ };
+
+ exports.getStub = name => stubCache[name];
+
+})();
\ No newline at end of file
diff --git a/spec/websocket.spec.js b/spec/websocket.spec.js
new file mode 100644
index 0000000..2822223
--- /dev/null
+++ b/spec/websocket.spec.js
@@ -0,0 +1,73 @@
+(function () {
+ 'use strict';
+
+ const chai = require('chai');
+ const expect = chai.expect;
+ const sinonChai = require('sinon-chai');
+ chai.use(sinonChai);
+ const io = require('socket.io-client');
+ const server = require('../src/server.js');
+
+ describe('basic socket.io example', function() {
+
+ var client;
+
+ beforeEach(function(done) {
+ // Start our server
+ server.start();
+
+ // connect a client to the server
+ client = io.connect('http://localhost:3000', {
+ query: 'name=test@xos.org&token=testToken&sessionId=testSession&id=1'
+ });
+
+ // when is connected start testing
+ client.on('connect', () => {
+ done();
+ });
+ });
+
+ afterEach((done) => {
+ // disconnect the client
+ if(client.connected) {
+ client.disconnect();
+ }
+ done();
+ });
+
+ it('should store user details for a new connection', () => {
+ const clients = require('../src/controllers/clients.js');
+ const user = clients.clients[0];
+ expect(user.name).to.equal('test@xos.org')
+ });
+
+ it('should not store the same user twice', (done) => {
+
+ // connect a client to the server
+ const client2 = io.connect('http://localhost:3000', {
+ query: 'name=test@xos.org&token=testToken&sessionId=testSession&id=1'
+ });
+
+ // when is connected start testing
+ client2.on('connect', () => {
+ setTimeout(() => {
+ const clients = require('../src/controllers/clients.js');
+ expect(clients.clients.length).to.equal(1)
+ done();
+ }, 100);
+ });
+
+ });
+
+ it('should remove a user on disconnect', (done) => {
+ client.disconnect();
+ // we need to wait for the event to be dispatched
+ setTimeout(() => {
+ const clients = require('../src/controllers/clients.js');
+ expect(clients.clients.length).to.equal(0)
+ done();
+ }, 100);
+ });
+
+ });
+})();
\ No newline at end of file