From b08c2f75787300adc4dbf9553b85b97bf384b7cd Mon Sep 17 00:00:00 2001 From: Hashir Baqai Date: Mon, 14 Nov 2016 12:56:20 -0800 Subject: [PATCH 1/8] adding missing close brackets --- test/opentok-test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/opentok-test.js b/test/opentok-test.js index 8d3897b9..5cf3d5ed 100644 --- a/test/opentok-test.js +++ b/test/opentok-test.js @@ -63,6 +63,7 @@ describe('OpenTok', function() { var opentok = new OpenTok(parseInt(apiKey), apiSecret); expect(opentok).to.be.an.instanceof(OpenTok); }); +}); describe('when initialized with an apiUrl', function() { beforeEach(function() { From 13734014f5b4e09d51b5e10699f8b5ebc9be30c5 Mon Sep 17 00:00:00 2001 From: Hashir Baqai Date: Tue, 15 Nov 2016 13:45:38 -0800 Subject: [PATCH 2/8] adding eslint and applying it to opentok-test.js --- .eslintrc | 3 + .gitignore | 1 + package.json | 3 + test/.eslintrc | 9 + test/opentok-test.js | 1167 ++++++++++++++++++++++-------------------- 5 files changed, 628 insertions(+), 555 deletions(-) create mode 100644 .eslintrc create mode 100644 test/.eslintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 00000000..e032d2a4 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": "airbnb-base/legacy" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index aafcb345..45cf1e0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ .DS_Store +.vscode *.log diff --git a/package.json b/package.json index 645c6768..9154dc0c 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,9 @@ }, "devDependencies": { "chai": "^3.5.0", + "eslint": "^3.10.1", + "eslint-config-airbnb-base": "^10.0.1", + "eslint-plugin-import": "^2.2.0", "grunt": "^1.0.1", "grunt-jasmine-node": "^0.3.1", "grunt-jsdoc": "^2.1.0", diff --git a/test/.eslintrc b/test/.eslintrc new file mode 100644 index 00000000..ef3a6ad3 --- /dev/null +++ b/test/.eslintrc @@ -0,0 +1,9 @@ +{ + "rules": { + "func-names": 0, + "no-unused-expressions": 0 + }, + "env": { + "mocha": true + } +} \ No newline at end of file diff --git a/test/opentok-test.js b/test/opentok-test.js index 5cf3d5ed..660d86d3 100644 --- a/test/opentok-test.js +++ b/test/opentok-test.js @@ -1,603 +1,687 @@ -var expect = require('chai').expect, - nock = require('nock'), - _ = require('lodash'); +/* */ +var expect = require('chai').expect; +var nock = require('nock'); +var _ = require('lodash'); // Subject -var OpenTok = require('../lib/opentok.js'), - Session = require('../lib/session.js'), - SipInterconnect = require('../lib/sipInterconnect.js'), - pkg = require('../package.json'); +var OpenTok = require('../lib/opentok.js'); +var Session = require('../lib/session.js'); +var SipInterconnect = require('../lib/sipInterconnect.js'); +var pkg = require('../package.json'); // Fixtures -var apiKey = '123456', - apiSecret = '1234567890abcdef1234567890abcdef1234567890', - apiUrl = 'http://mymock.example.com', - // This is specifically concocted for these tests (uses fake apiKey/apiSecret above) - sessionId = '1_MX4xMjM0NTZ-flNhdCBNYXIgMTUgMTQ6NDI6MjMgUERUIDIwMTR-MC40OTAxMzAyNX4', - badApiKey = 'badkey', - badApiSecret = 'badsecret', - goodSipUri = 'sip:siptesturl@tokbox.com', - badSipUri = 'siptesturl@tokbox.com', - defaultApiUrl = 'https://api.opentok.com', - defaultTimeoutLength = 20000; // 20 seconds +var apiKey = '123456'; +var apiSecret = '1234567890abcdef1234567890abcdef1234567890'; +var apiUrl = 'http://mymock.example.com'; +// This is specifically concocted for these tests (uses fake apiKey/apiSecret above) +var sessionId = '1_MX4xMjM0NTZ-flNhdCBNYXIgMTUgMTQ6NDI6MjMgUERUIDIwMTR-MC40OTAxMzAyNX4'; +var badApiKey = 'badkey'; +var badApiSecret = 'badsecret'; +var goodSipUri = 'sip:siptesturl@tokbox.com'; +var badSipUri = 'siptesturl@tokbox.com'; +var defaultApiUrl = 'https://api.opentok.com'; +var defaultTimeoutLength = 20000; // 20 seconds +var recording = false; + +// Helpers +var helpers = require('./helpers.js'); + nock.disableNetConnect(); -var recording = false; if (recording) { // set these values before changing the above to true apiKey = ''; apiSecret = ''; - //nock.enableNetConnect(); + // nock.enableNetConnect(); nock.recorder.rec(); } -// Helpers -var helpers = require('./helpers.js'); - -describe('OpenTok', function() { - it('should initialize with a valid apiKey and apiSecret', function() { +describe('OpenTok', function () { + it('should initialize with a valid apiKey and apiSecret', function () { var opentok = new OpenTok(apiKey, apiSecret); expect(opentok).to.be.an.instanceof(OpenTok); expect(opentok.apiKey).to.be.equal(apiKey); expect(opentok.apiSecret).to.be.equal(apiSecret); expect(opentok.apiUrl).to.be.equal(defaultApiUrl); }); - it('should initialize without `new`', function() { + it('should initialize without `new`', function () { var opentok = OpenTok(apiKey, apiSecret); expect(opentok).to.be.an.instanceof(OpenTok); }); - it('should not initialize with just an apiKey but no apiSecret', function() { - expect(function() { - var opentok = new OpenTok(apiKey); + it('should not initialize with just an apiKey but no apiSecret', function () { + expect(function () { + var opentok = new OpenTok(apiKey); // eslint-disable-line }).to.throw(Error); }); - it('should not initialize with incorrect type parameters', function() { - expect(function() { - var opentok = new OpenTok(new Date(), 'asdasdasdasdasd'); + it('should not initialize with incorrect type parameters', function () { + expect(function () { + var opentok = new OpenTok(new Date(), 'asdasdasdasdasd'); // eslint-disable-line }).to.throw(Error); - expect(function() { - opentok = new OpenTok(4, {}); + expect(function () { + opentok = new OpenTok(4, {}); // eslint-disable-line }).to.throw(Error); }); - it('should cooerce a number for the apiKey', function() { - var opentok = new OpenTok(parseInt(apiKey), apiSecret); + it('should cooerce a number for the apiKey', function () { + var opentok = new OpenTok(parseInt(apiKey, 10), apiSecret); expect(opentok).to.be.an.instanceof(OpenTok); }); }); - describe('when initialized with an apiUrl', function() { - beforeEach(function() { - this.opentok = new OpenTok(apiKey, apiSecret, apiUrl); - }); - it('exposes the custom apiUrl', function() { - expect(this.opentok.apiUrl).to.be.equal(apiUrl); - }); - it('sends its requests to the set apiUrl', function(done) { - var scope = nock(apiUrl) - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/session/create', "archiveMode=manual&p2p.preference=enabled") - .reply(200, "SESSIONID123456Wed Mar 19 23:35:24 PDT 2014", { server: 'nginx', - date: 'Thu, 20 Mar 2014 06:35:24 GMT', - 'content-type': 'text/xml', - connection: 'keep-alive', - 'access-control-allow-origin': '*', - 'x-tb-host': 'mantis503-nyc.tokbox.com', - 'content-length': '211' }); - this.opentok.createSession(function(err, session){ - if (err) return done(err); - expect(session).to.be.an.instanceof(Session); - expect(session.sessionId).to.equal('SESSIONID'); - scope.done(); +describe('when initialized with an apiUrl', function () { + beforeEach(function () { + this.opentok = new OpenTok(apiKey, apiSecret, apiUrl); + }); + it('exposes the custom apiUrl', function () { + expect(this.opentok.apiUrl).to.be.equal(apiUrl); + }); + it('sends its requests to the set apiUrl', function (done) { + var scope = nock(apiUrl) + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/session/create', 'archiveMode=manual&p2p.preference=enabled') + .reply(200, 'SESSIONID123456Wed Mar 19 23:35:24 PDT 2014', { server: 'nginx', + date: 'Thu, 20 Mar 2014 06:35:24 GMT', + 'content-type': 'text/xml', + connection: 'keep-alive', + 'access-control-allow-origin': '*', + 'x-tb-host': 'mantis503-nyc.tokbox.com', + 'content-length': '211' }); + this.opentok.createSession(function (err, session) { + if (err) { done(err); - }); + return; + } + expect(session).to.be.an.instanceof(Session); + expect(session.sessionId).to.equal('SESSIONID'); + scope.done(); + done(err); }); }); +}); - describe('when initialized with a proxy', function() { - beforeEach(function() { - // TODO: remove temporary proxy value - this.proxyUrl = 'http://localhost:8080'; - this.opentok = new OpenTok(apiKey, apiSecret, { proxy: this.proxyUrl }); - }); - it('sends its requests through an http proxy', function(done) { - this.timeout(10000); - var scope = nock('https://api.opentok.com:443') - .post('/session/create', "archiveMode=manual&p2p.preference=enabled") - .reply(200, "1_MX44NTQ1MTF-flN1biBKdWwgMTMgMjE6MjY6MzUgUERUIDIwMTR-MC40OTU0NzA0Nn5Qfg854511Sun Jul 13 21:26:35 PDT 2014", { server: 'nginx', +describe('when initialized with a proxy', function () { + beforeEach(function () { + // TODO: remove temporary proxy value + this.proxyUrl = 'http://localhost:8080'; + this.opentok = new OpenTok(apiKey, apiSecret, { proxy: this.proxyUrl }); + }); + it('sends its requests through an http proxy', function (done) { + var scope; + this.timeout(10000); + scope = nock('https://api.opentok.com:443') + .post('/session/create', 'archiveMode=manual&p2p.preference=enabled') + .reply(200, '1_MX44NTQ1MTF-flN1biBKdWwgMTMgMjE6MjY6MzUgUERUIDIwMTR-MC40OTU0NzA0Nn5Qfg854511Sun Jul 13 21:26:35 PDT 2014', { server: 'nginx', date: 'Mon, 14 Jul 2014 04:26:35 GMT', 'content-type': 'application/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'mantis402-oak.tokbox.com', 'content-length': '274' }); - this.opentok.createSession(function(err, session) { - scope.done(); - done(err); - }); + this.opentok.createSession(function (err) { + scope.done(); + done(err); }); }); +}); - describe('when a user agent addendum is needed', function() { - beforeEach(function() { - this.addendum = 'my-special-app'; - this.opentok = new OpenTok(apiKey, apiSecret, { uaAddendum: this.addendum }); - }); - it('appends the addendum in a create session request', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version+" "+this.addendum)) - .post('/session/create', "archiveMode=manual&p2p.preference=enabled") - .reply(200, "SESSIONID123456Wed Mar 19 23:35:24 PDT 2014", { server: 'nginx', +describe('when a user agent addendum is needed', function () { + beforeEach(function () { + this.addendum = 'my-special-app'; + this.opentok = new OpenTok(apiKey, apiSecret, { uaAddendum: this.addendum }); + }); + it('appends the addendum in a create session request', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version + ' ' + this.addendum)) + .post('/session/create', 'archiveMode=manual&p2p.preference=enabled') + .reply(200, 'SESSIONID123456Wed Mar 19 23:35:24 PDT 2014', { server: 'nginx', date: 'Thu, 20 Mar 2014 06:35:24 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'mantis503-nyc.tokbox.com', 'content-length': '211' }); - this.opentok.createSession(function(err, session){ - if (err) return done(err); - scope.done(); + this.opentok.createSession(function (err) { + if (err) { done(err); - }); - }); - it.skip('appends the addendum in an archiving request', function() { - // TODO: + return; + } + scope.done(); + done(err); }); }); + it.skip('appends the addendum in an archiving request', function () { + // TODO: + }); +}); - describe('when there is too much network latency', function() { - beforeEach(function() { - this.opentok = new OpenTok(apiKey, apiSecret); - }); - it('times out when the request takes longer than the default timeout', function(done) { - // make sure the mocha test runner doesn't time out for at least as long as we are willing to - // wait plus some reasonable amount of overhead time (100ms) - this.timeout(defaultTimeoutLength + 100); - var scope = nock('https://api.opentok.com:443') - .post('/session/create', "archiveMode=manual&p2p.preference=enabled") - .delayConnection(defaultTimeoutLength + 10) - .reply(200, "1_MX44NTQ1MTF-flN1biBKdWwgMTMgMjE6MjY6MzUgUERUIDIwMTR-MC40OTU0NzA0Nn5Qfg854511Sun Jul 13 21:26:35 PDT 2014", { server: 'nginx', +describe('when there is too much network latency', function () { + beforeEach(function () { + this.opentok = new OpenTok(apiKey, apiSecret); + }); + it.only('times out when the request takes longer than the default timeout', function (done) { + // make sure the mocha test runner doesn't time out for at least as long as we are willing to + // wait plus some reasonable amount of overhead time (100ms) + var scope; + this.timeout(defaultTimeoutLength + 100); + scope = nock('https://api.opentok.com:443') + .post('/session/create', 'archiveMode=manual&p2p.preference=enabled') + .delayConnection(defaultTimeoutLength + 10) + .reply(200, '1_MX44NTQ1MTF-flN1biBKdWwgMTMgMjE6MjY6MzUgUERUIDIwMTR-MC40OTU0NzA0Nn5Qfg854511Sun Jul 13 21:26:35 PDT 2014', { server: 'nginx', date: 'Mon, 14 Jul 2014 04:26:35 GMT', 'content-type': 'application/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'mantis402-oak.tokbox.com', 'content-length': '274' }); - this.opentok.createSession(function(err, session) { - expect(err).to.be.an.instanceof(Error); - scope.done(); - done(); - }); + this.opentok.createSession(function (err, session) { + console.log('sessionId' , session); + console.log('err', err); + expect(err).to.be.an.instanceof(Error); + scope.done(); + done(); }); }); +}); - describe('when initialized with bad credentials', function() { - beforeEach(function() { - this.opentok = new OpenTok(badApiKey, badApiSecret); - }); - describe('#createSession', function() { - it('throws a client error', function(done) { - var scope = nock('https://api.opentok.com:443') - .post('/session/create', "archiveMode=manual&p2p.preference=enabled") - .reply(403, "-1Invalid partner credentials", { server: 'nginx', +describe('when initialized with bad credentials', function () { + beforeEach(function () { + this.opentok = new OpenTok(badApiKey, badApiSecret); + }); + describe('#createSession', function () { + it('throws a client error', function (done) { + var scope = nock('https://api.opentok.com:443') + .post('/session/create', 'archiveMode=manual&p2p.preference=enabled') + .reply(403, '-1Invalid partner credentials', { server: 'nginx', date: 'Fri, 30 May 2014 19:37:12 GMT', 'content-type': 'application/xml', connection: 'keep-alive', 'content-length': '145' }); - this.opentok.createSession(function(err, session) { - expect(err).to.be.an.instanceof(Error); - scope.done(); - done(); - }); + this.opentok.createSession(function (err) { + expect(err).to.be.an.instanceof(Error); + scope.done(); + done(); }); }); }); +}); - describe('#createSession', function() { - - beforeEach(function() { - this.opentok = new OpenTok(apiKey, apiSecret); - }); +describe('#createSession', function () { + beforeEach(function () { + this.opentok = new OpenTok(apiKey, apiSecret); + }); - it('creates a new session', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/session/create', "archiveMode=manual&p2p.preference=enabled") - .reply(200, "SESSIONID123456Wed Mar 19 23:35:24 PDT 2014", { server: 'nginx', + it('creates a new session', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/session/create', 'archiveMode=manual&p2p.preference=enabled') + .reply(200, 'SESSIONID123456Wed Mar 19 23:35:24 PDT 2014', { server: 'nginx', date: 'Thu, 20 Mar 2014 06:35:24 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'mantis503-nyc.tokbox.com', 'content-length': '211' }); - // pass no options parameter - this.opentok.createSession(function(err, session){ - if (err) return done(err); - expect(session).to.be.an.instanceof(Session); - expect(session.sessionId).to.equal('SESSIONID'); - expect(session.mediaMode).to.equal('relayed'); - scope.done(); - done(err); - }); + // pass no options parameter + this.opentok.createSession(function (err, session) { + if (err) { + done(); + return; + } + expect(session).to.be.an.instanceof(Session); + expect(session.sessionId).to.equal('SESSIONID'); + expect(session.mediaMode).to.equal('relayed'); + scope.done(); + done(err); }); + }); - it('creates a media routed session', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/session/create', "archiveMode=manual&p2p.preference=disabled") - .reply(200, "SESSIONID123456Thu Mar 20 07:02:45 PDT 2014", { server: 'nginx', + it('creates a media routed session', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/session/create', 'archiveMode=manual&p2p.preference=disabled') + .reply(200, 'SESSIONID123456Thu Mar 20 07:02:45 PDT 2014', { server: 'nginx', date: 'Thu, 20 Mar 2014 14:02:45 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'oms506-nyc.tokbox.com', 'content-length': '211' }); - this.opentok.createSession({ 'mediaMode' : 'routed' }, function(err, session) { - if (err) return done(err); - expect(session).to.be.an.instanceof(Session); - expect(session.sessionId).to.equal('SESSIONID'); - expect(session.mediaMode).to.equal('routed'); - scope.done(); + this.opentok.createSession({ mediaMode: 'routed' }, function (err, session) { + if (err) { done(err); - }); + return; + } + expect(session).to.be.an.instanceof(Session); + expect(session.sessionId).to.equal('SESSIONID'); + expect(session.mediaMode).to.equal('routed'); + scope.done(); + done(err); }); + }); - it('creates a media relayed session even if the media mode is invalid', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/session/create', "archiveMode=manual&p2p.preference=enabled") - .reply(200, "SESSIONID123456Thu Mar 20 07:02:45 PDT 2014", { server: 'nginx', + it('creates a media relayed session even if the media mode is invalid', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/session/create', 'archiveMode=manual&p2p.preference=enabled') + .reply(200, 'SESSIONID123456Thu Mar 20 07:02:45 PDT 2014', { server: 'nginx', date: 'Thu, 20 Mar 2014 14:02:45 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'oms506-nyc.tokbox.com', 'content-length': '211' }); - this.opentok.createSession({ 'mediaMode' : 'blah' }, function(err, session) { - if (err) return done(err); - expect(session).to.be.an.instanceof(Session); - expect(session.sessionId).to.equal('SESSIONID'); - expect(session.mediaMode).to.equal('relayed'); - scope.done(); + this.opentok.createSession({ mediaMode: 'blah' }, function (err, session) { + if (err) { done(err); - }); + return; + } + expect(session).to.be.an.instanceof(Session); + expect(session.sessionId).to.equal('SESSIONID'); + expect(session.mediaMode).to.equal('relayed'); + scope.done(); + done(err); }); + }); - it('creates a session with manual archive mode', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/session/create', "archiveMode=manual&p2p.preference=disabled") - .reply(200, "SESSIONID123456Thu Mar 20 07:02:45 PDT 2014", { server: 'nginx', + it('creates a session with manual archive mode', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/session/create', 'archiveMode=manual&p2p.preference=disabled') + .reply(200, 'SESSIONID123456Thu Mar 20 07:02:45 PDT 2014', { server: 'nginx', date: 'Thu, 20 Mar 2014 14:02:45 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'oms506-nyc.tokbox.com', 'content-length': '211' }); - this.opentok.createSession({ 'mediaMode' : 'routed', 'archiveMode' : 'manual' }, function(err, session) { - if (err) return done(err); - expect(session).to.be.an.instanceof(Session); - expect(session.sessionId).to.equal('SESSIONID'); - expect(session.archiveMode).to.equal('manual'); - scope.done(); + this.opentok.createSession({ mediaMode: 'routed', archiveMode: 'manual' }, function (err, session) { + if (err) { done(err); - }); + return; + } + expect(session).to.be.an.instanceof(Session); + expect(session.sessionId).to.equal('SESSIONID'); + expect(session.archiveMode).to.equal('manual'); + scope.done(); + done(err); }); + }); - it('creates a session with always archive mode', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/session/create', "archiveMode=always&p2p.preference=disabled") - .reply(200, "SESSIONID123456Thu Mar 20 07:02:45 PDT 2014", { server: 'nginx', + it('creates a session with always archive mode', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/session/create', 'archiveMode=always&p2p.preference=disabled') + .reply(200, 'SESSIONID123456Thu Mar 20 07:02:45 PDT 2014', { server: 'nginx', date: 'Thu, 20 Mar 2014 14:02:45 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'oms506-nyc.tokbox.com', 'content-length': '211' }); - this.opentok.createSession({ 'mediaMode' : 'routed', 'archiveMode' : 'always' }, function(err, session) { - if (err) return done(err); - expect(session).to.be.an.instanceof(Session); - expect(session.sessionId).to.equal('SESSIONID'); - expect(session.archiveMode).to.equal('always'); - scope.done(); + this.opentok.createSession({ mediaMode: 'routed', archiveMode: 'always' }, function (err, session) { + if (err) { done(err); - }); + return; + } + expect(session).to.be.an.instanceof(Session); + expect(session.sessionId).to.equal('SESSIONID'); + expect(session.archiveMode).to.equal('always'); + scope.done(); + done(err); }); + }); - it('creates a session with manual archive mode even if the archive mode is invalid', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/session/create', "archiveMode=manual&p2p.preference=disabled") - .reply(200, "SESSIONID123456Thu Mar 20 07:02:45 PDT 2014", { server: 'nginx', + it('creates a session with manual archive mode even if the archive mode is invalid', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/session/create', 'archiveMode=manual&p2p.preference=disabled') + .reply(200, 'SESSIONID123456Thu Mar 20 07:02:45 PDT 2014', { server: 'nginx', date: 'Thu, 20 Mar 2014 14:02:45 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'oms506-nyc.tokbox.com', 'content-length': '211' }); - this.opentok.createSession({ 'mediaMode' : 'routed', 'archiveMode' : 'invalid' }, function(err, session) { - if (err) return done(err); - expect(session).to.be.an.instanceof(Session); - expect(session.sessionId).to.equal('SESSIONID'); - expect(session.archiveMode).to.equal('manual'); - scope.done(); + this.opentok.createSession({ mediaMode: 'routed', archiveMode: 'invalid' }, function (err, session) { + if (err) { done(err); - }); + return; + } + expect(session).to.be.an.instanceof(Session); + expect(session.sessionId).to.equal('SESSIONID'); + expect(session.archiveMode).to.equal('manual'); + scope.done(); + done(err); }); + }); - it('adds a location hint to the created session', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/session/create', "location=12.34.56.78&archiveMode=manual&p2p.preference=enabled") - .reply(200, "SESSIONID123456Thu Mar 20 07:17:22 PDT 2014", { server: 'nginx', + it('adds a location hint to the created session', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/session/create', 'location=12.34.56.78&archiveMode=manual&p2p.preference=enabled') + .reply(200, 'SESSIONID123456Thu Mar 20 07:17:22 PDT 2014', { server: 'nginx', date: 'Thu, 20 Mar 2014 14:17:22 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'oms506-nyc.tokbox.com', 'content-length': '211' }); - // passes location: '12.34.56.78' - this.opentok.createSession({ 'location': '12.34.56.78' }, function(err, session) { - if (err) return done(err); - expect(session).to.be.an.instanceof(Session); - expect(session.sessionId).to.equal('SESSIONID'); - expect(session.mediaMode).to.equal('relayed'); - expect(session.location).to.equal('12.34.56.78'); - scope.done(); + // passes location: '12.34.56.78' + this.opentok.createSession({ location: '12.34.56.78' }, function (err, session) { + if (err) { done(err); - }); + return; + } + expect(session).to.be.an.instanceof(Session); + expect(session.sessionId).to.equal('SESSIONID'); + expect(session.mediaMode).to.equal('relayed'); + expect(session.location).to.equal('12.34.56.78'); + scope.done(); + done(err); }); + }); - it('complains when the location value is not valid', function(done) { - this.opentok.createSession({ location: 'not an ip address' }, function(err) { - expect(err).to.be.an.instanceof(Error); - done(); - }); + it('complains when the location value is not valid', function (done) { + this.opentok.createSession({ location: 'not an ip address' }, function (err) { + expect(err).to.be.an.instanceof(Error); + done(); }); + }); - it('complains when the archive mode is always and the media mode is routed', function(done) { - this.opentok.createSession({ archiveMedia: 'always', mediaMode: 'routed' }, function(err) { - expect(err).to.be.an.instanceof(Error); - done(); - }); + it('complains when the archive mode is always and the media mode is routed', function (done) { + this.opentok.createSession({ archiveMedia: 'always', mediaMode: 'routed' }, function (err) { + expect(err).to.be.an.instanceof(Error); + done(); }); + }); - it('complains when there is no callback function', function() { - expect(function() { - var result = this.opentok.createSession(); - }).to.throw(Error); - }); + it('complains when there is no callback function', function () { + expect(function () { + this.opentok.createSession(); + }).to.throw(Error); + }); - it('complains when a server error takes place', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/session/create', "archiveMode=manual&p2p.preference=enabled") - .reply(503, "", { server: 'nginx', + it('complains when a server error takes place', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/session/create', 'archiveMode=manual&p2p.preference=enabled') + .reply(503, '', { server: 'nginx', date: 'Thu, 20 Mar 2014 06:35:24 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'mantis503-nyc.tokbox.com', 'content-length': '0' }); - this.opentok.createSession(function(err, session) { - expect(err).to.be.an.instanceof(Error); - expect(err.message).to.contain("A server error occurred"); - scope.done(); - done(); - }); + this.opentok.createSession(function (err) { + expect(err).to.be.an.instanceof(Error); + expect(err.message).to.contain('A server error occurred'); + scope.done(); + done(); }); + }); - it('returns a Session that can generate a token', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/session/create', "archiveMode=manual&p2p.preference=enabled") - .reply(200, ""+sessionId+"123456Wed Mar 19 23:35:24 PDT 2014", { server: 'nginx', + it('returns a Session that can generate a token', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/session/create', 'archiveMode=manual&p2p.preference=enabled') + .reply(200, '' + sessionId + '123456Wed Mar 19 23:35:24 PDT 2014', { server: 'nginx', date: 'Thu, 20 Mar 2014 06:35:24 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'mantis503-nyc.tokbox.com', 'content-length': '211' }); - // pass no options parameter - this.opentok.createSession(function(err, session){ - if (err) return done(err); - scope.done(); - var token = session.generateToken(); - expect(token).to.be.a('string'); + // pass no options parameter + this.opentok.createSession(function (err, session) { + var token; + if (err) { done(err); - }); + return; + } + scope.done(); + token = session.generateToken(); + expect(token).to.be.a('string'); + done(err); }); + }); - it('should not modify the options object parameter', function(done) { - var scope = nock('https://api.opentok.com:443') - .filteringRequestBody(function(path) { - return '*'; - }) - .post('/session/create', '*') - .reply(200, "SESSIONID123456Thu Mar 20 07:02:45 PDT 2014", { server: 'nginx', + it('should not modify the options object parameter', function (done) { + var scope = nock('https://api.opentok.com:443') + .filteringRequestBody(function () { + return '*'; + }) + .post('/session/create', '*') + .reply(200, 'SESSIONID123456Thu Mar 20 07:02:45 PDT 2014', { server: 'nginx', date: 'Thu, 20 Mar 2014 14:02:45 GMT', 'content-type': 'text/xml', connection: 'keep-alive', 'access-control-allow-origin': '*', 'x-tb-host': 'oms506-nyc.tokbox.com', 'content-length': '211' }); - var options = { mediaMode: 'routed', archiveMode: 'manual' }; - var optionsUntouched = _.clone(options); - this.opentok.createSession(options, function(err, session) { - if (err) return done(err); - scope.done(); - expect(options).to.deep.equal(optionsUntouched); - done(); - }); + var options = { mediaMode: 'routed', archiveMode: 'manual' }; + var optionsUntouched = _.clone(options); + this.opentok.createSession(options, function (err) { + if (err) { + done(err); + return; + } + scope.done(); + expect(options).to.deep.equal(optionsUntouched); + done(); }); + }); +}); + +describe('#generateToken', function () { + beforeEach(function () { + this.opentok = new OpenTok(apiKey, apiSecret); + this.sessionId = sessionId; }); + it('generates a token', function () { + // call generateToken with no options + var token = this.opentok.generateToken(this.sessionId); + var decoded; + expect(token).to.be.a('string'); + expect(helpers.verifyTokenSignature(token, apiSecret)).to.be.true; + decoded = helpers.decodeToken(token); + expect(decoded.partner_id).to.equal(apiKey); + expect(decoded.create_time).to.exist; + expect(decoded.nonce).to.exist; + }); - describe('#generateToken', function() { + it('assigns a role in the token', function () { + // expects one with no role defined to assign "publisher" + var defaultRoleToken = this.opentok.generateToken(this.sessionId); + var decoded; + var subscriberToken; + expect(defaultRoleToken).to.be.a('string'); + expect(helpers.verifyTokenSignature(defaultRoleToken, apiSecret)).to.be.true; + decoded = helpers.decodeToken(defaultRoleToken); + expect(decoded.role).to.equal('publisher'); + + // expects one with a valid role defined to set it + subscriberToken = this.opentok.generateToken(this.sessionId, { role: 'subscriber' }); + expect(subscriberToken).to.be.a('string'); + expect(helpers.verifyTokenSignature(subscriberToken, apiSecret)).to.be.true; + decoded = helpers.decodeToken(subscriberToken); + expect(decoded.role).to.equal('subscriber'); + + // expects one with an invalid role to complain + expect(function () { + this.opentok.generateToken(this.sessionId, { role: 5 }); + }).to.throw(Error); + }); - beforeEach(function() { - this.opentok = new OpenTok(apiKey, apiSecret); - this.sessionId = sessionId; - }); + it('sets an expiration time for the token', function () { + var now = Math.round((new Date().getTime()) / 1000); + var delta = 10; + var decoded; + + // expects a token with no expiration time to assign 1 day + var inOneDay = now + (60 * 60 * 24); + var defaultExpireToken = this.opentok.generateToken(this.sessionId); + var oneHourToken; + var inOneHour; + var oneHourAgo; + + var fractionalExpireTime; + var roundedToken; + + expect(defaultExpireToken).to.be.a('string'); + expect(helpers.verifyTokenSignature(defaultExpireToken, apiSecret)).to.be.true; + decoded = helpers.decodeToken(defaultExpireToken); + expect(decoded.expire_time).to.be.within(inOneDay - delta, inOneDay + delta); + + // expects a token with an expiration time to have it + inOneHour = now + (60 * 60); + oneHourToken = this.opentok.generateToken(this.sessionId, { expireTime: inOneHour }); + expect(oneHourToken).to.be.a('string'); + expect(helpers.verifyTokenSignature(oneHourToken, apiSecret)).to.be.true; + decoded = helpers.decodeToken(oneHourToken); + expect(decoded.expire_time).to.be.within(inOneHour - delta, inOneHour + delta); + + // expects a token with an invalid expiration time to complain + expect(function () { + this.opentok.generateToken(this.sessionId, { expireTime: 'not a time' }); + }).to.throw(Error); - it('generates a token', function() { - // call generateToken with no options - var token = this.opentok.generateToken(this.sessionId); - expect(token).to.be.a('string'); - expect(helpers.verifyTokenSignature(token, apiSecret)).to.be.true - var decoded = helpers.decodeToken(token); - expect(decoded.partner_id).to.equal(apiKey); - expect(decoded.create_time).to.exist - expect(decoded.nonce).to.exist - }); + oneHourAgo = now - (60 * 60); + expect(function () { + this.opentok.generateToken(this.sessionId, { expireTime: oneHourAgo }); + }).to.throw(Error); - it('assigns a role in the token', function() { - // expects one with no role defined to assign "publisher" - var defaultRoleToken = this.opentok.generateToken(this.sessionId); - expect(defaultRoleToken).to.be.a('string'); - expect(helpers.verifyTokenSignature(defaultRoleToken, apiSecret)).to.be.true - var decoded = helpers.decodeToken(defaultRoleToken); - expect(decoded.role).to.equal('publisher'); - - // expects one with a valid role defined to set it - var subscriberToken = this.opentok.generateToken(this.sessionId, { role : 'subscriber' }); - expect(subscriberToken).to.be.a('string'); - expect(helpers.verifyTokenSignature(subscriberToken, apiSecret)).to.be.true - decoded = helpers.decodeToken(subscriberToken); - expect(decoded.role).to.equal('subscriber'); - - // expects one with an invalid role to complain - expect(function() { - this.opentok.generateToken(this.sessionId, { role : 5 }); - }).to.throw(Error); - }); + // rounds down fractional expiration time + fractionalExpireTime = now + (60.5); + roundedToken = this.opentok.generateToken(this.sessionId, { expireTime: fractionalExpireTime }); + expect(helpers.verifyTokenSignature(roundedToken, apiSecret)).to.be.true; + decoded = helpers.decodeToken(roundedToken); + expect(decoded.expire_time).to.equal(Math.round(fractionalExpireTime).toString()); + }); - it('sets an expiration time for the token', function() { - var now = Math.round((new Date().getTime()) / 1000); - var delta = 10; - var decoded; - - // expects a token with no expiration time to assign 1 day - var inOneDay = now + (60*60*24); - var defaultExpireToken = this.opentok.generateToken(this.sessionId); - expect(defaultExpireToken).to.be.a('string'); - expect(helpers.verifyTokenSignature(defaultExpireToken, apiSecret)).to.be.true - decoded = helpers.decodeToken(defaultExpireToken); - expect(decoded.expire_time).to.be.within(inOneDay-delta, inOneDay+delta); - - // expects a token with an expiration time to have it - var inOneHour = now + (60*60); - var oneHourToken = this.opentok.generateToken(this.sessionId, { expireTime: inOneHour }); - expect(oneHourToken).to.be.a('string'); - expect(helpers.verifyTokenSignature(oneHourToken, apiSecret)).to.be.true - decoded = helpers.decodeToken(oneHourToken); - expect(decoded.expire_time).to.be.within(inOneHour-delta, inOneHour+delta); - - // expects a token with an invalid expiration time to complain - expect(function() { - this.opentok.generateToken(this.sessionId, { expireTime: "not a time" }); - }).to.throw(Error); - - var oneHourAgo = now - (60*60); - expect(function() { - this.opentok.generateToken(this.sessionId, { expireTime: oneHourAgo }); - }).to.throw(Error); - - // rounds down fractional expiration time - var fractionalExpireTime = now + (60.5); - var roundedToken = this.opentok.generateToken(this.sessionId, { expireTime: fractionalExpireTime }); - expect(helpers.verifyTokenSignature(roundedToken, apiSecret)).to.be.true - decoded = helpers.decodeToken(roundedToken); - expect(decoded.expire_time).to.equal(Math.round(fractionalExpireTime).toString()); - }); + it('sets connection data in the token', function () { + // expects a token with a connection data to have it + var sampleData = 'name=Johnny'; + var decoded; + var dataBearingToken = this.opentok.generateToken(this.sessionId, { data: sampleData }); + expect(dataBearingToken).to.be.a('string'); + expect(helpers.verifyTokenSignature(dataBearingToken, apiSecret)).to.be.true; + decoded = helpers.decodeToken(dataBearingToken); + expect(decoded.connection_data).to.equal(sampleData); + + // expects a token with invalid connection data to complain + expect(function () { + this.opentok.generateToken(this.sessionId, { data: { dont: 'work' } }); + }).to.throw(Error); - it('sets connection data in the token', function() { - // expects a token with a connection data to have it - var sampleData = 'name=Johnny'; - var dataBearingToken = this.opentok.generateToken(this.sessionId, { data: sampleData }); - expect(dataBearingToken).to.be.a('string'); - expect(helpers.verifyTokenSignature(dataBearingToken, apiSecret)).to.be.true - var decoded = helpers.decodeToken(dataBearingToken); - expect(decoded.connection_data).to.equal(sampleData); - - // expects a token with invalid connection data to complain - expect(function() { - this.opentok.generateToken(this.sessionId, { data: { 'dont': 'work' } }); - }).to.throw(Error); - - expect(function() { - this.opentok.generateToken(this.sessionId, { - data: Array(2000).join("a") // 1999 char string of all 'a's - }); - }).to.throw(Error); - }); + expect(function () { + this.opentok.generateToken(this.sessionId, { + data: Array(2000).join('a') // 1999 char string of all 'a's + }); + }).to.throw(Error); + }); - it('complains if the sessionId is not valid', function() { - expect(function() { - this.opentok.generateToken(); - }).to.throw(Error); + it('complains if the sessionId is not valid', function () { + expect(function () { + this.opentok.generateToken(); + }).to.throw(Error); - expect(function() { - this.opentok.generateToken('blahblahblah'); - }).to.throw(Error); - }); + expect(function () { + this.opentok.generateToken('blahblahblah'); + }).to.throw(Error); + }); - it('contains a unique nonce', function() { - // generate a few and show the nonce exists each time and that they are different - var tokens = [ - this.opentok.generateToken(this.sessionId), - this.opentok.generateToken(this.sessionId) - ]; - var nonces = _.map(tokens, function(token) { return helpers.decodeToken(token).nonce; }); - expect(_.uniq(nonces)).to.have.length(nonces.length); - }); + it('contains a unique nonce', function () { + // generate a few and show the nonce exists each time and that they are different + var tokens = [ + this.opentok.generateToken(this.sessionId), + this.opentok.generateToken(this.sessionId) + ]; + var nonces = _.map(tokens, function (token) { return helpers.decodeToken(token).nonce; }); + expect(_.uniq(nonces)).to.have.length(nonces.length); + }); - it('does not modify passed in options', function() { - var options = { data: 'test' }; - var optionsUntouched = _.clone(options); - this.opentok.generateToken(this.sessionId, options); - expect(options).to.deep.equal(optionsUntouched); - }); + it('does not modify passed in options', function () { + var options = { data: 'test' }; + var optionsUntouched = _.clone(options); + this.opentok.generateToken(this.sessionId, options); + expect(options).to.deep.equal(optionsUntouched); }); +}); -describe('#dial', function() { +describe('#dial', function () { + beforeEach(function () { + this.opentok = new OpenTok(apiKey, apiSecret); + this.sessionId = sessionId; + this.token = this.opentok.generateToken(this.sessionId); + }); - beforeEach(function() { - this.opentok = new OpenTok(apiKey, apiSecret); - this.sessionId = sessionId; - this.token = this.opentok.generateToken(this.sessionId); + it('dials a SIP gateway and adds a stream', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/v2/project/123456/dial', { + sessionId: this.sessionId, + token: this.token, + sip: { + uri: goodSipUri + } + }) + .reply(200, { + id: 'CONFERENCEID', + connectionId: 'CONNECTIONID', + streamId: 'STREAMID' + }); + this.opentok.dial(this.sessionId, this.token, goodSipUri, function (err, sipCall) { + if (err) { + done(err); + return; + } + expect(sipCall).to.be.an.instanceof(SipInterconnect); + expect(sipCall.id).to.equal('CONFERENCEID'); + expect(sipCall.streamId).to.equal('STREAMID'); + expect(sipCall.connectionId).to.equal('CONNECTIONID'); + scope.done(); + done(err); }); + }); - it('dials a SIP gateway and adds a stream', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/v2/project/123456/dial', { - sessionId: this.sessionId, - token: this.token, - sip: { - uri: goodSipUri + it('dials a SIP gateway and adds a stream with custom headers', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/v2/project/123456/dial', { + sessionId: this.sessionId, + token: this.token, + sip: { + uri: goodSipUri, + headers: { + someKey: 'someValue' } - }) - .reply(200, { - id: 'CONFERENCEID', - connectionId: 'CONNECTIONID', - streamId: 'STREAMID' - }); - this.opentok.dial(this.sessionId, this.token, goodSipUri, function (err, sipCall) { - if (err) return done(err); + } + }) + .reply(200, { + id: 'CONFERENCEID', + connectionId: 'CONNECTIONID', + streamId: 'STREAMID' + }); + this.opentok.dial(this.sessionId, this.token, goodSipUri, { headers: { someKey: 'someValue' } }, + function (err, sipCall) { + if (err) { + done(err); + return; + } expect(sipCall).to.be.an.instanceof(SipInterconnect); expect(sipCall.id).to.equal('CONFERENCEID'); expect(sipCall.streamId).to.equal('STREAMID'); @@ -605,149 +689,122 @@ describe('#dial', function() { scope.done(); done(err); }); - }); - - it('dials a SIP gateway and adds a stream with custom headers', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/v2/project/123456/dial', { - sessionId: this.sessionId, - token: this.token, - sip: { - uri: goodSipUri, - headers: { - someKey: 'someValue' - } - } - }) - .reply(200, { - id: 'CONFERENCEID', - connectionId: 'CONNECTIONID', - streamId: 'STREAMID' - }); - this.opentok.dial(this.sessionId, this.token, goodSipUri, {headers: {someKey: 'someValue'}}, - function (err, sipCall) { - if (err) return done(err); - expect(sipCall).to.be.an.instanceof(SipInterconnect); - expect(sipCall.id).to.equal('CONFERENCEID'); - expect(sipCall.streamId).to.equal('STREAMID'); - expect(sipCall.connectionId).to.equal('CONNECTIONID'); - scope.done(); - done(err); - }); - }); + }); - it('dials a SIP gateway and adds a stream with authentication', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/v2/project/123456/dial', { - sessionId: this.sessionId, - token: this.token, - sip: { - uri: goodSipUri, - auth: { - username: 'someUsername', - password: 'somePassword' - } + it('dials a SIP gateway and adds a stream with authentication', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/v2/project/123456/dial', { + sessionId: this.sessionId, + token: this.token, + sip: { + uri: goodSipUri, + auth: { + username: 'someUsername', + password: 'somePassword' } - }) - .reply(200, { - id: 'CONFERENCEID', - connectionId: 'CONNECTIONID', - streamId: 'STREAMID', - }); - this.opentok.dial(this.sessionId, this.token, goodSipUri, {auth: { - username: 'someUsername', password: 'somePassword'} - }, - function (err, sipCall) { - if (err) return done(err); - expect(sipCall).to.be.an.instanceof(SipInterconnect); - expect(sipCall.id).to.equal('CONFERENCEID'); - expect(sipCall.streamId).to.equal('STREAMID'); - expect(sipCall.connectionId).to.equal('CONNECTIONID'); - scope.done(); + } + }) + .reply(200, { + id: 'CONFERENCEID', + connectionId: 'CONNECTIONID', + streamId: 'STREAMID' + }); + this.opentok.dial(this.sessionId, this.token, goodSipUri, { auth: { + username: 'someUsername', password: 'somePassword' } + }, + function (err, sipCall) { + if (err) { done(err); - }); - }); + return; + } + expect(sipCall).to.be.an.instanceof(SipInterconnect); + expect(sipCall.id).to.equal('CONFERENCEID'); + expect(sipCall.streamId).to.equal('STREAMID'); + expect(sipCall.connectionId).to.equal('CONNECTIONID'); + scope.done(); + done(err); + }); + }); - it('dials a SIP gateway and adds an encrypted media stream', function(done) { - var scope = nock('https://api.opentok.com:443') - .matchHeader('x-tb-partner-auth', apiKey+':'+apiSecret) - .matchHeader('user-agent', new RegExp("OpenTok-Node-SDK\/"+pkg.version)) - .post('/v2/project/123456/dial', { - sessionId: this.sessionId, - token: this.token, - sip: { - uri: goodSipUri, - secure: true - } - }) - .reply(200, { - id: 'CONFERENCEID', - connectionId: 'CONNECTIONID', - streamId: 'STREAMID', - }); - this.opentok.dial(this.sessionId, this.token, goodSipUri, {secure: true}, - function (err, sipCall) { - if (err) return done(err); - expect(sipCall).to.be.an.instanceof(SipInterconnect); - expect(sipCall.id).to.equal('CONFERENCEID'); - expect(sipCall.streamId).to.equal('STREAMID'); - expect(sipCall.connectionId).to.equal('CONNECTIONID'); - scope.done(); + it('dials a SIP gateway and adds an encrypted media stream', function (done) { + var scope = nock('https://api.opentok.com:443') + .matchHeader('x-tb-partner-auth', apiKey + ':' + apiSecret) + .matchHeader('user-agent', new RegExp('OpenTok-Node-SDK/' + pkg.version)) + .post('/v2/project/123456/dial', { + sessionId: this.sessionId, + token: this.token, + sip: { + uri: goodSipUri, + secure: true + } + }) + .reply(200, { + id: 'CONFERENCEID', + connectionId: 'CONNECTIONID', + streamId: 'STREAMID' + }); + this.opentok.dial(this.sessionId, this.token, goodSipUri, { secure: true }, + function (err, sipCall) { + if (err) { done(err); - }); - }); - - it('complains if sessionId, token, SIP URI, or callback are missing or invalid', function() { - // Missing all params - expect(function() { - this.opentok.dial(); - }).to.throw(Error); - // Bad sessionId - expect(function() { - this.opentok.dial('blahblahblah'); - }).to.throw(Error); - // Missing token - expect(function() { - this.opentok.dial(this.sessionId); - }).to.throw(Error); - // Bad token - expect(function() { - this.opentok.dial(this.sessionId, 'blahblahblah'); - }).to.throw(Error); - // Missing SIP URI - expect(function() { - this.opentok.dial(this.sessionId, this.token); - }).to.throw(Error); - // Bad SIP URI - expect(function() { - this.opentok.dial(this.sessionId, this.token, badSipUri); - }).to.throw(Error); - // Bad sessionId, working token and SIP URI - expect(function() { - this.opentok.dial('someWrongSessionId', this.token, goodSipUri); - }).to.throw(Error); - // Good sessionId, bad token and good SIP URI - expect(function() { - this.opentok.dial(this.sessionId, 'blahblahblah', goodSipUri); - }).to.throw(Error); - // Good sessionId, good token, good SIP URI, null options, missing callback func - expect(function() { - this.opentok.dial(this.sessionId, this.token, goodSipUri, null); - }).to.throw(Error); - }); + return; + } + expect(sipCall).to.be.an.instanceof(SipInterconnect); + expect(sipCall.id).to.equal('CONFERENCEID'); + expect(sipCall.streamId).to.equal('STREAMID'); + expect(sipCall.connectionId).to.equal('CONNECTIONID'); + scope.done(); + done(err); + }); + }); - it('does not modify passed in options', function() { - var options = { data: 'test' }; - var optionsUntouched = _.clone(options); - this.opentok.dial(this.sessionId, this.token, 'sip:testsipuri@tokbox.com', options, - function (err, sipInterconnect) { - expect(options).to.deep.equal(optionsUntouched); - }); - }); + it('complains if sessionId, token, SIP URI, or callback are missing or invalid', function () { + // Missing all params + expect(function () { + this.opentok.dial(); + }).to.throw(Error); + // Bad sessionId + expect(function () { + this.opentok.dial('blahblahblah'); + }).to.throw(Error); + // Missing token + expect(function () { + this.opentok.dial(this.sessionId); + }).to.throw(Error); + // Bad token + expect(function () { + this.opentok.dial(this.sessionId, 'blahblahblah'); + }).to.throw(Error); + // Missing SIP URI + expect(function () { + this.opentok.dial(this.sessionId, this.token); + }).to.throw(Error); + // Bad SIP URI + expect(function () { + this.opentok.dial(this.sessionId, this.token, badSipUri); + }).to.throw(Error); + // Bad sessionId, working token and SIP URI + expect(function () { + this.opentok.dial('someWrongSessionId', this.token, goodSipUri); + }).to.throw(Error); + // Good sessionId, bad token and good SIP URI + expect(function () { + this.opentok.dial(this.sessionId, 'blahblahblah', goodSipUri); + }).to.throw(Error); + // Good sessionId, good token, good SIP URI, null options, missing callback func + expect(function () { + this.opentok.dial(this.sessionId, this.token, goodSipUri, null); + }).to.throw(Error); }); + it('does not modify passed in options', function () { + var options = { data: 'test' }; + var optionsUntouched = _.clone(options); + this.opentok.dial(this.sessionId, this.token, 'sip:testsipuri@tokbox.com', options, + function () { + expect(options).to.deep.equal(optionsUntouched); + }); + }); }); From 189a6606ff77cc5d61b013dd9776af33260b8b65 Mon Sep 17 00:00:00 2001 From: Hashir Baqai Date: Thu, 17 Nov 2016 17:33:32 -0800 Subject: [PATCH 3/8] adding a basic round of linting --- .eslintignore | 4 + .eslintrc | 5 +- Gruntfile.js | 18 +- lib/archiving.js | 176 ++++++------ lib/client.js | 75 ++--- lib/errors.js | 20 +- lib/opentok.js | 100 ++++--- lib/session.js | 13 +- lib/sipInterconnect.js | 14 +- spec/opentok_spec.js | 602 +++++++++++++++++++++-------------------- test/helpers.js | 29 +- test/opentok-test.js | 4 +- test/session-test.js | 70 ++--- 13 files changed, 580 insertions(+), 550 deletions(-) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..aad4a48d --- /dev/null +++ b/.eslintignore @@ -0,0 +1,4 @@ +node_modules +bower_components +sample +hbtest.js diff --git a/.eslintrc b/.eslintrc index e032d2a4..197d667f 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,6 @@ { - "extends": "airbnb-base/legacy" + "extends": "airbnb-base/legacy", + "rules": { + "no-restricted-syntax": ["off", "ForInStatement"] + } } \ No newline at end of file diff --git a/Gruntfile.js b/Gruntfile.js index f99c0a10..2e7de07a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,5 +1,4 @@ -module.exports = function(grunt) { - +module.exports = function (grunt) { // Project configuration. grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), @@ -14,17 +13,17 @@ module.exports = function(grunt) { jasmine_node: { options: { extensions: 'js', - specNameMatcher: 'spec', + specNameMatcher: 'spec' }, all: ['spec/'] }, - jsdoc : { - dist : { - src: ['lib/*.js'], - options: { - destination: 'docs' - } + jsdoc: { + dist: { + src: ['lib/*.js'], + options: { + destination: 'docs' } + } } }); @@ -33,5 +32,4 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-jsdoc'); grunt.registerTask('default', ['mochaTest', 'jasmine_node']); - }; diff --git a/lib/archiving.js b/lib/archiving.js index 8f9a1054..539ff3bc 100644 --- a/lib/archiving.js +++ b/lib/archiving.js @@ -1,10 +1,13 @@ -/*global require, exports*/ -/*jshint strict:false, eqnull:true */ +/* global require, exports*/ +/* jshint strict:false, eqnull:true */ -var request = require('request'), - errors = require('./errors'), - pkg = require('../package.json'), - _ = require('lodash'); +var request = require('request'); +var errors = require('./errors'); +var pkg = require('../package.json'); +var _ = require('lodash'); + +// functions +var api; /** * An object representing an OpenTok archive. @@ -80,10 +83,10 @@ var request = require('request'), * See the {@link OpenTok#startArchive OpenTok.startArchive()} method. * * @property {String} url -* The download URL of the available MP4 file. This is only set for an archive with the status set to -* "available"; for other archives, (including archives with the status "uploaded") this property is -* set to null. The download URL is obfuscated, and the file is only available from the URL for -* 10 minutes. To generate a new URL, call the {@link OpenTok#getArchive OpenTok.getArchive()} or +* The download URL of the available MP4 file. This is only set for an archive with the status set +* to "available"; for other archives, (including archives with the status "uploaded") this +* property is set to null. The download URL is obfuscated, and the file is only available from the +* URL for 10 minutes. To generate a new URL, call the {@link OpenTok#getArchive OpenTok.getArchive()} or * {@link OpenTok#listArchives OpenTok.listArchives()} method. * * @see {@link OpenTok#deleteArchive OpenTok.deleteArchive()} @@ -95,13 +98,14 @@ var request = require('request'), * @class Archive */ function Archive(config, properties) { - var hasProp = {}.hasOwnProperty, - id = properties.id, - key; + var hasProp = {}.hasOwnProperty; + var id = properties.id; + var key; for (key in properties) { - if (!hasProp.call(properties, key)) continue; - this[key] = properties[key]; + if (hasProp.call(properties, key)) { + this[key] = properties[key]; + } } /** @@ -128,7 +132,7 @@ function Archive(config, properties) { * @method #stop * @memberof Archive */ - this.stop = function(callback) { + this.stop = function (callback) { exports.stopArchive(config, id, callback); }; @@ -146,14 +150,14 @@ function Archive(config, properties) { * @method #delete * @memberof Archive */ - this.delete = function(callback) { + this.delete = function (callback) { exports.deleteArchive(config, id, callback); }; } -var api = function(config, method, path, body, callback) { +api = function (config, method, path, body, callback) { var rurl = config.apiEndpoint + '/v2/partner/' + config.apiKey + path; - if ("defaults" in request) { + if ('defaults' in request) { request = request.defaults(_.pick(config, 'proxy', 'timeout')); } request({ @@ -168,52 +172,53 @@ var api = function(config, method, path, body, callback) { }, callback); }; -exports.listArchives = function(config, options, callback) { - if(typeof options == 'function') { +exports.listArchives = function (config, options, callback) { + var qs = []; + + if (typeof options === 'function') { callback = options; options = {}; } - if(typeof callback != 'function') { - throw(new errors.ArgumentError('No callback given to listArchives')); + if (typeof callback !== 'function') { + throw (new errors.ArgumentError('No callback given to listArchives')); } - var qs = []; - if(options.offset) { + if (options.offset) { qs.push('offset=' + parseInt(options.offset, 10)); } - if(options.count) { + if (options.count) { qs.push('count=' + parseInt(options.count, 10)); } - api(config, 'GET', '/archive?' + qs.join('&'), null, function(err, response, body) { - if(!err && body ) { + api(config, 'GET', '/archive?' + qs.join('&'), null, function (err, response, body) { + if (!err && body) { try { body = JSON.parse(body); } catch (_err) { err = _err; } } - if(err || response.statusCode != 200) { - if(response && response.statusCode == 403) { + if (err || response.statusCode !== 200) { + if (response && response.statusCode === 403) { callback(new errors.AuthError('Invalid API key or secret')); } else { callback(new errors.RequestError('Unexpected response from OpenTok')); } } else { - callback(null, body.items.map(function(item){ + callback(null, body.items.map(function (item) { return new Archive(config, item); }), body.count); } }); }; -exports.startArchive = function(config, sessionId, options, callback) { - if(typeof options == 'function') { +exports.startArchive = function (config, sessionId, options, callback) { + if (typeof options === 'function') { callback = options; options = {}; } - if(typeof callback != 'function') { - throw(new errors.ArgumentError('No callback given to startArchive')); + if (typeof callback !== 'function') { + throw (new errors.ArgumentError('No callback given to startArchive')); } - if(sessionId == null || sessionId.length === 0) { + if (sessionId == null || sessionId.length === 0) { callback(new errors.ArgumentError('No session ID given')); return; } @@ -223,18 +228,18 @@ exports.startArchive = function(config, sessionId, options, callback) { hasAudio: options.hasAudio, hasVideo: options.hasVideo, outputMode: options.outputMode - }, function(err, response, body) { - if(err) { + }, function startArchiveCallback(err, response, body) { + if (err) { callback(err); - } else if(response.statusCode != 200) { - if(response && response.statusCode == 404) { + } else if (response.statusCode !== 200) { + if (response && response.statusCode === 404) { callback(new errors.ArchiveError('Session not found')); - } else if(response && response.statusCode == 403) { + } else if (response && response.statusCode === 403) { callback(new errors.AuthError('Invalid API key or secret')); } else { callback(new errors.RequestError('Unexpected response from OpenTok')); } - } else if(body.status != 'started') { + } else if (body.status !== 'started') { callback(new errors.RequestError('Unexpected response from OpenTok')); } else { callback(null, new Archive(config, body)); @@ -242,55 +247,54 @@ exports.startArchive = function(config, sessionId, options, callback) { }); }; -exports.stopArchive = function(config, archiveId, callback) { - if(typeof callback != 'function') { - throw(new errors.ArgumentError('No callback given to stopArchive')); +exports.stopArchive = function (config, archiveId, callback) { + if (typeof callback !== 'function') { + throw (new errors.ArgumentError('No callback given to stopArchive')); } - if(archiveId == null || archiveId.length === 0) { + if (archiveId == null || archiveId.length === 0) { callback(new errors.ArgumentError('No archive ID given')); return; } api(config, 'POST', '/archive/' + encodeURIComponent(archiveId) + '/stop', {}, - function(err, response, body) { - if(err) { - callback(err); - } else if(response.statusCode != 200) { - if(response && response.statusCode == 404) { - callback(new errors.ArchiveError('Archive not found')); - } else if(response && response.statusCode == 409) { - callback(new errors.ArchiveError(body.message)); - } else if(response && response.statusCode == 403) { - callback(new errors.AuthError('Invalid API key or secret')); + function stopArchiveCallback(err, response, body) { + if (err) { + callback(err); + } else if (response.statusCode !== 200) { + if (response && response.statusCode === 404) { + callback(new errors.ArchiveError('Archive not found')); + } else if (response && response.statusCode === 409) { + callback(new errors.ArchiveError(body.message)); + } else if (response && response.statusCode === 403) { + callback(new errors.AuthError('Invalid API key or secret')); + } else { + callback(new errors.RequestError('Unexpected response from OpenTok')); + } } else { - - callback(new errors.RequestError('Unexpected response from OpenTok')); + callback(null, new Archive(config, body)); } - } else { - callback(null, new Archive(config, body)); - } - }); + }); }; -exports.getArchive = function(config, archiveId, callback) { - if(typeof callback != 'function') { - throw(new errors.ArgumentError('No callback given to getArchive')); +exports.getArchive = function (config, archiveId, callback) { + if (typeof callback !== 'function') { + throw (new errors.ArgumentError('No callback given to getArchive')); } - if(archiveId == null || archiveId.length === 0) { + if (archiveId == null || archiveId.length === 0) { callback(new errors.ArgumentError('No archive ID given')); return; } - api(config, 'GET', '/archive/' + archiveId, null, function(err, response, body) { - if(!err && body ) { + api(config, 'GET', '/archive/' + archiveId, null, function getArchiveCallback(err, response, body) { + if (!err && body) { try { body = JSON.parse(body); } catch (_err) { err = _err; } } - if(err || response.statusCode != 200) { - if(response && response.statusCode == 404) { + if (err || response.statusCode !== 200) { + if (response && response.statusCode === 404) { callback(new errors.ArchiveError('Archive not found')); - } else if(response && response.statusCode == 403) { + } else if (response && response.statusCode === 403) { callback(new errors.AuthError('Invalid API key or secret')); } else { callback(new errors.RequestError('Unexpected response from OpenTok')); @@ -301,26 +305,26 @@ exports.getArchive = function(config, archiveId, callback) { }); }; -exports.deleteArchive = function(config, archiveId, callback) { - if(typeof callback != 'function') { - throw(new errors.ArgumentError('No callback given to deleteArchive')); +exports.deleteArchive = function (config, archiveId, callback) { + if (typeof callback !== 'function') { + throw (new errors.ArgumentError('No callback given to deleteArchive')); } - if(archiveId == null || archiveId.length === 0) { + if (archiveId == null || archiveId.length === 0) { callback(new errors.ArgumentError('No archive ID given')); return; } api(config, 'DELETE', '/archive/' + encodeURIComponent(archiveId), null, - function(err, response) { - if(err || response.statusCode != 204) { - if(response && response.statusCode == 404) { - callback(new errors.ArchiveError('Archive not found')); - } else if(response && response.statusCode == 403) { - callback(new errors.AuthError('Invalid API key or secret')); + function deleteArchiveCallback(err, response) { + if (err || response.statusCode !== 204) { + if (response && response.statusCode === 404) { + callback(new errors.ArchiveError('Archive not found')); + } else if (response && response.statusCode === 403) { + callback(new errors.AuthError('Invalid API key or secret')); + } else { + callback(new errors.RequestError('Unexpected response from OpenTok')); + } } else { - callback(new errors.RequestError('Unexpected response from OpenTok')); + callback(null); } - } else { - callback(null); - } - }); + }); }; diff --git a/lib/client.js b/lib/client.js index 96f9a746..f07e5361 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,48 +1,49 @@ -var util = require('util'), - requestRoot = require('request'), - request = requestRoot, - _ = require('lodash'), - to_json = require('xmljson').to_json, - pkg = require('../package.json'), - defaultConfig = { - apiKey: null, - apiSecret: null, - apiUrl: 'https://api.opentok.com', - endpoints: { - createSession: '/session/create', - dial: '/v2/project/<%apiKey%>/dial' - }, - request: { - timeout: 20000 // 20 seconds - } - }; - -var Client = function(c) { +var requestRoot = require('request'); +var request = requestRoot; +var _ = require('lodash'); +var toJson = require('xmljson').to_json; +var pkg = require('../package.json'); +var defaultConfig = { + apiKey: null, + apiSecret: null, + apiUrl: 'https://api.opentok.com', + endpoints: { + createSession: '/session/create', + dial: '/v2/project/<%apiKey%>/dial' + }, + request: { + timeout: 20000 // 20 seconds + } +}; + +var Client = function (c) { this.c = {}; this.config(_.defaults(c, defaultConfig)); }; -Client.prototype.config = function(c) { +Client.prototype.config = function (c) { if (c.endpoints && c.endpoints.dial && c.apiKey) { c.endpoints.dial = c.endpoints.dial.replace(/<%apiKey%>/g, c.apiKey); } _.merge(this.c, c); - if ("request" in this.c) { + if ('request' in this.c) { request = requestRoot.defaults(this.c.request); } return this.c; }; -Client.prototype.createSession = function(opts, cb) { +Client.prototype.createSession = function (opts, cb) { request.post({ // TODO: only works while apiUrl is always up to the domain, without the ending slash url: this.c.apiUrl + this.c.endpoints.createSession, form: opts, headers: this._generateHeaders() - }, function(err, resp, body) { - if (err) return cb(new Error('The request failed: '+err)); + }, function (err, resp, body) { + if (err) { + return cb(new Error('The request failed: ' + err)); + } // handle client errors if (resp.statusCode === 403) { @@ -54,26 +55,26 @@ Client.prototype.createSession = function(opts, cb) { return cb(new Error('A server error occurred: (' + resp.statusCode + ') ' + body)); } - to_json(body, function(err, json) { - if (err) return cb(new Error('Could not parse XML: '+err)); + toJson(body, function (error, json) { + if (error) return cb(new Error('Could not parse XML: ' + error)); cb(null, json); }); }); }; -Client.prototype.startArchive = function() { +Client.prototype.startArchive = function () { }; -Client.prototype.stopArchive = function() { +Client.prototype.stopArchive = function () { }; -Client.prototype.getArchive = function() { +Client.prototype.getArchive = function () { }; -Client.prototype.listArchives = function() { +Client.prototype.listArchives = function () { }; -Client.prototype.deleteArchive = function() { +Client.prototype.deleteArchive = function () { }; Client.prototype.dial = function (opts, cb) { @@ -82,8 +83,8 @@ Client.prototype.dial = function (opts, cb) { url: this.c.apiUrl + this.c.endpoints.dial, json: opts, headers: this._generateHeaders() - }, function(err, resp, body) { - if (err) return cb(new Error('The request failed: '+err)); + }, function (err, resp, body) { + if (err) return cb(new Error('The request failed: ' + err)); // handle client errors if (resp.statusCode === 400) { return cb(new Error('Bad session ID, token, or SIP URI (sip:user@domain.tld)')); @@ -102,7 +103,7 @@ Client.prototype.dial = function (opts, cb) { return cb(new Error('A server error occurred: (' + resp.statusCode + ') ' + body)); } - //Parse data from server + // Parse data from server cb(null, body); }); }; @@ -111,8 +112,8 @@ Client.prototype._generateHeaders = function () { return { 'User-Agent': 'OpenTok-Node-SDK/' + pkg.version + (this.c.uaAddendum ? ' ' + this.c.uaAddendum : ''), - 'X-TB-PARTNER-AUTH': this.c.apiKey+':'+this.c.apiSecret + 'X-TB-PARTNER-AUTH': this.c.apiKey + ':' + this.c.apiSecret }; -} +}; module.exports = Client; diff --git a/lib/errors.js b/lib/errors.js index 66ecb208..77fabfd4 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -1,31 +1,31 @@ -exports.ArgumentError = function(message) { +exports.ArgumentError = function (message) { this.message = message; -} +}; exports.ArgumentError.prototype = Object.create(Error.prototype); -exports.AuthError = function(message) { +exports.AuthError = function (message) { this.message = message; -} +}; exports.AuthError.prototype = Object.create(Error.prototype); -exports.ArchiveError = function(message) { +exports.ArchiveError = function (message) { this.message = message; -} +}; -exports.SipError = function(message) { +exports.SipError = function (message) { this.message = message; -} +}; exports.ArchiveError.prototype = Object.create(Error.prototype); -exports.RequestError = function(message) { +exports.RequestError = function (message) { this.message = message; -} +}; exports.RequestError.prototype = Object.create(Error.prototype); diff --git a/lib/opentok.js b/lib/opentok.js index b382c22c..07f335e2 100644 --- a/lib/opentok.js +++ b/lib/opentok.js @@ -3,21 +3,14 @@ */ // Dependencies -var net = require('net'), - querystring = require('querystring'), - crypto = require('crypto'), - _ = require('lodash'), - request = require('request'), - pkg = require('../package.json'), - encodeToken = require('opentok-token'), - Client = require('./client'), - Session = require('./session'), - archiving = require('./archiving'), - SipInterconnect = require('./sipInterconnect'), - errors = require('./errors'); - -// Internal Constants -var TOKEN_SENTINEL = "T1=="; +var net = require('net'); +var _ = require('lodash'); +var encodeToken = require('opentok-token'); +var Client = require('./client'); +var Session = require('./session'); +var archiving = require('./archiving'); +var SipInterconnect = require('./sipInterconnect'); +var errors = require('./errors'); /** * Contains methods for creating OpenTok sessions, generating tokens, and working with archives. @@ -36,7 +29,7 @@ var TOKEN_SENTINEL = "T1=="; * @param apiSecret {String} Your OpenTok API secret. (See your * TokBox account page.) */ -var OpenTok = function(apiKey, apiSecret, env) { +var OpenTok = function OpenTok(apiKey, apiSecret, env) { // we're loose about calling this constructor with `new`, we got your back if (!(this instanceof OpenTok)) return new OpenTok(apiKey, apiSecret, env); @@ -301,14 +294,15 @@ var OpenTok = function(apiKey, apiSecret, env) { */ OpenTok.prototype.dial = function (sessionId, token, sipUri, options, callback) { var self = this; + var body; - if (typeof options == 'function') { + if (typeof options === 'function') { callback = options; options = {}; } options = options || {}; if (typeof callback !== 'function') { - throw(new errors.ArgumentError('No callback given to dial')); + throw (new errors.ArgumentError('No callback given to dial')); } if (sessionId == null || sessionId.length === 0) { callback(new errors.ArgumentError('No session ID given')); @@ -322,7 +316,7 @@ OpenTok.prototype.dial = function (sessionId, token, sipUri, options, callback) callback(new errors.ArgumentError('No SIP URI given')); return; } - var body = { + body = { sessionId: sessionId, token: token, sip: { @@ -331,15 +325,15 @@ OpenTok.prototype.dial = function (sessionId, token, sipUri, options, callback) }; if (_.isObject(options.headers) && !_.isArray(options.headers)) { body.sip.headers = options.headers; - }; + } if (_.isObject(options.auth) && !_.isArray(options.auth)) { body.sip.auth = options.auth; - }; - if (!!options.secure) { + } + if (options.secure) { body.sip.secure = !!options.secure; } - this._client.dial(body, function(err, json) { - if (err) return callback(new Error('Failed to dial endpoint. '+err)); + this._client.dial(body, function (err, json) { + if (err) return callback(new Error('Failed to dial endpoint. ' + err)); callback(null, new SipInterconnect(self, json)); }); }; @@ -426,8 +420,9 @@ OpenTok.prototype.dial = function (sessionId, token, sipUri, options, callback) * * */ -OpenTok.prototype.createSession = function(opts, callback) { - var backupOpts, self = this; +OpenTok.prototype.createSession = function (opts, callback) { + var backupOpts; + var self = this; if (_.isFunction(opts)) { // shift arguments if the opts is left out @@ -439,22 +434,22 @@ OpenTok.prototype.createSession = function(opts, callback) { } // whitelist the keys allowed - _.pick(_.defaults(opts, { "mediaMode" : "relayed", "archiveMode" : "manual" }), - "mediaMode", "archiveMode", "location"); - if ( opts.mediaMode !== "routed" && opts.mediaMode !== "relayed" ) { - opts.mediaMode = "relayed"; + _.pick(_.defaults(opts, { mediaMode: 'relayed', archiveMode: 'manual' }), + 'mediaMode', 'archiveMode', 'location'); + if (opts.mediaMode !== 'routed' && opts.mediaMode !== 'relayed') { + opts.mediaMode = 'relayed'; } - if ( opts.archiveMode !== "manual" && opts.archiveMode !== "always" ) { - opts.archiveMode = "manual"; + if (opts.archiveMode !== 'manual' && opts.archiveMode !== 'always') { + opts.archiveMode = 'manual'; } - if ( opts.archiveMode == "always" && opts.mediaMode != "routed" ) { - return process.nextTick(function() { + if (opts.archiveMode === 'always' && opts.mediaMode !== 'routed') { + return process.nextTick(function () { callback(new Error('A session with always archive mode must also have the routed media mode.')); }); } - if ( "location" in opts && !net.isIPv4(opts.location) ) { - return process.nextTick(function() { + if ('location' in opts && !net.isIPv4(opts.location)) { + return process.nextTick(function () { callback(new Error('Invalid arguments when calling createSession, location must be an ' + 'IPv4 address')); }); @@ -466,14 +461,14 @@ OpenTok.prototype.createSession = function(opts, callback) { // avoid mutating passed in options opts = _.clone(opts); var mediaModeToParam = { - routed : 'disabled', - relayed : 'enabled' + routed: 'disabled', + relayed: 'enabled' }; opts['p2p.preference'] = mediaModeToParam[opts.mediaMode]; delete opts.mediaMode; - this._client.createSession(opts, function(err, json) { - if (err) return callback(new Error('Failed to createSession. '+err)); + this._client.createSession(opts, function (err, json) { + if (err) return callback(new Error('Failed to createSession. ' + err)); callback(null, new Session(self, json.sessions.Session.session_id, backupOpts)); }); }; @@ -522,20 +517,22 @@ OpenTok.prototype.createSession = function(opts, callback) { * * @return The token string. */ -OpenTok.prototype.generateToken = function(sessionId, opts) { - var decoded, tokenData, now = Math.round(new Date().getTime() / 1000); +OpenTok.prototype.generateToken = function (sessionId, opts) { + var decoded; + var tokenData; + var now = Math.round(new Date().getTime() / 1000); if (!opts) opts = {}; // avoid mutating passed in options opts = _.clone(opts); - if ( !_.isString(sessionId) ) { + if (!_.isString(sessionId)) { throw new Error('Token cannot be generated without a sessionId parameter'); } // validate the sessionId belongs to the apiKey of this OpenTok instance decoded = decodeSessionId(sessionId); - if ( !decoded || decoded.apiKey !== this.apiKey) { + if (!decoded || decoded.apiKey !== this.apiKey) { throw new Error('Token cannot be generated unless the session belongs to the API Key'); } @@ -550,7 +547,7 @@ OpenTok.prototype.generateToken = function(sessionId, opts) { tokenData = _.pick(_.defaults(opts, { session_id: sessionId, create_time: now, - expire_time: now + (60*60*24), // 1 day + expire_time: now + (60 * 60 * 24), // 1 day nonce: Math.random(), role: 'publisher' }), 'session_id', 'create_time', 'nonce', 'role', 'expire_time', 'connection_data'); @@ -579,15 +576,15 @@ OpenTok.prototype.generateToken = function(sessionId, opts) { * @returns {?SessionInfo} sessionInfo */ function decodeSessionId(sessionId) { - var fields, sessionInfo; + var fields; // remove sentinal (e.g. '1_', '2_') sessionId = sessionId.substring(2); // replace invalid base64 chars - sessionId = sessionId.replace(/-/g, "+").replace(/_/g, "/"); + sessionId = sessionId.replace(/-/g, '+').replace(/_/g, '/'); // base64 decode - sessionId = new Buffer(sessionId, "base64").toString("ascii"); + sessionId = new Buffer(sessionId, 'base64').toString('ascii'); // separate fields - fields = sessionId.split("~"); + fields = sessionId.split('~'); return { apiKey: fields[1], location: fields[2], @@ -630,8 +627,9 @@ function decodeSessionId(sessionId) { module.exports = OpenTok; -for(var key in errors) { - if(errors.hasOwnProperty(key)) { +var key; +for (key in errors) { + if (errors.hasOwnProperty(key)) { OpenTok[key] = errors[key]; } } diff --git a/lib/session.js b/lib/session.js index 4362c2fa..e022008a 100644 --- a/lib/session.js +++ b/lib/session.js @@ -8,16 +8,19 @@ * @class Session */ -var Session = function(ot, sessionId, properties) { +var Session = function Session(ot, sessionId, properties) { + var prop; this.ot = ot; this.sessionId = sessionId; for (prop in properties) { - this[prop] = properties[prop]; + if ({}.hasOwnProperty.call(properties, prop)) { + this[prop] = properties[prop]; + } } -} +}; -Session.prototype.generateToken = function(opts) { +Session.prototype.generateToken = function generateToken(opts) { return this.ot.generateToken(this.sessionId, opts); -} +}; module.exports = Session; diff --git a/lib/sipInterconnect.js b/lib/sipInterconnect.js index 43c3a43f..5daf55f3 100644 --- a/lib/sipInterconnect.js +++ b/lib/sipInterconnect.js @@ -1,5 +1,5 @@ -/*global require, exports*/ -/*jshint strict:false, eqnull:true */ +/* global require, exports */ +/* jshint strict:false, eqnull:true */ /** * An object representing an OpenTok SIP call. @@ -22,13 +22,13 @@ * @class SipInterconnect */ function SipInterconnect(config, properties) { - var hasProp = {}.hasOwnProperty, - id = properties.id, - key; + var hasProp = {}.hasOwnProperty; + var key; for (key in properties) { - if (!hasProp.call(properties, key)) continue; - this[key] = properties[key]; + if (hasProp.call(properties, key)) { + this[key] = properties[key]; + } } /** diff --git a/spec/opentok_spec.js b/spec/opentok_spec.js index f2ff0a5f..cfb60c09 100644 --- a/spec/opentok_spec.js +++ b/spec/opentok_spec.js @@ -1,28 +1,30 @@ -/*global require, describe, it, expect, waitsFor, runs, Buffer, jasmine */ -/*jshint strict:false */ -var OpenTok = require('../lib/opentok'), - nock = require('nock'); +/* global require, describe, it, expect, waitsFor, runs, Buffer, jasmine */ +/* jshint strict:false */ +var OpenTok = require('../lib/opentok'); +var nock = require('nock'); -describe('Archiving', function() { +describe('Archiving', function () { var opentok = new OpenTok('APIKEY', 'APISECRET'); - describe('startArchive', function() { + describe('startArchive', function () { var session = '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg'; - it('should return an Archive', function(done) { - + it('should return an Archive', function (done) { nock('https://api.opentok.com:443') - .post('/v2/partner/APIKEY/archive', {'sessionId':'1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg','name':'Bob'}) - .reply(200, '{\n \"createdAt\" : 1391149936527,\n \"duration\" : 0,\n \"id\" : \"4072fe0f-d499-4f2f-8237-64f5a9d936f5\",\n \"name\" : \"Bob\",\n \"partnerId\" : \"APIKEY\",\n \"reason\" : \"\",\n \"sessionId\" : \"1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg\",\n \"size\" : 0,\n \"status\" : \"started\",\n \"url\" : null\n}', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:32:16 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.startArchive(session, { name: 'Bob' }, function(err, archive) { + .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', name: 'Bob' }) + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "url" : null\n}', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:32:16 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.startArchive(session, { name: 'Bob' }, function startArchiveCallback(err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); - if(archive) { + if (archive) { expect(archive.name).toBe('Bob'); expect(archive.status).toBe('started'); expect(archive.stop).not.toBeNull(); @@ -30,23 +32,24 @@ describe('Archiving', function() { } done(); }); - }); - it('should work without the options', function(done) { - + it('should work without the options', function (done) { nock('https://api.opentok.com:443') - .post('/v2/partner/APIKEY/archive', {'sessionId':'1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg'}) - .reply(200, '{\n \"createdAt\" : 1391149936527,\n \"duration\" : 0,\n \"id\" : \"4072fe0f-d499-4f2f-8237-64f5a9d936f5\",\n \"name\" : null,\n \"partnerId\" : \"APIKEY\",\n \"reason\" : \"\",\n \"sessionId\" : \"1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg\",\n \"size\" : 0,\n \"status\" : \"started\",\n \"url\" : null\n}', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:32:16 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.startArchive(session, function(err, archive) { + .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg' }) + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "url" : null\n}', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:32:16 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.startArchive(session, function startArchiveCallback(err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); - if(archive) { + if (archive) { expect(archive.name).toBe(null); expect(archive.status).toBe('started'); expect(archive.stop).not.toBeNull(); @@ -54,90 +57,92 @@ describe('Archiving', function() { } done(); }); - }); - it('should return an error if session is null', function(done) { - opentok.startArchive(null, {}, function(err) { + it('should return an error if session is null', function (done) { + opentok.startArchive(null, {}, function (err) { expect(err).not.toBeNull(); expect(err.message).toBe('No session ID given'); done(); }); }); - it('should return an error if session ID is invalid', function(done) { - + it('should return an error if session ID is invalid', function (done) { nock('https://api.opentok.com:443') - .post('/v2/partner/APIKEY/archive', {'sessionId':'invalidSessionIDIam'}) - .reply(404, '{ \"message\" : \"responseString\" }', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:37:25 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.startArchive('invalidSessionIDIam', {}, function(err) { + .post('/v2/partner/APIKEY/archive', { sessionId: 'invalidSessionIDIam' }) + .reply(404, '{ "message" : "responseString" }', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:37:25 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.startArchive('invalidSessionIDIam', {}, function startArchiveCallback(err) { expect(err).not.toBeNull(); expect(err.message).toBe('Session not found'); done(); }); - }); - it('should return an error if session is p2p or has no connections', function(done) { - + it('should return an error if session is p2p or has no connections', function (done) { nock('https://api.opentok.com:443') - .post('/v2/partner/APIKEY/archive', {'sessionId':'1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg'}) - .reply(404, '{ \"message\" : \"responseString\" }', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:46:22 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.startArchive(session, {}, function(err) { + .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg' }) + .reply(404, '{ "message" : "responseString" }', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:46:22 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' }); + + opentok.startArchive(session, {}, function startArchiveCallback(err) { expect(err).not.toBeNull(); expect(err.message).toBe('Session not found'); done(); }); - }); - it('should return an error if any other HTTP status is returned', function(done) { - + it('should return an error if any other HTTP status is returned', function (done) { nock('https://api.opentok.com:443') - .post('/v2/partner/APIKEY/archive', {'sessionId':'1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg'}) - .reply(500, '{ \"message\" : \"responseString\" }', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:46:22 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.startArchive(session, function(err) { + .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg' }) + .reply(500, '{ "message" : "responseString" }', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:46:22 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.startArchive(session, function startArchiveCallback(err) { expect(err).not.toBeNull(); expect(err.message).toBe('Unexpected response from OpenTok'); done(); }); - }); - it('should throw an error if no callback is provided', function() { - - expect(function() { + it('should throw an error if no callback is provided', function () { + expect(function () { opentok.startArchive(session); }).toThrow(new OpenTok.ArgumentError('No callback given to startArchive')); - }); - it('should be able to archive with hasVideo to false', function(done) { - + it('should be able to archive with hasVideo to false', function (done) { nock('https://api.opentok.com:443') - .post('/v2/partner/APIKEY/archive', {'sessionId':'1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', 'hasAudio': true, hasVideo: false}) - .reply(200, '{\n \"createdAt\" : 1391149936527,\n \"duration\" : 0,\n \"id\" : \"4072fe0f-d499-4f2f-8237-64f5a9d936f5\",\n \"name\" : null,\n \"partnerId\" : \"APIKEY\",\n \"reason\" : \"\",\n \"sessionId\" : \"1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg\",\n \"size\" : 0,\n \"status\" : \"started\",\n \"hasAudio\" : true,\n \"hasVideo\" : false,\n \"url\" : null\n}', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:32:16 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.startArchive(session, {hasAudio: true, hasVideo: false}, function(err, archive) { + .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', hasAudio: true, hasVideo: false }) + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "hasAudio" : true,\n "hasVideo" : false,\n "url" : null\n}', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:32:16 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.startArchive(session, { hasAudio: true, hasVideo: false }, + function startArchiveCallback(err, archive) { expect(err).toBeNull(); expect(archive.hasAudio).toEqual(true); expect(archive.hasVideo).toEqual(false); @@ -145,34 +150,38 @@ describe('Archiving', function() { }); }); - it('should be able to archive if outputMode is individual', function(done) { - + it('should be able to archive if outputMode is individual', function (done) { nock('https://api.opentok.com:443') - .post('/v2/partner/APIKEY/archive', {'sessionId':'1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', 'outputMode': 'individual' }) - .reply(200, '{\n \"createdAt\" : 1391149936527,\n \"duration\" : 0,\n \"id\" : \"4072fe0f-d499-4f2f-8237-64f5a9d936f5\",\n \"name\" : null,\n \"partnerId\" : \"APIKEY\",\n \"reason\" : \"\",\n \"sessionId\" : \"1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg\",\n \"size\" : 0,\n \"status\" : \"started\",\n \"hasAudio\" : true,\n \"hasVideo\" : true,\n \"outputMode\" : \"individual\",\n \"url\" : null\n}', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:32:16 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.startArchive(session, {outputMode: 'individual'}, function(err, archive) { + .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', outputMode: 'individual' }) + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "hasAudio" : true,\n "hasVideo" : true,\n "outputMode" : "individual",\n "url" : null\n}', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:32:16 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.startArchive(session, { outputMode: 'individual' }, function startArchiveCallback(err, archive) { expect(err).toBeNull(); expect(archive.outputMode).toEqual('individual'); done(); }); }); - it('should be able to archive if outputMode is composed', function(done) { - + it('should be able to archive if outputMode is composed', function (done) { nock('https://api.opentok.com:443') - .post('/v2/partner/APIKEY/archive', {'sessionId':'1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', 'outputMode': 'composed' }) - .reply(200, '{\n \"createdAt\" : 1391149936527,\n \"duration\" : 0,\n \"id\" : \"4072fe0f-d499-4f2f-8237-64f5a9d936f5\",\n \"name\" : null,\n \"partnerId\" : \"APIKEY\",\n \"reason\" : \"\",\n \"sessionId\" : \"1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg\",\n \"size\" : 0,\n \"status\" : \"started\",\n \"hasAudio\" : true,\n \"hasVideo\" : true,\n \"outputMode\" : \"composed\",\n \"url\" : null\n}', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:32:16 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.startArchive(session, {outputMode: 'composed'}, function(err, archive) { + .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', outputMode: 'composed' }) + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "hasAudio" : true,\n "hasVideo" : true,\n "outputMode" : "composed",\n "url" : null\n}', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:32:16 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.startArchive(session, { outputMode: 'composed' }, function startArchiveCallback(err, archive) { expect(err).toBeNull(); expect(archive.outputMode).toEqual('composed'); done(); @@ -180,22 +189,25 @@ describe('Archiving', function() { }); }); - describe('getArchive', function() { + describe('getArchive', function () { var archiveID = 'd4c27726-d965-4456-8b07-0cca1a4f4802'; - it('should return an archive', function(done) { + it('should return an archive', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/d4c27726-d965-4456-8b07-0cca1a4f4802') - .reply(200, '{\n \"createdAt\" : 1389986091000,\n \"duration\" : 300,\n \"id\" : \"d4c27726-d965-4456-8b07-0cca1a4f4802\",\n \"name\" : \"Bob\",\n \"partnerId\" : \"APIKEY\",\n \"reason\" : \"\",\n \"sessionId\" : \"1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4\",\n \"size\" : 331266,\n \"status\" : \"available\",\n \"url\" : \"http://tokbox.com.archive2.s3.amazonaws.com/APIKEY%2Fd4c27726-d965-4456-8b07-0cca1a4f4802%2Farchive.mp4?Expires=1391151552&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=IiFhiMDUyvP6Q6EChXioePxvp6g%3D\"\n}', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:49:12 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.getArchive(archiveID, function(err, archive) { + .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "available",\n "url" : "http://tokbox.com.archive2.s3.amazonaws.com/APIKEY%2Fd4c27726-d965-4456-8b07-0cca1a4f4802%2Farchive.mp4?Expires=1391151552&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=IiFhiMDUyvP6Q6EChXioePxvp6g%3D"\n}', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:49:12 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.getArchive(archiveID, function (err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); - if(archive) { + if (archive) { expect(archive.name).toBe('Bob'); expect(archive.status).toBe('available'); expect(archive.stop).not.toBeNull(); @@ -203,136 +215,139 @@ describe('Archiving', function() { } done(); }); - }); - it('should allow archives with paused status', function(done) { + it('should allow archives with paused status', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/d4c27726-d965-4456-8b07-0cca1a4f4802') - .reply(200, '{\n \"createdAt\" : 1389986091000,\n \"duration\" : 300,\n \"id\" : \"d4c27726-d965-4456-8b07-0cca1a4f4802\",\n \"name\" : \"Bob\",\n \"partnerId\" : \"APIKEY\",\n \"reason\" : \"\",\n \"sessionId\" : \"1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4\",\n \"size\" : 331266,\n \"status\" : \"paused\",\n \"url\" : \"http://tokbox.com.archive2.s3.amazonaws.com/APIKEY%2Fd4c27726-d965-4456-8b07-0cca1a4f4802%2Farchive.mp4?Expires=1391151552&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=IiFhiMDUyvP6Q6EChXioePxvp6g%3D\"\n}', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:49:12 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.getArchive(archiveID, function(err, archive) { + .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "paused",\n "url" : "http://tokbox.com.archive2.s3.amazonaws.com/APIKEY%2Fd4c27726-d965-4456-8b07-0cca1a4f4802%2Farchive.mp4?Expires=1391151552&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=IiFhiMDUyvP6Q6EChXioePxvp6g%3D"\n}', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:49:12 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.getArchive(archiveID, function getArchiveCallback(err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); - if(archive) { + if (archive) { expect(archive.status).toBe('paused'); } done(); }); - }); - it('should return an expired archive', function(done) { + it('should return an expired archive', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/d4c27726-d965-4456-8b07-0cca1a4f4802') - .reply(200, '{\n \"createdAt\" : 1389986091000,\n \"duration\" : 300,\n \"id\" : \"d4c27726-d965-4456-8b07-0cca1a4f4802\",\n \"name\" : \"Bob\",\n \"partnerId\" : \"APIKEY\",\n \"reason\" : \"\",\n \"sessionId\" : \"1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4\",\n \"size\" : 331266,\n \"status\" : \"expired\",\n \"url\" : null\n}', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:49:12 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.getArchive(archiveID, function(err, archive) { + .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "expired",\n "url" : null\n}', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:49:12 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.getArchive(archiveID, function getArchiveCallback(err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); - if(archive) { + if (archive) { expect(archive.status).toBe('expired'); } done(); }); - }); - it('should return archives with unknown properties', function(done) { + it('should return archives with unknown properties', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/d4c27726-d965-4456-8b07-0cca1a4f4802') - .reply(200, '{\n \"createdAt\" : 1389986091000,\n \"duration\" : 300,\n \"id\" : \"d4c27726-d965-4456-8b07-0cca1a4f4802\",\n \"name\" : \"Bob\",\n \"partnerId\" : \"APIKEY\",\n \"reason\" : \"\",\n \"sessionId\" : \"1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4\",\n \"size\" : 331266,\n \"status\" : \"expired\",\n \"url\" : null,\n \"notarealproperty\" : \"not a real value\"\n}', { server: 'nginx', - date: 'Fri, 31 Jan 2014 06:49:12 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.getArchive(archiveID, function(err, archive) { + .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "expired",\n "url" : null,\n "notarealproperty" : "not a real value"\n}', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:49:12 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.getArchive(archiveID, function getArchiveCallback(err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); done(); }); - }); - it('should return an error if archive ID is null', function(done) { - - opentok.getArchive(null, function(err) { + it('should return an error if archive ID is null', function (done) { + opentok.getArchive(null, function getArchiveCallback(err) { expect(err).not.toBeNull(); expect(err.message).toBe('No archive ID given'); done(); }); - }); - it('should return an error if archive ID is invalid', function(done) { - + it('should return an error if archive ID is invalid', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/01614A9B-6BB3-4691-846E-F8A59555AD21') - .reply(404, '{ \"message\" : \"null\" }', { server: 'nginx', - date: 'Mon, 03 Feb 2014 23:30:54 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.getArchive('01614A9B-6BB3-4691-846E-F8A59555AD21', function(err) { + .reply(404, '{ "message" : "null" }', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:49:12 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.getArchive('01614A9B-6BB3-4691-846E-F8A59555AD21', function getArchiveCallback(err) { expect(err).not.toBeNull(); expect(err.message).toBe('Archive not found'); done(); }); - }); - it('should return an error if any other HTTP status is returned', function(done) { + it('should return an error if any other HTTP status is returned', function (done) { // nock.recorder.rec(); - nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/01614A9B-6BB3-4691-846E-F8A59555AD21') - .reply(500, '{ \"message\" : \"Something went wrong\" }', { server: 'nginx', - date: 'Mon, 03 Feb 2014 23:30:54 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.getArchive('01614A9B-6BB3-4691-846E-F8A59555AD21', function(err) { + .reply(500, '{ "message" : "Something went wrong" }', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:49:12 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.getArchive('01614A9B-6BB3-4691-846E-F8A59555AD21', function (err) { expect(err).not.toBeNull(); expect(err.message).toBe('Unexpected response from OpenTok'); done(); }); - }); - it('should throw an error if no callback is provided', function() { - - expect(function() { + it('should throw an error if no callback is provided', function () { + expect(function () { opentok.getArchive(archiveID); }).toThrow(new OpenTok.ArgumentError('No callback given to getArchive')); - }); - }); - describe('listArchives', function() { - - it('should return an array of archives and a total count', function(done) { - + describe('listArchives', function () { + it('should return an array of archives and a total count', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive?count=5') - .reply(200, '{\n "count" : 149,\n "items" : [ {\n "createdAt" : 1391457926000,\n "duration" : 3,\n "id" : "16231874-7ce7-4f5e-a30a-4513c4df480d",\n "name" : "",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 6590,\n "status" : "available",\n "url" : "http://some/video1.mp4"\n }, {\n "createdAt" : 1391218315000,\n "duration" : 0,\n "id" : "0931d1d7-4198-4db2-bf8a-097924421eb2",\n "name" : "Archive 3",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 3150,\n "status" : "available",\n "url" : "http://some/video2.mp4"\n }, {\n "createdAt" : 1391218274000,\n "duration" : 9,\n "id" : "e7198f93-d8fa-448d-b134-ac3355ce2eb7",\n "name" : "Archive 4",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 12691,\n "status" : "available",\n "url" : "http://some/video3.mp4"\n }, {\n "createdAt" : 1391218252000,\n "duration" : 17,\n "id" : "ae531f74-218c-4abd-bbe4-1f6bd92e9449",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 21566,\n "status" : "available",\n "url" : "http://some/video4.mp4"\n }, {\n "createdAt" : 1391218139000,\n "duration" : 73,\n "id" : "cf2fd890-7ea0-4f43-a6a7-432ea9dc4c51",\n "name" : "Archive 5",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 83158,\n "status" : "available",\n "url" : "http://some/video5.mp4"\n } ]\n}', { server: 'nginx', - date: 'Mon, 03 Feb 2014 23:38:53 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.listArchives({ count: 5 }, function(err, archives, total) { + .reply(200, '{\n "count" : 149,\n "items" : [ {\n "createdAt" : 1391457926000,\n "duration" : 3,\n "id" : "16231874-7ce7-4f5e-a30a-4513c4df480d",\n "name" : "",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 6590,\n "status" : "available",\n "url" : "http://some/video1.mp4"\n }, {\n "createdAt" : 1391218315000,\n "duration" : 0,\n "id" : "0931d1d7-4198-4db2-bf8a-097924421eb2",\n "name" : "Archive 3",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 3150,\n "status" : "available",\n "url" : "http://some/video2.mp4"\n }, {\n "createdAt" : 1391218274000,\n "duration" : 9,\n "id" : "e7198f93-d8fa-448d-b134-ac3355ce2eb7",\n "name" : "Archive 4",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 12691,\n "status" : "available",\n "url" : "http://some/video3.mp4"\n }, {\n "createdAt" : 1391218252000,\n "duration" : 17,\n "id" : "ae531f74-218c-4abd-bbe4-1f6bd92e9449",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 21566,\n "status" : "available",\n "url" : "http://some/video4.mp4"\n }, {\n "createdAt" : 1391218139000,\n "duration" : 73,\n "id" : "cf2fd890-7ea0-4f43-a6a7-432ea9dc4c51",\n "name" : "Archive 5",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 83158,\n "status" : "available",\n "url" : "http://some/video5.mp4"\n } ]\n}', + { + server: 'nginx', + date: 'Mon, 03 Feb 2014 23:38:53 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.listArchives({ count: 5 }, function (err, archives, total) { expect(err).toBeNull(); expect(total).toBe(149); expect(archives).toEqual(jasmine.any(Array)); @@ -347,20 +362,21 @@ describe('Archiving', function() { expect(archives[0].url).toBe('http://some/video1.mp4'); done(); }); - }); - it('should allow options to be optional', function(done) { - + it('should allow options to be optional', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive?') - .reply(200, '{\n "count" : 149,\n "items" : [ {\n "createdAt" : 1391457926000,\n "duration" : 3,\n "id" : "16231874-7ce7-4f5e-a30a-4513c4df480d",\n "name" : "",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 6590,\n "status" : "available",\n "url" : "http://some/video1.mp4"\n }, {\n "createdAt" : 1391218315000,\n "duration" : 0,\n "id" : "0931d1d7-4198-4db2-bf8a-097924421eb2",\n "name" : "Archive 3",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 3150,\n "status" : "available",\n "url" : "http://some/video2.mp4"\n }, {\n "createdAt" : 1391218274000,\n "duration" : 9,\n "id" : "e7198f93-d8fa-448d-b134-ac3355ce2eb7",\n "name" : "Archive 4",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 12691,\n "status" : "available",\n "url" : "http://some/video3.mp4"\n }, {\n "createdAt" : 1391218252000,\n "duration" : 17,\n "id" : "ae531f74-218c-4abd-bbe4-1f6bd92e9449",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 21566,\n "status" : "available",\n "url" : "http://some/video4.mp4"\n }, {\n "createdAt" : 1391218139000,\n "duration" : 73,\n "id" : "cf2fd890-7ea0-4f43-a6a7-432ea9dc4c51",\n "name" : "Archive 5",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 83158,\n "status" : "available",\n "url" : "http://some/video5.mp4"\n } ]\n}', { server: 'nginx', - date: 'Mon, 03 Feb 2014 23:38:53 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.listArchives(function(err, archives, total) { + .reply(200, '{\n "count" : 149,\n "items" : [ {\n "createdAt" : 1391457926000,\n "duration" : 3,\n "id" : "16231874-7ce7-4f5e-a30a-4513c4df480d",\n "name" : "",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 6590,\n "status" : "available",\n "url" : "http://some/video1.mp4"\n }, {\n "createdAt" : 1391218315000,\n "duration" : 0,\n "id" : "0931d1d7-4198-4db2-bf8a-097924421eb2",\n "name" : "Archive 3",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 3150,\n "status" : "available",\n "url" : "http://some/video2.mp4"\n }, {\n "createdAt" : 1391218274000,\n "duration" : 9,\n "id" : "e7198f93-d8fa-448d-b134-ac3355ce2eb7",\n "name" : "Archive 4",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 12691,\n "status" : "available",\n "url" : "http://some/video3.mp4"\n }, {\n "createdAt" : 1391218252000,\n "duration" : 17,\n "id" : "ae531f74-218c-4abd-bbe4-1f6bd92e9449",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 21566,\n "status" : "available",\n "url" : "http://some/video4.mp4"\n }, {\n "createdAt" : 1391218139000,\n "duration" : 73,\n "id" : "cf2fd890-7ea0-4f43-a6a7-432ea9dc4c51",\n "name" : "Archive 5",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 83158,\n "status" : "available",\n "url" : "http://some/video5.mp4"\n } ]\n}', + { + server: 'nginx', + date: 'Mon, 03 Feb 2014 23:38:53 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.listArchives(function (err, archives, total) { expect(err).toBeNull(); expect(total).toBe(149); expect(archives).toEqual(jasmine.any(Array)); @@ -375,19 +391,21 @@ describe('Archiving', function() { expect(archives[0].url).toBe('http://some/video1.mp4'); done(); }); - }); - it('should return an error if any other HTTP status is returned', function(done) { + it('should return an error if any other HTTP status is returned', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive?count=5') - .reply(500, '{\"message\":\"Some error\"}', { server: 'nginx', - date: 'Mon, 03 Feb 2014 23:38:53 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.listArchives({ count: 5 }, function(err, archives, total) { + .reply(500, '{"message":"Some error"}', + { + server: 'nginx', + date: 'Mon, 03 Feb 2014 23:38:53 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.listArchives({ count: 5 }, function (err, archives, total) { expect(archives).toBeUndefined(); expect(total).toBeUndefined(); expect(err).not.toBeNull(); @@ -396,30 +414,29 @@ describe('Archiving', function() { }); }); - it('should throw an error if no callback is provided', function() { - - expect(function() { + it('should throw an error if no callback is provided', function () { + expect(function () { opentok.listArchives(); }).toThrow(new OpenTok.ArgumentError('No callback given to listArchives')); - }); - }); - describe('stopArchive', function() { - - it('should return an archive', function(done) { + describe('stopArchive', function () { + it('should return an archive', function (done) { var archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive/ca138a6c-380f-4de9-b2b2-bc78b3a117e2/stop', {}) - .reply(200, '{\n \"createdAt\" : 1391471703000,\n \"duration\" : 0,\n \"id\" : \"ca138a6c-380f-4de9-b2b2-bc78b3a117e2\",\n \"name\" : \"PHP Archiving Sample App\",\n \"partnerId\" : \"APIKEY\",\n \"reason\" : \"\",\n \"sessionId\" : \"1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg\",\n \"size\" : 0,\n \"status\" : \"stopped\",\n \"url\" : null\n}', { server: 'nginx', - date: 'Mon, 03 Feb 2014 23:56:29 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.stopArchive(archiveId, function(err, archive) { + .reply(200, '{\n "createdAt" : 1391471703000,\n "duration" : 0,\n "id" : "ca138a6c-380f-4de9-b2b2-bc78b3a117e2",\n "name" : "PHP Archiving Sample App",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "stopped",\n "url" : null\n}', + { + server: 'nginx', + date: 'Mon, 03 Feb 2014 23:56:29 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.stopArchive(archiveId, function stopArchiveCallback(err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); expect(archive.status).toBe('stopped'); @@ -427,26 +444,28 @@ describe('Archiving', function() { }); }); - it('should return an error if archive ID is null', function(done) { - opentok.stopArchive(null, function(err, archive) { + it('should return an error if archive ID is null', function (done) { + opentok.stopArchive(null, function (err, archive) { expect(archive).toBeUndefined(); expect(err).not.toBeNull(); expect(err.message).toBe('No archive ID given'); done(); }); - }); - it('should return an error if archive ID is invalid', function(done) { + it('should return an error if archive ID is invalid', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive/AN-INVALID-ARCHIVE-ID/stop', {}) - .reply(404, '{ \"message\" : \"Not found. You passed in an invalid archive ID.\" }', { server: 'nginx', - date: 'Tue, 04 Feb 2014 00:51:02 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.stopArchive('AN-INVALID-ARCHIVE-ID', function(err, archive) { + .reply(404, '{ "message" : "Not found. You passed in an invalid archive ID." }', + { + server: 'nginx', + date: 'Tue, 04 Feb 2014 00:51:02 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.stopArchive('AN-INVALID-ARCHIVE-ID', function stopArchiveCallback(err, archive) { expect(archive).toBeUndefined(); expect(err).not.toBeNull(); expect(err.message).toBe('Archive not found'); @@ -454,127 +473,128 @@ describe('Archiving', function() { }); }); - it('should return an error if the archive is not currently started', function(done) { - + it('should return an error if the archive is not currently started', function (done) { + var archiveId; nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive/ca138a6c-380f-4de9-b2b2-bc78b3a117e2/stop', {}) - .reply(409, '{ \"message\" : \"Conflict. You are trying to stop an archive that is not recording.\" }', { server: 'nginx', - date: 'Tue, 04 Feb 2014 00:52:38 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - var archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; - opentok.stopArchive(archiveId, function(err, archive) { + .reply(409, '{ "message" : "Conflict. You are trying to stop an archive that is not recording." }', + { + server: 'nginx', + date: 'Fri, 31 Jan 2014 06:49:12 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; + opentok.stopArchive(archiveId, function stopArchiveCallback(err, archive) { expect(archive).toBeUndefined(); expect(err).not.toBeNull(); expect(err.message).toBe('Conflict. You are trying to stop an archive that is not recording.'); done(); }); - }); - it('should return an error if any other HTTP status is returned', function(done) { - + it('should return an error if any other HTTP status is returned', function (done) { + var archiveId; nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive/ca138a6c-380f-4de9-b2b2-bc78b3a117e2/stop', {}) - .reply(500, '{ \"message\" : \"Some other error.\" }', { server: 'nginx', - date: 'Tue, 04 Feb 2014 00:52:38 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - var archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; - opentok.stopArchive(archiveId, function(err, archive) { + .reply(500, '{ "message" : "Some other error." }', + { + server: 'nginx', + date: 'Tue, 04 Feb 2014 00:52:38 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; + opentok.stopArchive(archiveId, function stopArchiveCallback(err, archive) { expect(archive).toBeUndefined(); expect(err).not.toBeNull(); expect(err.message).toBe('Unexpected response from OpenTok'); done(); }); - }); - it('should throw an error if no callback is provided', function() { - - expect(function() { + it('should throw an error if no callback is provided', function () { + expect(function () { opentok.stopArchive('ca138a6c-380f-4de9-b2b2-bc78b3a117e2'); }).toThrow(new OpenTok.ArgumentError('No callback given to stopArchive')); - }); - }); - describe('deleteArchive', function() { - - it('should return no error on success', function(done) { - + describe('deleteArchive', function () { + it('should return no error on success', function (done) { + var archiveId; nock('https://api.opentok.com:443') .delete('/v2/partner/APIKEY/archive/ca138a6c-380f-4de9-b2b2-bc78b3a117e2') - .reply(204, '', { server: 'nginx', - date: 'Tue, 04 Feb 2014 01:05:40 GMT', - connection: 'keep-alive' }); - - var archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; - opentok.deleteArchive(archiveId, function(err) { + .reply(204, '', + { + server: 'nginx', + date: 'Tue, 04 Feb 2014 01:05:40 GMT', + connection: 'keep-alive' + }); + + archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; + opentok.deleteArchive(archiveId, function deleteArchiveCallback(err) { expect(err).toBeNull(); done(); }); - }); - it('should return an error if archive ID is null', function(done) { - opentok.deleteArchive(null, function(err) { + it('should return an error if archive ID is null', function (done) { + opentok.deleteArchive(null, function deleteArchiveCallback(err) { expect(err).not.toBeNull(); expect(err.message).toBe('No archive ID given'); done(); }); }); - it('should return an error if archive ID is invalid', function(done) { - + it('should return an error if archive ID is invalid', function (done) { nock('https://api.opentok.com:443') .delete('/v2/partner/APIKEY/archive/AN-INVALID-ARCHIVE-ID') - .reply(404, '{ \"message\" : \"Not found. You passed in an invalid archive ID.\" }', { server: 'nginx', - date: 'Tue, 04 Feb 2014 01:10:39 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - opentok.deleteArchive('AN-INVALID-ARCHIVE-ID', function(err) { + .reply(404, '{ "message" : "Not found. You passed in an invalid archive ID." }', + { + server: 'nginx', + date: 'Tue, 04 Feb 2014 01:10:39 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + opentok.deleteArchive('AN-INVALID-ARCHIVE-ID', function deleteArchiveCallback(err) { expect(err).not.toBeNull(); expect(err.message).toBe('Archive not found'); done(); }); - }); - it('should return an error if any other HTTP status is returned', function(done) { - + it('should return an error if any other HTTP status is returned', function (done) { + var archiveId; nock('https://api.opentok.com:443') .delete('/v2/partner/APIKEY/archive/ca138a6c-380f-4de9-b2b2-bc78b3a117e2') - .reply(500, '{ \"message\" : \"Some other error.\" }', { server: 'nginx', - date: 'Tue, 04 Feb 2014 00:52:38 GMT', - 'content-type': 'application/json', - 'transfer-encoding': 'chunked', - connection: 'keep-alive' }); - - var archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; - opentok.deleteArchive(archiveId, function(err) { + .reply(500, '{ "message" : "Some other error." }', + { + server: 'nginx', + date: 'Tue, 04 Feb 2014 00:52:38 GMT', + 'content-type': 'application/json', + 'transfer-encoding': 'chunked', + connection: 'keep-alive' + }); + + archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; + opentok.deleteArchive(archiveId, function deleteArchiveCallback(err) { expect(err).not.toBeNull(); expect(err.message).toBe('Unexpected response from OpenTok'); done(); }); - }); - it('should throw an error if no callback is provided', function() { - - expect(function() { + it('should throw an error if no callback is provided', function () { + expect(function () { opentok.deleteArchive('ca138a6c-380f-4de9-b2b2-bc78b3a117e2'); }).toThrow(new OpenTok.ArgumentError('No callback given to deleteArchive')); - }); - }); - }); diff --git a/test/helpers.js b/test/helpers.js index c176b12b..95814189 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -1,30 +1,29 @@ // Test Helpers -var qs = require('querystring'), - crypto = require('crypto'), - _ = require('lodash'); +var qs = require('querystring'); +var crypto = require('crypto'); +var _ = require('lodash'); -exports.decodeToken = function(token) { +function signString(unsigned, key) { + var hmac = crypto.createHmac('sha1', key); + hmac.update(unsigned); + return hmac.digest('hex'); +} + +exports.decodeToken = function (token) { var parsed = {}; var encoded = token.substring(4); // remove 'T1==' - var decoded = new Buffer(encoded, "base64").toString("ascii"); + var decoded = new Buffer(encoded, 'base64').toString('ascii'); var tokenParts = decoded.split(':'); - tokenParts.forEach(function(part) { + tokenParts.forEach(function (part) { _.merge(parsed, qs.parse(part)); }); return parsed; }; -exports.verifyTokenSignature = function(token, apiSecret) { +exports.verifyTokenSignature = function (token, apiSecret) { var encoded = token.substring(4); // remove 'T1==' - var decoded = new Buffer(encoded, "base64").toString("ascii"); + var decoded = new Buffer(encoded, 'base64').toString('ascii'); var tokenParts = decoded.split(':'); var sig = qs.parse(tokenParts[0]).sig; return signString(tokenParts[1], apiSecret) === sig; }; - -function signString(unsigned, key) { - var hmac = crypto.createHmac('sha1', key); - hmac.update(unsigned); - return hmac.digest('hex'); -} - diff --git a/test/opentok-test.js b/test/opentok-test.js index 660d86d3..260fd267 100644 --- a/test/opentok-test.js +++ b/test/opentok-test.js @@ -174,9 +174,7 @@ describe('when there is too much network latency', function () { 'access-control-allow-origin': '*', 'x-tb-host': 'mantis402-oak.tokbox.com', 'content-length': '274' }); - this.opentok.createSession(function (err, session) { - console.log('sessionId' , session); - console.log('err', err); + this.opentok.createSession(function (err) { expect(err).to.be.an.instanceof(Error); scope.done(); done(); diff --git a/test/session-test.js b/test/session-test.js index 86821b7c..9c7daf72 100644 --- a/test/session-test.js +++ b/test/session-test.js @@ -1,91 +1,93 @@ var expect = require('chai').expect; // Subject -var Session = require('../lib/session.js'), - OpenTok = require('../lib/opentok.js'); +var Session = require('../lib/session.js'); +var OpenTok = require('../lib/opentok.js'); // Fixtures -var apiKey = '123456', - apiSecret = '1234567890abcdef1234567890abcdef1234567890', - // This is specifically concocted for these tests (uses fake apiKey/apiSecret above) - sessionId = '1_MX4xMjM0NTZ-flNhdCBNYXIgMTUgMTQ6NDI6MjMgUERUIDIwMTR-MC40OTAxMzAyNX4'; +var apiKey = '123456'; +var apiSecret = '1234567890abcdef1234567890abcdef1234567890'; +// This is specifically concocted for these tests (uses fake apiKey/apiSecret above) +var sessionId = '1_MX4xMjM0NTZ-flNhdCBNYXIgMTUgMTQ6NDI6MjMgUERUIDIwMTR-MC40OTAxMzAyNX4'; -describe('Session', function() { - beforeEach(function() { +describe('Session', function () { + beforeEach(function () { this.opentok = new OpenTok(apiKey, apiSecret); }); - it('initializes with no options', function() { + it('initializes with no options', function () { var session = new Session(this.opentok, sessionId); - expect(session).to.be.an.instanceof(Session); - expect(session.sessionId).to.equal(sessionId); + expect(session).to.be.an.instanceof(Session); + expect(session.sessionId).to.equal(sessionId); }); - describe('when initialized with a media mode', function() { - it('has a mediaMode property', function() { - var session = new Session(this.opentok, sessionId, { mediaMode: "relayed" }); + describe('when initialized with a media mode', function () { + it('has a mediaMode property', function () { + var session = new Session(this.opentok, sessionId, { mediaMode: 'relayed' }); expect(session).to.be.an.instanceof(Session); - expect(session.mediaMode).to.equal("relayed"); - session = new Session(this.opentok, sessionId, { mediaMode: "routed" }); + expect(session.mediaMode).to.equal('relayed'); + session = new Session(this.opentok, sessionId, { mediaMode: 'routed' }); expect(session).to.be.an.instanceof(Session); - expect(session.mediaMode).to.equal("routed"); + expect(session.mediaMode).to.equal('routed'); }); - it('does not have a location property', function() { - var session = new Session(this.opentok, sessionId, { mediaMode: "relayed" }); + it('does not have a location property', function () { + var session = new Session(this.opentok, sessionId, { mediaMode: 'relayed' }); expect(session).to.be.an.instanceof(Session); - expect(session.location).to.not.exist + expect(session.location).to.not.exist; }); }); - describe('when initialized with just a location option', function() { - it('has a location property', function() { + describe('when initialized with just a location option', function () { + it('has a location property', function () { var session = new Session(this.opentok, sessionId, { location: '12.34.56.78' }); expect(session).to.be.an.instanceof(Session); expect(session.location).to.equal('12.34.56.78'); }); - it('does not have a mediaMode property', function() { + it('does not have a mediaMode property', function () { var session = new Session(this.opentok, sessionId, { location: '12.34.56.78' }); expect(session).to.be.an.instanceof(Session); - expect(session.mediaMode).to.not.exist + expect(session.mediaMode).to.not.exist; }); }); - describe('#generateToken', function() { - beforeEach(function() { + describe('#generateToken', function () { + beforeEach(function () { this.session = new Session(this.opentok, sessionId); }); // TODO: check all the invalid stuff - it('generates tokens', function() { + it('generates tokens', function () { var token = this.session.generateToken(); expect(token).to.be.a('string'); // TODO: decode and check its properties }); - it('assigns a role in the token', function() { + it('assigns a role in the token', function () { var token = this.session.generateToken(); expect(token).to.be.a('string'); // TODO: decode and check that its a publisher - token = this.session.generateToken({role:'subscriber'}); + token = this.session.generateToken({ role: 'subscriber' }); expect(token).to.be.a('string'); // TODO: decode and check that its a subscriber }); - it('assigns an expire time in the token', function() { - var token = this.session.generateToken(); + it('assigns an expire time in the token', function () { + var token; + var inAWhile; + + token = this.session.generateToken(); expect(token).to.be.a('string'); // TODO: decode and check that its expireTime is one day - var inAWhile =(new Date().getTime() / 1000) + (10); + inAWhile = (new Date().getTime() / 1000) + (10); token = this.session.generateToken({ expireTime: inAWhile }); expect(token).to.be.a('string'); // TODO: decode and check that the time is right }); - it('assigns an connection data to the token', function() { + it('assigns an connection data to the token', function () { var token = this.session.generateToken({ data: 'name=Johnny' }); expect(token).to.be.a('string'); // TODO: decode and check its data }); }); - }); From 3ca12a8a1f309ab0ad693c78be7e9b5be5435a13 Mon Sep 17 00:00:00 2001 From: Hashir Baqai Date: Thu, 17 Nov 2016 21:44:01 -0800 Subject: [PATCH 4/8] removing a local file from .eslintignore --- .eslintignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index aad4a48d..fb72de90 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,4 @@ node_modules bower_components sample -hbtest.js + From ffe0595d9d0e15794faef38cb5aa89aa323246f3 Mon Sep 17 00:00:00 2001 From: Hashir Baqai Date: Mon, 21 Nov 2016 18:47:05 -0800 Subject: [PATCH 5/8] undoing .only so that whole test suite runs --- test/opentok-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/opentok-test.js b/test/opentok-test.js index 260fd267..72de2233 100644 --- a/test/opentok-test.js +++ b/test/opentok-test.js @@ -159,7 +159,7 @@ describe('when there is too much network latency', function () { beforeEach(function () { this.opentok = new OpenTok(apiKey, apiSecret); }); - it.only('times out when the request takes longer than the default timeout', function (done) { + it('times out when the request takes longer than the default timeout', function (done) { // make sure the mocha test runner doesn't time out for at least as long as we are willing to // wait plus some reasonable amount of overhead time (100ms) var scope; From 72879681c3dc2d193de7bffe4a7b9f83b70138fe Mon Sep 17 00:00:00 2001 From: Hashir Baqai Date: Mon, 21 Nov 2016 19:04:40 -0800 Subject: [PATCH 6/8] Adding mocha explicitly since grunt-mocha-test dropped auto install after npm 3+ --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 9154dc0c..da52c102 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "grunt-jasmine-node": "^0.3.1", "grunt-jsdoc": "^2.1.0", "grunt-mocha-test": "^0.12.7", + "mocha": "^3.1.2", "nock": "^8.0.0" }, "dependencies": { From f394cdc7a9f9d6435e24349d12c5d7389725dfc9 Mon Sep 17 00:00:00 2001 From: Hashir Baqai Date: Tue, 22 Nov 2016 19:00:05 -0800 Subject: [PATCH 7/8] fixing errors in opentok_spec which were causing failures --- spec/opentok_spec.js | 226 +++++++++++++++---------------------------- 1 file changed, 79 insertions(+), 147 deletions(-) diff --git a/spec/opentok_spec.js b/spec/opentok_spec.js index cfb60c09..90616d43 100644 --- a/spec/opentok_spec.js +++ b/spec/opentok_spec.js @@ -12,7 +12,7 @@ describe('Archiving', function () { it('should return an Archive', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', name: 'Bob' }) - .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "url" : null\n}', + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "url" : null\n}', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:32:16 GMT', @@ -21,7 +21,7 @@ describe('Archiving', function () { connection: 'keep-alive' }); - opentok.startArchive(session, { name: 'Bob' }, function startArchiveCallback(err, archive) { + opentok.startArchive(session, { name: 'Bob' }, function (err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); if (archive) { @@ -37,16 +37,13 @@ describe('Archiving', function () { it('should work without the options', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg' }) - .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "url" : null\n}', - { - server: 'nginx', + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "url" : null\n}', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:32:16 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.startArchive(session, function startArchiveCallback(err, archive) { + opentok.startArchive(session, function (err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); if (archive) { @@ -70,16 +67,13 @@ describe('Archiving', function () { it('should return an error if session ID is invalid', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive', { sessionId: 'invalidSessionIDIam' }) - .reply(404, '{ "message" : "responseString" }', - { - server: 'nginx', + .reply(404, '{ "message" : "responseString" }', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:37:25 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.startArchive('invalidSessionIDIam', {}, function startArchiveCallback(err) { + opentok.startArchive('invalidSessionIDIam', {}, function (err) { expect(err).not.toBeNull(); expect(err.message).toBe('Session not found'); done(); @@ -89,15 +83,13 @@ describe('Archiving', function () { it('should return an error if session is p2p or has no connections', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg' }) - .reply(404, '{ "message" : "responseString" }', - { - server: 'nginx', + .reply(404, '{ "message" : "responseString" }', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:46:22 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', connection: 'keep-alive' }); - opentok.startArchive(session, {}, function startArchiveCallback(err) { + opentok.startArchive(session, {}, function (err) { expect(err).not.toBeNull(); expect(err.message).toBe('Session not found'); done(); @@ -107,16 +99,13 @@ describe('Archiving', function () { it('should return an error if any other HTTP status is returned', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg' }) - .reply(500, '{ "message" : "responseString" }', - { - server: 'nginx', + .reply(500, '{ "message" : "responseString" }', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:46:22 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.startArchive(session, function startArchiveCallback(err) { + opentok.startArchive(session, function (err) { expect(err).not.toBeNull(); expect(err.message).toBe('Unexpected response from OpenTok'); done(); @@ -132,17 +121,13 @@ describe('Archiving', function () { it('should be able to archive with hasVideo to false', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', hasAudio: true, hasVideo: false }) - .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "hasAudio" : true,\n "hasVideo" : false,\n "url" : null\n}', - { - server: 'nginx', + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "hasAudio" : true,\n "hasVideo" : false,\n "url" : null\n}', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:32:16 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.startArchive(session, { hasAudio: true, hasVideo: false }, - function startArchiveCallback(err, archive) { + opentok.startArchive(session, { hasAudio: true, hasVideo: false }, function (err, archive) { expect(err).toBeNull(); expect(archive.hasAudio).toEqual(true); expect(archive.hasVideo).toEqual(false); @@ -153,16 +138,13 @@ describe('Archiving', function () { it('should be able to archive if outputMode is individual', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', outputMode: 'individual' }) - .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "hasAudio" : true,\n "hasVideo" : true,\n "outputMode" : "individual",\n "url" : null\n}', - { - server: 'nginx', + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "hasAudio" : true,\n "hasVideo" : true,\n "outputMode" : "individual",\n "url" : null\n}', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:32:16 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.startArchive(session, { outputMode: 'individual' }, function startArchiveCallback(err, archive) { + opentok.startArchive(session, { outputMode: 'individual' }, function (err, archive) { expect(err).toBeNull(); expect(archive.outputMode).toEqual('individual'); done(); @@ -172,16 +154,13 @@ describe('Archiving', function () { it('should be able to archive if outputMode is composed', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', outputMode: 'composed' }) - .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "hasAudio" : true,\n "hasVideo" : true,\n "outputMode" : "composed",\n "url" : null\n}', - { - server: 'nginx', + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "hasAudio" : true,\n "hasVideo" : true,\n "outputMode" : "composed",\n "url" : null\n}', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:32:16 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.startArchive(session, { outputMode: 'composed' }, function startArchiveCallback(err, archive) { + opentok.startArchive(session, { outputMode: 'composed' }, function (err, archive) { expect(err).toBeNull(); expect(archive.outputMode).toEqual('composed'); done(); @@ -195,14 +174,11 @@ describe('Archiving', function () { it('should return an archive', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/d4c27726-d965-4456-8b07-0cca1a4f4802') - .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "available",\n "url" : "http://tokbox.com.archive2.s3.amazonaws.com/APIKEY%2Fd4c27726-d965-4456-8b07-0cca1a4f4802%2Farchive.mp4?Expires=1391151552&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=IiFhiMDUyvP6Q6EChXioePxvp6g%3D"\n}', - { - server: 'nginx', + .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "available",\n "url" : "http://tokbox.com.archive2.s3.amazonaws.com/APIKEY%2Fd4c27726-d965-4456-8b07-0cca1a4f4802%2Farchive.mp4?Expires=1391151552&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=IiFhiMDUyvP6Q6EChXioePxvp6g%3D"\n}', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:49:12 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); opentok.getArchive(archiveID, function (err, archive) { expect(err).toBeNull(); @@ -220,16 +196,13 @@ describe('Archiving', function () { it('should allow archives with paused status', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/d4c27726-d965-4456-8b07-0cca1a4f4802') - .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "paused",\n "url" : "http://tokbox.com.archive2.s3.amazonaws.com/APIKEY%2Fd4c27726-d965-4456-8b07-0cca1a4f4802%2Farchive.mp4?Expires=1391151552&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=IiFhiMDUyvP6Q6EChXioePxvp6g%3D"\n}', - { - server: 'nginx', + .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "paused",\n "url" : "http://tokbox.com.archive2.s3.amazonaws.com/APIKEY%2Fd4c27726-d965-4456-8b07-0cca1a4f4802%2Farchive.mp4?Expires=1391151552&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=IiFhiMDUyvP6Q6EChXioePxvp6g%3D"\n}', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:49:12 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.getArchive(archiveID, function getArchiveCallback(err, archive) { + opentok.getArchive(archiveID, function (err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); if (archive) { @@ -242,16 +215,13 @@ describe('Archiving', function () { it('should return an expired archive', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/d4c27726-d965-4456-8b07-0cca1a4f4802') - .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "expired",\n "url" : null\n}', - { - server: 'nginx', + .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "expired",\n "url" : null\n}', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:49:12 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.getArchive(archiveID, function getArchiveCallback(err, archive) { + opentok.getArchive(archiveID, function (err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); if (archive) { @@ -264,16 +234,13 @@ describe('Archiving', function () { it('should return archives with unknown properties', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/d4c27726-d965-4456-8b07-0cca1a4f4802') - .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "expired",\n "url" : null,\n "notarealproperty" : "not a real value"\n}', - { - server: 'nginx', + .reply(200, '{\n "createdAt" : 1389986091000,\n "duration" : 300,\n "id" : "d4c27726-d965-4456-8b07-0cca1a4f4802",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-fkZyaSBKYW4gMTcgMTE6MTQ6NTAgUFNUIDIwMTR-MC4xNTM4NDExNH4",\n "size" : 331266,\n "status" : "expired",\n "url" : null,\n "notarealproperty" : "not a real value"\n}', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:49:12 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.getArchive(archiveID, function getArchiveCallback(err, archive) { + opentok.getArchive(archiveID, function (err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); done(); @@ -281,7 +248,7 @@ describe('Archiving', function () { }); it('should return an error if archive ID is null', function (done) { - opentok.getArchive(null, function getArchiveCallback(err) { + opentok.getArchive(null, function (err) { expect(err).not.toBeNull(); expect(err.message).toBe('No archive ID given'); done(); @@ -291,16 +258,13 @@ describe('Archiving', function () { it('should return an error if archive ID is invalid', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/01614A9B-6BB3-4691-846E-F8A59555AD21') - .reply(404, '{ "message" : "null" }', - { - server: 'nginx', - date: 'Fri, 31 Jan 2014 06:49:12 GMT', + .reply(404, '{ "message" : "null" }', { server: 'nginx', + date: 'Mon, 03 Feb 2014 23:30:54 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.getArchive('01614A9B-6BB3-4691-846E-F8A59555AD21', function getArchiveCallback(err) { + opentok.getArchive('01614A9B-6BB3-4691-846E-F8A59555AD21', function (err) { expect(err).not.toBeNull(); expect(err.message).toBe('Archive not found'); done(); @@ -309,16 +273,14 @@ describe('Archiving', function () { it('should return an error if any other HTTP status is returned', function (done) { // nock.recorder.rec(); + nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive/01614A9B-6BB3-4691-846E-F8A59555AD21') - .reply(500, '{ "message" : "Something went wrong" }', - { - server: 'nginx', - date: 'Fri, 31 Jan 2014 06:49:12 GMT', + .reply(500, '{ "message" : "Something went wrong" }', { server: 'nginx', + date: 'Mon, 03 Feb 2014 23:30:54 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); opentok.getArchive('01614A9B-6BB3-4691-846E-F8A59555AD21', function (err) { expect(err).not.toBeNull(); @@ -338,14 +300,11 @@ describe('Archiving', function () { it('should return an array of archives and a total count', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive?count=5') - .reply(200, '{\n "count" : 149,\n "items" : [ {\n "createdAt" : 1391457926000,\n "duration" : 3,\n "id" : "16231874-7ce7-4f5e-a30a-4513c4df480d",\n "name" : "",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 6590,\n "status" : "available",\n "url" : "http://some/video1.mp4"\n }, {\n "createdAt" : 1391218315000,\n "duration" : 0,\n "id" : "0931d1d7-4198-4db2-bf8a-097924421eb2",\n "name" : "Archive 3",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 3150,\n "status" : "available",\n "url" : "http://some/video2.mp4"\n }, {\n "createdAt" : 1391218274000,\n "duration" : 9,\n "id" : "e7198f93-d8fa-448d-b134-ac3355ce2eb7",\n "name" : "Archive 4",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 12691,\n "status" : "available",\n "url" : "http://some/video3.mp4"\n }, {\n "createdAt" : 1391218252000,\n "duration" : 17,\n "id" : "ae531f74-218c-4abd-bbe4-1f6bd92e9449",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 21566,\n "status" : "available",\n "url" : "http://some/video4.mp4"\n }, {\n "createdAt" : 1391218139000,\n "duration" : 73,\n "id" : "cf2fd890-7ea0-4f43-a6a7-432ea9dc4c51",\n "name" : "Archive 5",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 83158,\n "status" : "available",\n "url" : "http://some/video5.mp4"\n } ]\n}', - { - server: 'nginx', + .reply(200, '{\n "count" : 149,\n "items" : [ {\n "createdAt" : 1391457926000,\n "duration" : 3,\n "id" : "16231874-7ce7-4f5e-a30a-4513c4df480d",\n "name" : "",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 6590,\n "status" : "available",\n "url" : "http://some/video1.mp4"\n }, {\n "createdAt" : 1391218315000,\n "duration" : 0,\n "id" : "0931d1d7-4198-4db2-bf8a-097924421eb2",\n "name" : "Archive 3",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 3150,\n "status" : "available",\n "url" : "http://some/video2.mp4"\n }, {\n "createdAt" : 1391218274000,\n "duration" : 9,\n "id" : "e7198f93-d8fa-448d-b134-ac3355ce2eb7",\n "name" : "Archive 4",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 12691,\n "status" : "available",\n "url" : "http://some/video3.mp4"\n }, {\n "createdAt" : 1391218252000,\n "duration" : 17,\n "id" : "ae531f74-218c-4abd-bbe4-1f6bd92e9449",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 21566,\n "status" : "available",\n "url" : "http://some/video4.mp4"\n }, {\n "createdAt" : 1391218139000,\n "duration" : 73,\n "id" : "cf2fd890-7ea0-4f43-a6a7-432ea9dc4c51",\n "name" : "Archive 5",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 83158,\n "status" : "available",\n "url" : "http://some/video5.mp4"\n } ]\n}', { server: 'nginx', date: 'Mon, 03 Feb 2014 23:38:53 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); opentok.listArchives({ count: 5 }, function (err, archives, total) { expect(err).toBeNull(); @@ -367,14 +326,11 @@ describe('Archiving', function () { it('should allow options to be optional', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive?') - .reply(200, '{\n "count" : 149,\n "items" : [ {\n "createdAt" : 1391457926000,\n "duration" : 3,\n "id" : "16231874-7ce7-4f5e-a30a-4513c4df480d",\n "name" : "",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 6590,\n "status" : "available",\n "url" : "http://some/video1.mp4"\n }, {\n "createdAt" : 1391218315000,\n "duration" : 0,\n "id" : "0931d1d7-4198-4db2-bf8a-097924421eb2",\n "name" : "Archive 3",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 3150,\n "status" : "available",\n "url" : "http://some/video2.mp4"\n }, {\n "createdAt" : 1391218274000,\n "duration" : 9,\n "id" : "e7198f93-d8fa-448d-b134-ac3355ce2eb7",\n "name" : "Archive 4",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 12691,\n "status" : "available",\n "url" : "http://some/video3.mp4"\n }, {\n "createdAt" : 1391218252000,\n "duration" : 17,\n "id" : "ae531f74-218c-4abd-bbe4-1f6bd92e9449",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 21566,\n "status" : "available",\n "url" : "http://some/video4.mp4"\n }, {\n "createdAt" : 1391218139000,\n "duration" : 73,\n "id" : "cf2fd890-7ea0-4f43-a6a7-432ea9dc4c51",\n "name" : "Archive 5",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 83158,\n "status" : "available",\n "url" : "http://some/video5.mp4"\n } ]\n}', - { - server: 'nginx', + .reply(200, '{\n "count" : 149,\n "items" : [ {\n "createdAt" : 1391457926000,\n "duration" : 3,\n "id" : "16231874-7ce7-4f5e-a30a-4513c4df480d",\n "name" : "",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 6590,\n "status" : "available",\n "url" : "http://some/video1.mp4"\n }, {\n "createdAt" : 1391218315000,\n "duration" : 0,\n "id" : "0931d1d7-4198-4db2-bf8a-097924421eb2",\n "name" : "Archive 3",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 3150,\n "status" : "available",\n "url" : "http://some/video2.mp4"\n }, {\n "createdAt" : 1391218274000,\n "duration" : 9,\n "id" : "e7198f93-d8fa-448d-b134-ac3355ce2eb7",\n "name" : "Archive 4",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 12691,\n "status" : "available",\n "url" : "http://some/video3.mp4"\n }, {\n "createdAt" : 1391218252000,\n "duration" : 17,\n "id" : "ae531f74-218c-4abd-bbe4-1f6bd92e9449",\n "name" : null,\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 21566,\n "status" : "available",\n "url" : "http://some/video4.mp4"\n }, {\n "createdAt" : 1391218139000,\n "duration" : 73,\n "id" : "cf2fd890-7ea0-4f43-a6a7-432ea9dc4c51",\n "name" : "Archive 5",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "SESSION_ID",\n "size" : 83158,\n "status" : "available",\n "url" : "http://some/video5.mp4"\n } ]\n}', { server: 'nginx', date: 'Mon, 03 Feb 2014 23:38:53 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); opentok.listArchives(function (err, archives, total) { expect(err).toBeNull(); @@ -396,14 +352,11 @@ describe('Archiving', function () { it('should return an error if any other HTTP status is returned', function (done) { nock('https://api.opentok.com:443') .get('/v2/partner/APIKEY/archive?count=5') - .reply(500, '{"message":"Some error"}', - { - server: 'nginx', + .reply(500, '{"message":"Some error"}', { server: 'nginx', date: 'Mon, 03 Feb 2014 23:38:53 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); opentok.listArchives({ count: 5 }, function (err, archives, total) { expect(archives).toBeUndefined(); @@ -427,16 +380,13 @@ describe('Archiving', function () { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive/ca138a6c-380f-4de9-b2b2-bc78b3a117e2/stop', {}) - .reply(200, '{\n "createdAt" : 1391471703000,\n "duration" : 0,\n "id" : "ca138a6c-380f-4de9-b2b2-bc78b3a117e2",\n "name" : "PHP Archiving Sample App",\n "partnerId" : "APIKEY",\n "reason" : ",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "stopped",\n "url" : null\n}', - { - server: 'nginx', + .reply(200, '{\n "createdAt" : 1391471703000,\n "duration" : 0,\n "id" : "ca138a6c-380f-4de9-b2b2-bc78b3a117e2",\n "name" : "PHP Archiving Sample App",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "stopped",\n "url" : null\n}', { server: 'nginx', date: 'Mon, 03 Feb 2014 23:56:29 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.stopArchive(archiveId, function stopArchiveCallback(err, archive) { + opentok.stopArchive(archiveId, function (err, archive) { expect(err).toBeNull(); expect(archive).not.toBeNull(); expect(archive.status).toBe('stopped'); @@ -456,16 +406,13 @@ describe('Archiving', function () { it('should return an error if archive ID is invalid', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive/AN-INVALID-ARCHIVE-ID/stop', {}) - .reply(404, '{ "message" : "Not found. You passed in an invalid archive ID." }', - { - server: 'nginx', + .reply(404, '{ "message" : "Not found. You passed in an invalid archive ID." }', { server: 'nginx', date: 'Tue, 04 Feb 2014 00:51:02 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.stopArchive('AN-INVALID-ARCHIVE-ID', function stopArchiveCallback(err, archive) { + opentok.stopArchive('AN-INVALID-ARCHIVE-ID', function (err, archive) { expect(archive).toBeUndefined(); expect(err).not.toBeNull(); expect(err.message).toBe('Archive not found'); @@ -474,20 +421,17 @@ describe('Archiving', function () { }); it('should return an error if the archive is not currently started', function (done) { - var archiveId; + var archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; + nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive/ca138a6c-380f-4de9-b2b2-bc78b3a117e2/stop', {}) - .reply(409, '{ "message" : "Conflict. You are trying to stop an archive that is not recording." }', - { - server: 'nginx', - date: 'Fri, 31 Jan 2014 06:49:12 GMT', + .reply(409, '{ "message" : "Conflict. You are trying to stop an archive that is not recording." }', { server: 'nginx', + date: 'Tue, 04 Feb 2014 00:52:38 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; - opentok.stopArchive(archiveId, function stopArchiveCallback(err, archive) { + opentok.stopArchive(archiveId, function (err, archive) { expect(archive).toBeUndefined(); expect(err).not.toBeNull(); expect(err.message).toBe('Conflict. You are trying to stop an archive that is not recording.'); @@ -496,20 +440,17 @@ describe('Archiving', function () { }); it('should return an error if any other HTTP status is returned', function (done) { - var archiveId; + var archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; + nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive/ca138a6c-380f-4de9-b2b2-bc78b3a117e2/stop', {}) - .reply(500, '{ "message" : "Some other error." }', - { - server: 'nginx', + .reply(500, '{ "message" : "Some other error." }', { server: 'nginx', date: 'Tue, 04 Feb 2014 00:52:38 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; - opentok.stopArchive(archiveId, function stopArchiveCallback(err, archive) { + opentok.stopArchive(archiveId, function (err, archive) { expect(archive).toBeUndefined(); expect(err).not.toBeNull(); expect(err.message).toBe('Unexpected response from OpenTok'); @@ -526,25 +467,22 @@ describe('Archiving', function () { describe('deleteArchive', function () { it('should return no error on success', function (done) { - var archiveId; + var archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; + nock('https://api.opentok.com:443') .delete('/v2/partner/APIKEY/archive/ca138a6c-380f-4de9-b2b2-bc78b3a117e2') - .reply(204, '', - { - server: 'nginx', + .reply(204, '', { server: 'nginx', date: 'Tue, 04 Feb 2014 01:05:40 GMT', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; - opentok.deleteArchive(archiveId, function deleteArchiveCallback(err) { + opentok.deleteArchive(archiveId, function (err) { expect(err).toBeNull(); done(); }); }); it('should return an error if archive ID is null', function (done) { - opentok.deleteArchive(null, function deleteArchiveCallback(err) { + opentok.deleteArchive(null, function (err) { expect(err).not.toBeNull(); expect(err.message).toBe('No archive ID given'); done(); @@ -554,16 +492,13 @@ describe('Archiving', function () { it('should return an error if archive ID is invalid', function (done) { nock('https://api.opentok.com:443') .delete('/v2/partner/APIKEY/archive/AN-INVALID-ARCHIVE-ID') - .reply(404, '{ "message" : "Not found. You passed in an invalid archive ID." }', - { - server: 'nginx', + .reply(404, '{ "message" : "Not found. You passed in an invalid archive ID." }', { server: 'nginx', date: 'Tue, 04 Feb 2014 01:10:39 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - opentok.deleteArchive('AN-INVALID-ARCHIVE-ID', function deleteArchiveCallback(err) { + opentok.deleteArchive('AN-INVALID-ARCHIVE-ID', function (err) { expect(err).not.toBeNull(); expect(err.message).toBe('Archive not found'); done(); @@ -571,20 +506,17 @@ describe('Archiving', function () { }); it('should return an error if any other HTTP status is returned', function (done) { - var archiveId; + var archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; + nock('https://api.opentok.com:443') .delete('/v2/partner/APIKEY/archive/ca138a6c-380f-4de9-b2b2-bc78b3a117e2') - .reply(500, '{ "message" : "Some other error." }', - { - server: 'nginx', + .reply(500, '{ "message" : "Some other error." }', { server: 'nginx', date: 'Tue, 04 Feb 2014 00:52:38 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); - archiveId = 'ca138a6c-380f-4de9-b2b2-bc78b3a117e2'; - opentok.deleteArchive(archiveId, function deleteArchiveCallback(err) { + opentok.deleteArchive(archiveId, function (err) { expect(err).not.toBeNull(); expect(err.message).toBe('Unexpected response from OpenTok'); done(); From faffdc957a1b4215bdd3db0e7009013639ee9780 Mon Sep 17 00:00:00 2001 From: Hashir Baqai Date: Wed, 23 Nov 2016 11:30:27 -0800 Subject: [PATCH 8/8] more consistency in whitespacing --- spec/opentok_spec.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/spec/opentok_spec.js b/spec/opentok_spec.js index 90616d43..307323c2 100644 --- a/spec/opentok_spec.js +++ b/spec/opentok_spec.js @@ -12,14 +12,11 @@ describe('Archiving', function () { it('should return an Archive', function (done) { nock('https://api.opentok.com:443') .post('/v2/partner/APIKEY/archive', { sessionId: '1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg', name: 'Bob' }) - .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "url" : null\n}', - { - server: 'nginx', + .reply(200, '{\n "createdAt" : 1391149936527,\n "duration" : 0,\n "id" : "4072fe0f-d499-4f2f-8237-64f5a9d936f5",\n "name" : "Bob",\n "partnerId" : "APIKEY",\n "reason" : "",\n "sessionId" : "1_MX4xMDB-MTI3LjAuMC4xflR1ZSBKYW4gMjggMTU6NDg6NDAgUFNUIDIwMTR-MC43NjAyOTYyfg",\n "size" : 0,\n "status" : "started",\n "url" : null\n}', { server: 'nginx', date: 'Fri, 31 Jan 2014 06:32:16 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', - connection: 'keep-alive' - }); + connection: 'keep-alive' }); opentok.startArchive(session, { name: 'Bob' }, function (err, archive) { expect(err).toBeNull();