HttpControllerTests.coffee 3.7 KB

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