diff --git a/server/presence.js b/server/presence.js index 276fdec5..bf16e7a1 100644 --- a/server/presence.js +++ b/server/presence.js @@ -122,10 +122,13 @@ api = { var isAnon = !req.session.email; var user; + logger.debug({firstRequest: firstRequest, email: !!req.session.email}, + "/stream connection made"); + if (isAnon) - user = api._setupUser(anons, api._genId()); + user = api._setupUser(anons, api._genId(), firstRequest); else - user = api._setupUser(users, req.session.email); + user = api._setupUser(users, req.session.email, firstRequest); user.touch(); diff --git a/static/spa/talkilla/js/spa.js b/static/spa/talkilla/js/spa.js index 048ac4b9..df0144d6 100644 --- a/static/spa/talkilla/js/spa.js +++ b/static/spa/talkilla/js/spa.js @@ -25,6 +25,7 @@ var TalkillaSPA = (function() { this.server.on("reconnection", this._onServerEvent.bind(this,"reconnection")); this.server.on("message", this._onServerMessage.bind(this)); + } TalkillaSPA.prototype = { @@ -38,11 +39,17 @@ var TalkillaSPA = (function() { else if (type === "reconnection") this.port.post("reconnection", (new payloads.Reconnection(event))); else if (type === "connected") { - this.port.post(type, { - addresses: [{type: "email", value: this.email}], - capabilities: this.capabilities - }); - this.server.presenceRequest(); + if ("email" in this) { + this.port.post(type, { + addresses: [{type: "email", value: this.email}], + capabilities: this.capabilities + }); + this.server.presenceRequest(); + } else { + this.port.post(type, { + capabilities: this.capabilities + }); + } } else this.port.post(type, event); diff --git a/test/frontend/spa/talkilla_spa_test.js b/test/frontend/spa/talkilla_spa_test.js index 7b7724e0..cbb51c9e 100644 --- a/test/frontend/spa/talkilla_spa_test.js +++ b/test/frontend/spa/talkilla_spa_test.js @@ -1,5 +1,5 @@ /* global sinon, SPAPort, Server, TalkillaSPA, expect, payloads */ -/* jshint unused:false */ + "use strict"; describe("TalkillaSPA", function() { @@ -12,6 +12,8 @@ describe("TalkillaSPA", function() { sandbox = sinon.sandbox.create(); port = new SPAPort(); server = new Server(); + + sandbox.stub(server, "connect"); spa = new TalkillaSPA(port, server, {capabilities: ["call", "move"]}); }); @@ -20,24 +22,75 @@ describe("TalkillaSPA", function() { expect(spa.capabilities).to.be.a("array"); expect(spa.capabilities).eql(["call", "move"]); }); - }); describe("#_onServerEvent", function() { - it("should post a connect event to the port", function() { - spa.email = "foo"; - sandbox.stub(spa.port, "post"); + describe("connected", function () { - spa.server.trigger("connected"); + // XXX email shouldn't be a public member of the API, I don't think, + // and as a result, and we should be avoiding using it to do the tests + // I suspect we really want to be testing the connect and connected + // events as a pair. + + it("should post a connected event containing the email address " + + "to the worker port if spa.email is set", function() { + + spa.email = "foo"; + sandbox.stub(spa.port, "post"); + + spa.server.trigger("connected"); + + sinon.assert.calledOnce(spa.port.post); + sinon.assert.calledWithExactly( + spa.port.post, "connected", { + addresses: [{type: "email", value: "foo"}], + capabilities: ["call", "move"] + } + ); + }); + + it("should post a connected event to the worker port if " + + "not containing an email address is spa.email is not set", function() { + + delete spa.email; + sandbox.stub(spa.port, "post"); + + spa.server.trigger("connected"); + + sinon.assert.calledOnce(spa.port.post); + sinon.assert.calledWithExactly( + spa.port.post, "connected", { + capabilities: ["call", "move"] + }); + }); + + it("should post a presenceRequest to the server if spa.email is set", + function() { + spa.email = "foo"; + sandbox.stub(spa.port, "post"); + sandbox.stub(spa.server, "presenceRequest"); + + spa.server.trigger("connected"); + + sinon.assert.calledOnce(spa.server.presenceRequest); + sinon.assert.calledWithExactly(spa.server.presenceRequest); + }); + + + it("should not post a presenceRequest to the server if spa.email" + + " is not set", function() { + + delete spa.email; + + sandbox.stub(spa.port, "post"); + sandbox.stub(spa.server, "presenceRequest"); + + spa.server.trigger("connected"); + + sinon.assert.notCalled(spa.server.presenceRequest); + }); - sinon.assert.calledOnce(spa.port.post); - sinon.assert.calledWithExactly( - spa.port.post, "connected", { - addresses: [{type: "email", value: "foo"}], - capabilities: ["call", "move"] - } - ); }); it("should post a reconnection event to the port", function() { @@ -80,7 +133,6 @@ describe("TalkillaSPA", function() { describe("#_onConnect", function() { it("should connect to the server", function() { - sandbox.stub(spa.server, "connect"); // The Talkilla SPA doesn't need any credentials. This is // handled via cookies. diff --git a/test/server/presence_test.js b/test/server/presence_test.js index 7048bdfb..51b05bca 100644 --- a/test/server/presence_test.js +++ b/test/server/presence_test.js @@ -415,7 +415,7 @@ describe("presence", function() { // away from testing the impl with stubs and instead look at the // side effect on anons for this specific test sinon.assert.calledOnce(api._setupUser); - sinon.assert.calledWithExactly(api._setupUser, anons, fakeId); + sinon.assert.calledWithExactly(api._setupUser, anons, fakeId, false); }); });