HttpControllerTests.coffee 3.0 KB

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