WebsocketControllerTests.coffee 27 KB

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