Skip to content

Commit

Permalink
#470 Finish #463
Browse files Browse the repository at this point in the history
Conflicts:
	lib/webserver/index.js
  • Loading branch information
itayw committed May 19, 2014
2 parents 6d7864f + 0f2095d commit ba13631
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 42 deletions.
6 changes: 3 additions & 3 deletions config/default.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.0.11
version: 0.0.13
interfaces:
webserver:
enabled: true
Expand All @@ -7,7 +7,7 @@ interfaces:
port: 8080
securePort: 8081
secure: true
secureOnly: false
secureOnly: true
keyFile: ./config/certs/localhost.key
certFile: ./config/certs/localhost.csr
websocket:
Expand All @@ -19,7 +19,7 @@ interfaces:
headers:
X-Powered-By: joola.io
repl:
enabled: true
enabled: false
port: 1337
store:
config:
Expand Down
7 changes: 6 additions & 1 deletion lib/dispatch/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ exports.connectedClients = {
};

var result = [];
var clients = joola.io.sockets.clients();
var clients = [];

if (joola.io && joola.io.sockets)
clients.concat(joola.io.sockets.clients());
if (joola.sio && joola.sio.sockets)
clients.concat(joola.sio.sockets.clients());
clients.forEach(function (c) {
result.push({
id: c.id,
Expand Down
6 changes: 5 additions & 1 deletion lib/dispatch/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ exports.list = {
return callback(err);

/* istanbul ignore if */
if (typeof value === 'undefined')
if (typeof value === 'undefined' || value === null)
value = {};

Object.keys(value).forEach(function (key) {
Expand Down Expand Up @@ -489,6 +489,10 @@ exports.verifyAPIToken = {
run: function (context, APIToken, callback) {
callback = callback || function () {
};

console.log(context, APIToken);


var _user;
joola.config.get('workspaces', function (err, orgs) {
Object.keys(orgs).forEach(function (org) {
Expand Down
37 changes: 28 additions & 9 deletions lib/webserver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ webserver.start = function (options, callback) {
joola.io.sockets.on('connection', handleSocketIOConnection);

return callback(null, self.http);
}).on('connection', function (socket) {
sockets.push(socket);
socket.on('close', function () {
sockets.splice(sockets.indexOf(socket), 1);
});
}).on('error', function (err) {
if (err.code == 'EADDRINUSE' && (joola.config.get('webserver') || options.webserver)) {

Expand Down Expand Up @@ -273,7 +278,6 @@ webserver.start = function (options, callback) {
joola.logger.debug('joola.io HTTPS server listening on port ' + self.options.securePort);
joola.state.set('webserver-https', 'working', 'webserver-https is up.');


var secure_socketioWildcard = require('socket.io-wildcard');
joola.sio = secure_socketioWildcard(require('socket.io')).listen(self.https, {secure: true, log: false });

Expand All @@ -290,8 +294,12 @@ webserver.start = function (options, callback) {
]);

joola.sio.sockets.on('connection', handleSocketIOConnection);

return callback(null, self.https);
}).on('connection', function (socket) {
sockets.push(socket);
socket.on('close', function () {
sockets.splice(sockets.indexOf(socket), 1);
});
}).on('error', function (err) {
if (err.code == 'EADDRINUSE' && (joola.config.get('webserver') || options.webserver)) {
//fireof an immediate callback with the error
Expand Down Expand Up @@ -333,27 +341,38 @@ webserver.start = function (options, callback) {
});
};

var sockets = [];
webserver.stop = function (callback) {
var self = webserver;

joola.logger.warn('Stopping HTTP/HTTPs web servers.');

for (var i = 0; i < sockets.length; i++) {
sockets[i].destroy();
}

if (self.http) {
self.http.close();
}
if (self.https)
if (self.https) {
self.https.close();
}

if (typeof callback === 'function')
callback(null);
};

webserver.verify = function (callback) {
if (joola.state.controls['webserver-http'].state != 'working' ||
(joola.state.controls['webserver-https'] && joola.state.controls['webserver-https'].state != 'working')) {
return callback(new Error('Failed to validate servers'));
var self = webserver;

if (!self.options.secureOnly) {
if (joola.state.controls['webserver-http'].state != 'working')
return callback(new Error('Failed to validate servers'));
}
else {
//webserver is live (if it should be)
return callback(null);
if (self.options.secure) {
if (joola.state.controls['webserver-https'].state != 'working')
return callback(new Error('Failed to validate servers'));
}

return callback(null);
};
2 changes: 2 additions & 0 deletions lib/webserver/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ exports.setup = function (app) {

var middleware = require('../middleware/index').middleware;

app.get('/users/verifyAPIToken', middleware('/users/verifyAPIToken'), exports.router);

app.get('/system/version', middleware('/system/version'), exports.router);
app.get('/system/whoami', middleware('/system/whoami'), exports.router);
app.get('/system/nodeUID', middleware('/system/nodeUID'), exports.router);
Expand Down
34 changes: 17 additions & 17 deletions test/unit/1_runtime/routes.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var http = require('http');
var https = require('https');

describe("routes", function () {
before(function (done) {
Expand All @@ -23,7 +23,7 @@ describe("routes", function () {
it("should return a 401 route [GET]", function (done) {
var options = {
host: 'localhost',
port: '8080',
port: '8081',
path: '/api/system/nodeDetails'
};

Expand All @@ -44,13 +44,13 @@ describe("routes", function () {
});
};

http.request(options, callback).end();
https.request(options, callback).end();
});

it("should get the SDK", function (done) {
var options = {
host: 'localhost',
port: '8080',
port: '8081',
path: '/joola.io.js'
};

Expand All @@ -71,13 +71,13 @@ describe("routes", function () {
});
};

http.request(options, callback).end();
https.request(options, callback).end();
});

it("should get the SDK [w/ token]", function (done) {
var options = {
host: 'localhost',
port: '8080',
port: '8081',
path: '/joola.io.js?token=12345'
};

Expand All @@ -98,13 +98,13 @@ describe("routes", function () {
});
};

http.request(options, callback).end();
https.request(options, callback).end();
});

it("should get the minified SDK", function (done) {
var options = {
host: 'localhost',
port: '8080',
port: '8081',
path: '/joola.io.min.js'
};

Expand All @@ -125,13 +125,13 @@ describe("routes", function () {
});
};

http.request(options, callback).end();
https.request(options, callback).end();
});

it("should get the minified SDK [w/ token]", function (done) {
var options = {
host: 'localhost',
port: '8080',
port: '8081',
path: '/joola.io.min.js?token=12345'
};

Expand All @@ -152,13 +152,13 @@ describe("routes", function () {
});
};

http.request(options, callback).end();
https.request(options, callback).end();
});

it("should get the CSS", function (done) {
var options = {
host: 'localhost',
port: '8080',
port: '8081',
path: '/joola.io.css'
};

Expand All @@ -179,13 +179,13 @@ describe("routes", function () {
});
};

http.request(options, callback).end();
https.request(options, callback).end();
});

it("should get the IP", function (done) {
var options = {
host: 'localhost',
port: '8080',
port: '8081',
path: '/ip'
};

Expand All @@ -206,13 +206,13 @@ describe("routes", function () {
});
};

http.request(options, callback).end();
https.request(options, callback).end();
});

xit("should create a test error", function (done) {
var options = {
host: 'localhost',
port: '8080',
port: '8081',
path: '/api/test/createtesterror'
};

Expand All @@ -233,6 +233,6 @@ describe("routes", function () {
});
};

http.request(options, callback).end();
https.request(options, callback).end();
});
});
10 changes: 5 additions & 5 deletions test/unit/1_runtime/webserver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ describe("webserver", function () {
});
});

it("should have HTTP port open", function (done) {
request.get('http://' + joola.config.interfaces.webserver.host + ':' + joola.config.interfaces.webserver.port + '', function (err, response, body) {
xit("should have HTTP port open", function (done) {
request.get('https://' + joola.config.interfaces.webserver.host + ':' + joola.config.interfaces.webserver.securePort+ '', function (err, response, body) {
if (err)
return done(err);

Expand All @@ -70,7 +70,7 @@ describe("webserver", function () {
xit("should have WebSocket", function (done) {
var called = false;
var io = require('socket.io-client');
var socket = io.connect('http://' + joola.config.interfaces.webserver.host + ':' + joola.config.interfaces.webserver.port);
var socket = io.connect('https://' + joola.config.interfaces.webserver.host + ':' + joola.config.interfaces.webserver.securePort);
socket.on('connect', function () {
socket.disconnect();
if (!called) {
Expand Down Expand Up @@ -116,7 +116,7 @@ describe("webserver", function () {
});

it("should serve api endpoints", function (done) {
request.get('http://' + joola.config.interfaces.webserver.host + ':' + joola.config.interfaces.webserver.port + '/meta', function (err, response, body) {
request.get('https://' + joola.config.interfaces.webserver.host + ':' + joola.config.interfaces.webserver.securePort + '/meta', function (err, response, body) {
if (err)
return done(err);

Expand All @@ -126,7 +126,7 @@ describe("webserver", function () {
});

xit("should serve api endpoints [system version]", function (done) {
request.get('http://' + joola.config.interfaces.webserver.host + ':' + joola.config.interfaces.webserver.port + '/system/version?APIToken=apitoken-demo', function (err, response, body) {
request.get('https://' + joola.config.interfaces.webserver.host + ':' + joola.config.interfaces.webserver.securePort + '/system/version?APIToken=apitoken-demo', function (err, response, body) {
if (err)
return done(err);

Expand Down
2 changes: 1 addition & 1 deletion test/unit/3_auth/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("auth", function () {
});

it("should return static content with no login issues", function (done) {
browser.visit('http://' + joola.config.interfaces.webserver.host + ':' + joola.config.interfaces.webserver.port + '/ico/favicon.ico', function () {
browser.visit('https://' + joola.config.interfaces.webserver.host + ':' + joola.config.interfaces.webserver.securePort + '/ico/favicon.ico', function () {
expect(browser.success).to.equal(true);
return done();
});
Expand Down
7 changes: 3 additions & 4 deletions test/unit/4_dispatch/workspaces.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@

var async = require('async');


describe("workspaces", function () {
before(function (done) {
this.context = {user: _token.user};
this.uid = joola.common.uuid();

joolaio.set('APIToken', 'apitoken-demo', done);
done();
});

after(function (done) {
var self = this;
joola.dispatch.workspaces.delete(this.context, 'test-workspace-' + this.uid, function () {
joola.dispatch.workspaces.delete(self.context, 'test-workspace1-' + this.uid, function () {
joolaio.set('APIToken', 'apitoken-test', done);
done();
});
});
});
Expand Down Expand Up @@ -133,7 +132,7 @@ describe("workspaces", function () {
joola.dispatch.workspaces.list(self.context, function (err, workspaces) {
if (err)
return done(err);

var exist = _.filter(workspaces, function (item) {
return item.key == 'test-workspace-' + self.uid;
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/starthere.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ before(function (done) {
global.joola_proxy = joola;
global.uid = joola.common.uuid();
global.workspace = '_test';
joolaio.init({host: 'http://127.0.0.1:8080', APIToken: 'apitoken-test', debug: {enabled: true}}, function (err) {
joolaio.init({host: 'https://127.0.0.1:8081', APIToken: 'apitoken-test', debug: {enabled: true}}, function (err) {
if (err)
return done(err);
});
Expand Down

0 comments on commit ba13631

Please sign in to comment.