DocumentControllerTests.coffee 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Documents/DocumentController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. events = require "events"
  8. MockRequest = require "../helpers/MockRequest"
  9. MockResponse = require "../helpers/MockResponse"
  10. Errors = require "../../../../app/js/Features/Errors/Errors"
  11. describe "DocumentController", ->
  12. beforeEach ->
  13. @DocumentController = SandboxedModule.require modulePath, requires:
  14. "logger-sharelatex":
  15. log:->
  16. err:->
  17. "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
  18. @res = new MockResponse()
  19. @req = new MockRequest()
  20. @next = sinon.stub()
  21. @project_id = "project-id-123"
  22. @doc_id = "doc-id-123"
  23. @doc_lines = ["one", "two", "three"]
  24. @version = 42
  25. @rev = 5
  26. describe "getDocument", ->
  27. beforeEach ->
  28. @req.params =
  29. Project_id: @project_id
  30. doc_id: @doc_id
  31. describe "when the document exists", ->
  32. beforeEach ->
  33. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev)
  34. @DocumentController.getDocument(@req, @res, @next)
  35. it "should get the document from Mongo", ->
  36. @ProjectEntityHandler.getDoc
  37. .calledWith(@project_id, @doc_id)
  38. .should.equal true
  39. it "should return the document data to the client as JSON", ->
  40. @res.type.should.equal "json"
  41. @res.body.should.equal JSON.stringify
  42. lines: @doc_lines
  43. describe "when the document doesn't exist", ->
  44. beforeEach ->
  45. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, new Errors.NotFoundError("not found"), null)
  46. @DocumentController.getDocument(@req, @res, @next)
  47. it "should call next with the NotFoundError", ->
  48. @next.calledWith(new Errors.NotFoundError("not found"))
  49. .should.equal true
  50. describe "setDocument", ->
  51. beforeEach ->
  52. @req.params =
  53. Project_id: @project_id
  54. doc_id: @doc_id
  55. describe "when the document exists", ->
  56. beforeEach ->
  57. @ProjectEntityHandler.updateDocLines = sinon.stub().callsArg(3)
  58. @req.body =
  59. lines: @doc_lines
  60. @DocumentController.setDocument(@req, @res, @next)
  61. it "should update the document in Mongo", ->
  62. @ProjectEntityHandler.updateDocLines
  63. .calledWith(@project_id, @doc_id, @doc_lines)
  64. .should.equal true
  65. it "should return a successful response", ->
  66. @res.success.should.equal true
  67. describe "when the document doesn't exist", ->
  68. beforeEach ->
  69. @ProjectEntityHandler.updateDocLines = sinon.stub().callsArgWith(3, new Errors.NotFoundError("document does not exist"))
  70. @req.body =
  71. lines: @doc_lines
  72. @DocumentController.setDocument(@req, @res, @next)
  73. it "should call next with the NotFoundError", ->
  74. @next.calledWith(new Errors.NotFoundError("not found"))
  75. .should.equal true