WebsocketControllerTests.coffee 9.8 KB

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