flushAndDeleteDocTests.coffee 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.flushAndDeleteDoc", ->
  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. @res =
  19. send: sinon.stub()
  20. @req =
  21. params:
  22. project_id: @project_id
  23. doc_id: @doc_id
  24. @next = sinon.stub()
  25. describe "successfully", ->
  26. beforeEach ->
  27. @DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArgWith(2)
  28. @HttpController.flushAndDeleteDoc(@req, @res, @next)
  29. it "should flush and delete the doc", ->
  30. @DocumentManager.flushAndDeleteDocWithLock
  31. .calledWith(@project_id, @doc_id)
  32. .should.equal true
  33. it "should return a successful No Content response", ->
  34. @res.send
  35. .calledWith(204)
  36. .should.equal true
  37. it "should log the request", ->
  38. @logger.log
  39. .calledWith(doc_id: @doc_id, project_id: @project_id, "deleting doc via http")
  40. .should.equal true
  41. it "should time the request", ->
  42. @Metrics.Timer::done.called.should.equal true
  43. describe "when an errors occurs", ->
  44. beforeEach ->
  45. @DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArgWith(2, new Error("oops"))
  46. @HttpController.flushAndDeleteDoc(@req, @res, @next)
  47. it "should call next with the error", ->
  48. @next
  49. .calledWith(new Error("oops"))
  50. .should.equal true