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