HttpControllerTests.coffee 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. @doc_id = "doc-id-123"
  14. @project_id = "project-id-123"
  15. @version = 42
  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 = Date.now() - 10000
  35. @to = Date.now()
  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 = Date.now()
  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