VersioningApiControllerTests.coffee 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. spies = require('chai-spies')
  2. chai = require('chai').use(spies)
  3. sinon = require('sinon')
  4. should = chai.should()
  5. modulePath = "../../../../app/js/Features/Versioning/VersioningApiController"
  6. SandboxedModule = require('sandboxed-module')
  7. MockRequest = require "../helpers/MockRequest"
  8. MockResponse = require "../helpers/MockResponse"
  9. describe "VersioningApiController", ->
  10. beforeEach ->
  11. @handler = {}
  12. @controller = SandboxedModule.require modulePath, requires:
  13. './VersioningApiHandler':@handler
  14. @req = new MockRequest()
  15. @res = new MockResponse()
  16. describe "takeSnapshot", ->
  17. beforeEach ->
  18. @error = null
  19. @handler.takeSnapshot = (project_id, message, callback) =>
  20. callback(@error)
  21. sinon.spy @handler, "takeSnapshot"
  22. @req.params = Project_id: "project_id"
  23. describe "successfully", ->
  24. beforeEach ->
  25. @error = null
  26. @req.body = message: "hello world"
  27. @controller.takeSnapshot(@req, @res)
  28. it "should call VersioningApiHandler.takeSnapshot", ->
  29. @handler.takeSnapshot.calledWith(
  30. "project_id", @req.body.message
  31. ).should.equal true
  32. it "should return a successful response", ->
  33. @res.returned.should.equal true
  34. @res.success.should.equal true
  35. @res.body.should.equal "{}"
  36. describe "without message", ->
  37. beforeEach ->
  38. @error = null
  39. @controller.takeSnapshot(@req, @res)
  40. it "should use a default message", ->
  41. @handler.takeSnapshot.calledWith(
  42. "project_id", "Manual snapshot"
  43. ).should.equal true
  44. describe "with errors", ->
  45. beforeEach ->
  46. @error = new Error("Oops")
  47. @next = sinon.stub()
  48. @controller.takeSnapshot(@req, @res, @next)
  49. it "should call next() with the error", ->
  50. @res.returned.should.equal false
  51. @next.called.should.equal true
  52. it 'enable versioning in the handler', (done)->
  53. project_id = "1234"
  54. @handler.enableVersioning = (sentProjectId, callback)->
  55. sentProjectId.should.equal project_id
  56. callback null
  57. @controller.enableVersioning project_id, ->
  58. done()
  59. it 'proxys list versions', (done)->
  60. shouldProxy @controller.listVersions, @handler, done
  61. it 'proxys get version', (done)->
  62. shouldProxy @controller.getVersion, @handler, done
  63. it 'proxys get verion file', (done)->
  64. shouldProxy @controller.getVersionFile, @handler, done
  65. shouldProxy = (fun, handler, callback)->
  66. req = {"stuf":"here"}
  67. res = {"other":"stuff"}
  68. handler.proxyToVersioningApi = (proxyReq, proxyRes)->
  69. proxyReq.should.deep.equal req
  70. proxyRes.should.deep.equal res
  71. callback()
  72. fun req, res