WebsocketControllerTests.coffee 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  31. afterEach ->
  32. tk.reset()
  33. describe "joinProject", ->
  34. describe "when authorised", ->
  35. beforeEach ->
  36. @project = {
  37. name: "Test Project"
  38. owner: {
  39. _id: @owner_id = "mock-owner-id-123"
  40. }
  41. }
  42. @privilegeLevel = "owner"
  43. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, @project, @privilegeLevel)
  44. @WebsocketController.joinProject @client, @user, @project_id, @callback
  45. it "should load the project from web", ->
  46. @WebApiManager.joinProject
  47. .calledWith(@project_id, @user._id)
  48. .should.equal true
  49. it "should join the project room", ->
  50. @client.join.calledWith(@project_id).should.equal true
  51. it "should set the privilege level on the client", ->
  52. @client.set.calledWith("privilege_level", @privilegeLevel).should.equal true
  53. it "should set the user's id on the client", ->
  54. @client.set.calledWith("user_id", @user._id).should.equal true
  55. it "should set the user's email on the client", ->
  56. @client.set.calledWith("email", @user.email).should.equal true
  57. it "should set the user's first_name on the client", ->
  58. @client.set.calledWith("first_name", @user.first_name).should.equal true
  59. it "should set the user's last_name on the client", ->
  60. @client.set.calledWith("last_name", @user.last_name).should.equal true
  61. it "should set the user's sign up date on the client", ->
  62. @client.set.calledWith("signup_date", @user.signUpDate).should.equal true
  63. it "should set the user's login_count on the client", ->
  64. @client.set.calledWith("login_count", @user.loginCount).should.equal true
  65. it "should set the connected time on the client", ->
  66. @client.set.calledWith("connected_time", new Date()).should.equal true
  67. it "should set the project_id on the client", ->
  68. @client.set.calledWith("project_id", @project_id).should.equal true
  69. it "should set the project owner id on the client", ->
  70. @client.set.calledWith("owner_id", @owner_id).should.equal true
  71. it "should call the callback with the project, privilegeLevel and protocolVersion", ->
  72. @callback
  73. .calledWith(null, @project, @privilegeLevel, @WebsocketController.PROTOCOL_VERSION)
  74. .should.equal true
  75. describe "when not authorized", ->
  76. beforeEach ->
  77. @WebApiManager.joinProject = sinon.stub().callsArgWith(2, null, null, null)
  78. @WebsocketController.joinProject @client, @user, @project_id, @callback
  79. it "should return an error", ->
  80. @callback
  81. .calledWith(new Error("not authorized"))
  82. .should.equal true
  83. describe "joinDoc", ->
  84. beforeEach ->
  85. @doc_id = "doc-id-123"
  86. @doc_lines = ["doc", "lines"]
  87. @version = 42
  88. @ops = ["mock", "ops"]
  89. @client.params.project_id = @project_id
  90. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, null)
  91. @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(3, null, @doc_lines, @version, @ops)
  92. describe "with a fromVersion", ->
  93. beforeEach ->
  94. @fromVersion = 40
  95. @WebsocketController.joinDoc @client, @doc_id, @fromVersion, @callback
  96. it "should check that the client is authorized to view the project", ->
  97. @AuthorizationManager.assertClientCanViewProject
  98. .calledWith(@client)
  99. .should.equal true
  100. it "should get the document from the DocumentUpdaterManager", ->
  101. @DocumentUpdaterManager.getDocument
  102. .calledWith(@project_id, @doc_id, @fromVersion)
  103. .should.equal true
  104. it "should join the client to room for the doc_id", ->
  105. @client.join
  106. .calledWith(@doc_id)
  107. .should.equal true
  108. it "should call the callback with the lines, version and ops", ->
  109. @callback
  110. .calledWith(null, @doc_lines, @version, @ops)
  111. .should.equal true
  112. describe "with doclines that need escaping", ->
  113. beforeEach ->
  114. @doc_lines.push ["räksmörgås"]
  115. @WebsocketController.joinDoc @client, @doc_id, -1, @callback
  116. it "should call the callback with the escaped lines", ->
  117. escaped_lines = @callback.args[0][1]
  118. escaped_word = escaped_lines.pop()
  119. escaped_word.should.equal 'räksmörgås'
  120. # Check that unescaping works
  121. decodeURIComponent(escape(escaped_word)).should.equal "räksmörgås"
  122. describe "when not authorized", ->
  123. beforeEach ->
  124. @AuthorizationManager.assertClientCanViewProject = sinon.stub().callsArgWith(1, @err = new Error("not authorized"))
  125. @WebsocketController.joinDoc @client, @doc_id, -1, @callback
  126. it "should call the callback with an error", ->
  127. @callback.calledWith(@err).should.equal true
  128. it "should not call the DocumentUpdaterManager", ->
  129. @DocumentUpdaterManager.getDocument.called.should.equal false
  130. describe "leaveDoc", ->
  131. beforeEach ->
  132. @doc_id = "doc-id-123"
  133. @client.params.project_id = @project_id
  134. @WebsocketController.leaveDoc @client, @doc_id, @callback
  135. it "should remove the client from the doc_id room", ->
  136. @client.leave
  137. .calledWith(@doc_id).should.equal true
  138. it "should call the callback", ->
  139. @callback.called.should.equal true