HttpControllerTests.coffee 3.1 KB

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