WebsocketControllerTests.coffee 26 KB

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