HttpControllerTests.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. describe "flushUpdatesWithLock", ->
  18. beforeEach ->
  19. @req =
  20. params:
  21. doc_id: @doc_id
  22. @res =
  23. send: sinon.stub()
  24. @UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(1)
  25. @HttpController.flushUpdatesWithLock @req, @res, @next
  26. it "should process the updates", ->
  27. @UpdatesManager.processUncompressedUpdatesWithLock
  28. .calledWith(@doc_id)
  29. .should.equal true
  30. it "should return a success code", ->
  31. @res.send.calledWith(204).should.equal true
  32. describe "getDiff", ->
  33. beforeEach ->
  34. @from = 42
  35. @to = 45
  36. @req =
  37. params:
  38. doc_id: @doc_id
  39. project_id: @project_id
  40. query:
  41. from: @from.toString()
  42. to: @to.toString()
  43. @res =
  44. send: sinon.stub()
  45. @diff = [ u: "mock-diff" ]
  46. @DiffManager.getDiff = sinon.stub().callsArgWith(4, null, @diff)
  47. @HttpController.getDiff @req, @res, @next
  48. it "should get the diff", ->
  49. @DiffManager.getDiff
  50. .calledWith(@project_id, @doc_id, parseInt(@from, 10), parseInt(@to, 10))
  51. .should.equal true
  52. it "should return the diff", ->
  53. @res.send.calledWith(JSON.stringify(diff: @diff)).should.equal true
  54. describe "getUpdates", ->
  55. beforeEach ->
  56. @to = 42
  57. @limit = 10
  58. @req =
  59. params:
  60. doc_id: @doc_id
  61. project_id: @project_id
  62. query:
  63. to: @to.toString()
  64. limit: @limit.toString()
  65. @res =
  66. send: sinon.stub()
  67. @rawUpdates = [{
  68. v: @v = 42
  69. op: @op = "mock-op"
  70. meta: @meta = "mock-meta"
  71. doc_id: @doc_id
  72. }]
  73. @UpdatesManager.getUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @rawUpdates)
  74. @HttpController.getUpdates @req, @res, @next
  75. it "should get the updates", ->
  76. @UpdatesManager.getUpdatesWithUserInfo
  77. .calledWith(@doc_id, to: @to, limit: @limit)
  78. .should.equal true
  79. it "should return the formatted updates", ->
  80. updates = for update in @rawUpdates
  81. {
  82. meta: @meta
  83. v: @v
  84. }
  85. @res.send.calledWith(JSON.stringify(updates: updates)).should.equal true
  86. describe "RestoreManager", ->
  87. beforeEach ->
  88. @version = "42"
  89. @req =
  90. params:
  91. doc_id: @doc_id
  92. project_id: @project_id
  93. version: @version
  94. @res =
  95. send: sinon.stub()
  96. @RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(3)
  97. @HttpController.restore @req, @res, @next
  98. it "should restore the document", ->
  99. @RestoreManager.restoreToBeforeVersion
  100. .calledWith(@project_id, @doc_id, parseInt(@version, 10))
  101. .should.equal true
  102. it "should return a success code", ->
  103. @res.send.calledWith(204).should.equal true