WebsocketControllerTests.coffee 33 KB

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