HttpControllerTests.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/HttpController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "HttpController", ->
  8. beforeEach ->
  9. @HttpController = SandboxedModule.require modulePath, requires:
  10. "logger-sharelatex": { log: sinon.stub() }
  11. "./UpdatesManager": @UpdatesManager = {}
  12. "./DiffManager": @DiffManager = {}
  13. "./RestoreManager": @RestoreManager = {}
  14. @doc_id = "doc-id-123"
  15. @project_id = "project-id-123"
  16. @next = sinon.stub()
  17. @user_id = "mock-user-123"
  18. @now = Date.now()
  19. describe "flushUpdatesWithLock", ->
  20. beforeEach ->
  21. @req =
  22. params:
  23. doc_id: @doc_id
  24. @res =
  25. send: sinon.stub()
  26. @UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(1)
  27. @HttpController.flushUpdatesWithLock @req, @res, @next
  28. it "should process the updates", ->
  29. @UpdatesManager.processUncompressedUpdatesWithLock
  30. .calledWith(@doc_id)
  31. .should.equal true
  32. it "should return a success code", ->
  33. @res.send.calledWith(204).should.equal true
  34. describe "getDiff", ->
  35. beforeEach ->
  36. @from = 42
  37. @to = 45
  38. @req =
  39. params:
  40. doc_id: @doc_id
  41. project_id: @project_id
  42. query:
  43. from: @from.toString()
  44. to: @to.toString()
  45. @res =
  46. send: sinon.stub()
  47. @diff = [ u: "mock-diff" ]
  48. @DiffManager.getDiff = sinon.stub().callsArgWith(4, null, @diff)
  49. @HttpController.getDiff @req, @res, @next
  50. it "should get the diff", ->
  51. @DiffManager.getDiff
  52. .calledWith(@project_id, @doc_id, parseInt(@from, 10), parseInt(@to, 10))
  53. .should.equal true
  54. it "should return the diff", ->
  55. @res.send.calledWith(JSON.stringify(diff: @diff)).should.equal true
  56. describe "getUpdates", ->
  57. beforeEach ->
  58. @to = 42
  59. @limit = 10
  60. @req =
  61. params:
  62. doc_id: @doc_id
  63. project_id: @project_id
  64. query:
  65. to: @to.toString()
  66. limit: @limit.toString()
  67. @res =
  68. send: sinon.stub()
  69. @rawUpdates = ["raw-updates"]
  70. @updatesView = ["updates-view"]
  71. @HttpController._buildUpdatesView = sinon.stub().returns(@updatesView)
  72. @UpdatesManager.getUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @rawUpdates)
  73. @HttpController.getUpdates @req, @res, @next
  74. it "should get the updates", ->
  75. @UpdatesManager.getUpdatesWithUserInfo
  76. .calledWith(@doc_id, to: @to, limit: @limit)
  77. .should.equal true
  78. it "should build the updates view", ->
  79. @HttpController._buildUpdatesView
  80. .calledWith(@rawUpdates)
  81. .should.equal true
  82. it "should return the formatted updates", ->
  83. @res.send.calledWith(JSON.stringify(updates: @updatesView)).should.equal true
  84. describe "_buildUpdatesView", ->
  85. it "should concat updates that are close in time", ->
  86. expect(@HttpController._buildUpdatesView [{
  87. meta:
  88. user: @user_2 = { id: "mock-user-2" }
  89. start_ts: @now + 20
  90. end_ts: @now + 30
  91. v: 5
  92. }, {
  93. meta:
  94. user: @user_1 = { id: "mock-user-1" }
  95. start_ts: @now
  96. end_ts: @now + 10
  97. v: 4
  98. }]).to.deep.equal [{
  99. meta:
  100. users: [@user_1, @user_2]
  101. start_ts: @now
  102. end_ts: @now + 30
  103. fromV: 4
  104. toV: 5
  105. }]
  106. it "should leave updates that are far apart in time", ->
  107. oneDay = 1000 * 60 * 60 * 24
  108. expect(@HttpController._buildUpdatesView [{
  109. meta:
  110. user: @user_2 = { id: "mock-user-2" }
  111. start_ts: @now + oneDay
  112. end_ts: @now + oneDay + 10
  113. v: 5
  114. }, {
  115. meta:
  116. user: @user_1 = { id: "mock-user-2" }
  117. start_ts: @now
  118. end_ts: @now + 10
  119. v: 4
  120. }]).to.deep.equal [{
  121. meta:
  122. users: [@user_2]
  123. start_ts: @now + oneDay
  124. end_ts: @now + oneDay + 10
  125. fromV: 5
  126. toV: 5
  127. }, {
  128. meta:
  129. users: [@user_1]
  130. start_ts: @now
  131. end_ts: @now + 10
  132. fromV: 4
  133. toV: 4
  134. }]
  135. describe "RestoreManager", ->
  136. beforeEach ->
  137. @version = "42"
  138. @req =
  139. params:
  140. doc_id: @doc_id
  141. project_id: @project_id
  142. version: @version
  143. headers:
  144. "x-user-id": @user_id
  145. @res =
  146. send: sinon.stub()
  147. @RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(4)
  148. @HttpController.restore @req, @res, @next
  149. it "should restore the document", ->
  150. @RestoreManager.restoreToBeforeVersion
  151. .calledWith(@project_id, @doc_id, parseInt(@version, 10), @user_id)
  152. .should.equal true
  153. it "should return a success code", ->
  154. @res.send.calledWith(204).should.equal true