WebsocketControllerTests.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. "./ConnectedUsersManager": @ConnectedUsersManager = {}
  32. "./WebsocketLoadBalancer": @WebsocketLoadBalancer = {}
  33. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  34. afterEach ->
  35. tk.reset()
  36. describe "joinProject", ->
  37. describe "when authorised", ->
  38. beforeEach ->
  39. @client.id = "mock-client-id"
  40. @project = {
  41. name: "Test Project"
  42. owner: {
  43. _id: @owner_id = "mock-owner-id-123"
  44. }
  45. }
  46. @privilegeLevel = "owner"
  47. @ConnectedUsersManager.updateUserPosition = sinon.stub().callsArg(4)
  48. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, @project, @privilegeLevel)
  49. @WebsocketController.joinProject @client, @user, @project_id, @callback
  50. it "should load the project from web", ->
  51. @WebApiManager.joinProject
  52. .calledWith(@project_id, @user._id)
  53. .should.equal true
  54. it "should join the project room", ->
  55. @client.join.calledWith(@project_id).should.equal true
  56. it "should set the privilege level on the client", ->
  57. @client.set.calledWith("privilege_level", @privilegeLevel).should.equal true
  58. it "should set the user's id on the client", ->
  59. @client.set.calledWith("user_id", @user._id).should.equal true
  60. it "should set the user's email on the client", ->
  61. @client.set.calledWith("email", @user.email).should.equal true
  62. it "should set the user's first_name on the client", ->
  63. @client.set.calledWith("first_name", @user.first_name).should.equal true
  64. it "should set the user's last_name on the client", ->
  65. @client.set.calledWith("last_name", @user.last_name).should.equal true
  66. it "should set the user's sign up date on the client", ->
  67. @client.set.calledWith("signup_date", @user.signUpDate).should.equal true
  68. it "should set the user's login_count on the client", ->
  69. @client.set.calledWith("login_count", @user.loginCount).should.equal true
  70. it "should set the connected time on the client", ->
  71. @client.set.calledWith("connected_time", new Date()).should.equal true
  72. it "should set the project_id on the client", ->
  73. @client.set.calledWith("project_id", @project_id).should.equal true
  74. it "should set the project owner id on the client", ->
  75. @client.set.calledWith("owner_id", @owner_id).should.equal true
  76. it "should call the callback with the project, privilegeLevel and protocolVersion", ->
  77. @callback
  78. .calledWith(null, @project, @privilegeLevel, @WebsocketController.PROTOCOL_VERSION)
  79. .should.equal true
  80. it "should mark the user as connected in ConnectedUsersManager", ->
  81. @ConnectedUsersManager.updateUserPosition
  82. .calledWith(@project_id, @client.id, @user, null)
  83. .should.equal true
  84. describe "when not authorized", ->
  85. beforeEach ->
  86. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, null, null)
  87. @WebsocketController.joinProject @client, @user, @project_id, @callback
  88. it "should return an error", ->
  89. @callback
  90. .calledWith(new Error("not authorized"))
  91. .should.equal true
  92. describe "joinDoc", ->
  93. beforeEach ->
  94. @doc_id = "doc-id-123"
  95. @doc_lines = ["doc", "lines"]
  96. @version = 42
  97. @ops = ["mock", "ops"]
  98. @client.params.project_id = @project_id
  99. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  100. @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(3, null, @doc_lines, @version, @ops)
  101. describe "with a fromVersion", ->
  102. beforeEach ->
  103. @fromVersion = 40
  104. @WebsocketController.joinDoc @client, @doc_id, @fromVersion, @callback
  105. it "should check that the client is authorized to view the project", ->
  106. @AuthorizationManager.assertClientCanViewProject
  107. .calledWith(@client)
  108. .should.equal true
  109. it "should get the document from the DocumentUpdaterManager", ->
  110. @DocumentUpdaterManager.getDocument
  111. .calledWith(@project_id, @doc_id, @fromVersion)
  112. .should.equal true
  113. it "should join the client to room for the doc_id", ->
  114. @client.join
  115. .calledWith(@doc_id)
  116. .should.equal true
  117. it "should call the callback with the lines, version and ops", ->
  118. @callback
  119. .calledWith(null, @doc_lines, @version, @ops)
  120. .should.equal true
  121. describe "with doclines that need escaping", ->
  122. beforeEach ->
  123. @doc_lines.push ["räksmörgås"]
  124. @WebsocketController.joinDoc @client, @doc_id, -1, @callback
  125. it "should call the callback with the escaped lines", ->
  126. escaped_lines = @callback.args[0][1]
  127. escaped_word = escaped_lines.pop()
  128. escaped_word.should.equal 'räksmörgås'
  129. # Check that unescaping works
  130. decodeURIComponent(escape(escaped_word)).should.equal "räksmörgås"
  131. describe "when not authorized", ->
  132. beforeEach ->
  133. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
  134. @WebsocketController.joinDoc @client, @doc_id, -1, @callback
  135. it "should call the callback with an error", ->
  136. @callback.calledWith(@err).should.equal true
  137. it "should not call the DocumentUpdaterManager", ->
  138. @DocumentUpdaterManager.getDocument.called.should.equal false
  139. describe "leaveDoc", ->
  140. beforeEach ->
  141. @doc_id = "doc-id-123"
  142. @client.params.project_id = @project_id
  143. @WebsocketController.leaveDoc @client, @doc_id, @callback
  144. it "should remove the client from the doc_id room", ->
  145. @client.leave
  146. .calledWith(@doc_id).should.equal true
  147. it "should call the callback", ->
  148. @callback.called.should.equal true
  149. describe "getConnectedUsers", ->
  150. beforeEach ->
  151. @client.params.project_id = @project_id
  152. @users = ["mock", "users"]
  153. @ConnectedUsersManager.getConnectedUsers = sinon.stub().callsArgWith(1, null, @users)
  154. describe "when authorized", ->
  155. beforeEach ->
  156. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  157. @WebsocketController.getConnectedUsers @client, @callback
  158. it "should check that the client is authorized to view the project", ->
  159. @AuthorizationManager.assertClientCanViewProject
  160. .calledWith(@client)
  161. .should.equal true
  162. it "should get the connected users for the project", ->
  163. @ConnectedUsersManager.getConnectedUsers
  164. .calledWith(@project_id)
  165. .should.equal true
  166. it "should return the users", ->
  167. @callback.calledWith(null, @users).should.equal true
  168. describe "when not authorized", ->
  169. beforeEach ->
  170. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
  171. @WebsocketController.getConnectedUsers @client, @callback
  172. it "should not get the connected users for the project", ->
  173. @ConnectedUsersManager.getConnectedUsers
  174. .called
  175. .should.equal false
  176. it "should return an error", ->
  177. @callback.calledWith(@err).should.equal true
  178. describe "updateClientPosition", ->
  179. beforeEach ->
  180. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  181. @ConnectedUsersManager.updateUserPosition = sinon.stub().callsArgWith(4)
  182. @update = {
  183. doc_id: @doc_id = "doc-id-123"
  184. row: @row = 42
  185. column: @column = 37
  186. }
  187. describe "with a logged in user", ->
  188. beforeEach ->
  189. @clientParams = {
  190. project_id: @project_id
  191. first_name: @first_name = "Douglas"
  192. last_name: @last_name = "Adams"
  193. email: @email = "joe@example.com"
  194. user_id: @user_id = "user-id-123"
  195. }
  196. @client.get = (param, callback) => callback null, @clientParams[param]
  197. @WebsocketController.updateClientPosition @client, @update
  198. @populatedCursorData =
  199. doc_id: @doc_id,
  200. id: @client.id
  201. name: "#{@first_name} #{@last_name}"
  202. row: @row
  203. column: @column
  204. email: @email
  205. user_id: @user_id
  206. it "should send the update to the project room with the user's name", ->
  207. @WebsocketLoadBalancer.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
  208. it "should send the cursor data to the connected user manager", (done)->
  209. @ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, {
  210. user_id: @user_id,
  211. email: @email,
  212. first_name: @first_name,
  213. last_name: @last_name
  214. }, {
  215. row: @row
  216. column: @column
  217. doc_id: @doc_id
  218. }).should.equal true
  219. done()
  220. describe "with an anonymous user", ->
  221. beforeEach ->
  222. @clientParams = {
  223. project_id: @project_id
  224. }
  225. @client.get = (param, callback) => callback null, @clientParams[param]
  226. @WebsocketController.updateClientPosition @client, @update
  227. it "should send the update to the project room with an anonymous name", ->
  228. @WebsocketLoadBalancer.emitToRoom
  229. .calledWith(@project_id, "clientTracking.clientUpdated", {
  230. doc_id: @doc_id,
  231. id: @client.id
  232. name: "Anonymous"
  233. row: @row
  234. column: @column
  235. })
  236. .should.equal true
  237. it "should not send cursor data to the connected user manager", (done)->
  238. @ConnectedUsersManager.updateUserPosition.called.should.equal false
  239. done()
  240. describe "applyOtUpdate", ->
  241. beforeEach ->
  242. @update = {op: {p: 12, t: "foo"}}
  243. @client.params.user_id = @user_id
  244. @client.params.project_id = @project_id
  245. @AuthorizationManager.assertClientCanEditProject = sinon.stub().callsArg(1)
  246. @DocumentUpdaterManager.queueChange = sinon.stub().callsArg(3)
  247. describe "succesfully", ->
  248. beforeEach ->
  249. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  250. it "should set the source of the update to the client id", ->
  251. @update.meta.source.should.equal @client.id
  252. it "should set the user_id of the update to the user id", ->
  253. @update.meta.user_id.should.equal @user_id
  254. it "should queue the update", ->
  255. @DocumentUpdaterManager.queueChange
  256. .calledWith(@project_id, @doc_id, @update)
  257. .should.equal true
  258. it "should call the callback", ->
  259. @callback.called.should.equal true
  260. # it "should update the active users metric", ->
  261. # @metrics.set.calledWith("editor.active-users", @user_id).should.equal true
  262. #
  263. # it "should update the active projects metric", ->
  264. # @metrics.set.calledWith("editor.active-projects", @project_id).should.equal true
  265. #
  266. # it "should increment the doc updates", ->
  267. # @metrics.inc.calledWith("editor.doc-update").should.equal true
  268. describe "unsuccessfully", ->
  269. beforeEach ->
  270. @client.disconnect = sinon.stub()
  271. @DocumentUpdaterManager.queueChange = sinon.stub().callsArgWith(3, @error = new Error("Something went wrong"))
  272. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  273. it "should disconnect the client", ->
  274. @client.disconnect.called.should.equal true
  275. it "should log an error", ->
  276. @logger.error.called.should.equal true
  277. it "should call the callback with the error", ->
  278. @callback.calledWith(@error).should.equal true
  279. describe "when not authorized", ->
  280. beforeEach ->
  281. @client.disconnect = sinon.stub()
  282. @AuthorizationManager.assertClientCanEditProject = sinon.stub().callsArgWith(1, @error = new Error("not authorized"))
  283. @WebsocketController.applyOtUpdate @client, @doc_id, @update, @callback
  284. it "should disconnect the client", ->
  285. @client.disconnect.called.should.equal true
  286. it "should log an error", ->
  287. @logger.error.called.should.equal true
  288. it "should call the callback with the error", ->
  289. @callback.calledWith(@error).should.equal true