diff --git a/services/real-time/app/coffee/Router.coffee b/services/real-time/app/coffee/Router.coffee index 14d2f698a8..da26d46015 100644 --- a/services/real-time/app/coffee/Router.coffee +++ b/services/real-time/app/coffee/Router.coffee @@ -30,11 +30,10 @@ module.exports = Router = logger.log session: session, client_id: client.id, "client connected" - user = session.user - if !user? or !user._id? - logger.log "terminating session without authenticated user" - client.disconnect() - return + if !session or !session.user? + user = {_id: "anonymous-user"} + else + user = session.user client.on "joinProject", (data = {}, callback) -> WebsocketController.joinProject client, user, data.project_id, (err, args...) -> diff --git a/services/real-time/test/acceptance/coffee/ClientTrackingTests.coffee b/services/real-time/test/acceptance/coffee/ClientTrackingTests.coffee index 90071e61ec..a602df52d2 100644 --- a/services/real-time/test/acceptance/coffee/ClientTrackingTests.coffee +++ b/services/real-time/test/acceptance/coffee/ClientTrackingTests.coffee @@ -9,46 +9,47 @@ FixturesManager = require "./helpers/FixturesManager" async = require "async" describe "clientTracking", -> - before (done) -> - async.series [ - (cb) => - FixturesManager.setUpProject { - privilegeLevel: "owner" - project: { name: "Test Project" } - }, (error, {@user_id, @project_id}) => cb() - - (cb) => - @clientA = RealTimeClient.connect() - @clientA.on "connect", cb - - (cb) => - @clientB = RealTimeClient.connect() - @clientB.on "connect", cb - - (cb) => - @clientA.emit "joinProject", { - project_id: @project_id - }, cb - - (cb) => - @clientB.emit "joinProject", { - project_id: @project_id - }, cb - ], done - describe "when a client updates its cursor location", -> before (done) -> - @updates = [] - @clientB.on "clientTracking.clientUpdated", (data) => - @updates.push data + async.series [ + (cb) => + FixturesManager.setUpProject { + privilegeLevel: "owner" + project: { name: "Test Project" } + }, (error, {@user_id, @project_id}) => cb() - @clientA.emit "clientTracking.updatePosition", { - row: @row = 42 - column: @column = 36 - doc_id: @doc_id = "mock-doc-id" - }, (error) -> - throw error if error? - setTimeout done, 300 # Give the message a chance to reach client B. + (cb) => + @clientA = RealTimeClient.connect() + @clientA.on "connect", cb + + (cb) => + @clientB = RealTimeClient.connect() + + @clientB.on "connect", cb + + (cb) => + @clientA.emit "joinProject", { + project_id: @project_id + }, cb + + (cb) => + @clientB.emit "joinProject", { + project_id: @project_id + }, cb + + (cb) => + @updates = [] + @clientB.on "clientTracking.clientUpdated", (data) => + @updates.push data + + @clientA.emit "clientTracking.updatePosition", { + row: @row = 42 + column: @column = 36 + doc_id: @doc_id = "mock-doc-id" + }, (error) -> + throw error if error? + setTimeout cb, 300 # Give the message a chance to reach client B. + ], done it "should tell other clients about the update", -> @updates.should.deep.equal [ @@ -74,6 +75,58 @@ describe "clientTracking", -> return done() throw new Error("user was never found") + describe "when an anonymous client updates its cursor location", -> + before (done) -> + async.series [ + (cb) => + FixturesManager.setUpProject { + privilegeLevel: "owner" + project: { name: "Test Project" } + publicAccess: "readAndWrite" + }, (error, {@user_id, @project_id}) => cb() - describe "anonymous users", -> - it "should test something..." + (cb) => + @clientA = RealTimeClient.connect() + @clientA.on "connect", cb + (cb) => + @clientA.emit "joinProject", { + project_id: @project_id + }, cb + + (cb) => + RealTimeClient.setSession({}, cb) + + (cb) => + @anonymous = RealTimeClient.connect() + @anonymous.on "connect", cb + + (cb) => + @anonymous.emit "joinProject", { + project_id: @project_id + }, cb + + (cb) => + @updates = [] + @clientA.on "clientTracking.clientUpdated", (data) => + @updates.push data + + @anonymous.emit "clientTracking.updatePosition", { + row: @row = 42 + column: @column = 36 + doc_id: @doc_id = "mock-doc-id" + }, (error) -> + throw error if error? + setTimeout cb, 300 # Give the message a chance to reach client B. + ], done + + it "should tell other clients about the update", -> + @updates.should.deep.equal [ + { + row: @row + column: @column + doc_id: @doc_id + id: @anonymous.socket.sessionid + user_id: "anonymous-user" + name: "Anonymous" + } + ] diff --git a/services/real-time/test/acceptance/coffee/SessionTests.coffee b/services/real-time/test/acceptance/coffee/SessionTests.coffee index 618635db58..fa5378a1f5 100644 --- a/services/real-time/test/acceptance/coffee/SessionTests.coffee +++ b/services/real-time/test/acceptance/coffee/SessionTests.coffee @@ -32,47 +32,3 @@ describe "Session", -> break expect(included).to.equal true done() - - describe "without an established session", -> - before (done) -> - RealTimeClient.unsetSession (error) => - throw error if error? - @client = RealTimeClient.connect() - done() - - it "should get disconnected", (done) -> - @client.on "disconnect", () -> - done() - - it "not should appear in the list of connected clients", (done) -> - RealTimeClient.getConnectedClients (error, clients) => - included = false - for client in clients - if client.client_id == @client.socket.sessionid - included = true - break - expect(included).to.equal false - done() - - describe "without a valid user set on the session", -> - before (done) -> - RealTimeClient.setSession { - foo: "bar" - }, (error) => - throw error if error? - @client = RealTimeClient.connect() - done() - - it "should get disconnected", (done) -> - @client.on "disconnect", () -> - done() - - it "not should appear in the list of connected clients", (done) -> - RealTimeClient.getConnectedClients (error, clients) => - included = false - for client in clients - if client.client_id == @client.socket.sessionid - included = true - break - expect(included).to.equal false - done() \ No newline at end of file diff --git a/services/real-time/test/acceptance/coffee/helpers/FixturesManager.coffee b/services/real-time/test/acceptance/coffee/helpers/FixturesManager.coffee index ad584ce608..9ee5f4ef6f 100644 --- a/services/real-time/test/acceptance/coffee/helpers/FixturesManager.coffee +++ b/services/real-time/test/acceptance/coffee/helpers/FixturesManager.coffee @@ -7,10 +7,12 @@ module.exports = FixturesManager = options.user_id ||= FixturesManager.getRandomId() options.project_id ||= FixturesManager.getRandomId() options.project ||= { name: "Test Project" } - {project_id, user_id, privilegeLevel, project} = options + {project_id, user_id, privilegeLevel, project, publicAccess} = options privileges = {} privileges[user_id] = privilegeLevel + if publicAccess + privileges["anonymous-user"] = publicAccess MockWebServer.createMockProject(project_id, privileges, project) MockWebServer.run (error) =>