WebsocketControllerTests.coffee 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. chai = require('chai')
  2. should = chai.should()
  3. sinon = require("sinon")
  4. expect = chai.expect
  5. modulePath = "../../../app/js/WebsocketController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. tk = require "timekeeper"
  8. describe 'WebsocketController', ->
  9. beforeEach ->
  10. tk.freeze(new Date())
  11. @project_id = "project-id-123"
  12. @user = {
  13. _id: @user_id = "user-id-123"
  14. first_name: "James"
  15. last_name: "Allen"
  16. email: "james@example.com"
  17. signUpDate: new Date("2014-01-01")
  18. loginCount: 42
  19. }
  20. @callback = sinon.stub()
  21. @client =
  22. id: @client_id = "mock-client-id-123"
  23. publicId: "other-id-#{Math.random()}"
  24. params: {}
  25. set: sinon.stub()
  26. get: (param, cb) -> cb null, @params[param]
  27. join: sinon.stub()
  28. leave: sinon.stub()
  29. @WebsocketController = SandboxedModule.require modulePath, requires:
  30. "./WebApiManager": @WebApiManager = {}
  31. "./AuthorizationManager": @AuthorizationManager = {}
  32. "./DocumentUpdaterManager": @DocumentUpdaterManager = {}
  33. "./ConnectedUsersManager": @ConnectedUsersManager = {}
  34. "./WebsocketLoadBalancer": @WebsocketLoadBalancer = {}
  35. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
  36. "metrics-sharelatex": @metrics =
  37. inc: sinon.stub()
  38. set: sinon.stub()
  39. "./RoomManager": @RoomManager = {}
  40. afterEach ->
  41. tk.reset()
  42. describe "joinProject", ->
  43. describe "when authorised", ->
  44. beforeEach ->
  45. @client.id = "mock-client-id"
  46. @project = {
  47. name: "Test Project"
  48. owner: {
  49. _id: @owner_id = "mock-owner-id-123"
  50. }
  51. }
  52. @privilegeLevel = "owner"
  53. @ConnectedUsersManager.updateUserPosition = sinon.stub().callsArg(4)
  54. @isRestrictedUser = true
  55. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, @project, @privilegeLevel, @isRestrictedUser)
  56. @RoomManager.joinProject = sinon.stub().callsArg(2)
  57. @WebsocketController.joinProject @client, @user, @project_id, @callback
  58. it "should load the project from web", ->
  59. @WebApiManager.joinProject
  60. .calledWith(@project_id, @user)
  61. .should.equal true
  62. it "should join the project room", ->
  63. @RoomManager.joinProject.calledWith(@client, @project_id).should.equal true
  64. it "should set the privilege level on the client", ->
  65. @client.set.calledWith("privilege_level", @privilegeLevel).should.equal true
  66. it "should set the user's id on the client", ->
  67. @client.set.calledWith("user_id", @user._id).should.equal true
  68. it "should set the user's email on the client", ->
  69. @client.set.calledWith("email", @user.email).should.equal true
  70. it "should set the user's first_name on the client", ->
  71. @client.set.calledWith("first_name", @user.first_name).should.equal true
  72. it "should set the user's last_name on the client", ->
  73. @client.set.calledWith("last_name", @user.last_name).should.equal true
  74. it "should set the user's sign up date on the client", ->
  75. @client.set.calledWith("signup_date", @user.signUpDate).should.equal true
  76. it "should set the user's login_count on the client", ->
  77. @client.set.calledWith("login_count", @user.loginCount).should.equal true
  78. it "should set the connected time on the client", ->
  79. @client.set.calledWith("connected_time", new Date()).should.equal true
  80. it "should set the project_id on the client", ->
  81. @client.set.calledWith("project_id", @project_id).should.equal true
  82. it "should set the project owner id on the client", ->
  83. @client.set.calledWith("owner_id", @owner_id).should.equal true
  84. it "should set the is_restricted_user flag on the client", ->
  85. @client.set.calledWith("is_restricted_user", @isRestrictedUser).should.equal true
  86. it "should call the callback with the project, privilegeLevel and protocolVersion", ->
  87. @callback
  88. .calledWith(null, @project, @privilegeLevel, @WebsocketController.PROTOCOL_VERSION)
  89. .should.equal true
  90. it "should mark the user as connected in ConnectedUsersManager", ->
  91. @ConnectedUsersManager.updateUserPosition
  92. .calledWith(@project_id, @client.publicId, @user, null)
  93. .should.equal true
  94. it "should increment the join-project metric", ->
  95. @metrics.inc.calledWith("editor.join-project").should.equal true
  96. describe "when not authorized", ->
  97. beforeEach ->
  98. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, null, null)
  99. @WebsocketController.joinProject @client, @user, @project_id, @callback
  100. it "should return an error", ->
  101. @callback
  102. .calledWith(sinon.match({message: "not authorized"}))
  103. .should.equal true
  104. it "should not log an error", ->
  105. @logger.error.called.should.equal false
  106. describe "when the subscribe failed", ->
  107. beforeEach ->
  108. @client.id = "mock-client-id"
  109. @project = {
  110. name: "Test Project"
  111. owner: {
  112. _id: @owner_id = "mock-owner-id-123"
  113. }
  114. }
  115. @privilegeLevel = "owner"
  116. @ConnectedUsersManager.updateUserPosition = sinon.stub().callsArg(4)
  117. @isRestrictedUser = true
  118. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, @project, @privilegeLevel, @isRestrictedUser)
  119. @RoomManager.joinProject = sinon.stub().callsArgWith(2, new Error("subscribe failed"))
  120. @WebsocketController.joinProject @client, @user, @project_id, @callback
  121. it "should return an error", ->
  122. @callback
  123. .calledWith(sinon.match({message: "subscribe failed"}))
  124. .should.equal true
  125. @callback.args[0][0].message.should.equal "subscribe failed"
  126. describe "leaveProject", ->
  127. beforeEach ->
  128. @DocumentUpdaterManager.flushProjectToMongoAndDelete = sinon.stub().callsArg(1)
  129. @ConnectedUsersManager.markUserAsDisconnected = sinon.stub().callsArg(2)
  130. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  131. @RoomManager.leaveProjectAndDocs = sinon.stub()
  132. @clientsInRoom = []
  133. @io =
  134. sockets:
  135. clients: (room_id) =>
  136. if room_id != @project_id
  137. throw "expected room_id to be project_id"
  138. return @clientsInRoom
  139. @client.params.project_id = @project_id
  140. @client.params.user_id = @user_id
  141. @WebsocketController.FLUSH_IF_EMPTY_DELAY = 0
  142. tk.reset() # Allow setTimeout to work.
  143. describe "when the client did not joined a project yet", ->
  144. beforeEach (done) ->
  145. @client.params = {}
  146. @WebsocketController.leaveProject @io, @client, done
  147. it "should bail out when calling leaveProject", () ->
  148. @WebsocketLoadBalancer.emitToRoom.called.should.equal false
  149. @RoomManager.leaveProjectAndDocs.called.should.equal false
  150. @ConnectedUsersManager.markUserAsDisconnected.called.should.equal false
  151. it "should not inc any metric", () ->
  152. @metrics.inc.called.should.equal false
  153. describe "when the project is empty", ->
  154. beforeEach (done) ->
  155. @clientsInRoom = []
  156. @WebsocketController.leaveProject @io, @client, done
  157. it "should end clientTracking.clientDisconnected to the project room", ->
  158. @WebsocketLoadBalancer.emitToRoom
  159. .calledWith(@project_id, "clientTracking.clientDisconnected", @client.publicId)
  160. .should.equal true
  161. it "should mark the user as disconnected", ->
  162. @ConnectedUsersManager.markUserAsDisconnected
  163. .calledWith(@project_id, @client.publicId)
  164. .should.equal true
  165. it "should flush the project in the document updater", ->
  166. @DocumentUpdaterManager.flushProjectToMongoAndDelete
  167. .calledWith(@project_id)
  168. .should.equal true
  169. it "should increment the leave-project metric", ->
  170. @metrics.inc.calledWith("editor.leave-project").should.equal true
  171. it "should track the disconnection in RoomManager", ->
  172. @RoomManager.leaveProjectAndDocs
  173. .calledWith(@client)
  174. .should.equal true
  175. describe "when the project is not empty", ->
  176. beforeEach ->
  177. @clientsInRoom = ["mock-remaining-client"]
  178. @WebsocketController.leaveProject @io, @client
  179. it "should not flush the project in the document updater", ->
  180. @DocumentUpdaterManager.flushProjectToMongoAndDelete
  181. .called.should.equal false
  182. describe "when client has not authenticated", ->
  183. beforeEach (done) ->
  184. @client.params.user_id = null
  185. @client.params.project_id = null
  186. @WebsocketController.leaveProject @io, @client, done
  187. it "should not end clientTracking.clientDisconnected to the project room", ->
  188. @WebsocketLoadBalancer.emitToRoom
  189. .calledWith(@project_id, "clientTracking.clientDisconnected", @client.publicId)
  190. .should.equal false
  191. it "should not mark the user as disconnected", ->
  192. @ConnectedUsersManager.markUserAsDisconnected
  193. .calledWith(@project_id, @client.publicId)
  194. .should.equal false
  195. it "should not flush the project in the document updater", ->
  196. @DocumentUpdaterManager.flushProjectToMongoAndDelete
  197. .calledWith(@project_id)
  198. .should.equal false
  199. it "should not increment the leave-project metric", ->
  200. @metrics.inc.calledWith("editor.leave-project").should.equal false
  201. describe "when client has not joined a project", ->
  202. beforeEach (done) ->
  203. @client.params.user_id = @user_id
  204. @client.params.project_id = null
  205. @WebsocketController.leaveProject @io, @client, done
  206. it "should not end clientTracking.clientDisconnected to the project room", ->
  207. @WebsocketLoadBalancer.emitToRoom
  208. .calledWith(@project_id, "clientTracking.clientDisconnected", @client.publicId)
  209. .should.equal false
  210. it "should not mark the user as disconnected", ->
  211. @ConnectedUsersManager.markUserAsDisconnected
  212. .calledWith(@project_id, @client.publicId)
  213. .should.equal false
  214. it "should not flush the project in the document updater", ->
  215. @DocumentUpdaterManager.flushProjectToMongoAndDelete
  216. .calledWith(@project_id)
  217. .should.equal false
  218. it "should not increment the leave-project metric", ->
  219. @metrics.inc.calledWith("editor.leave-project").should.equal false
  220. describe "joinDoc", ->
  221. beforeEach ->
  222. @doc_id = "doc-id-123"
  223. @doc_lines = ["doc", "lines"]
  224. @version = 42
  225. @ops = ["mock", "ops"]
  226. @ranges = { "mock": "ranges" }
  227. @options = {}
  228. @client.params.project_id = @project_id
  229. @client.params.is_restricted_user = false
  230. @AuthorizationManager.addAccessToDoc = sinon.stub()
  231. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  232. @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(3, null, @doc_lines, @version, @ranges, @ops)
  233. @RoomManager.joinDoc = sinon.stub().callsArg(2)
  234. describe "works", ->
  235. beforeEach ->
  236. @WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
  237. it "should check that the client is authorized to view the project", ->
  238. @AuthorizationManager.assertClientCanViewProject
  239. .calledWith(@client)
  240. .should.equal true
  241. it "should get the document from the DocumentUpdaterManager with fromVersion", ->
  242. @DocumentUpdaterManager.getDocument
  243. .calledWith(@project_id, @doc_id, -1)
  244. .should.equal true
  245. it "should add permissions for the client to access the doc", ->
  246. @AuthorizationManager.addAccessToDoc
  247. .calledWith(@client, @doc_id)
  248. .should.equal true
  249. it "should join the client to room for the doc_id", ->
  250. @RoomManager.joinDoc
  251. .calledWith(@client, @doc_id)
  252. .should.equal true
  253. it "should call the callback with the lines, version, ranges and ops", ->
  254. @callback
  255. .calledWith(null, @doc_lines, @version, @ops, @ranges)
  256. .should.equal true
  257. it "should increment the join-doc metric", ->
  258. @metrics.inc.calledWith("editor.join-doc").should.equal true
  259. describe "with a fromVersion", ->
  260. beforeEach ->
  261. @fromVersion = 40
  262. @WebsocketController.joinDoc @client, @doc_id, @fromVersion, @options, @callback
  263. it "should get the document from the DocumentUpdaterManager with fromVersion", ->
  264. @DocumentUpdaterManager.getDocument
  265. .calledWith(@project_id, @doc_id, @fromVersion)
  266. .should.equal true
  267. describe "with doclines that need escaping", ->
  268. beforeEach ->
  269. @doc_lines.push ["räksmörgås"]
  270. @WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
  271. it "should call the callback with the escaped lines", ->
  272. escaped_lines = @callback.args[0][1]
  273. escaped_word = escaped_lines.pop()
  274. escaped_word.should.equal 'räksmörgås'
  275. # Check that unescaping works
  276. decodeURIComponent(escape(escaped_word)).should.equal "räksmörgås"
  277. describe "with comments that need encoding", ->
  278. beforeEach ->
  279. @ranges.comments = [{ op: { c: "räksmörgås" } }]
  280. @WebsocketController.joinDoc @client, @doc_id, -1, { encodeRanges: true }, @callback
  281. it "should call the callback with the encoded comment", ->
  282. encoded_comments = @callback.args[0][4]
  283. encoded_comment = encoded_comments.comments.pop()
  284. encoded_comment_text = encoded_comment.op.c
  285. encoded_comment_text.should.equal 'räksmörgås'
  286. describe "with changes that need encoding", ->
  287. it "should call the callback with the encoded insert change", ->
  288. @ranges.changes = [{ op: { i: "räksmörgås" } }]
  289. @WebsocketController.joinDoc @client, @doc_id, -1, { encodeRanges: true }, @callback
  290. encoded_changes = @callback.args[0][4]
  291. encoded_change = encoded_changes.changes.pop()
  292. encoded_change_text = encoded_change.op.i
  293. encoded_change_text.should.equal 'räksmörgås'
  294. it "should call the callback with the encoded delete change", ->
  295. @ranges.changes = [{ op: { d: "räksmörgås" } }]
  296. @WebsocketController.joinDoc @client, @doc_id, -1, { encodeRanges: true }, @callback
  297. encoded_changes = @callback.args[0][4]
  298. encoded_change = encoded_changes.changes.pop()
  299. encoded_change_text = encoded_change.op.d
  300. encoded_change_text.should.equal 'räksmörgås'
  301. describe "when not authorized", ->
  302. beforeEach ->
  303. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
  304. @WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
  305. it "should call the callback with an error", ->
  306. @callback.calledWith(sinon.match({message: "not authorized"})).should.equal true
  307. it "should not call the DocumentUpdaterManager", ->
  308. @DocumentUpdaterManager.getDocument.called.should.equal false
  309. describe "with a restricted client", ->
  310. beforeEach ->
  311. @ranges.comments = [{op: {a: 1}}, {op: {a: 2}}]
  312. @client.params.is_restricted_user = true
  313. @WebsocketController.joinDoc @client, @doc_id, -1, @options, @callback
  314. it "should overwrite ranges.comments with an empty list", ->
  315. ranges = @callback.args[0][4]
  316. expect(ranges.comments).to.deep.equal []
  317. describe "leaveDoc", ->
  318. beforeEach ->
  319. @doc_id = "doc-id-123"
  320. @client.params.project_id = @project_id
  321. @RoomManager.leaveDoc = sinon.stub()
  322. @WebsocketController.leaveDoc @client, @doc_id, @callback
  323. it "should remove the client from the doc_id room", ->
  324. @RoomManager.leaveDoc
  325. .calledWith(@client, @doc_id).should.equal true
  326. it "should call the callback", ->
  327. @callback.called.should.equal true
  328. it "should increment the leave-doc metric", ->
  329. @metrics.inc.calledWith("editor.leave-doc").should.equal true
  330. describe "getConnectedUsers", ->
  331. beforeEach ->
  332. @client.params.project_id = @project_id
  333. @users = ["mock", "users"]
  334. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  335. @ConnectedUsersManager.getConnectedUsers = sinon.stub().callsArgWith(1, null, @users)
  336. describe "when authorized", ->
  337. beforeEach (done) ->
  338. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  339. @WebsocketController.getConnectedUsers @client, (args...) =>
  340. @callback(args...)
  341. done()
  342. it "should check that the client is authorized to view the project", ->
  343. @AuthorizationManager.assertClientCanViewProject
  344. .calledWith(@client)
  345. .should.equal true
  346. it "should broadcast a request to update the client list", ->
  347. @WebsocketLoadBalancer.emitToRoom
  348. .calledWith(@project_id, "clientTracking.refresh")
  349. .should.equal true
  350. it "should get the connected users for the project", ->
  351. @ConnectedUsersManager.getConnectedUsers
  352. .calledWith(@project_id)
  353. .should.equal true
  354. it "should return the users", ->
  355. @callback.calledWith(null, @users).should.equal true
  356. it "should increment the get-connected-users metric", ->
  357. @metrics.inc.calledWith("editor.get-connected-users").should.equal true
  358. describe "when not authorized", ->
  359. beforeEach ->
  360. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
  361. @WebsocketController.getConnectedUsers @client, @callback
  362. it "should not get the connected users for the project", ->
  363. @ConnectedUsersManager.getConnectedUsers
  364. .called
  365. .should.equal false
  366. it "should return an error", ->
  367. @callback.calledWith(@err).should.equal true
  368. describe "when restricted user", ->
  369. beforeEach ->
  370. @client.params.is_restricted_user = true
  371. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  372. @WebsocketController.getConnectedUsers @client, @callback
  373. it "should return an empty array of users", ->
  374. @callback.calledWith(null, []).should.equal true
  375. it "should not get the connected users for the project", ->
  376. @ConnectedUsersManager.getConnectedUsers
  377. .called
  378. .should.equal false
  379. describe "updateClientPosition", ->
  380. beforeEach ->
  381. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  382. @ConnectedUsersManager.updateUserPosition = sinon.stub().callsArgWith(4)
  383. @AuthorizationManager.assertClientCanViewProjectAndDoc = sinon.stub().callsArgWith(2, null)
  384. @update = {
  385. doc_id: @doc_id = "doc-id-123"
  386. row: @row = 42
  387. column: @column = 37
  388. }
  389. describe "with a logged in user", ->
  390. beforeEach ->
  391. @clientParams = {
  392. project_id: @project_id
  393. first_name: @first_name = "Douglas"
  394. last_name: @last_name = "Adams"
  395. email: @email = "joe@example.com"
  396. user_id: @user_id = "user-id-123"
  397. }
  398. @client.get = (param, callback) => callback null, @clientParams[param]
  399. @WebsocketController.updateClientPosition @client, @update
  400. @populatedCursorData =
  401. doc_id: @doc_id,
  402. id: @client.publicId
  403. name: "#{@first_name} #{@last_name}"
  404. row: @row
  405. column: @column
  406. email: @email
  407. user_id: @user_id
  408. it "should send the update to the project room with the user's name", ->
  409. @WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
  410. it "should send the cursor data to the connected user manager", (done)->
  411. @ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.publicId, {
  412. _id: @user_id,
  413. email: @email,
  414. first_name: @first_name,
  415. last_name: @last_name
  416. }, {
  417. row: @row
  418. column: @column
  419. doc_id: @doc_id
  420. }).should.equal true
  421. done()
  422. it "should increment the update-client-position metric at 0.1 frequency", ->
  423. @metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
  424. describe "with a logged in user who has no last_name set", ->
  425. beforeEach ->
  426. @clientParams = {
  427. project_id: @project_id
  428. first_name: @first_name = "Douglas"
  429. last_name: undefined
  430. email: @email = "joe@example.com"
  431. user_id: @user_id = "user-id-123"
  432. }
  433. @client.get = (param, callback) => callback null, @clientParams[param]
  434. @WebsocketController.updateClientPosition @client, @update
  435. @populatedCursorData =
  436. doc_id: @doc_id,
  437. id: @client.publicId
  438. name: "#{@first_name}"
  439. row: @row
  440. column: @column
  441. email: @email
  442. user_id: @user_id
  443. it "should send the update to the project room with the user's name", ->
  444. @WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
  445. it "should send the cursor data to the connected user manager", (done)->
  446. @ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.publicId, {
  447. _id: @user_id,
  448. email: @email,
  449. first_name: @first_name,
  450. last_name: undefined
  451. }, {
  452. row: @row
  453. column: @column
  454. doc_id: @doc_id
  455. }).should.equal true
  456. done()
  457. it "should increment the update-client-position metric at 0.1 frequency", ->
  458. @metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
  459. describe "with a logged in user who has no first_name set", ->
  460. beforeEach ->
  461. @clientParams = {
  462. project_id: @project_id
  463. first_name: undefined
  464. last_name: @last_name = "Adams"
  465. email: @email = "joe@example.com"
  466. user_id: @user_id = "user-id-123"
  467. }
  468. @client.get = (param, callback) => callback null, @clientParams[param]
  469. @WebsocketController.updateClientPosition @client, @update
  470. @populatedCursorData =
  471. doc_id: @doc_id,
  472. id: @client.publicId
  473. name: "#{@last_name}"
  474. row: @row
  475. column: @column
  476. email: @email
  477. user_id: @user_id
  478. it "should send the update to the project room with the user's name", ->
  479. @WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
  480. it "should send the cursor data to the connected user manager", (done)->
  481. @ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.publicId, {
  482. _id: @user_id,
  483. email: @email,
  484. first_name: undefined,
  485. last_name: @last_name
  486. }, {
  487. row: @row
  488. column: @column
  489. doc_id: @doc_id
  490. }).should.equal true
  491. done()
  492. it "should increment the update-client-position metric at 0.1 frequency", ->
  493. @metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
  494. describe "with a logged in user who has no names set", ->
  495. beforeEach ->
  496. @clientParams = {
  497. project_id: @project_id
  498. first_name: undefined
  499. last_name: undefined
  500. email: @email = "joe@example.com"
  501. user_id: @user_id = "user-id-123"
  502. }
  503. @client.get = (param, callback) => callback null, @clientParams[param]
  504. @WebsocketController.updateClientPosition @client, @update
  505. it "should send the update to the project name with no name", ->
  506. @WebsocketLoadBalancer.emitToRoom
  507. .calledWith(@project_id, "clientTracking.clientUpdated", {
  508. doc_id: @doc_id,
  509. id: @client.publicId,
  510. user_id: @user_id,
  511. name: "",
  512. row: @row,
  513. column: @column,
  514. email: @email
  515. })
  516. .should.equal true
  517. describe "with an anonymous user", ->
  518. beforeEach ->
  519. @clientParams = {
  520. project_id: @project_id
  521. }
  522. @client.get = (param, callback) => callback null, @clientParams[param]
  523. @WebsocketController.updateClientPosition @client, @update
  524. it "should send the update to the project room with no name", ->
  525. @WebsocketLoadBalancer.emitToRoom
  526. .calledWith(@project_id, "clientTracking.clientUpdated", {
  527. doc_id: @doc_id,
  528. id: @client.publicId
  529. name: ""
  530. row: @row
  531. column: @column
  532. })
  533. .should.equal true
  534. it "should not send cursor data to the connected user manager", (done)->
  535. @ConnectedUsersManager.updateUserPosition.called.should.equal false
  536. done()
  537. describe "applyOtUpdate", ->
  538. beforeEach ->
  539. @update = {op: {p: 12, t: "foo"}}
  540. @client.params.user_id = @user_id
  541. @client.params.project_id = @project_id
  542. @WebsocketController._assertClientCanApplyUpdate = sinon.stub().yields()
  543. @DocumentUpdaterManager.queueChange = sinon.stub().callsArg(3)
  544. describe "succesfully", ->
  545. beforeEach ->
  546. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  547. it "should set the source of the update to the client id", ->
  548. @update.meta.source.should.equal @client.publicId
  549. it "should set the user_id of the update to the user id", ->
  550. @update.meta.user_id.should.equal @user_id
  551. it "should queue the update", ->
  552. @DocumentUpdaterManager.queueChange
  553. .calledWith(@project_id, @doc_id, @update)
  554. .should.equal true
  555. it "should call the callback", ->
  556. @callback.called.should.equal true
  557. it "should increment the doc updates", ->
  558. @metrics.inc.calledWith("editor.doc-update").should.equal true
  559. describe "unsuccessfully", ->
  560. beforeEach ->
  561. @client.disconnect = sinon.stub()
  562. @DocumentUpdaterManager.queueChange = sinon.stub().callsArgWith(3, @error = new Error("Something went wrong"))
  563. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  564. it "should disconnect the client", ->
  565. @client.disconnect.called.should.equal true
  566. it "should log an error", ->
  567. @logger.error.called.should.equal true
  568. it "should call the callback with the error", ->
  569. @callback.calledWith(@error).should.equal true
  570. describe "when not authorized", ->
  571. beforeEach ->
  572. @client.disconnect = sinon.stub()
  573. @WebsocketController._assertClientCanApplyUpdate = sinon.stub().yields(@error = new Error("not authorized"))
  574. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  575. # This happens in a setTimeout to allow the client a chance to receive the error first.
  576. # I'm not sure how to unit test, but it is acceptance tested.
  577. # it "should disconnect the client", ->
  578. # @client.disconnect.called.should.equal true
  579. it "should log a warning", ->
  580. @logger.warn.called.should.equal true
  581. it "should call the callback with the error", ->
  582. @callback.calledWith(@error).should.equal true
  583. describe "update_too_large", ->
  584. beforeEach (done) ->
  585. @client.disconnect = sinon.stub()
  586. @client.emit = sinon.stub()
  587. @client.params.user_id = @user_id
  588. @client.params.project_id = @project_id
  589. error = new Error("update is too large")
  590. error.updateSize = 7372835
  591. @DocumentUpdaterManager.queueChange = sinon.stub().callsArgWith(3, error)
  592. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  593. setTimeout ->
  594. done()
  595. , 201
  596. it "should call the callback with no error", ->
  597. @callback.called.should.equal true
  598. @callback.args[0].should.deep.equal []
  599. it "should log a warning with the size and context", ->
  600. @logger.warn.called.should.equal true
  601. @logger.warn.args[0].should.deep.equal [{
  602. @user_id, @project_id, @doc_id, updateSize: 7372835
  603. }, 'update is too large']
  604. it "should send an otUpdateError the client", ->
  605. @client.emit.calledWith('otUpdateError').should.equal true
  606. it "should disconnect the client", ->
  607. @client.disconnect.called.should.equal true
  608. describe "_assertClientCanApplyUpdate", ->
  609. beforeEach ->
  610. @edit_update = { op: [{i: "foo", p: 42}, {c: "bar", p: 132}] } # comments may still be in an edit op
  611. @comment_update = { op: [{c: "bar", p: 132}] }
  612. @AuthorizationManager.assertClientCanEditProjectAndDoc = sinon.stub()
  613. @AuthorizationManager.assertClientCanViewProjectAndDoc = sinon.stub()
  614. describe "with a read-write client", ->
  615. it "should return successfully", (done) ->
  616. @AuthorizationManager.assertClientCanEditProjectAndDoc.yields(null)
  617. @WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @edit_update, (error) ->
  618. expect(error).to.be.null
  619. done()
  620. describe "with a read-only client and an edit op", ->
  621. it "should return an error", (done) ->
  622. @AuthorizationManager.assertClientCanEditProjectAndDoc.yields(new Error("not authorized"))
  623. @AuthorizationManager.assertClientCanViewProjectAndDoc.yields(null)
  624. @WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @edit_update, (error) ->
  625. expect(error.message).to.equal "not authorized"
  626. done()
  627. describe "with a read-only client and a comment op", ->
  628. it "should return successfully", (done) ->
  629. @AuthorizationManager.assertClientCanEditProjectAndDoc.yields(new Error("not authorized"))
  630. @AuthorizationManager.assertClientCanViewProjectAndDoc.yields(null)
  631. @WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @comment_update, (error) ->
  632. expect(error).to.be.null
  633. done()
  634. describe "with a totally unauthorized client", ->
  635. it "should return an error", (done) ->
  636. @AuthorizationManager.assertClientCanEditProjectAndDoc.yields(new Error("not authorized"))
  637. @AuthorizationManager.assertClientCanViewProjectAndDoc.yields(new Error("not authorized"))
  638. @WebsocketController._assertClientCanApplyUpdate @client, @doc_id, @comment_update, (error) ->
  639. expect(error.message).to.equal "not authorized"
  640. done()