HttpControllerTests.coffee 4.7 KB

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