2014-03-05 11:31:52 -05:00
|
|
|
chai = require('chai')
|
|
|
|
chai.should()
|
|
|
|
sinon = require("sinon")
|
|
|
|
modulePath = "../../../../app/js/Features/TrackChanges/TrackChangesController"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe "TrackChangesController", ->
|
|
|
|
beforeEach ->
|
|
|
|
@TrackChangesController = SandboxedModule.require modulePath, requires:
|
2014-03-11 08:13:46 -04:00
|
|
|
"request" : @request = sinon.stub()
|
2014-03-05 11:31:52 -05:00
|
|
|
"settings-sharelatex": @settings = {}
|
|
|
|
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
|
2014-03-11 14:01:14 -04:00
|
|
|
"../Authentication/AuthenticationController": @AuthenticationController = {}
|
2014-03-05 11:31:52 -05:00
|
|
|
|
|
|
|
describe "proxyToTrackChangesApi", ->
|
|
|
|
beforeEach ->
|
2014-03-11 08:13:46 -04:00
|
|
|
@req = { url: "/mock/url", method: "POST" }
|
2014-03-05 11:31:52 -05:00
|
|
|
@res = "mock-res"
|
|
|
|
@next = sinon.stub()
|
2014-03-11 14:01:14 -04:00
|
|
|
@user_id = "user-id-123"
|
2014-03-05 11:31:52 -05:00
|
|
|
@settings.apis =
|
|
|
|
trackchanges:
|
|
|
|
url: "http://trackchanges.example.com"
|
|
|
|
@proxy =
|
|
|
|
events: {}
|
|
|
|
pipe: sinon.stub()
|
|
|
|
on: (event, handler) -> @events[event] = handler
|
2014-03-11 08:13:46 -04:00
|
|
|
@request.returns @proxy
|
2014-03-11 14:01:14 -04:00
|
|
|
@AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id)
|
2014-03-05 11:31:52 -05:00
|
|
|
@TrackChangesController.proxyToTrackChangesApi @req, @res, @next
|
|
|
|
|
|
|
|
describe "successfully", ->
|
2014-03-11 14:01:14 -04:00
|
|
|
it "should get the user id", ->
|
|
|
|
@AuthenticationController.getLoggedInUserId
|
|
|
|
.calledWith(@req)
|
|
|
|
.should.equal true
|
|
|
|
|
2014-03-05 11:31:52 -05:00
|
|
|
it "should call the track changes api", ->
|
2014-03-11 08:13:46 -04:00
|
|
|
@request
|
|
|
|
.calledWith({
|
|
|
|
url: "#{@settings.apis.trackchanges.url}#{@req.url}"
|
|
|
|
method: @req.method
|
2014-03-11 14:01:14 -04:00
|
|
|
headers:
|
|
|
|
"X-User-Id": @user_id
|
2014-03-11 08:13:46 -04:00
|
|
|
})
|
2014-03-05 11:31:52 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should pipe the response to the client", ->
|
|
|
|
@proxy.pipe
|
|
|
|
.calledWith(@res)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "with an error", ->
|
|
|
|
beforeEach ->
|
|
|
|
@proxy.events["error"].call(@proxy, @error = new Error("oops"))
|
|
|
|
|
|
|
|
it "should pass the error up the call chain", ->
|
|
|
|
@next.calledWith(@error).should.equal true
|
|
|
|
|