diff --git a/.travis.yml b/.travis.yml index 7a655c6..32f4c6e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,6 @@ services: redis-server before_install: - "sudo apt-get install python-virtualenv" - - npm i grunt-cli -g script: make travis diff --git a/loop/constants.js b/loop/constants.js new file mode 100644 index 0000000..39fd749 --- /dev/null +++ b/loop/constants.js @@ -0,0 +1,38 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +module.exports = { + CALL_STATES: { + INIT: "init", + HALF_INITIATED: "half-initiated", + ALERTING: "alerting", + CONNECTING: "connecting", + HALF_CONNECTED: "half-connected", + CONNECTED: "connected", + TERMINATED: "terminated" + }, + MESSAGE_EVENTS: { + ACCEPT: "accept", + MEDIA_UP: "media-up", + TERMINATE: "terminate" + }, + MESSAGE_TYPES: { + HELLO: "hello", + ACTION: "action", + PROGRESS: "progress", + ECHO: "echo", + ERROR: "error" + }, + MESSAGE_REASONS: { + BUSY: "busy", + CANCEL: "cancel", + TIMEOUT: "timeout" + }, + ERROR_REASONS: { + BAD_AUTHENTICATION: "bad authentication", + BAD_CALLID: "bad callId" + } +}; diff --git a/loop/storage/redis.js b/loop/storage/redis.js index cdb8d2f..7aa3ac2 100644 --- a/loop/storage/redis.js +++ b/loop/storage/redis.js @@ -4,7 +4,8 @@ "use strict"; var redis = require("redis"); -var async = require('async'); +var async = require("async"); +var constants = require("../constants"); function RedisStorage(options, settings) { this._settings = settings; @@ -412,7 +413,7 @@ RedisStorage.prototype = { var key = 'callstate.' + callId; - if(state === "terminated") { + if(state === constants.CALL_STATES.TERMINATED) { self._client.del(key, callback); return; } @@ -450,22 +451,22 @@ RedisStorage.prototype = { } switch (score) { case 1: - callback(null, "init"); + callback(null, constants.CALL_STATES.INIT); break; case 2: - callback(null, "half-initiated"); + callback(null, constants.CALL_STATES.HALF_INITIATED); break; case 3: - callback(null, "alerting"); + callback(null, constants.CALL_STATES.ALERTING); break; case 4: - callback(null, "connecting"); + callback(null, constants.CALL_STATES.CONNECTING); break; case 5: - callback(null, "half-connected"); + callback(null, constants.CALL_STATES.HALF_CONNECTED); break; case 6: - callback(null, "connected"); + callback(null, constants.CALL_STATES.CONNECTED); break; default: // Ensure a call exists if nothing is stored on this key. @@ -475,7 +476,7 @@ RedisStorage.prototype = { return; } if (result !== null) { - callback(null, "terminated"); + callback(null, constants.CALL_STATES.TERMINATED); return; } callback(null, null); diff --git a/loop/websockets.js b/loop/websockets.js index 2ce9509..c293c44 100644 --- a/loop/websockets.js +++ b/loop/websockets.js @@ -8,6 +8,7 @@ var PubSub = require('./pubsub'); var conf = require('./config').conf; var hekaLogger = require('./logger').hekaLogger; var isoDateString = require("./utils").isoDateString; +var constants = require("./constants"); /** * Sends an error to the given callback, if there is any. @@ -99,7 +100,7 @@ MessageHandler.prototype = { if (serverError(err, callback)) return; if (call === null) { - callback(new Error("bad callId")); + callback(new Error(constants.ERROR_REASONS.BAD_CALLID)); return; } @@ -108,7 +109,7 @@ MessageHandler.prototype = { } else if (call.wsCallerToken === tokenId) { session.type = "caller"; } else { - callback(new Error("bad authentication")); + callback(new Error(constants.ERROR_REASONS.BAD_AUTHENTICATION)); return; } @@ -124,15 +125,15 @@ MessageHandler.prototype = { var terminate; if (channel === session.callId) { - if (receivedState === "terminated" || - receivedState === "connected") { + if (receivedState === constants.CALL_STATES.TERMINATED || + receivedState === constants.CALL_STATES.CONNECTED) { terminate = "closeConnection"; } if (session.receivedState !== receivedState) { session.receivedState = receivedState; var message = { - messageType: "progress", + messageType: constants.MESSAGE_TYPES.PROGRESS, state: receivedState }; if (reason !== undefined) { @@ -154,9 +155,12 @@ MessageHandler.prototype = { setTimeout(function() { self.storage.getCallState(session.callId, function(err, state) { if (serverError(err, callback)) return; - if (state === 'terminated' || state === 'half-initiated') { - self.broadcastState(session.callId, "terminated:timeout"); - self.storage.setCallState(session.callId, 'terminated'); + if (state === constants.CALL_STATES.TERMINATED || + state === constants.CALL_STATES.HALF_INITIATED) { + self.broadcastState(session.callId, + constants.CALL_STATES.TERMINATED + ":" + + constants.MESSAGE_REASONS.TIMEOUT); + self.storage.setCallState(session.callId, constants.CALL_STATES.TERMINATED); } }); }, timeoutTTL * 1000); @@ -167,28 +171,40 @@ MessageHandler.prototype = { // Don't publish the half-initiated state, it's only for internal // use. var helloState = currentState; - if (currentState === "half-initiated") { - helloState = "init"; + if (currentState === constants.CALL_STATES.HALF_INITIATED) { + helloState = constants.CALL_STATES.INIT; } callback(null, { - messageType: "hello", + messageType: constants.MESSAGE_TYPES.HELLO, state: helloState }); // After the hello phase and as soon the callee is connected, // the call changes to the "alerting" state. - if (currentState === "init" || currentState === "half-initiated") { - self.broadcastState(session.callId, "init." + session.type, - timeoutTTL); + if (currentState === constants.CALL_STATES.INIT || + currentState === constants.CALL_STATES.HALF_INITIATED) { + self.broadcastState( + session.callId, + constants.CALL_STATES.INIT + "." + session.type, + timeoutTTL + ); if (session.type === "callee") { // We are now in "alerting" mode. setTimeout(function() { self.storage.getCallState(session.callId, function(err, state) { if (serverError(err, callback)) return; - if (state === "alerting" || state === "terminated") { - self.broadcastState(session.callId, "terminated:timeout"); - self.storage.setCallState(session.callId, 'terminated'); + if (state === constants.CALL_STATES.ALERTING || + state === constants.CALL_STATES.TERMINATED) { + self.broadcastState( + session.callId, + constants.CALL_STATES.TERMINATED + ":" + + constants.MESSAGE_REASONS.TIMEOUT + ); + self.storage.setCallState( + session.callId, + constants.CALL_STATES.TERMINATED + ); } }); }, self.conf.ringingDuration * 1000); @@ -214,7 +230,11 @@ MessageHandler.prototype = { return; } - var validEvents = ["accept", "media-up", "terminate"]; + var validEvents = [ + constants.MESSAGE_EVENTS.ACCEPT, + constants.MESSAGE_EVENTS.MEDIA_UP, + constants.MESSAGE_EVENTS.TERMINATE + ]; var event = message.event; var self = this; @@ -227,7 +247,7 @@ MessageHandler.prototype = { } // If terminate, close the call - if (event === "terminate") { + if (event === constants.MESSAGE_EVENTS.TERMINATE) { var state = "terminated"; // Check the reason is valid. @@ -248,8 +268,8 @@ MessageHandler.prototype = { // Ensure half-connected is not send twice by the same party. var validateState = function(currentState) { - if (currentState === "connecting" || - currentState === "half-connected") { + if (currentState === constants.CALL_STATES.CONNECTING || + currentState === constants.CALL_STATES.HALF_CONNECTED) { return "connected." + session.type; } return null; @@ -258,15 +278,19 @@ MessageHandler.prototype = { var stateMachine = { "accept": { transitions: [ - ["alerting", "connecting"] + [constants.CALL_STATES.ALERTING, constants.CALL_STATES.CONNECTING] ], actuator: function() { setTimeout(function() { + // Alerting for too long self.storage.getCallState(session.callId, function(err, state) { if (serverError(err, callback)) return; - if (state !== "connected") { - self.broadcastState(session.callId, "terminated:timeout"); - self.storage.setCallState(session.callId, 'terminated'); + if (state !== constants.CALL_STATES.CONNECTED) { + self.broadcastState(session.callId, + constants.CALL_STATES.TERMINATED + ":" + + constants.MESSAGE_REASONS.TIMEOUT); + self.storage.setCallState(session.callId, + constants.CALL_STATES.TERMINATED); } }); }, self.conf.connectionDuration * 1000); @@ -274,8 +298,8 @@ MessageHandler.prototype = { }, "media-up": { transitions: [ - ["connecting"], - ["half-connected"] + [constants.CALL_STATES.CONNECTING], + [constants.CALL_STATES.HALF_CONNECTED] ], validator: validateState } @@ -334,19 +358,20 @@ MessageHandler.prototype = { self.storage.getCallState(callId, function(err, redisCurrentState) { if (serverError(err)) return; - if (redisCurrentState === "terminated" && parts[1] !== undefined) { + if (redisCurrentState === constants.CALL_STATES.TERMINATED && + parts[1] !== undefined) { redisCurrentState += ":" + parts[1]; } - if (redisCurrentState !== "half-initiated") { + if (redisCurrentState !== constants.CALL_STATES.HALF_INITIATED) { self.pub.publish(callId, redisCurrentState, function(err) { if (serverError(err)) return; }); } if (conf.get("metrics") && - (redisCurrentState === "connected" || - redisCurrentState === "terminated")) { + (redisCurrentState === constants.CALL_STATES.CONNECTED || + redisCurrentState === constants.CALL_STATES.TERMINATED)) { hekaLogger.log('info', { op: 'websocket.summary', callId: callId, @@ -363,7 +388,7 @@ MessageHandler.prototype = { **/ createError: function(errorMessage) { return this.encode({ - messageType: "error", + messageType: constants.MESSAGE_TYPES.ERROR, reason: errorMessage }); }, diff --git a/test/functional_test.js b/test/functional_test.js index f567a1f..12c84d9 100644 --- a/test/functional_test.js +++ b/test/functional_test.js @@ -11,6 +11,7 @@ var sinon = require("sinon"); var randomBytes = require("crypto").randomBytes; var assert = sinon.assert; +var constants = require("../loop/constants"); var loop = require("../loop"); var apiRouter = loop.apiRouter; var app = loop.app; @@ -923,7 +924,7 @@ function runOnPrefix(apiPrefix) { callToken: callToken, callType: 'audio', urlCreationDate: urlCreationDate, - callState: "init", + callState: constants.CALL_STATES.INIT, timestamp: parseInt(Date.now() / 1000, 10) }, { @@ -938,7 +939,7 @@ function runOnPrefix(apiPrefix) { callToken: callToken, callType: 'audio-video', urlCreationDate: urlCreationDate, - callState: "init", + callState: constants.CALL_STATES.INIT, timestamp: parseInt(Date.now() / 1000, 10) + 1 }, { @@ -950,7 +951,7 @@ function runOnPrefix(apiPrefix) { apiKey: tokBoxConfig.credentials.default.apiKey, sessionId: fakeCallInfo.session3, calleeToken: fakeCallInfo.token2, - callState: "terminated", + callState: constants.CALL_STATES.TERMINATED, callToken: callToken, callType: 'audio-video', urlCreationDate: urlCreationDate, @@ -979,7 +980,7 @@ function runOnPrefix(apiPrefix) { if (err) throw err; var callsList = calls.filter(function(call) { - return call.callState !== "terminated"; + return call.callState !== constants.CALL_STATES.TERMINATED; }).map(function(call) { return { callId: call.callId, @@ -1039,7 +1040,7 @@ function runOnPrefix(apiPrefix) { apiKey: tokBoxConfig.credentials.default.apiKey, sessionId: fakeCallInfo.session3, calleeToken: fakeCallInfo.token2, - callState: "init", + callState: constants.CALL_STATES.INIT, callType: 'audio-video', timestamp: parseInt(Date.now() / 1000, 10) + 3 }; diff --git a/test/websockets_test.js b/test/websockets_test.js index 0f1b3ff..6eb3b33 100644 --- a/test/websockets_test.js +++ b/test/websockets_test.js @@ -12,6 +12,7 @@ var ws = require('ws'); var Token = require("express-hawkauth").Token; var hmac = require("../loop/hmac"); +var constants = require("../loop/constants"); var loop = require("../loop"); var server = loop.server; var storage = loop.storage; @@ -24,7 +25,7 @@ function createCall(callId, user, cb) { userMac: user, sessionId: '1234', calleeToken: '1234', - callState: "init", + callState: constants.CALL_STATES.INIT, timestamp: Date.now(), wsCallerToken: "callerToken", wsCalleeToken: "calleeToken" @@ -73,12 +74,12 @@ describe('websockets', function() { it('should echo back a message', function(done) { client.on('message', function(data) { var message = JSON.parse(data); - expect(message.messageType).eql('echo'); + expect(message.messageType).eql(constants.MESSAGE_TYPES.ECHO); expect(message.echo).eql('foo'); done(); }); client.send(JSON.stringify({ - messageType: 'echo', + messageType: constants.MESSAGE_TYPES.ECHO, echo: 'foo' })); }); @@ -89,12 +90,12 @@ describe('websockets', function() { if (err) throw err; client.on('message', function(data) { var error = JSON.parse(data); - expect(error.messageType).eql('error'); - expect(error.reason).eql('bad authentication'); + expect(error.messageType).eql(constants.MESSAGE_TYPES.ERROR); + expect(error.reason).eql(constants.ERROR_REASONS.BAD_AUTHENTICATION); done(); }); client.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: 'wrongCalleeToken', callId: callId })); @@ -105,13 +106,13 @@ describe('websockets', function() { function(done) { client.on('message', function(data) { var error = JSON.parse(data); - expect(error.messageType).eql('error'); - expect(error.reason).eql('bad callId'); + expect(error.messageType).eql(constants.MESSAGE_TYPES.ERROR); + expect(error.reason).eql(constants.ERROR_REASONS.BAD_CALLID); done(); }); client.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: '1234' })); @@ -121,20 +122,20 @@ describe('websockets', function() { function(done) { var callId = randomBytes(16).toString('hex'); - // Create a call and set its state to "init". + // Create a call and set its state to constants.CALL_STATES.INIT. createCall(callId, hawkCredentials.id, function(err) { if (err) throw err; - storage.setCallState(callId, "init", function(err) { + storage.setCallState(callId, constants.CALL_STATES.INIT, function(err) { if (err) throw err; client.on('message', function(data) { var message = JSON.parse(data); - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); done(); }); client.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); @@ -147,17 +148,17 @@ describe('websockets', function() { createCall(callId, hawkCredentials.id, function(err) { if (err) throw err; - storage.setCallState(callId, "init", function(err) { + storage.setCallState(callId, constants.CALL_STATES.INIT, function(err) { if (err) throw err; client.on('message', function(data) { var message = JSON.parse(data); - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); done(); }); client.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -187,10 +188,10 @@ describe('websockets', function() { caller.on('close', function() { caller.isClosed = true; }); caller.on('open', function() { - // Create a call and initialize its state to "init". + // Create a call and initialize its state to constants.CALL_STATES.INIT. createCall(callId, hawkCredentials.id, function(err) { if (err) throw err; - storage.setCallState(callId, "init", + storage.setCallState(callId, constants.CALL_STATES.INIT, conf.get("timers").supervisoryDuration, function(err) { if (err) throw err; done(); @@ -216,12 +217,12 @@ describe('websockets', function() { var message = JSON.parse(data); // First message should be "hello/init". if (calleeMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else { // Second should be "progress/alerting". - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); done(); } calleeMsgCount++; @@ -229,14 +230,14 @@ describe('websockets', function() { // Caller registers to the socket. caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); // Callee registers to the socket. callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -256,24 +257,24 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); if (callerMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else if (callerMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } else if (callerMsgCount === 2) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); caller.send(JSON.stringify({ - messageType: 'action', - event: 'media-up' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.MEDIA_UP })); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTED); } callerMsgCount++; }); @@ -288,41 +289,41 @@ describe('websockets', function() { callee.on('message', function(data) { var message = JSON.parse(data); if (calleeMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else if (calleeMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); callee.send(JSON.stringify({ - messageType: 'action', - event: 'accept' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.ACCEPT })); } else if (calleeMsgCount === 2) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); callee.send(JSON.stringify({ - messageType: 'action', - event: 'media-up' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.MEDIA_UP })); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTED); } calleeMsgCount++; }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -335,32 +336,32 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); if (callerMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else if (callerMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } else if (callerMsgCount === 2) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); caller.send(JSON.stringify({ - messageType: 'action', - event: 'media-up' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.MEDIA_UP })); } else if (callerMsgCount === 3) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("half-connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.HALF_CONNECTED); callee.send(JSON.stringify({ - messageType: 'action', - event: 'media-up' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.MEDIA_UP })); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTED); caller.isClosed = true; if (callee.isClosed) done(); } @@ -370,24 +371,24 @@ describe('websockets', function() { callee.on('message', function(data) { var message = JSON.parse(data); if (calleeMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else if (calleeMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); callee.send(JSON.stringify({ - messageType: 'action', - event: 'accept' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.ACCEPT })); } else if (calleeMsgCount === 2) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); } else if (calleeMsgCount === 3) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("half-connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.HALF_CONNECTED); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTED); callee.isClosed = true; if (caller.isClosed) done(); } @@ -396,13 +397,13 @@ describe('websockets', function() { caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -425,37 +426,37 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); - if (message.state === "half-connected") { + if (message.state === constants.CALL_STATES.HALF_CONNECTED) { caller.send(JSON.stringify({ - messageType: 'action', - event: 'media-up' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.MEDIA_UP })); } }); callee.on('message', function(data) { var message = JSON.parse(data); - if (message.state === "alerting") { + if (message.state === constants.CALL_STATES.ALERTING) { callee.send(JSON.stringify({ - messageType: 'action', - event: 'accept' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.ACCEPT })); - } else if (message.state === "connecting") { + } else if (message.state === constants.CALL_STATES.CONNECTING) { callee.send(JSON.stringify({ - messageType: 'action', - event: 'media-up' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.MEDIA_UP })); } }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -469,17 +470,17 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); - if (message.messageType === "hello") { + if (message.messageType === constants.MESSAGE_TYPES.HELLO) { caller.send(JSON.stringify({ - messageType: 'action', - event: 'terminate', - reason: 'cancel' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.TERMINATE, + reason: constants.MESSAGE_REASONS.CANCEL })); } }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); @@ -496,7 +497,7 @@ describe('websockets', function() { }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); @@ -511,21 +512,21 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); - if (message.messageType === "hello") { + if (message.messageType === constants.MESSAGE_TYPES.HELLO) { caller.send(JSON.stringify({ - messageType: 'action', - event: 'terminate', + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.TERMINATE, reason: 't#i5-!s-the-@nd' })); } else { - expect(message.messageType).eql("error"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.ERROR); expect(message.reason) .eql("Invalid reason: should be alphanumeric"); } }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); @@ -539,21 +540,21 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); - if (message.messageType === "hello") { + if (message.messageType === constants.MESSAGE_TYPES.HELLO) { caller.send(JSON.stringify({ - messageType: 'action', - event: 'terminate', - reason: 'cancel' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.TERMINATE, + reason: constants.MESSAGE_REASONS.CANCEL })); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("terminated"); - expect(message.reason).eql("cancel"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.TERMINATED); + expect(message.reason).eql(constants.MESSAGE_REASONS.CANCEL); } }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); @@ -567,27 +568,27 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); - if (message.messageType === "hello") return; - if (message.messageType === "progress") { + if (message.messageType === constants.MESSAGE_TYPES.HELLO) return; + if (message.messageType === constants.MESSAGE_TYPES.PROGRESS) { caller.send(JSON.stringify({ - messageType: 'action', - event: 'media-up' + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.MEDIA_UP })); } else { - expect(message.messageType).eql("error"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.ERROR); expect(message.reason).eql( "No transition from alerting state with media-up event."); } }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -602,18 +603,18 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); - if (message.messageType === "progress") { - expect(message.state).eql("terminated"); + if (message.messageType === constants.MESSAGE_TYPES.PROGRESS) { + expect(message.state).eql(constants.CALL_STATES.TERMINATED); expect(message.reason).eql("timeout"); storage.getCallState(callId, function(err, state) { if (err) throw err; - expect(state).eql("terminated"); + expect(state).eql(constants.CALL_STATES.TERMINATED); }); } }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); @@ -628,18 +629,18 @@ describe('websockets', function() { callee.on('message', function(data) { var message = JSON.parse(data); - if (message.messageType === "progress") { - expect(message.state).eql("terminated"); + if (message.messageType === constants.MESSAGE_TYPES.PROGRESS) { + expect(message.state).eql(constants.CALL_STATES.TERMINATED); expect(message.reason).eql("timeout"); storage.getCallState(callId, function(err, state) { if (err) throw err; - expect(state).eql("terminated"); + expect(state).eql(constants.CALL_STATES.TERMINATED); }); } }); callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -649,20 +650,20 @@ describe('websockets', function() { "less than X seconds", function(done) { caller.on('message', function(data) { var message = JSON.parse(data); - if (message.messageType === "hello") { + if (message.messageType === constants.MESSAGE_TYPES.HELLO) { // The callee connects! callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); - } else if (message.messageType === "progress"){ - expect(message.state).not.eql("terminated"); + } else if (message.messageType === constants.MESSAGE_TYPES.PROGRESS){ + expect(message.state).not.eql(constants.CALL_STATES.TERMINATED); done(); } }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); @@ -672,20 +673,20 @@ describe('websockets', function() { "less than X seconds", function(done) { callee.on('message', function(data) { var message = JSON.parse(data); - if (message.messageType === "hello") { + if (message.messageType === constants.MESSAGE_TYPES.HELLO) { // The callee connects! caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); - } else if (message.messageType === "progress"){ - expect(message.state).not.eql("terminated"); + } else if (message.messageType === constants.MESSAGE_TYPES.PROGRESS){ + expect(message.state).not.eql(constants.CALL_STATES.TERMINATED); done(); } }); callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -716,11 +717,11 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); if (callerMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else if (callerMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } callerMsgCount++; }); @@ -728,32 +729,32 @@ describe('websockets', function() { callee.on('message', function(data) { var message = JSON.parse(data); if (calleeMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else if (calleeMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("terminated"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.TERMINATED); expect(message.reason).eql("timeout"); storage.getCallState(callId, function(err, state) { if (err) throw err; - expect(state).eql("terminated"); + expect(state).eql(constants.CALL_STATES.TERMINATED); }); } calleeMsgCount++; }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); // The callee connects! callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -766,11 +767,11 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); if (callerMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else if (callerMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } callerMsgCount++; }); @@ -778,32 +779,32 @@ describe('websockets', function() { callee.on('message', function(data) { var message = JSON.parse(data); if (calleeMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); callee.send(JSON.stringify({ - messageType: "action", - event: "accept" + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.ACCEPT })); } else if (calleeMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); done(); } calleeMsgCount++; }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); // The callee connects! callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -830,21 +831,21 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); if (callerMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else if (callerMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } else if (callerMsgCount === 2) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("terminated"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.TERMINATED); expect(message.reason).eql("timeout"); storage.getCallState(callId, function(err, state) { if (err) throw err; - expect(state).eql("terminated"); + expect(state).eql(constants.CALL_STATES.TERMINATED); }); } callerMsgCount++; @@ -853,39 +854,39 @@ describe('websockets', function() { callee.on('message', function(data) { var message = JSON.parse(data); if (calleeMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); callee.send(JSON.stringify({ - messageType: "action", - event: "accept" + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.ACCEPT })); } else if (calleeMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } else if (calleeMsgCount === 2) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("terminated"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.TERMINATED); expect(message.reason).eql("timeout"); storage.getCallState(callId, function(err, state) { if (err) throw err; - expect(state).eql("terminated"); + expect(state).eql(constants.CALL_STATES.TERMINATED); }); } calleeMsgCount++; }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); // The callee connects! callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -912,28 +913,28 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); if (callerMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else if (callerMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } else if (callerMsgCount === 2) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); callee.send(JSON.stringify({ - messageType: "action", - event: "media-up" + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.MEDIA_UP })); } else if (callerMsgCount === 3) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("half-connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.HALF_CONNECTED); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("terminated"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.TERMINATED); expect(message.reason).eql("timeout"); storage.getCallState(callId, function(err, state) { if (err) throw err; - expect(state).eql("terminated"); + expect(state).eql(constants.CALL_STATES.TERMINATED); }); } callerMsgCount++; @@ -942,42 +943,42 @@ describe('websockets', function() { callee.on('message', function(data) { var message = JSON.parse(data); if (calleeMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); caller.send(JSON.stringify({ - messageType: "action", - event: "accept" + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.ACCEPT })); } else if (calleeMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } else if (calleeMsgCount === 2) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); } else if (calleeMsgCount === 3) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("half-connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.HALF_CONNECTED); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("terminated"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.TERMINATED); expect(message.reason).eql("timeout"); storage.getCallState(callId, function(err, state) { if (err) throw err; - expect(state).eql("terminated"); + expect(state).eql(constants.CALL_STATES.TERMINATED); }); } calleeMsgCount++; }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); // The callee connects! callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId })); @@ -1003,24 +1004,24 @@ describe('websockets', function() { caller.on('message', function(data) { var message = JSON.parse(data); if (callerMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); } else if (callerMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } else if (callerMsgCount === 2) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); caller.send(JSON.stringify({ - messageType: "action", - event: "media-up" + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.MEDIA_UP })); } else if (callerMsgCount === 3) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("half-connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.HALF_CONNECTED); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTED); } callerMsgCount++; }); @@ -1028,41 +1029,41 @@ describe('websockets', function() { callee.on('message', function(data) { var message = JSON.parse(data); if (calleeMsgCount === 0) { - expect(message.messageType).eql("hello"); - expect(message.state).eql("init"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.HELLO); + expect(message.state).eql(constants.CALL_STATES.INIT); callee.send(JSON.stringify({ - messageType: "action", - event: "accept" + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.ACCEPT })); } else if (calleeMsgCount === 1) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("alerting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.ALERTING); } else if (calleeMsgCount === 2) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connecting"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTING); } else if (calleeMsgCount === 3) { - expect(message.messageType).eql("progress"); - expect(message.state).eql("half-connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.HALF_CONNECTED); callee.send(JSON.stringify({ - messageType: "action", - event: "media-up" + messageType: constants.MESSAGE_TYPES.ACTION, + event: constants.MESSAGE_EVENTS.MEDIA_UP })); } else { - expect(message.messageType).eql("progress"); - expect(message.state).eql("connected"); + expect(message.messageType).eql(constants.MESSAGE_TYPES.PROGRESS); + expect(message.state).eql(constants.CALL_STATES.CONNECTED); } calleeMsgCount++; }); caller.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "callerToken", callId: callId })); // The callee connects! callee.send(JSON.stringify({ - messageType: 'hello', + messageType: constants.MESSAGE_TYPES.HELLO, auth: "calleeToken", callId: callId }));