HttpControllerTests.coffee 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "./PackManager": @PackManager = {}
  15. "./DocArchiveManager": @DocArchiveManager = {}
  16. @doc_id = "doc-id-123"
  17. @project_id = "project-id-123"
  18. @next = sinon.stub()
  19. @user_id = "mock-user-123"
  20. @now = Date.now()
  21. describe "flushDoc", ->
  22. beforeEach ->
  23. @req =
  24. params:
  25. doc_id: @doc_id
  26. project_id: @project_id
  27. @res =
  28. send: sinon.stub()
  29. @UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(2)
  30. @HttpController.flushDoc @req, @res, @next
  31. it "should process the updates", ->
  32. @UpdatesManager.processUncompressedUpdatesWithLock
  33. .calledWith(@project_id, @doc_id)
  34. .should.equal true
  35. it "should return a success code", ->
  36. @res.send.calledWith(204).should.equal true
  37. describe "flushProject", ->
  38. beforeEach ->
  39. @req =
  40. params:
  41. project_id: @project_id
  42. @res =
  43. send: sinon.stub()
  44. @UpdatesManager.processUncompressedUpdatesForProject = sinon.stub().callsArg(1)
  45. @HttpController.flushProject @req, @res, @next
  46. it "should process the updates", ->
  47. @UpdatesManager.processUncompressedUpdatesForProject
  48. .calledWith(@project_id)
  49. .should.equal true
  50. it "should return a success code", ->
  51. @res.send.calledWith(204).should.equal true
  52. describe "getDiff", ->
  53. beforeEach ->
  54. @from = 42
  55. @to = 45
  56. @req =
  57. params:
  58. doc_id: @doc_id
  59. project_id: @project_id
  60. query:
  61. from: @from.toString()
  62. to: @to.toString()
  63. @res =
  64. send: sinon.stub()
  65. @diff = [ u: "mock-diff" ]
  66. @DiffManager.getDiff = sinon.stub().callsArgWith(4, null, @diff)
  67. @HttpController.getDiff @req, @res, @next
  68. it "should get the diff", ->
  69. @DiffManager.getDiff
  70. .calledWith(@project_id, @doc_id, parseInt(@from, 10), parseInt(@to, 10))
  71. .should.equal true
  72. it "should return the diff", ->
  73. @res.send.calledWith(JSON.stringify(diff: @diff)).should.equal true
  74. describe "getUpdates", ->
  75. beforeEach ->
  76. @before = Date.now()
  77. @nextBeforeTimestamp = @before - 100
  78. @min_count = 10
  79. @req =
  80. params:
  81. project_id: @project_id
  82. query:
  83. before: @before.toString()
  84. min_count: @min_count.toString()
  85. @res =
  86. send: sinon.stub()
  87. @updates = ["mock-summarized-updates"]
  88. @UpdatesManager.getSummarizedProjectUpdates = sinon.stub().callsArgWith(2, null, @updates, @nextBeforeTimestamp)
  89. @HttpController.getUpdates @req, @res, @next
  90. it "should get the updates", ->
  91. @UpdatesManager.getSummarizedProjectUpdates
  92. .calledWith(@project_id, before: @before, min_count: @min_count)
  93. .should.equal true
  94. it "should return the formatted updates", ->
  95. @res.send.calledWith(JSON.stringify(updates: @updates, nextBeforeTimestamp: @nextBeforeTimestamp)).should.equal true
  96. describe "RestoreManager", ->
  97. beforeEach ->
  98. @version = "42"
  99. @req =
  100. params:
  101. doc_id: @doc_id
  102. project_id: @project_id
  103. version: @version
  104. headers:
  105. "x-user-id": @user_id
  106. @res =
  107. send: sinon.stub()
  108. @RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(4)
  109. @HttpController.restore @req, @res, @next
  110. it "should restore the document", ->
  111. @RestoreManager.restoreToBeforeVersion
  112. .calledWith(@project_id, @doc_id, parseInt(@version, 10), @user_id)
  113. .should.equal true
  114. it "should return a success code", ->
  115. @res.send.calledWith(204).should.equal true
  116. describe "archiveProject", ->
  117. beforeEach ->
  118. @req =
  119. params:
  120. project_id: @project_id
  121. @res =
  122. send: sinon.stub()
  123. @DocArchiveManager.archiveAllDocsChanges = sinon.stub().callsArg(1)
  124. @HttpController.archiveProject @req, @res, @next
  125. it "should process archive doc changes", ->
  126. @DocArchiveManager.archiveAllDocsChanges
  127. .calledWith(@project_id)
  128. .should.equal true
  129. it "should return a success code", ->
  130. @res.send.calledWith(204).should.equal true
  131. describe "unArchiveProject", ->
  132. beforeEach ->
  133. @req =
  134. params:
  135. project_id: @project_id
  136. @res =
  137. send: sinon.stub()
  138. @DocArchiveManager.unArchiveAllDocsChanges = sinon.stub().callsArg(1)
  139. @HttpController.unArchiveProject @req, @res, @next
  140. it "should process unarchive doc changes", ->
  141. @DocArchiveManager.unArchiveAllDocsChanges
  142. .calledWith(@project_id)
  143. .should.equal true
  144. it "should return a success code", ->
  145. @res.send.calledWith(204).should.equal true