|
|
@@ -37,10 +37,10 @@ describe 'WebsocketController', ->
|
|
|
inc: sinon.stub()
|
|
|
set: sinon.stub()
|
|
|
"./RoomManager": @RoomManager = {}
|
|
|
-
|
|
|
+
|
|
|
afterEach ->
|
|
|
tk.reset()
|
|
|
-
|
|
|
+
|
|
|
describe "joinProject", ->
|
|
|
describe "when authorised", ->
|
|
|
beforeEach ->
|
|
|
@@ -53,65 +53,61 @@ describe 'WebsocketController', ->
|
|
|
}
|
|
|
@privilegeLevel = "owner"
|
|
|
@ConnectedUsersManager.updateUserPosition = sinon.stub().callsArg(4)
|
|
|
- @isRestrictedUser = true
|
|
|
- @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, @project, @privilegeLevel, @isRestrictedUser)
|
|
|
+ @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, @project, @privilegeLevel)
|
|
|
@RoomManager.joinProject = sinon.stub().callsArg(2)
|
|
|
@WebsocketController.joinProject @client, @user, @project_id, @callback
|
|
|
-
|
|
|
+
|
|
|
it "should load the project from web", ->
|
|
|
@WebApiManager.joinProject
|
|
|
.calledWith(@project_id, @user)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should join the project room", ->
|
|
|
@RoomManager.joinProject.calledWith(@client, @project_id).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should set the privilege level on the client", ->
|
|
|
@client.set.calledWith("privilege_level", @privilegeLevel).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should set the user's id on the client", ->
|
|
|
@client.set.calledWith("user_id", @user._id).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should set the user's email on the client", ->
|
|
|
@client.set.calledWith("email", @user.email).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should set the user's first_name on the client", ->
|
|
|
@client.set.calledWith("first_name", @user.first_name).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should set the user's last_name on the client", ->
|
|
|
@client.set.calledWith("last_name", @user.last_name).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should set the user's sign up date on the client", ->
|
|
|
@client.set.calledWith("signup_date", @user.signUpDate).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should set the user's login_count on the client", ->
|
|
|
@client.set.calledWith("login_count", @user.loginCount).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should set the connected time on the client", ->
|
|
|
@client.set.calledWith("connected_time", new Date()).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should set the project_id on the client", ->
|
|
|
@client.set.calledWith("project_id", @project_id).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should set the project owner id on the client", ->
|
|
|
@client.set.calledWith("owner_id", @owner_id).should.equal true
|
|
|
-
|
|
|
- it "should set the is_restricted_user flag on the client", ->
|
|
|
- @client.set.calledWith("is_restricted_user", @isRestrictedUser).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should call the callback with the project, privilegeLevel and protocolVersion", ->
|
|
|
@callback
|
|
|
.calledWith(null, @project, @privilegeLevel, @WebsocketController.PROTOCOL_VERSION)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should mark the user as connected in ConnectedUsersManager", ->
|
|
|
@ConnectedUsersManager.updateUserPosition
|
|
|
.calledWith(@project_id, @client.id, @user, null)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should increment the join-project metric", ->
|
|
|
@metrics.inc.calledWith("editor.join-project").should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "when not authorized", ->
|
|
|
beforeEach ->
|
|
|
@WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, null, null)
|
|
|
@@ -142,30 +138,30 @@ describe 'WebsocketController', ->
|
|
|
@client.params.user_id = @user_id
|
|
|
@WebsocketController.FLUSH_IF_EMPTY_DELAY = 0
|
|
|
tk.reset() # Allow setTimeout to work.
|
|
|
-
|
|
|
+
|
|
|
describe "when the project is empty", ->
|
|
|
beforeEach (done) ->
|
|
|
@clientsInRoom = []
|
|
|
@WebsocketController.leaveProject @io, @client, done
|
|
|
-
|
|
|
+
|
|
|
it "should end clientTracking.clientDisconnected to the project room", ->
|
|
|
@WebsocketLoadBalancer.emitToRoom
|
|
|
.calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should mark the user as disconnected", ->
|
|
|
@ConnectedUsersManager.markUserAsDisconnected
|
|
|
.calledWith(@project_id, @client.id)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should flush the project in the document updater", ->
|
|
|
@DocumentUpdaterManager.flushProjectToMongoAndDelete
|
|
|
.calledWith(@project_id)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should increment the leave-project metric", ->
|
|
|
@metrics.inc.calledWith("editor.leave-project").should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should track the disconnection in RoomManager", ->
|
|
|
@RoomManager.leaveProjectAndDocs
|
|
|
.calledWith(@client)
|
|
|
@@ -175,7 +171,7 @@ describe 'WebsocketController', ->
|
|
|
beforeEach ->
|
|
|
@clientsInRoom = ["mock-remaining-client"]
|
|
|
@WebsocketController.leaveProject @io, @client
|
|
|
-
|
|
|
+
|
|
|
it "should not flush the project in the document updater", ->
|
|
|
@DocumentUpdaterManager.flushProjectToMongoAndDelete
|
|
|
.called.should.equal false
|
|
|
@@ -236,7 +232,7 @@ describe 'WebsocketController', ->
|
|
|
@ops = ["mock", "ops"]
|
|
|
@ranges = { "mock": "ranges" }
|
|
|
@options = {}
|
|
|
-
|
|
|
+
|
|
|
@client.params.project_id = @project_id
|
|
|
@AuthorizationManager.addAccessToDoc = sinon.stub()
|
|
|
@AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
|
|
|
@@ -279,7 +275,7 @@ describe 'WebsocketController', ->
|
|
|
beforeEach ->
|
|
|
@fromVersion = 40
|
|
|
@WebsocketController.joinDoc @client, @doc_id, @fromVersion, @options, @callback
|
|
|
-
|
|
|
+
|
|
|
it "should get the document from the DocumentUpdaterManager with fromVersion", ->
|
|
|
@DocumentUpdaterManager.getDocument
|
|
|
.calledWith(@project_id, @doc_id, @fromVersion)
|
|
|
@@ -289,7 +285,7 @@ describe 'WebsocketController', ->
|
|
|
beforeEach ->
|
|
|
@doc_lines.push ["räksmörgås"]
|
|
|
@WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
|
|
|
-
|
|
|
+
|
|
|
it "should call the callback with the escaped lines", ->
|
|
|
escaped_lines = @callback.args[0][1]
|
|
|
escaped_word = escaped_lines.pop()
|
|
|
@@ -331,44 +327,44 @@ describe 'WebsocketController', ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
|
|
|
@WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
|
|
|
-
|
|
|
+
|
|
|
it "should call the callback with an error", ->
|
|
|
@callback.calledWith(@err).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should not call the DocumentUpdaterManager", ->
|
|
|
@DocumentUpdaterManager.getDocument.called.should.equal false
|
|
|
-
|
|
|
+
|
|
|
describe "leaveDoc", ->
|
|
|
beforeEach ->
|
|
|
- @doc_id = "doc-id-123"
|
|
|
+ @doc_id = "doc-id-123"
|
|
|
@client.params.project_id = @project_id
|
|
|
@RoomManager.leaveDoc = sinon.stub()
|
|
|
@WebsocketController.leaveDoc @client, @doc_id, @callback
|
|
|
-
|
|
|
+
|
|
|
it "should remove the client from the doc_id room", ->
|
|
|
@RoomManager.leaveDoc
|
|
|
.calledWith(@client, @doc_id).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should call the callback", ->
|
|
|
@callback.called.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should increment the leave-doc metric", ->
|
|
|
@metrics.inc.calledWith("editor.leave-doc").should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "getConnectedUsers", ->
|
|
|
beforeEach ->
|
|
|
@client.params.project_id = @project_id
|
|
|
@users = ["mock", "users"]
|
|
|
@WebsocketLoadBalancer.emitToRoom = sinon.stub()
|
|
|
@ConnectedUsersManager.getConnectedUsers = sinon.stub().callsArgWith(1, null, @users)
|
|
|
-
|
|
|
+
|
|
|
describe "when authorized", ->
|
|
|
beforeEach (done) ->
|
|
|
@AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
|
|
|
@WebsocketController.getConnectedUsers @client, (args...) =>
|
|
|
@callback(args...)
|
|
|
done()
|
|
|
-
|
|
|
+
|
|
|
it "should check that the client is authorized to view the project", ->
|
|
|
@AuthorizationManager.assertClientCanViewProject
|
|
|
.calledWith(@client)
|
|
|
@@ -383,40 +379,26 @@ describe 'WebsocketController', ->
|
|
|
@ConnectedUsersManager.getConnectedUsers
|
|
|
.calledWith(@project_id)
|
|
|
.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should return the users", ->
|
|
|
@callback.calledWith(null, @users).should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should increment the get-connected-users metric", ->
|
|
|
@metrics.inc.calledWith("editor.get-connected-users").should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "when not authorized", ->
|
|
|
beforeEach ->
|
|
|
@AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
|
|
|
@WebsocketController.getConnectedUsers @client, @callback
|
|
|
-
|
|
|
+
|
|
|
it "should not get the connected users for the project", ->
|
|
|
@ConnectedUsersManager.getConnectedUsers
|
|
|
.called
|
|
|
.should.equal false
|
|
|
-
|
|
|
+
|
|
|
it "should return an error", ->
|
|
|
@callback.calledWith(@err).should.equal true
|
|
|
-
|
|
|
- describe "when restricted user", ->
|
|
|
- beforeEach ->
|
|
|
- @client.params.is_restricted_user = true
|
|
|
- @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
|
|
|
- @WebsocketController.getConnectedUsers @client, @callback
|
|
|
-
|
|
|
- it "should return an empty array of users", ->
|
|
|
- @callback.calledWith(null, []).should.equal true
|
|
|
-
|
|
|
- it "should not get the connected users for the project", ->
|
|
|
- @ConnectedUsersManager.getConnectedUsers
|
|
|
- .called
|
|
|
- .should.equal false
|
|
|
-
|
|
|
+
|
|
|
describe "updateClientPosition", ->
|
|
|
beforeEach ->
|
|
|
@WebsocketLoadBalancer.emitToRoom = sinon.stub()
|
|
|
@@ -440,7 +422,7 @@ describe 'WebsocketController', ->
|
|
|
@client.get = (param, callback) => callback null, @clientParams[param]
|
|
|
@WebsocketController.updateClientPosition @client, @update
|
|
|
|
|
|
- @populatedCursorData =
|
|
|
+ @populatedCursorData =
|
|
|
doc_id: @doc_id,
|
|
|
id: @client.id
|
|
|
name: "#{@first_name} #{@last_name}"
|
|
|
@@ -480,7 +462,7 @@ describe 'WebsocketController', ->
|
|
|
@client.get = (param, callback) => callback null, @clientParams[param]
|
|
|
@WebsocketController.updateClientPosition @client, @update
|
|
|
|
|
|
- @populatedCursorData =
|
|
|
+ @populatedCursorData =
|
|
|
doc_id: @doc_id,
|
|
|
id: @client.id
|
|
|
name: "#{@first_name}"
|
|
|
@@ -520,7 +502,7 @@ describe 'WebsocketController', ->
|
|
|
@client.get = (param, callback) => callback null, @clientParams[param]
|
|
|
@WebsocketController.updateClientPosition @client, @update
|
|
|
|
|
|
- @populatedCursorData =
|
|
|
+ @populatedCursorData =
|
|
|
doc_id: @doc_id,
|
|
|
id: @client.id
|
|
|
name: "#{@last_name}"
|
|
|
@@ -621,7 +603,7 @@ describe 'WebsocketController', ->
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
@callback.called.should.equal true
|
|
|
-
|
|
|
+
|
|
|
it "should increment the doc updates", ->
|
|
|
@metrics.inc.calledWith("editor.doc-update").should.equal true
|
|
|
|
|
|
@@ -639,7 +621,7 @@ describe 'WebsocketController', ->
|
|
|
|
|
|
it "should call the callback with the error", ->
|
|
|
@callback.calledWith(@error).should.equal true
|
|
|
-
|
|
|
+
|
|
|
describe "when not authorized", ->
|
|
|
beforeEach ->
|
|
|
@client.disconnect = sinon.stub()
|
|
|
@@ -663,7 +645,7 @@ describe 'WebsocketController', ->
|
|
|
@comment_update = { op: [{c: "bar", p: 132}] }
|
|
|
@AuthorizationManager.assertClientCanEditProjectAndDoc = sinon.stub()
|
|
|
@AuthorizationManager.assertClientCanViewProjectAndDoc = sinon.stub()
|
|
|
-
|
|
|
+
|
|
|
describe "with a read-write client", ->
|
|
|
it "should return successfully", (done) ->
|
|
|
@AuthorizationManager.assertClientCanEditProjectAndDoc.yields(null)
|
|
|
@@ -678,7 +660,7 @@ describe 'WebsocketController', ->
|
|
|
@WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @edit_update, (error) ->
|
|
|
expect(error.message).to.equal "not authorized"
|
|
|
done()
|
|
|
-
|
|
|
+
|
|
|
describe "with a read-only client and a comment op", ->
|
|
|
it "should return successfully", (done) ->
|
|
|
@AuthorizationManager.assertClientCanEditProjectAndDoc.yields(new Error("not authorized"))
|
|
|
@@ -686,7 +668,7 @@ describe 'WebsocketController', ->
|
|
|
@WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @comment_update, (error) ->
|
|
|
expect(error).to.be.null
|
|
|
done()
|
|
|
-
|
|
|
+
|
|
|
describe "with a totally unauthorized client", ->
|
|
|
it "should return an error", (done) ->
|
|
|
@AuthorizationManager.assertClientCanEditProjectAndDoc.yields(new Error("not authorized"))
|