diff --git a/services/web/app/coffee/Features/TrackChanges/TrackChangesController.coffee b/services/web/app/coffee/Features/TrackChanges/TrackChangesController.coffee new file mode 100644 index 0000000000..bb7193b5a7 --- /dev/null +++ b/services/web/app/coffee/Features/TrackChanges/TrackChangesController.coffee @@ -0,0 +1,13 @@ +logger = require "logger-sharelatex" +request = require "request" +settings = require "settings-sharelatex" + +module.exports = TrackChangesController = + proxyToTrackChangesApi: (req, res, next = (error) ->) -> + url = settings.apis.trackchanges.url + req.url + logger.log url: url, "proxying to track-changes api" + getReq = request.get(url) + getReq.pipe(res) + getReq.on "error", (error) -> + logger.error err: error, "track-changes API error" + next(error) \ No newline at end of file diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index c724173e3d..4c9f88b31a 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -32,6 +32,7 @@ CompileController = require("./Features/Compile/CompileController") HealthCheckController = require("./Features/HealthCheck/HealthCheckController") ProjectDownloadsController = require "./Features/Downloads/ProjectDownloadsController" FileStoreController = require("./Features/FileStore/FileStoreController") +TrackChangesController = require("./Features/TrackChanges/TrackChangesController") logger = require("logger-sharelatex") httpAuth = require('express').basicAuth (user, pass)-> @@ -122,6 +123,9 @@ module.exports = class Router app.get '/Project/:Project_id/version', SecutiryManager.requestCanAccessProject, versioningController.listVersions app.get '/Project/:Project_id/version/:Version_id', SecutiryManager.requestCanAccessProject, versioningController.getVersion + app.get "/project/:Project_id/doc/:doc_id/updates", SecutiryManager.requestCanAccessProject, TrackChangesController.proxyToTrackChangesApi + app.get "/project/:Project_id/doc/:doc_id/diff", SecutiryManager.requestCanAccessProject, TrackChangesController.proxyToTrackChangesApi + app.post '/project/:project_id/leave', AuthenticationController.requireLogin(), CollaboratorsController.removeSelfFromProject app.get '/project/:Project_id/collaborators', SecutiryManager.requestCanAccessProject(allow_auth_token: true), CollaboratorsController.getCollaborators diff --git a/services/web/test/UnitTests/coffee/TrackChanges/TrackChangesControllerTests.coffee b/services/web/test/UnitTests/coffee/TrackChanges/TrackChangesControllerTests.coffee new file mode 100644 index 0000000000..e98305b939 --- /dev/null +++ b/services/web/test/UnitTests/coffee/TrackChanges/TrackChangesControllerTests.coffee @@ -0,0 +1,46 @@ +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: + "request" : @request = {} + "settings-sharelatex": @settings = {} + "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()} + + describe "proxyToTrackChangesApi", -> + beforeEach -> + @req = { url: "/mock/url" } + @res = "mock-res" + @next = sinon.stub() + @settings.apis = + trackchanges: + url: "http://trackchanges.example.com" + @proxy = + events: {} + pipe: sinon.stub() + on: (event, handler) -> @events[event] = handler + @request.get = sinon.stub().returns @proxy + @TrackChangesController.proxyToTrackChangesApi @req, @res, @next + + describe "successfully", -> + it "should call the track changes api", -> + @request.get + .calledWith("#{@settings.apis.trackchanges.url}#{@req.url}") + .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 +