HttpControllerTests.coffee 3.6 KB

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