TrackChangesControllerTests.coffee 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. chai = require('chai')
  2. chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../../app/js/Features/TrackChanges/TrackChangesController"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "TrackChangesController", ->
  7. beforeEach ->
  8. @user_id = "user-id-123"
  9. @AuthenticationController =
  10. getLoggedInUserId: sinon.stub().returns(@user_id)
  11. @TrackChangesController = SandboxedModule.require modulePath, requires:
  12. "request" : @request = sinon.stub()
  13. "settings-sharelatex": @settings = {}
  14. "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
  15. "../Authentication/AuthenticationController": @AuthenticationController
  16. describe "proxyToTrackChangesApi", ->
  17. beforeEach ->
  18. @req = { url: "/mock/url", method: "POST" }
  19. @res = "mock-res"
  20. @next = sinon.stub()
  21. @settings.apis =
  22. trackchanges:
  23. url: "http://trackchanges.example.com"
  24. @proxy =
  25. events: {}
  26. pipe: sinon.stub()
  27. on: (event, handler) -> @events[event] = handler
  28. @request.returns @proxy
  29. @TrackChangesController.proxyToTrackChangesApi @req, @res, @next
  30. describe "successfully", ->
  31. it "should get the user id", ->
  32. @AuthenticationController.getLoggedInUserId
  33. .calledWith(@req)
  34. .should.equal true
  35. it "should call the track changes api", ->
  36. @request
  37. .calledWith({
  38. url: "#{@settings.apis.trackchanges.url}#{@req.url}"
  39. method: @req.method
  40. headers:
  41. "X-User-Id": @user_id
  42. })
  43. .should.equal true
  44. it "should pipe the response to the client", ->
  45. @proxy.pipe
  46. .calledWith(@res)
  47. .should.equal true
  48. describe "with an error", ->
  49. beforeEach ->
  50. @proxy.events["error"].call(@proxy, @error = new Error("oops"))
  51. it "should pass the error up the call chain", ->
  52. @next.calledWith(@error).should.equal true