HttpControllerTests.coffee 3.0 KB

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