Merge pull request #416 from sharelatex/hof-history-disabled-resync

disable history resync when project-history is not enabled
This commit is contained in:
Hayden Faulds
2018-03-16 14:08:22 +00:00
committed by GitHub
5 changed files with 106 additions and 56 deletions
@@ -47,6 +47,13 @@ UnsupportedExportRecordsError = (message) ->
return error return error
UnsupportedExportRecordsError.prototype.__proto___ = Error.prototype UnsupportedExportRecordsError.prototype.__proto___ = Error.prototype
ProjectHistoryDisabledError = (message) ->
error = new Error(message)
error.name = "ProjectHistoryDisabledError "
error.__proto__ = ProjectHistoryDisabledError.prototype
return error
ProjectHistoryDisabledError.prototype.__proto___ = Error.prototype
module.exports = Errors = module.exports = Errors =
NotFoundError: NotFoundError NotFoundError: NotFoundError
ServiceNotConfiguredError: ServiceNotConfiguredError ServiceNotConfiguredError: ServiceNotConfiguredError
@@ -55,3 +62,4 @@ module.exports = Errors =
UnsupportedFileTypeError: UnsupportedFileTypeError UnsupportedFileTypeError: UnsupportedFileTypeError
UnsupportedBrandError: UnsupportedBrandError UnsupportedBrandError: UnsupportedBrandError
UnsupportedExportRecordsError: UnsupportedExportRecordsError UnsupportedExportRecordsError: UnsupportedExportRecordsError
ProjectHistoryDisabledError: ProjectHistoryDisabledError
@@ -2,9 +2,10 @@ logger = require "logger-sharelatex"
request = require "request" request = require "request"
settings = require "settings-sharelatex" settings = require "settings-sharelatex"
AuthenticationController = require "../Authentication/AuthenticationController" AuthenticationController = require "../Authentication/AuthenticationController"
Errors = require "../Errors/Errors"
HistoryManager = require "./HistoryManager"
ProjectDetailsHandler = require "../Project/ProjectDetailsHandler" ProjectDetailsHandler = require "../Project/ProjectDetailsHandler"
ProjectEntityUpdateHandler = require "../Project/ProjectEntityUpdateHandler" ProjectEntityUpdateHandler = require "../Project/ProjectEntityUpdateHandler"
HistoryManager = require "./HistoryManager"
module.exports = HistoryController = module.exports = HistoryController =
selectHistoryApi: (req, res, next = (error) ->) -> selectHistoryApi: (req, res, next = (error) ->) ->
@@ -67,5 +68,6 @@ module.exports = HistoryController =
resyncProjectHistory: (req, res, next = (error) ->) -> resyncProjectHistory: (req, res, next = (error) ->) ->
project_id = req.params.Project_id project_id = req.params.Project_id
ProjectEntityUpdateHandler.resyncProjectHistory project_id, (error) -> ProjectEntityUpdateHandler.resyncProjectHistory project_id, (error) ->
return res.sendStatus(404) if error instanceof Errors.ProjectHistoryDisabledError
return next(error) if error? return next(error) if error?
res.sendStatus 204 res.sendStatus 204
@@ -316,8 +316,13 @@ module.exports = ProjectEntityUpdateHandler = self =
# This doesn't directly update project structure but we need to take the lock # This doesn't directly update project structure but we need to take the lock
# to prevent anything else being queued before the resync update # to prevent anything else being queued before the resync update
resyncProjectHistory: wrapWithLock (project_id, callback) -> resyncProjectHistory: wrapWithLock (project_id, callback) ->
ProjectGetter.getProject project_id, rootFolder: true, (error, project) -> ProjectGetter.getProject project_id, rootFolder: true, overleaf: true, (error, project) ->
return callback(error) if error? return callback(error) if error?
if !project.overleaf?.history?.id?
error = new Errors.ProjectHistoryDisabledError("project history not enabled for #{project_id}")
return callback(error)
ProjectEntityHandler.getAllEntitiesFromProject project, (error, docs, files) -> ProjectEntityHandler.getAllEntitiesFromProject project, (error, docs, files) ->
return callback(error) if error? return callback(error) if error?
@@ -1,6 +1,9 @@
chai = require('chai') chai = require('chai')
chai.should() chai.should()
sinon = require("sinon") sinon = require("sinon")
Errors = require "../../../../app/js/Features/Errors/Errors"
modulePath = "../../../../app/js/Features/History/HistoryController" modulePath = "../../../../app/js/Features/History/HistoryController"
SandboxedModule = require('sandboxed-module') SandboxedModule = require('sandboxed-module')
@@ -14,8 +17,9 @@ describe "HistoryController", ->
"request" : @request = sinon.stub() "request" : @request = sinon.stub()
"settings-sharelatex": @settings = {} "settings-sharelatex": @settings = {}
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()} "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
"./HistoryManager": @HistoryManager = {}
"../Authentication/AuthenticationController": @AuthenticationController "../Authentication/AuthenticationController": @AuthenticationController
"../Errors/Errors": Errors
"./HistoryManager": @HistoryManager = {}
"../Project/ProjectDetailsHandler": @ProjectDetailsHandler = {} "../Project/ProjectDetailsHandler": @ProjectDetailsHandler = {}
"../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {} "../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {}
@settings.apis = @settings.apis =
@@ -202,22 +206,40 @@ describe "HistoryController", ->
@res.json.calledWith(@data_with_users).should.equal false @res.json.calledWith(@data_with_users).should.equal false
describe "resyncProjectHistory", -> describe "resyncProjectHistory", ->
beforeEach -> describe "for a project without project-history enabled", ->
@project_id = 'mock-project-id' beforeEach ->
@req = params: Project_id: @project_id @project_id = 'mock-project-id'
@res = sendStatus: sinon.stub() @req = params: Project_id: @project_id
@next = sinon.stub() @res = sendStatus: sinon.stub()
@next = sinon.stub()
@ProjectEntityUpdateHandler.resyncProjectHistory = sinon.stub().yields() @error = new Errors.ProjectHistoryDisabledError()
@ProjectEntityUpdateHandler.resyncProjectHistory = sinon.stub().yields(@error)
@HistoryController.resyncProjectHistory @req, @res, @next @HistoryController.resyncProjectHistory @req, @res, @next
it "resyncs the project", -> it "response with a 404", ->
@ProjectEntityUpdateHandler.resyncProjectHistory @res.sendStatus
.calledWith(@project_id) .calledWith(404)
.should.equal true .should.equal true
it "responds with a 204", -> describe "for a project with project-history enabled", ->
@res.sendStatus beforeEach ->
.calledWith(204) @project_id = 'mock-project-id'
.should.equal true @req = params: Project_id: @project_id
@res = sendStatus: sinon.stub()
@next = sinon.stub()
@ProjectEntityUpdateHandler.resyncProjectHistory = sinon.stub().yields()
@HistoryController.resyncProjectHistory @req, @res, @next
it "resyncs the project", ->
@ProjectEntityUpdateHandler.resyncProjectHistory
.calledWith(@project_id)
.should.equal true
it "responds with a 204", ->
@res.sendStatus
.calledWith(204)
.should.equal true
@@ -746,49 +746,62 @@ describe 'ProjectEntityUpdateHandler', ->
.should.equal true .should.equal true
describe "resyncProjectHistory", -> describe "resyncProjectHistory", ->
beforeEach -> describe "a project without project-history enabled", ->
@ProjectGetter.getProject = sinon.stub().yields(null, @project) beforeEach ->
docs = [ @project.ovreleaf = {}
doc: _id: doc_id @ProjectGetter.getProject = sinon.stub().yields(null, @project)
path: 'main.tex'
]
files = [
file: _id: file_id
path: 'universe.png'
]
@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub().yields(null, docs, files)
@FileStoreHandler._buildUrl = (project_id, file_id) ->
"www.filestore.test/#{project_id}/#{file_id}"
@DocumentUpdaterHandler.resyncProjectHistory = sinon.stub().yields()
@ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
it 'gets the project', -> it "should return an error", ->
@ProjectGetter.getProject error = new Errors.ProjectHistoryDisabledError("project history not enabled for #{project_id}")
.calledWith(project_id) @callback.calledWith(error).should.equal true
.should.equal true
it 'gets the entities for the project', -> describe "a project with project-history enabled", ->
@ProjectEntityHandler.getAllEntitiesFromProject beforeEach ->
.calledWith(@project) @project.overleaf = history: id: 4
.should.equal true @ProjectGetter.getProject = sinon.stub().yields(null, @project)
docs = [
doc: _id: doc_id
path: 'main.tex'
]
files = [
file: _id: file_id
path: 'universe.png'
]
@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub().yields(null, docs, files)
@FileStoreHandler._buildUrl = (project_id, file_id) ->
"www.filestore.test/#{project_id}/#{file_id}"
@DocumentUpdaterHandler.resyncProjectHistory = sinon.stub().yields()
it 'tells the doc updater to sync the project', -> @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
docs = [
doc: doc_id
path: 'main.tex'
]
files = [
file: file_id
path: 'universe.png'
url: "www.filestore.test/#{project_id}/#{file_id}"
]
@DocumentUpdaterHandler.resyncProjectHistory
.calledWith(project_id, docs, files)
.should.equal true
it 'calls the callback', -> it 'gets the project', ->
@callback.called.should.equal true @ProjectGetter.getProject
.calledWith(project_id)
.should.equal true
it 'gets the entities for the project', ->
@ProjectEntityHandler.getAllEntitiesFromProject
.calledWith(@project)
.should.equal true
it 'tells the doc updater to sync the project', ->
docs = [
doc: doc_id
path: 'main.tex'
]
files = [
file: file_id
path: 'universe.png'
url: "www.filestore.test/#{project_id}/#{file_id}"
]
@DocumentUpdaterHandler.resyncProjectHistory
.calledWith(project_id, docs, files)
.should.equal true
it 'calls the callback', ->
@callback.called.should.equal true
describe "_cleanUpEntity", -> describe "_cleanUpEntity", ->
beforeEach -> beforeEach ->