setDocTests.coffee 1.7 KB

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