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