2014-11-10 06:27:08 -05:00
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
sinon = require("sinon")
|
2017-03-15 11:45:52 -04:00
|
|
|
expect = chai.expect
|
2014-11-10 06:27:08 -05:00
|
|
|
modulePath = "../../../app/js/WebsocketController.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
tk = require "timekeeper"
|
|
|
|
|
|
|
|
describe 'WebsocketController', ->
|
|
|
|
beforeEach ->
|
|
|
|
tk.freeze(new Date())
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@user = {
|
2014-11-13 12:07:05 -05:00
|
|
|
_id: @user_id = "user-id-123"
|
2014-11-10 06:27:08 -05:00
|
|
|
first_name: "James"
|
|
|
|
last_name: "Allen"
|
|
|
|
email: "james@example.com"
|
|
|
|
signUpDate: new Date("2014-01-01")
|
|
|
|
loginCount: 42
|
|
|
|
}
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@client =
|
2014-11-13 12:07:05 -05:00
|
|
|
id: @client_id = "mock-client-id-123"
|
2014-11-12 10:54:55 -05:00
|
|
|
params: {}
|
2014-11-10 06:27:08 -05:00
|
|
|
set: sinon.stub()
|
2014-11-12 10:54:55 -05:00
|
|
|
get: (param, cb) -> cb null, @params[param]
|
2014-11-10 06:27:08 -05:00
|
|
|
join: sinon.stub()
|
2014-11-12 11:51:48 -05:00
|
|
|
leave: sinon.stub()
|
2014-11-10 06:27:08 -05:00
|
|
|
@WebsocketController = SandboxedModule.require modulePath, requires:
|
|
|
|
"./WebApiManager": @WebApiManager = {}
|
2014-11-12 10:54:55 -05:00
|
|
|
"./AuthorizationManager": @AuthorizationManager = {}
|
|
|
|
"./DocumentUpdaterManager": @DocumentUpdaterManager = {}
|
2014-11-13 08:05:49 -05:00
|
|
|
"./ConnectedUsersManager": @ConnectedUsersManager = {}
|
2014-11-13 11:03:37 -05:00
|
|
|
"./WebsocketLoadBalancer": @WebsocketLoadBalancer = {}
|
2017-11-13 04:41:04 -05:00
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
|
2014-11-17 08:12:49 -05:00
|
|
|
"metrics-sharelatex": @metrics =
|
|
|
|
inc: sinon.stub()
|
|
|
|
set: sinon.stub()
|
2019-07-19 03:49:57 -04:00
|
|
|
"./RoomManager": @RoomManager = {}
|
2014-11-10 06:27:08 -05:00
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
tk.reset()
|
|
|
|
|
|
|
|
describe "joinProject", ->
|
|
|
|
describe "when authorised", ->
|
|
|
|
beforeEach ->
|
2014-11-13 08:05:49 -05:00
|
|
|
@client.id = "mock-client-id"
|
2014-11-10 06:27:08 -05:00
|
|
|
@project = {
|
|
|
|
name: "Test Project"
|
|
|
|
owner: {
|
|
|
|
_id: @owner_id = "mock-owner-id-123"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@privilegeLevel = "owner"
|
2014-11-13 08:05:49 -05:00
|
|
|
@ConnectedUsersManager.updateUserPosition = sinon.stub().callsArg(4)
|
2014-11-10 06:27:08 -05:00
|
|
|
@WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, @project, @privilegeLevel)
|
2019-07-23 12:02:09 -04:00
|
|
|
@RoomManager.joinProject = sinon.stub().callsArg(2)
|
2014-11-10 06:27:08 -05:00
|
|
|
@WebsocketController.joinProject @client, @user, @project_id, @callback
|
|
|
|
|
|
|
|
it "should load the project from web", ->
|
|
|
|
@WebApiManager.joinProject
|
2017-09-26 09:21:41 -04:00
|
|
|
.calledWith(@project_id, @user)
|
2014-11-10 06:27:08 -05:00
|
|
|
.should.equal true
|
|
|
|
|
2014-11-13 06:48:49 -05:00
|
|
|
it "should join the project room", ->
|
2019-07-19 03:49:57 -04:00
|
|
|
@RoomManager.joinProject.calledWith(@client, @project_id).should.equal true
|
2014-11-13 06:48:49 -05:00
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
it "should set the privilege level on the client", ->
|
|
|
|
@client.set.calledWith("privilege_level", @privilegeLevel).should.equal true
|
|
|
|
|
2014-11-10 06:27:08 -05:00
|
|
|
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 call the callback with the project, privilegeLevel and protocolVersion", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(null, @project, @privilegeLevel, @WebsocketController.PROTOCOL_VERSION)
|
|
|
|
.should.equal true
|
2014-11-13 08:05:49 -05:00
|
|
|
|
|
|
|
it "should mark the user as connected in ConnectedUsersManager", ->
|
|
|
|
@ConnectedUsersManager.updateUserPosition
|
|
|
|
.calledWith(@project_id, @client.id, @user, null)
|
|
|
|
.should.equal true
|
2014-11-17 08:12:49 -05:00
|
|
|
|
|
|
|
it "should increment the join-project metric", ->
|
|
|
|
@metrics.inc.calledWith("editor.join-project").should.equal true
|
2014-11-10 06:27:08 -05:00
|
|
|
|
|
|
|
describe "when not authorized", ->
|
|
|
|
beforeEach ->
|
|
|
|
@WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, null, null)
|
|
|
|
@WebsocketController.joinProject @client, @user, @project_id, @callback
|
|
|
|
|
|
|
|
it "should return an error", ->
|
|
|
|
@callback
|
2014-11-12 10:54:55 -05:00
|
|
|
.calledWith(new Error("not authorized"))
|
|
|
|
.should.equal true
|
2019-06-21 01:30:12 -04:00
|
|
|
|
|
|
|
it "should not log an error", ->
|
|
|
|
@logger.error.called.should.equal false
|
|
|
|
|
2014-11-14 11:51:55 -05:00
|
|
|
describe "leaveProject", ->
|
|
|
|
beforeEach ->
|
|
|
|
@DocumentUpdaterManager.flushProjectToMongoAndDelete = sinon.stub().callsArg(1)
|
|
|
|
@ConnectedUsersManager.markUserAsDisconnected = sinon.stub().callsArg(2)
|
|
|
|
@WebsocketLoadBalancer.emitToRoom = sinon.stub()
|
2019-07-19 03:49:57 -04:00
|
|
|
@RoomManager.leaveProjectAndDocs = sinon.stub()
|
2014-11-14 11:51:55 -05:00
|
|
|
@clientsInRoom = []
|
|
|
|
@io =
|
|
|
|
sockets:
|
|
|
|
clients: (room_id) =>
|
|
|
|
if room_id != @project_id
|
|
|
|
throw "expected room_id to be project_id"
|
|
|
|
return @clientsInRoom
|
|
|
|
@client.params.project_id = @project_id
|
2017-02-23 07:04:36 -05:00
|
|
|
@client.params.user_id = @user_id
|
2014-11-14 11:51:55 -05:00
|
|
|
@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
|
2014-11-12 10:54:55 -05:00
|
|
|
|
2014-11-17 08:12:49 -05:00
|
|
|
it "should increment the leave-project metric", ->
|
|
|
|
@metrics.inc.calledWith("editor.leave-project").should.equal true
|
2014-11-14 11:51:55 -05:00
|
|
|
|
2019-07-19 03:49:57 -04:00
|
|
|
it "should track the disconnection in RoomManager", ->
|
|
|
|
@RoomManager.leaveProjectAndDocs
|
|
|
|
.calledWith(@client)
|
|
|
|
.should.equal true
|
|
|
|
|
2014-11-14 11:51:55 -05:00
|
|
|
describe "when the project is not empty", ->
|
|
|
|
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
|
2017-02-23 07:04:36 -05:00
|
|
|
|
|
|
|
describe "when client has not authenticated", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@client.params.user_id = null
|
|
|
|
@client.params.project_id = null
|
|
|
|
@WebsocketController.leaveProject @io, @client, done
|
|
|
|
|
|
|
|
it "should not end clientTracking.clientDisconnected to the project room", ->
|
|
|
|
@WebsocketLoadBalancer.emitToRoom
|
|
|
|
.calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
|
|
|
|
.should.equal false
|
|
|
|
|
|
|
|
it "should not mark the user as disconnected", ->
|
|
|
|
@ConnectedUsersManager.markUserAsDisconnected
|
|
|
|
.calledWith(@project_id, @client.id)
|
|
|
|
.should.equal false
|
|
|
|
|
|
|
|
it "should not flush the project in the document updater", ->
|
|
|
|
@DocumentUpdaterManager.flushProjectToMongoAndDelete
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal false
|
|
|
|
|
|
|
|
it "should increment the leave-project metric", ->
|
|
|
|
@metrics.inc.calledWith("editor.leave-project").should.equal true
|
|
|
|
|
|
|
|
describe "when client has not joined a project", ->
|
|
|
|
beforeEach (done) ->
|
|
|
|
@client.params.user_id = @user_id
|
|
|
|
@client.params.project_id = null
|
|
|
|
@WebsocketController.leaveProject @io, @client, done
|
|
|
|
|
|
|
|
it "should not end clientTracking.clientDisconnected to the project room", ->
|
|
|
|
@WebsocketLoadBalancer.emitToRoom
|
|
|
|
.calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
|
|
|
|
.should.equal false
|
|
|
|
|
|
|
|
it "should not mark the user as disconnected", ->
|
|
|
|
@ConnectedUsersManager.markUserAsDisconnected
|
|
|
|
.calledWith(@project_id, @client.id)
|
|
|
|
.should.equal false
|
|
|
|
|
|
|
|
it "should not flush the project in the document updater", ->
|
|
|
|
@DocumentUpdaterManager.flushProjectToMongoAndDelete
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal false
|
|
|
|
|
|
|
|
it "should increment the leave-project metric", ->
|
|
|
|
@metrics.inc.calledWith("editor.leave-project").should.equal true
|
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
describe "joinDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@doc_id = "doc-id-123"
|
|
|
|
@doc_lines = ["doc", "lines"]
|
|
|
|
@version = 42
|
|
|
|
@ops = ["mock", "ops"]
|
2016-12-08 06:37:31 -05:00
|
|
|
@ranges = { "mock": "ranges" }
|
2017-09-22 05:42:51 -04:00
|
|
|
@options = {}
|
2014-11-12 10:54:55 -05:00
|
|
|
|
|
|
|
@client.params.project_id = @project_id
|
2016-09-02 11:35:00 -04:00
|
|
|
@AuthorizationManager.addAccessToDoc = sinon.stub()
|
2014-11-12 10:54:55 -05:00
|
|
|
@AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
|
2016-12-08 06:37:31 -05:00
|
|
|
@DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(3, null, @doc_lines, @version, @ranges, @ops)
|
2019-07-23 12:02:09 -04:00
|
|
|
@RoomManager.joinDoc = sinon.stub().callsArg(2)
|
2017-09-22 05:42:51 -04:00
|
|
|
|
|
|
|
describe "works", ->
|
2014-11-12 10:54:55 -05:00
|
|
|
beforeEach ->
|
2017-09-22 05:42:51 -04:00
|
|
|
@WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
|
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
it "should check that the client is authorized to view the project", ->
|
|
|
|
@AuthorizationManager.assertClientCanViewProject
|
|
|
|
.calledWith(@client)
|
|
|
|
.should.equal true
|
2017-09-22 05:42:51 -04:00
|
|
|
|
|
|
|
it "should get the document from the DocumentUpdaterManager with fromVersion", ->
|
2014-11-12 10:54:55 -05:00
|
|
|
@DocumentUpdaterManager.getDocument
|
2017-09-22 05:42:51 -04:00
|
|
|
.calledWith(@project_id, @doc_id, -1)
|
2014-11-12 10:54:55 -05:00
|
|
|
.should.equal true
|
2016-09-02 11:35:00 -04:00
|
|
|
|
|
|
|
it "should add permissions for the client to access the doc", ->
|
|
|
|
@AuthorizationManager.addAccessToDoc
|
|
|
|
.calledWith(@client, @doc_id)
|
|
|
|
.should.equal true
|
2017-09-22 05:42:51 -04:00
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
it "should join the client to room for the doc_id", ->
|
2019-07-19 03:49:57 -04:00
|
|
|
@RoomManager.joinDoc
|
|
|
|
.calledWith(@client, @doc_id)
|
2014-11-12 10:54:55 -05:00
|
|
|
.should.equal true
|
2017-09-22 05:42:51 -04:00
|
|
|
|
2016-12-08 06:37:31 -05:00
|
|
|
it "should call the callback with the lines, version, ranges and ops", ->
|
2014-11-12 10:54:55 -05:00
|
|
|
@callback
|
2016-12-08 06:37:31 -05:00
|
|
|
.calledWith(null, @doc_lines, @version, @ops, @ranges)
|
2014-11-12 10:54:55 -05:00
|
|
|
.should.equal true
|
2017-09-22 05:42:51 -04:00
|
|
|
|
2014-11-17 08:12:49 -05:00
|
|
|
it "should increment the join-doc metric", ->
|
|
|
|
@metrics.inc.calledWith("editor.join-doc").should.equal true
|
2017-09-22 05:42:51 -04:00
|
|
|
|
|
|
|
describe "with a fromVersion", ->
|
|
|
|
beforeEach ->
|
|
|
|
@fromVersion = 40
|
|
|
|
@WebsocketController.joinDoc @client, @doc_id, @fromVersion, @options, @callback
|
2014-11-17 08:12:49 -05:00
|
|
|
|
2017-09-22 05:42:51 -04:00
|
|
|
it "should get the document from the DocumentUpdaterManager with fromVersion", ->
|
|
|
|
@DocumentUpdaterManager.getDocument
|
|
|
|
.calledWith(@project_id, @doc_id, @fromVersion)
|
|
|
|
.should.equal true
|
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
describe "with doclines that need escaping", ->
|
|
|
|
beforeEach ->
|
|
|
|
@doc_lines.push ["räksmörgås"]
|
2017-09-22 05:42:51 -04:00
|
|
|
@WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
|
2014-11-12 10:54:55 -05:00
|
|
|
|
|
|
|
it "should call the callback with the escaped lines", ->
|
|
|
|
escaped_lines = @callback.args[0][1]
|
|
|
|
escaped_word = escaped_lines.pop()
|
|
|
|
escaped_word.should.equal 'räksmörgås'
|
|
|
|
# Check that unescaping works
|
|
|
|
decodeURIComponent(escape(escaped_word)).should.equal "räksmörgås"
|
2017-09-22 05:42:51 -04:00
|
|
|
|
2017-09-22 05:43:11 -04:00
|
|
|
describe "with comments that need encoding", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ranges.comments = [{ op: { c: "räksmörgås" } }]
|
|
|
|
@WebsocketController.joinDoc @client, @doc_id, -1, { encodeRanges: true }, @callback
|
|
|
|
|
|
|
|
it "should call the callback with the encoded comment", ->
|
|
|
|
encoded_comments = @callback.args[0][4]
|
|
|
|
encoded_comment = encoded_comments.comments.pop()
|
|
|
|
encoded_comment_text = encoded_comment.op.c
|
|
|
|
encoded_comment_text.should.equal 'räksmörgås'
|
|
|
|
|
|
|
|
describe "with changes that need encoding", ->
|
|
|
|
it "should call the callback with the encoded insert change", ->
|
|
|
|
@ranges.changes = [{ op: { i: "räksmörgås" } }]
|
|
|
|
@WebsocketController.joinDoc @client, @doc_id, -1, { encodeRanges: true }, @callback
|
|
|
|
|
|
|
|
encoded_changes = @callback.args[0][4]
|
|
|
|
encoded_change = encoded_changes.changes.pop()
|
|
|
|
encoded_change_text = encoded_change.op.i
|
|
|
|
encoded_change_text.should.equal 'räksmörgås'
|
|
|
|
|
|
|
|
it "should call the callback with the encoded delete change", ->
|
|
|
|
@ranges.changes = [{ op: { d: "räksmörgås" } }]
|
|
|
|
@WebsocketController.joinDoc @client, @doc_id, -1, { encodeRanges: true }, @callback
|
|
|
|
|
|
|
|
encoded_changes = @callback.args[0][4]
|
|
|
|
encoded_change = encoded_changes.changes.pop()
|
|
|
|
encoded_change_text = encoded_change.op.d
|
|
|
|
encoded_change_text.should.equal 'räksmörgås'
|
|
|
|
|
2014-11-12 10:54:55 -05:00
|
|
|
describe "when not authorized", ->
|
|
|
|
beforeEach ->
|
|
|
|
@AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
|
2017-09-22 05:42:51 -04:00
|
|
|
@WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
|
2014-11-12 10:54:55 -05:00
|
|
|
|
|
|
|
it "should call the callback with an error", ->
|
|
|
|
@callback.calledWith(@err).should.equal true
|
|
|
|
|
|
|
|
it "should not call the DocumentUpdaterManager", ->
|
2014-11-12 11:51:48 -05:00
|
|
|
@DocumentUpdaterManager.getDocument.called.should.equal false
|
|
|
|
|
|
|
|
describe "leaveDoc", ->
|
|
|
|
beforeEach ->
|
|
|
|
@doc_id = "doc-id-123"
|
|
|
|
@client.params.project_id = @project_id
|
2019-07-19 03:49:57 -04:00
|
|
|
@RoomManager.leaveDoc = sinon.stub()
|
2014-11-12 11:51:48 -05:00
|
|
|
@WebsocketController.leaveDoc @client, @doc_id, @callback
|
|
|
|
|
|
|
|
it "should remove the client from the doc_id room", ->
|
2019-07-19 03:49:57 -04:00
|
|
|
@RoomManager.leaveDoc
|
|
|
|
.calledWith(@client, @doc_id).should.equal true
|
2014-11-12 11:51:48 -05:00
|
|
|
|
|
|
|
it "should call the callback", ->
|
2014-11-13 08:05:49 -05:00
|
|
|
@callback.called.should.equal true
|
|
|
|
|
2014-11-17 08:12:49 -05:00
|
|
|
it "should increment the leave-doc metric", ->
|
|
|
|
@metrics.inc.calledWith("editor.leave-doc").should.equal true
|
|
|
|
|
2014-11-13 08:05:49 -05:00
|
|
|
describe "getConnectedUsers", ->
|
|
|
|
beforeEach ->
|
|
|
|
@client.params.project_id = @project_id
|
|
|
|
@users = ["mock", "users"]
|
2019-08-14 08:03:14 -04:00
|
|
|
@WebsocketLoadBalancer.emitToRoom = sinon.stub()
|
2014-11-13 08:05:49 -05:00
|
|
|
@ConnectedUsersManager.getConnectedUsers = sinon.stub().callsArgWith(1, null, @users)
|
|
|
|
|
|
|
|
describe "when authorized", ->
|
2019-08-14 08:03:14 -04:00
|
|
|
beforeEach (done) ->
|
2014-11-13 08:05:49 -05:00
|
|
|
@AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
|
2019-08-14 08:03:14 -04:00
|
|
|
@WebsocketController.getConnectedUsers @client, (args...) =>
|
|
|
|
@callback(args...)
|
|
|
|
done()
|
2014-11-13 08:05:49 -05:00
|
|
|
|
|
|
|
it "should check that the client is authorized to view the project", ->
|
|
|
|
@AuthorizationManager.assertClientCanViewProject
|
|
|
|
.calledWith(@client)
|
|
|
|
.should.equal true
|
2019-08-14 08:03:14 -04:00
|
|
|
|
|
|
|
it "should broadcast a request to update the client list", ->
|
|
|
|
@WebsocketLoadBalancer.emitToRoom
|
|
|
|
.calledWith(@project_id, "clientTracking.refresh")
|
|
|
|
.should.equal true
|
|
|
|
|
2014-11-13 08:05:49 -05:00
|
|
|
it "should get the connected users for the project", ->
|
|
|
|
@ConnectedUsersManager.getConnectedUsers
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the users", ->
|
|
|
|
@callback.calledWith(null, @users).should.equal true
|
2014-11-17 08:12:49 -05:00
|
|
|
|
|
|
|
it "should increment the get-connected-users metric", ->
|
|
|
|
@metrics.inc.calledWith("editor.get-connected-users").should.equal true
|
2014-11-13 08:05:49 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-11-13 10:27:18 -05:00
|
|
|
describe "updateClientPosition", ->
|
|
|
|
beforeEach ->
|
2014-11-13 11:03:37 -05:00
|
|
|
@WebsocketLoadBalancer.emitToRoom = sinon.stub()
|
2014-11-13 10:27:18 -05:00
|
|
|
@ConnectedUsersManager.updateUserPosition = sinon.stub().callsArgWith(4)
|
2016-09-02 11:35:00 -04:00
|
|
|
@AuthorizationManager.assertClientCanViewProjectAndDoc = sinon.stub().callsArgWith(2, null)
|
2014-11-13 10:27:18 -05:00
|
|
|
@update = {
|
|
|
|
doc_id: @doc_id = "doc-id-123"
|
|
|
|
row: @row = 42
|
|
|
|
column: @column = 37
|
|
|
|
}
|
|
|
|
|
|
|
|
describe "with a logged in user", ->
|
|
|
|
beforeEach ->
|
|
|
|
@clientParams = {
|
|
|
|
project_id: @project_id
|
|
|
|
first_name: @first_name = "Douglas"
|
|
|
|
last_name: @last_name = "Adams"
|
|
|
|
email: @email = "joe@example.com"
|
|
|
|
user_id: @user_id = "user-id-123"
|
|
|
|
}
|
|
|
|
@client.get = (param, callback) => callback null, @clientParams[param]
|
|
|
|
@WebsocketController.updateClientPosition @client, @update
|
|
|
|
|
|
|
|
@populatedCursorData =
|
|
|
|
doc_id: @doc_id,
|
|
|
|
id: @client.id
|
|
|
|
name: "#{@first_name} #{@last_name}"
|
|
|
|
row: @row
|
|
|
|
column: @column
|
|
|
|
email: @email
|
|
|
|
user_id: @user_id
|
|
|
|
|
2014-11-13 11:03:37 -05:00
|
|
|
it "should send the update to the project room with the user's name", ->
|
|
|
|
@WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
|
2014-11-13 10:27:18 -05:00
|
|
|
|
|
|
|
it "should send the cursor data to the connected user manager", (done)->
|
|
|
|
@ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, {
|
2015-02-05 08:41:31 -05:00
|
|
|
_id: @user_id,
|
2014-11-13 10:27:18 -05:00
|
|
|
email: @email,
|
|
|
|
first_name: @first_name,
|
|
|
|
last_name: @last_name
|
|
|
|
}, {
|
|
|
|
row: @row
|
|
|
|
column: @column
|
|
|
|
doc_id: @doc_id
|
|
|
|
}).should.equal true
|
|
|
|
done()
|
2019-02-08 10:39:51 -05:00
|
|
|
|
2014-11-17 08:12:49 -05:00
|
|
|
it "should increment the update-client-position metric at 0.1 frequency", ->
|
|
|
|
@metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
|
2014-11-13 10:27:18 -05:00
|
|
|
|
2017-12-12 10:27:50 -05:00
|
|
|
describe "with a logged in user who has no last_name set", ->
|
|
|
|
beforeEach ->
|
|
|
|
@clientParams = {
|
|
|
|
project_id: @project_id
|
|
|
|
first_name: @first_name = "Douglas"
|
|
|
|
last_name: undefined
|
|
|
|
email: @email = "joe@example.com"
|
|
|
|
user_id: @user_id = "user-id-123"
|
|
|
|
}
|
|
|
|
@client.get = (param, callback) => callback null, @clientParams[param]
|
|
|
|
@WebsocketController.updateClientPosition @client, @update
|
|
|
|
|
|
|
|
@populatedCursorData =
|
|
|
|
doc_id: @doc_id,
|
|
|
|
id: @client.id
|
|
|
|
name: "#{@first_name}"
|
|
|
|
row: @row
|
|
|
|
column: @column
|
|
|
|
email: @email
|
|
|
|
user_id: @user_id
|
|
|
|
|
|
|
|
it "should send the update to the project room with the user's name", ->
|
|
|
|
@WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
|
|
|
|
|
|
|
|
it "should send the cursor data to the connected user manager", (done)->
|
|
|
|
@ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, {
|
|
|
|
_id: @user_id,
|
|
|
|
email: @email,
|
|
|
|
first_name: @first_name,
|
|
|
|
last_name: undefined
|
|
|
|
}, {
|
|
|
|
row: @row
|
|
|
|
column: @column
|
|
|
|
doc_id: @doc_id
|
|
|
|
}).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should increment the update-client-position metric at 0.1 frequency", ->
|
|
|
|
@metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
|
|
|
|
|
2017-12-13 05:28:35 -05:00
|
|
|
describe "with a logged in user who has no first_name set", ->
|
|
|
|
beforeEach ->
|
|
|
|
@clientParams = {
|
|
|
|
project_id: @project_id
|
|
|
|
first_name: undefined
|
|
|
|
last_name: @last_name = "Adams"
|
|
|
|
email: @email = "joe@example.com"
|
|
|
|
user_id: @user_id = "user-id-123"
|
|
|
|
}
|
|
|
|
@client.get = (param, callback) => callback null, @clientParams[param]
|
|
|
|
@WebsocketController.updateClientPosition @client, @update
|
|
|
|
|
|
|
|
@populatedCursorData =
|
|
|
|
doc_id: @doc_id,
|
|
|
|
id: @client.id
|
|
|
|
name: "#{@last_name}"
|
|
|
|
row: @row
|
|
|
|
column: @column
|
|
|
|
email: @email
|
|
|
|
user_id: @user_id
|
|
|
|
|
|
|
|
it "should send the update to the project room with the user's name", ->
|
|
|
|
@WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
|
|
|
|
|
|
|
|
it "should send the cursor data to the connected user manager", (done)->
|
|
|
|
@ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, {
|
|
|
|
_id: @user_id,
|
|
|
|
email: @email,
|
|
|
|
first_name: undefined,
|
|
|
|
last_name: @last_name
|
|
|
|
}, {
|
|
|
|
row: @row
|
|
|
|
column: @column
|
|
|
|
doc_id: @doc_id
|
|
|
|
}).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should increment the update-client-position metric at 0.1 frequency", ->
|
|
|
|
@metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
|
2019-02-08 10:39:51 -05:00
|
|
|
describe "with a logged in user who has no names set", ->
|
|
|
|
beforeEach ->
|
|
|
|
@clientParams = {
|
|
|
|
project_id: @project_id
|
|
|
|
first_name: undefined
|
|
|
|
last_name: undefined
|
|
|
|
email: @email = "joe@example.com"
|
|
|
|
user_id: @user_id = "user-id-123"
|
|
|
|
}
|
|
|
|
@client.get = (param, callback) => callback null, @clientParams[param]
|
|
|
|
@WebsocketController.updateClientPosition @client, @update
|
|
|
|
|
|
|
|
it "should send the update to the project name with no name", ->
|
|
|
|
@WebsocketLoadBalancer.emitToRoom
|
|
|
|
.calledWith(@project_id, "clientTracking.clientUpdated", {
|
|
|
|
doc_id: @doc_id,
|
|
|
|
id: @client.id,
|
|
|
|
user_id: @user_id,
|
|
|
|
name: "",
|
|
|
|
row: @row,
|
|
|
|
column: @column,
|
|
|
|
email: @email
|
|
|
|
})
|
|
|
|
.should.equal true
|
2017-12-13 05:28:35 -05:00
|
|
|
|
|
|
|
|
2014-11-13 10:27:18 -05:00
|
|
|
describe "with an anonymous user", ->
|
|
|
|
beforeEach ->
|
|
|
|
@clientParams = {
|
|
|
|
project_id: @project_id
|
|
|
|
}
|
|
|
|
@client.get = (param, callback) => callback null, @clientParams[param]
|
|
|
|
@WebsocketController.updateClientPosition @client, @update
|
|
|
|
|
2019-02-08 10:39:51 -05:00
|
|
|
it "should send the update to the project room with no name", ->
|
2014-11-13 11:03:37 -05:00
|
|
|
@WebsocketLoadBalancer.emitToRoom
|
|
|
|
.calledWith(@project_id, "clientTracking.clientUpdated", {
|
|
|
|
doc_id: @doc_id,
|
|
|
|
id: @client.id
|
2019-02-08 10:39:51 -05:00
|
|
|
name: ""
|
2014-11-13 11:03:37 -05:00
|
|
|
row: @row
|
|
|
|
column: @column
|
|
|
|
})
|
|
|
|
.should.equal true
|
2014-11-13 12:07:05 -05:00
|
|
|
|
2014-11-13 10:27:18 -05:00
|
|
|
it "should not send cursor data to the connected user manager", (done)->
|
|
|
|
@ConnectedUsersManager.updateUserPosition.called.should.equal false
|
2014-11-13 12:07:05 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
describe "applyOtUpdate", ->
|
|
|
|
beforeEach ->
|
|
|
|
@update = {op: {p: 12, t: "foo"}}
|
|
|
|
@client.params.user_id = @user_id
|
2014-11-14 05:12:35 -05:00
|
|
|
@client.params.project_id = @project_id
|
2017-03-15 11:45:52 -04:00
|
|
|
@WebsocketController._assertClientCanApplyUpdate = sinon.stub().yields()
|
2014-11-13 12:07:05 -05:00
|
|
|
@DocumentUpdaterManager.queueChange = sinon.stub().callsArg(3)
|
|
|
|
|
|
|
|
describe "succesfully", ->
|
|
|
|
beforeEach ->
|
2014-11-14 05:12:35 -05:00
|
|
|
@WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
|
2014-11-13 12:07:05 -05:00
|
|
|
|
|
|
|
it "should set the source of the update to the client id", ->
|
|
|
|
@update.meta.source.should.equal @client.id
|
|
|
|
|
|
|
|
it "should set the user_id of the update to the user id", ->
|
|
|
|
@update.meta.user_id.should.equal @user_id
|
|
|
|
|
|
|
|
it "should queue the update", ->
|
|
|
|
@DocumentUpdaterManager.queueChange
|
|
|
|
.calledWith(@project_id, @doc_id, @update)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
2014-11-17 08:12:49 -05:00
|
|
|
|
|
|
|
it "should increment the doc updates", ->
|
|
|
|
@metrics.inc.calledWith("editor.doc-update").should.equal true
|
2014-11-13 12:07:05 -05:00
|
|
|
|
|
|
|
describe "unsuccessfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@client.disconnect = sinon.stub()
|
|
|
|
@DocumentUpdaterManager.queueChange = sinon.stub().callsArgWith(3, @error = new Error("Something went wrong"))
|
2014-11-14 05:12:35 -05:00
|
|
|
@WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
|
2014-11-13 12:07:05 -05:00
|
|
|
|
|
|
|
it "should disconnect the client", ->
|
|
|
|
@client.disconnect.called.should.equal true
|
|
|
|
|
|
|
|
it "should log an error", ->
|
|
|
|
@logger.error.called.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with the error", ->
|
|
|
|
@callback.calledWith(@error).should.equal true
|
|
|
|
|
|
|
|
describe "when not authorized", ->
|
|
|
|
beforeEach ->
|
|
|
|
@client.disconnect = sinon.stub()
|
2017-03-15 11:45:52 -04:00
|
|
|
@WebsocketController._assertClientCanApplyUpdate = sinon.stub().yields(@error = new Error("not authorized"))
|
2014-11-14 05:12:35 -05:00
|
|
|
@WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
|
2014-11-13 12:07:05 -05:00
|
|
|
|
2014-11-14 10:30:18 -05:00
|
|
|
# This happens in a setTimeout to allow the client a chance to receive the error first.
|
|
|
|
# I'm not sure how to unit test, but it is acceptance tested.
|
|
|
|
# it "should disconnect the client", ->
|
|
|
|
# @client.disconnect.called.should.equal true
|
2014-11-13 12:07:05 -05:00
|
|
|
|
2017-11-13 04:41:04 -05:00
|
|
|
it "should log a warning", ->
|
|
|
|
@logger.warn.called.should.equal true
|
2014-11-13 12:07:05 -05:00
|
|
|
|
|
|
|
it "should call the callback with the error", ->
|
|
|
|
@callback.calledWith(@error).should.equal true
|
2017-03-15 11:45:52 -04:00
|
|
|
|
|
|
|
describe "_assertClientCanApplyUpdate", ->
|
|
|
|
beforeEach ->
|
|
|
|
@edit_update = { op: [{i: "foo", p: 42}, {c: "bar", p: 132}] } # comments may still be in an edit op
|
|
|
|
@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)
|
|
|
|
@WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @edit_update, (error) ->
|
|
|
|
expect(error).to.be.null
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe "with a read-only client and an edit op", ->
|
|
|
|
it "should return an error", (done) ->
|
|
|
|
@AuthorizationManager.assertClientCanEditProjectAndDoc.yields(new Error("not authorized"))
|
|
|
|
@AuthorizationManager.assertClientCanViewProjectAndDoc.yields(null)
|
|
|
|
@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"))
|
|
|
|
@AuthorizationManager.assertClientCanViewProjectAndDoc.yields(null)
|
|
|
|
@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"))
|
|
|
|
@AuthorizationManager.assertClientCanViewProjectAndDoc.yields(new Error("not authorized"))
|
|
|
|
@WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @comment_update, (error) ->
|
|
|
|
expect(error.message).to.equal "not authorized"
|
|
|
|
done()
|