WebsocketControllerTests.coffee 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. chai = require('chai')
  2. should = chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../app/js/WebsocketController.js"
  5. SandboxedModule = require('sandboxed-module')
  6. tk = require "timekeeper"
  7. describe 'WebsocketController', ->
  8. beforeEach ->
  9. tk.freeze(new Date())
  10. @project_id = "project-id-123"
  11. @user = {
  12. _id: @user_id = "user-id-123"
  13. first_name: "James"
  14. last_name: "Allen"
  15. email: "james@example.com"
  16. signUpDate: new Date("2014-01-01")
  17. loginCount: 42
  18. }
  19. @callback = sinon.stub()
  20. @client =
  21. id: @client_id = "mock-client-id-123"
  22. params: {}
  23. set: sinon.stub()
  24. get: (param, cb) -> cb null, @params[param]
  25. join: sinon.stub()
  26. leave: sinon.stub()
  27. @WebsocketController = SandboxedModule.require modulePath, requires:
  28. "./WebApiManager": @WebApiManager = {}
  29. "./AuthorizationManager": @AuthorizationManager = {}
  30. "./DocumentUpdaterManager": @DocumentUpdaterManager = {}
  31. "./TrackChangesManager": @TrackChangesManager = {}
  32. "./ConnectedUsersManager": @ConnectedUsersManager = {}
  33. "./WebsocketLoadBalancer": @WebsocketLoadBalancer = {}
  34. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  35. "metrics-sharelatex": @metrics =
  36. inc: sinon.stub()
  37. set: sinon.stub()
  38. afterEach ->
  39. tk.reset()
  40. describe "joinProject", ->
  41. describe "when authorised", ->
  42. beforeEach ->
  43. @client.id = "mock-client-id"
  44. @project = {
  45. name: "Test Project"
  46. owner: {
  47. _id: @owner_id = "mock-owner-id-123"
  48. }
  49. }
  50. @privilegeLevel = "owner"
  51. @ConnectedUsersManager.updateUserPosition = sinon.stub().callsArg(4)
  52. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, @project, @privilegeLevel)
  53. @WebsocketController.joinProject @client, @user, @project_id, @callback
  54. it "should load the project from web", ->
  55. @WebApiManager.joinProject
  56. .calledWith(@project_id, @user._id)
  57. .should.equal true
  58. it "should join the project room", ->
  59. @client.join.calledWith(@project_id).should.equal true
  60. it "should set the privilege level on the client", ->
  61. @client.set.calledWith("privilege_level", @privilegeLevel).should.equal true
  62. it "should set the user's id on the client", ->
  63. @client.set.calledWith("user_id", @user._id).should.equal true
  64. it "should set the user's email on the client", ->
  65. @client.set.calledWith("email", @user.email).should.equal true
  66. it "should set the user's first_name on the client", ->
  67. @client.set.calledWith("first_name", @user.first_name).should.equal true
  68. it "should set the user's last_name on the client", ->
  69. @client.set.calledWith("last_name", @user.last_name).should.equal true
  70. it "should set the user's sign up date on the client", ->
  71. @client.set.calledWith("signup_date", @user.signUpDate).should.equal true
  72. it "should set the user's login_count on the client", ->
  73. @client.set.calledWith("login_count", @user.loginCount).should.equal true
  74. it "should set the connected time on the client", ->
  75. @client.set.calledWith("connected_time", new Date()).should.equal true
  76. it "should set the project_id on the client", ->
  77. @client.set.calledWith("project_id", @project_id).should.equal true
  78. it "should set the project owner id on the client", ->
  79. @client.set.calledWith("owner_id", @owner_id).should.equal true
  80. it "should call the callback with the project, privilegeLevel and protocolVersion", ->
  81. @callback
  82. .calledWith(null, @project, @privilegeLevel, @WebsocketController.PROTOCOL_VERSION)
  83. .should.equal true
  84. it "should mark the user as connected in ConnectedUsersManager", ->
  85. @ConnectedUsersManager.updateUserPosition
  86. .calledWith(@project_id, @client.id, @user, null)
  87. .should.equal true
  88. it "should increment the join-project metric", ->
  89. @metrics.inc.calledWith("editor.join-project").should.equal true
  90. describe "when not authorized", ->
  91. beforeEach ->
  92. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, null, null)
  93. @WebsocketController.joinProject @client, @user, @project_id, @callback
  94. it "should return an error", ->
  95. @callback
  96. .calledWith(new Error("not authorized"))
  97. .should.equal true
  98. describe "leaveProject", ->
  99. beforeEach ->
  100. @DocumentUpdaterManager.flushProjectToMongoAndDelete = sinon.stub().callsArg(1)
  101. @TrackChangesManager.flushProject = sinon.stub().callsArg(1)
  102. @ConnectedUsersManager.markUserAsDisconnected = sinon.stub().callsArg(2)
  103. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  104. @clientsInRoom = []
  105. @io =
  106. sockets:
  107. clients: (room_id) =>
  108. if room_id != @project_id
  109. throw "expected room_id to be project_id"
  110. return @clientsInRoom
  111. @client.params.project_id = @project_id
  112. @WebsocketController.FLUSH_IF_EMPTY_DELAY = 0
  113. tk.reset() # Allow setTimeout to work.
  114. describe "when the project is empty", ->
  115. beforeEach (done) ->
  116. @clientsInRoom = []
  117. @WebsocketController.leaveProject @io, @client, done
  118. it "should end clientTracking.clientDisconnected to the project room", ->
  119. @WebsocketLoadBalancer.emitToRoom
  120. .calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
  121. .should.equal true
  122. it "should mark the user as disconnected", ->
  123. @ConnectedUsersManager.markUserAsDisconnected
  124. .calledWith(@project_id, @client.id)
  125. .should.equal true
  126. it "should flush the project in the document updater", ->
  127. @DocumentUpdaterManager.flushProjectToMongoAndDelete
  128. .calledWith(@project_id)
  129. .should.equal true
  130. it "should flush the changes in the track changes api", ->
  131. @TrackChangesManager.flushProject
  132. .calledWith(@project_id)
  133. .should.equal true
  134. it "should increment the leave-project metric", ->
  135. @metrics.inc.calledWith("editor.leave-project").should.equal true
  136. describe "when the project is not empty", ->
  137. beforeEach ->
  138. @clientsInRoom = ["mock-remaining-client"]
  139. @WebsocketController.leaveProject @io, @client
  140. it "should not flush the project in the document updater", ->
  141. @DocumentUpdaterManager.flushProjectToMongoAndDelete
  142. .called.should.equal false
  143. it "should not flush the changes in the track changes api", ->
  144. @TrackChangesManager.flushProject
  145. .called.should.equal false
  146. describe "joinDoc", ->
  147. beforeEach ->
  148. @doc_id = "doc-id-123"
  149. @doc_lines = ["doc", "lines"]
  150. @version = 42
  151. @ops = ["mock", "ops"]
  152. @client.params.project_id = @project_id
  153. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  154. @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(3, null, @doc_lines, @version, @ops)
  155. describe "with a fromVersion", ->
  156. beforeEach ->
  157. @fromVersion = 40
  158. @WebsocketController.joinDoc @client, @doc_id, @fromVersion, @callback
  159. it "should check that the client is authorized to view the project", ->
  160. @AuthorizationManager.assertClientCanViewProject
  161. .calledWith(@client)
  162. .should.equal true
  163. it "should get the document from the DocumentUpdaterManager", ->
  164. @DocumentUpdaterManager.getDocument
  165. .calledWith(@project_id, @doc_id, @fromVersion)
  166. .should.equal true
  167. it "should join the client to room for the doc_id", ->
  168. @client.join
  169. .calledWith(@doc_id)
  170. .should.equal true
  171. it "should call the callback with the lines, version and ops", ->
  172. @callback
  173. .calledWith(null, @doc_lines, @version, @ops)
  174. .should.equal true
  175. it "should increment the join-doc metric", ->
  176. @metrics.inc.calledWith("editor.join-doc").should.equal true
  177. describe "with doclines that need escaping", ->
  178. beforeEach ->
  179. @doc_lines.push ["räksmörgås"]
  180. @WebsocketController.joinDoc @client, @doc_id, -1, @callback
  181. it "should call the callback with the escaped lines", ->
  182. escaped_lines = @callback.args[0][1]
  183. escaped_word = escaped_lines.pop()
  184. escaped_word.should.equal 'räksmörgås'
  185. # Check that unescaping works
  186. decodeURIComponent(escape(escaped_word)).should.equal "räksmörgås"
  187. describe "when not authorized", ->
  188. beforeEach ->
  189. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
  190. @WebsocketController.joinDoc @client, @doc_id, -1, @callback
  191. it "should call the callback with an error", ->
  192. @callback.calledWith(@err).should.equal true
  193. it "should not call the DocumentUpdaterManager", ->
  194. @DocumentUpdaterManager.getDocument.called.should.equal false
  195. describe "leaveDoc", ->
  196. beforeEach ->
  197. @doc_id = "doc-id-123"
  198. @client.params.project_id = @project_id
  199. @WebsocketController.leaveDoc @client, @doc_id, @callback
  200. it "should remove the client from the doc_id room", ->
  201. @client.leave
  202. .calledWith(@doc_id).should.equal true
  203. it "should call the callback", ->
  204. @callback.called.should.equal true
  205. it "should increment the leave-doc metric", ->
  206. @metrics.inc.calledWith("editor.leave-doc").should.equal true
  207. describe "getConnectedUsers", ->
  208. beforeEach ->
  209. @client.params.project_id = @project_id
  210. @users = ["mock", "users"]
  211. @ConnectedUsersManager.getConnectedUsers = sinon.stub().callsArgWith(1, null, @users)
  212. describe "when authorized", ->
  213. beforeEach ->
  214. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  215. @WebsocketController.getConnectedUsers @client, @callback
  216. it "should check that the client is authorized to view the project", ->
  217. @AuthorizationManager.assertClientCanViewProject
  218. .calledWith(@client)
  219. .should.equal true
  220. it "should get the connected users for the project", ->
  221. @ConnectedUsersManager.getConnectedUsers
  222. .calledWith(@project_id)
  223. .should.equal true
  224. it "should return the users", ->
  225. @callback.calledWith(null, @users).should.equal true
  226. it "should increment the get-connected-users metric", ->
  227. @metrics.inc.calledWith("editor.get-connected-users").should.equal true
  228. describe "when not authorized", ->
  229. beforeEach ->
  230. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
  231. @WebsocketController.getConnectedUsers @client, @callback
  232. it "should not get the connected users for the project", ->
  233. @ConnectedUsersManager.getConnectedUsers
  234. .called
  235. .should.equal false
  236. it "should return an error", ->
  237. @callback.calledWith(@err).should.equal true
  238. describe "updateClientPosition", ->
  239. beforeEach ->
  240. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  241. @ConnectedUsersManager.updateUserPosition = sinon.stub().callsArgWith(4)
  242. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  243. @update = {
  244. doc_id: @doc_id = "doc-id-123"
  245. row: @row = 42
  246. column: @column = 37
  247. }
  248. describe "with a logged in user", ->
  249. beforeEach ->
  250. @clientParams = {
  251. project_id: @project_id
  252. first_name: @first_name = "Douglas"
  253. last_name: @last_name = "Adams"
  254. email: @email = "joe@example.com"
  255. user_id: @user_id = "user-id-123"
  256. }
  257. @client.get = (param, callback) => callback null, @clientParams[param]
  258. @WebsocketController.updateClientPosition @client, @update
  259. @populatedCursorData =
  260. doc_id: @doc_id,
  261. id: @client.id
  262. name: "#{@first_name} #{@last_name}"
  263. row: @row
  264. column: @column
  265. email: @email
  266. user_id: @user_id
  267. it "should send the update to the project room with the user's name", ->
  268. @WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
  269. it "should send the cursor data to the connected user manager", (done)->
  270. @ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, {
  271. _id: @user_id,
  272. email: @email,
  273. first_name: @first_name,
  274. last_name: @last_name
  275. }, {
  276. row: @row
  277. column: @column
  278. doc_id: @doc_id
  279. }).should.equal true
  280. done()
  281. it "should increment the update-client-position metric at 0.1 frequency", ->
  282. @metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
  283. describe "with an anonymous user", ->
  284. beforeEach ->
  285. @clientParams = {
  286. project_id: @project_id
  287. }
  288. @client.get = (param, callback) => callback null, @clientParams[param]
  289. @WebsocketController.updateClientPosition @client, @update
  290. it "should send the update to the project room with an anonymous name", ->
  291. @WebsocketLoadBalancer.emitToRoom
  292. .calledWith(@project_id, "clientTracking.clientUpdated", {
  293. doc_id: @doc_id,
  294. id: @client.id
  295. name: "Anonymous"
  296. row: @row
  297. column: @column
  298. })
  299. .should.equal true
  300. it "should not send cursor data to the connected user manager", (done)->
  301. @ConnectedUsersManager.updateUserPosition.called.should.equal false
  302. done()
  303. describe "applyOtUpdate", ->
  304. beforeEach ->
  305. @update = {op: {p: 12, t: "foo"}}
  306. @client.params.user_id = @user_id
  307. @client.params.project_id = @project_id
  308. @AuthorizationManager.assertClientCanEditProject = sinon.stub().callsArg(1)
  309. @DocumentUpdaterManager.queueChange = sinon.stub().callsArg(3)
  310. describe "succesfully", ->
  311. beforeEach ->
  312. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  313. it "should set the source of the update to the client id", ->
  314. @update.meta.source.should.equal @client.id
  315. it "should set the user_id of the update to the user id", ->
  316. @update.meta.user_id.should.equal @user_id
  317. it "should queue the update", ->
  318. @DocumentUpdaterManager.queueChange
  319. .calledWith(@project_id, @doc_id, @update)
  320. .should.equal true
  321. it "should call the callback", ->
  322. @callback.called.should.equal true
  323. it "should update the active users metric", ->
  324. @metrics.set.calledWith("editor.active-users", @user_id).should.equal true
  325. it "should update the active projects metric", ->
  326. @metrics.set.calledWith("editor.active-projects", @project_id).should.equal true
  327. it "should increment the doc updates", ->
  328. @metrics.inc.calledWith("editor.doc-update").should.equal true
  329. describe "unsuccessfully", ->
  330. beforeEach ->
  331. @client.disconnect = sinon.stub()
  332. @DocumentUpdaterManager.queueChange = sinon.stub().callsArgWith(3, @error = new Error("Something went wrong"))
  333. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  334. it "should disconnect the client", ->
  335. @client.disconnect.called.should.equal true
  336. it "should log an error", ->
  337. @logger.error.called.should.equal true
  338. it "should call the callback with the error", ->
  339. @callback.calledWith(@error).should.equal true
  340. describe "when not authorized", ->
  341. beforeEach ->
  342. @client.disconnect = sinon.stub()
  343. @AuthorizationManager.assertClientCanEditProject = sinon.stub().callsArgWith(1, @error = new Error("not authorized"))
  344. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  345. # This happens in a setTimeout to allow the client a chance to receive the error first.
  346. # I'm not sure how to unit test, but it is acceptance tested.
  347. # it "should disconnect the client", ->
  348. # @client.disconnect.called.should.equal true
  349. it "should log an error", ->
  350. @logger.error.called.should.equal true
  351. it "should call the callback with the error", ->
  352. @callback.calledWith(@error).should.equal true