flushDocIfLoadedTests.coffee 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/HttpController.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors.js"
  7. describe "HttpController - flushDocIfLoaded", ->
  8. beforeEach ->
  9. @HttpController = SandboxedModule.require modulePath, requires:
  10. "./DocumentManager": @DocumentManager = {}
  11. "./ProjectManager": {}
  12. "logger-sharelatex" : @logger = { log: sinon.stub() }
  13. "./Metrics": @Metrics = {}
  14. @Metrics.Timer = class Timer
  15. done: sinon.stub()
  16. @project_id = "project-id-123"
  17. @doc_id = "doc-id-123"
  18. @lines = ["one", "two", "three"]
  19. @version = 42
  20. @res =
  21. send: sinon.stub()
  22. @req =
  23. params:
  24. project_id: @project_id
  25. doc_id: @doc_id
  26. @next = sinon.stub()
  27. describe "successfully", ->
  28. beforeEach ->
  29. @DocumentManager.flushDocIfLoadedWithLock = sinon.stub().callsArgWith(2)
  30. @HttpController.flushDocIfLoaded(@req, @res, @next)
  31. it "should flush the doc", ->
  32. @DocumentManager.flushDocIfLoadedWithLock
  33. .calledWith(@project_id, @doc_id)
  34. .should.equal true
  35. it "should return a successful No Content response", ->
  36. @res.send
  37. .calledWith(204)
  38. .should.equal true
  39. it "should log the request", ->
  40. @logger.log
  41. .calledWith(doc_id: @doc_id, project_id: @project_id, "flushing doc via http")
  42. .should.equal true
  43. it "should time the request", ->
  44. @Metrics.Timer::done.called.should.equal true
  45. describe "when an errors occurs", ->
  46. beforeEach ->
  47. @DocumentManager.flushDocIfLoadedWithLock = sinon.stub().callsArgWith(2, new Error("oops"))
  48. @HttpController.flushDocIfLoaded(@req, @res, @next)
  49. it "should call next with the error", ->
  50. @next
  51. .calledWith(new Error("oops"))
  52. .should.equal true