route history requests based on project

either to track changes or to project history service
This commit is contained in:
Brian Gough 2017-11-03 16:44:37 +00:00
parent 773277e3a0
commit 17a180ea8e
3 changed files with 98 additions and 13 deletions

View file

@ -2,6 +2,7 @@ logger = require "logger-sharelatex"
request = require "request"
settings = require "settings-sharelatex"
AuthenticationController = require "../Authentication/AuthenticationController"
ProjectDetailsHandler = require "../Project/ProjectDetailsHandler"
module.exports = HistoryController =
initializeProject: (callback = (error, history_id) ->) ->
@ -27,11 +28,22 @@ module.exports = HistoryController =
error = new Error("project-history returned a non-success status code: #{res.statusCode}")
callback error
selectHistoryApi: (req, res, next = (error) ->) ->
project_id = req.params?.Project_id
# find out which type of history service this project uses
ProjectDetailsHandler.getDetails project_id, (err, project) ->
return next(err) if err?
if project?.overleaf?.history?
req.useProjectHistory = true
else
req.useProjectHistory = false
next()
proxyToHistoryApi: (req, res, next = (error) ->) ->
user_id = AuthenticationController.getLoggedInUserId req
url = HistoryController.buildHistoryServiceUrl() + req.url
url = HistoryController.buildHistoryServiceUrl(req.useProjectHistory) + req.url
logger.log url: url, "proxying to track-changes api"
logger.log url: url, "proxying to history api"
getReq = request(
url: url
method: req.method
@ -40,11 +52,13 @@ module.exports = HistoryController =
)
getReq.pipe(res)
getReq.on "error", (error) ->
logger.error err: error, "track-changes API error"
logger.error url: url, err: error, "history API error"
next(error)
buildHistoryServiceUrl: () ->
if settings.apis.project_history?.enabled
buildHistoryServiceUrl: (useProjectHistory) ->
# choose a history service, either document-level (trackchanges)
# or project-level (project_history)
if settings.apis.project_history?.enabled && useProjectHistory
return settings.apis.project_history.url
else
return settings.apis.trackchanges.url

View file

@ -194,9 +194,9 @@ module.exports = class Router
webRouter.post '/project/:Project_id/rename', AuthorizationMiddlewear.ensureUserCanAdminProject, ProjectController.renameProject
webRouter.get "/project/:Project_id/updates", AuthorizationMiddlewear.ensureUserCanReadProject, HistoryController.proxyToHistoryApi
webRouter.get "/project/:Project_id/doc/:doc_id/diff", AuthorizationMiddlewear.ensureUserCanReadProject, HistoryController.proxyToHistoryApi
webRouter.post "/project/:Project_id/doc/:doc_id/version/:version_id/restore", AuthorizationMiddlewear.ensureUserCanReadProject, HistoryController.proxyToHistoryApi
webRouter.get "/project/:Project_id/updates", AuthorizationMiddlewear.ensureUserCanReadProject, HistoryController.selectHistoryApi, HistoryController.proxyToHistoryApi
webRouter.get "/project/:Project_id/doc/:doc_id/diff", AuthorizationMiddlewear.ensureUserCanReadProject, HistoryController.selectHistoryApi, HistoryController.proxyToHistoryApi
webRouter.post "/project/:Project_id/doc/:doc_id/version/:version_id/restore", AuthorizationMiddlewear.ensureUserCanReadProject, HistoryController.selectHistoryApi, HistoryController.proxyToHistoryApi
webRouter.get '/Project/:Project_id/download/zip', AuthorizationMiddlewear.ensureUserCanReadProject, ProjectDownloadsController.downloadProject
webRouter.get '/project/download/zip', AuthorizationMiddlewear.ensureUserCanReadMultipleProjects, ProjectDownloadsController.downloadMultipleProjects

View file

@ -15,6 +15,7 @@ describe "HistoryController", ->
"settings-sharelatex": @settings = {}
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
"../Authentication/AuthenticationController": @AuthenticationController
"../Project/ProjectDetailsHandler": @ProjectDetailsHandler = {}
@settings.apis =
trackchanges:
enabled: false
@ -22,6 +23,29 @@ describe "HistoryController", ->
project_history:
url: "http://project_history.example.com"
describe "selectHistoryApi", ->
beforeEach ->
@req = { url: "/mock/url", method: "POST" }
@res = "mock-res"
@next = sinon.stub()
describe "for a project with project history", ->
beforeEach ->
@ProjectDetailsHandler.getDetails = sinon.stub().callsArgWith(1, null, {overleaf:{history:{}}})
@HistoryController.selectHistoryApi @req, @res, @next
it "should set the flag for project history to true", ->
@req.useProjectHistory.should.equal true
describe "for any other project ", ->
beforeEach ->
@ProjectDetailsHandler.getDetails = sinon.stub().callsArgWith(1, null, {})
@HistoryController.selectHistoryApi @req, @res, @next
it "should not set the flag for project history to false", ->
@req.useProjectHistory.should.equal false
describe "proxyToHistoryApi", ->
beforeEach ->
@req = { url: "/mock/url", method: "POST" }
@ -33,10 +57,13 @@ describe "HistoryController", ->
on: (event, handler) -> @events[event] = handler
@request.returns @proxy
describe "successfully", ->
describe "with project history enabled", ->
describe "with project history enabled", ->
beforeEach ->
@settings.apis.project_history.enabled = true
describe "for a project with the project history flag", ->
beforeEach ->
@settings.apis.project_history.enabled = true
@req.useProjectHistory = true
@HistoryController.proxyToHistoryApi @req, @res, @next
it "should get the user id", ->
@ -59,9 +86,53 @@ describe "HistoryController", ->
.calledWith(@res)
.should.equal true
describe "with project history disabled", ->
describe "for a project without the project history flag", ->
beforeEach ->
@settings.apis.project_history.enabled = false
@req.useProjectHistory = false
@HistoryController.proxyToHistoryApi @req, @res, @next
it "should get the user id", ->
@AuthenticationController.getLoggedInUserId
.calledWith(@req)
.should.equal true
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 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
describe "for a project with the project history flag", ->
beforeEach ->
@req.useProjectHistory = 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
describe "for a project without the project history flag", ->
beforeEach ->
@req.useProjectHistory = false
@HistoryController.proxyToHistoryApi @req, @res, @next
it "should call the track changes api", ->