diff --git a/services/web/app/coffee/Features/History/HistoryController.coffee b/services/web/app/coffee/Features/History/HistoryController.coffee index d4f42b38b1..6ab7f917eb 100644 --- a/services/web/app/coffee/Features/History/HistoryController.coffee +++ b/services/web/app/coffee/Features/History/HistoryController.coffee @@ -6,7 +6,8 @@ AuthenticationController = require "../Authentication/AuthenticationController" module.exports = HistoryController = proxyToHistoryApi: (req, res, next = (error) ->) -> user_id = AuthenticationController.getLoggedInUserId req - url = settings.apis.trackchanges.url + req.url + url = HistoryController.buildHistoryServiceUrl() + req.url + logger.log url: url, "proxying to track-changes api" getReq = request( url: url @@ -18,3 +19,9 @@ module.exports = HistoryController = getReq.on "error", (error) -> logger.error err: error, "track-changes API error" next(error) + + buildHistoryServiceUrl: () -> + if settings.apis.project_history.enabled + return settings.apis.project_history.url + else + return settings.apis.trackchanges.url diff --git a/services/web/app/coffee/Features/History/HistoryManager.coffee b/services/web/app/coffee/Features/History/HistoryManager.coffee deleted file mode 100644 index ea3f492613..0000000000 --- a/services/web/app/coffee/Features/History/HistoryManager.coffee +++ /dev/null @@ -1,28 +0,0 @@ -settings = require "settings-sharelatex" -request = require "request" -logger = require "logger-sharelatex" - -module.exports = HistoryManager = - flushProject: (project_id, callback = (error) ->) -> - logger.log project_id: project_id, "flushing project in track-changes api" - url = "#{settings.apis.trackchanges.url}/project/#{project_id}/flush" - request.post url, (error, res, body) -> - return callback(error) if error? - if 200 <= res.statusCode < 300 - callback(null) - else - error = new Error("track-changes api responded with non-success code: #{res.statusCode} #{url}") - logger.error err: error, project_id: project_id, "error flushing project in track-changes api" - callback(error) - - archiveProject: (project_id, callback = ()->)-> - logger.log project_id: project_id, "archving project in track-changes api" - url = "#{settings.apis.trackchanges.url}/project/#{project_id}/archive" - request.post url, (error, res, body) -> - return callback(error) if error? - if 200 <= res.statusCode < 300 - callback(null) - else - error = new Error("track-changes api responded with non-success code: #{res.statusCode} #{url}") - logger.error err: error, project_id: project_id, "error archving project in track-changes api" - callback(error) \ No newline at end of file diff --git a/services/web/config/settings.defaults.coffee b/services/web/config/settings.defaults.coffee index 94e9ae5007..fc893be0d5 100644 --- a/services/web/config/settings.defaults.coffee +++ b/services/web/config/settings.defaults.coffee @@ -104,6 +104,9 @@ module.exports = settings = url : "http://localhost:3005" trackchanges: url : "http://localhost:3015" + project_history: + enabled: false + url : "http://localhost:3054" docstore: url : "http://localhost:3016" pubUrl: "http://localhost:3016" diff --git a/services/web/test/UnitTests/coffee/History/HistoryControllerTests.coffee b/services/web/test/UnitTests/coffee/History/HistoryControllerTests.coffee index 577aae6a9d..db5fade45a 100644 --- a/services/web/test/UnitTests/coffee/History/HistoryControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/History/HistoryControllerTests.coffee @@ -22,37 +22,60 @@ describe "HistoryController", -> @next = sinon.stub() @settings.apis = trackchanges: + enabled: false url: "http://trackchanges.example.com" + project_history: + url: "http://project_history.example.com" @proxy = events: {} pipe: sinon.stub() on: (event, handler) -> @events[event] = handler @request.returns @proxy - @HistoryController.proxyToHistoryApi @req, @res, @next describe "successfully", -> - it "should get the user id", -> - @AuthenticationController.getLoggedInUserId - .calledWith(@req) - .should.equal true + describe "with project history enabled", -> + beforeEach -> + @settings.apis.project_history.enabled = true + @HistoryController.proxyToHistoryApi @req, @res, @next - it "should call the track changes api", -> - @request - .calledWith({ - url: "#{@settings.apis.trackchanges.url}#{@req.url}" - method: @req.method - headers: - "X-User-Id": @user_id - }) - .should.equal true + it "should get the user id", -> + @AuthenticationController.getLoggedInUserId + .calledWith(@req) + .should.equal true - it "should pipe the response to the client", -> - @proxy.pipe - .calledWith(@res) - .should.equal true + it "should call the project history api", -> + @request + .calledWith({ + url: "#{@settings.apis.project_history.url}#{@req.url}" + method: @req.method + headers: + "X-User-Id": @user_id + }) + .should.equal true + + it "should pipe the response to the client", -> + @proxy.pipe + .calledWith(@res) + .should.equal true + + describe "with project history disabled", -> + beforeEach -> + @settings.apis.project_history.enabled = false + @HistoryController.proxyToHistoryApi @req, @res, @next + + it "should call the track changes api", -> + @request + .calledWith({ + url: "#{@settings.apis.trackchanges.url}#{@req.url}" + method: @req.method + headers: + "X-User-Id": @user_id + }) + .should.equal true describe "with an error", -> beforeEach -> + @HistoryController.proxyToHistoryApi @req, @res, @next @proxy.events["error"].call(@proxy, @error = new Error("oops")) it "should pass the error up the call chain", -> diff --git a/services/web/test/UnitTests/coffee/History/HistoryManagerTests.coffee b/services/web/test/UnitTests/coffee/History/HistoryManagerTests.coffee deleted file mode 100644 index 65b22812ea..0000000000 --- a/services/web/test/UnitTests/coffee/History/HistoryManagerTests.coffee +++ /dev/null @@ -1,63 +0,0 @@ -chai = require('chai') -expect = chai.expect -chai.should() -sinon = require("sinon") -modulePath = "../../../../app/js/Features/History/HistoryManager" -SandboxedModule = require('sandboxed-module') - -describe "HistoryManager", -> - beforeEach -> - @HistoryManager = SandboxedModule.require modulePath, requires: - "request" : @request = sinon.stub() - "settings-sharelatex": @settings = - apis: - trackchanges: - url: "trackchanges.sharelatex.com" - "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()} - @project_id = "project-id-123" - @callback = sinon.stub() - @request.post = sinon.stub() - - describe "flushProject", -> - describe "with a successful response code", -> - beforeEach -> - @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204, "") - @HistoryManager.flushProject @project_id, @callback - - it "should flush the project in the track changes api", -> - @request.post - .calledWith("#{@settings.apis.trackchanges.url}/project/#{@project_id}/flush") - .should.equal true - - it "should call the callback without an error", -> - @callback.calledWith(null).should.equal true - - describe "with a failed response code", -> - beforeEach -> - @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500, "") - @HistoryManager.flushProject @project_id, @callback - - it "should call the callback with an error", -> - @callback.calledWith(new Error("track-changes api responded with a non-success code: 500")).should.equal true - - it "should log the error", -> - @logger.error - .calledWith({ - err: new Error("track-changes api responded with a non-success code: 500") - project_id: @project_id - }, "error flushing project in track-changes api") - .should.equal true - - describe "ArchiveProject", -> - - it "should call the post endpoint", (done)-> - @request.post.callsArgWith(1, null, {}) - @HistoryManager.archiveProject @project_id, (err)=> - @request.post.calledWith("#{@settings.apis.trackchanges.url}/project/#{@project_id}/archive") - done() - - it "should return an error on a non success", (done)-> - @request.post.callsArgWith(1, null, {statusCode:500}) - @HistoryManager.archiveProject @project_id, (err)=> - expect(err).to.exist - done() \ No newline at end of file