Merge branch 'sk-fix-cooldown-tpds'

This commit is contained in:
Shane Kilkelly 2017-05-12 09:11:23 +01:00
commit 1640dfa79a
4 changed files with 60 additions and 4 deletions

View file

@ -22,6 +22,9 @@ module.exports = ErrorController =
if error instanceof Errors.NotFoundError
logger.warn {err: error, url: req.url}, "not found error"
ErrorController.notFound req, res
else if error instanceof Errors.TooManyRequestsError
logger.warn {err: error, url: req.url}, "too many requests error"
res.sendStatus(429)
else
logger.error err: error, url:req.url, method:req.method, user:user, "error passed to top level next middlewear"
ErrorController.serverError req, res

View file

@ -11,8 +11,18 @@ ServiceNotConfiguredError = (message) ->
error.name = "ServiceNotConfiguredError"
error.__proto__ = ServiceNotConfiguredError.prototype
return error
ServiceNotConfiguredError.prototype.__proto__ = Error.prototype
TooManyRequestsError = (message) ->
error = new Error(message)
error.name = "TooManyRequestsError"
error.__proto__ = TooManyRequestsError.prototype
return error
TooManyRequestsError.prototype.__proto__ = Error.prototype
module.exports = Errors =
NotFoundError: NotFoundError
ServiceNotConfiguredError: ServiceNotConfiguredError
TooManyRequestsError: TooManyRequestsError

View file

@ -5,6 +5,8 @@ projectCreationHandler = require('../Project/ProjectCreationHandler')
projectDeleter = require('../Project/ProjectDeleter')
ProjectRootDocManager = require "../Project/ProjectRootDocManager"
FileTypeManager = require('../Uploads/FileTypeManager')
CooldownManager = require('../Cooldown/CooldownManager')
Errors = require('../Errors/Errors')
commitMessage = "Before update from Dropbox"
@ -24,10 +26,15 @@ module.exports =
cb err, project
getOrCreateProject (err, project)->
return callback(err) if err?
FileTypeManager.shouldIgnore path, (err, shouldIgnore)->
if shouldIgnore
return callback()
updateMerger.mergeUpdate user_id, project._id, path, updateRequest, source, callback
CooldownManager.isProjectOnCooldown project._id, (err, projectIsOnCooldown) ->
return callback(err) if err?
if projectIsOnCooldown
logger.log {projectId: project._id}, "project is on cooldown, denying request"
return callback(new Errors.TooManyRequestsError('project on cooldown'))
FileTypeManager.shouldIgnore path, (err, shouldIgnore)->
if shouldIgnore
return callback()
updateMerger.mergeUpdate user_id, project._id, path, updateRequest, source, callback
deleteUpdate: (user_id, projectName, path, source, callback)->

View file

@ -1,6 +1,7 @@
SandboxedModule = require('sandboxed-module')
sinon = require('sinon')
require('chai').should()
expect = require('chai').expect
modulePath = require('path').join __dirname, '../../../../app/js/Features/ThirdPartyDataStore/TpdsUpdateHandler.js'
describe 'TpdsUpdateHandler', ->
@ -19,6 +20,8 @@ describe 'TpdsUpdateHandler', ->
@rootDocManager = setRootDocAutomatically:sinon.stub()
@FileTypeManager =
shouldIgnore: sinon.stub().callsArgWith(1, null, false)
@CooldownManager =
isProjectOnCooldown: sinon.stub().callsArgWith(1, null, false)
@handler = SandboxedModule.require modulePath, requires:
'./UpdateMerger': @updateMerger
'./Editor/EditorController': @editorController
@ -27,6 +30,7 @@ describe 'TpdsUpdateHandler', ->
'../Project/ProjectDeleter': @projectDeleter
"../Project/ProjectRootDocManager" : @rootDocManager
'../Uploads/FileTypeManager': @FileTypeManager
'../Cooldown/CooldownManager': @CooldownManager
'logger-sharelatex': log:->
@user_id = "dsad29jlkjas"
@source = "dropbox"
@ -67,6 +71,38 @@ describe 'TpdsUpdateHandler', ->
@updateMerger.mergeUpdate.called.should.equal false
done()
it 'should check if the project is on cooldown', (done) ->
@CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, null, false)
@projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
path = "/path/here"
update = {}
@updateMerger.mergeUpdate = sinon.stub()
@updateMerger.mergeUpdate.withArgs(@user_id, @project_id, path, update, @source).callsArg(5)
@handler.newUpdate @user_id, @project.name, path, update, @source, (err) =>
expect(err).to.be.oneOf [null, undefined]
@CooldownManager.isProjectOnCooldown.callCount.should.equal 1
@CooldownManager.isProjectOnCooldown.calledWith(@project_id).should.equal true
@FileTypeManager.shouldIgnore.callCount.should.equal 1
@updateMerger.mergeUpdate.callCount.should.equal 1
done()
it 'should return error and not proceed with update if project is on cooldown', (done) ->
@CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, null, true)
@projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
@FileTypeManager.shouldIgnore = sinon.stub().callsArgWith(1, null, false)
path = "/path/here"
update = {}
@updateMerger.mergeUpdate = sinon.stub()
@updateMerger.mergeUpdate.withArgs(@user_id, @project_id, path, update, @source).callsArg(5)
@handler.newUpdate @user_id, @project.name, path, update, @source, (err) =>
expect(err).to.not.be.oneOf [null, undefined]
expect(err).to.be.instanceof Error
@CooldownManager.isProjectOnCooldown.callCount.should.equal 1
@CooldownManager.isProjectOnCooldown.calledWith(@project_id).should.equal true
@FileTypeManager.shouldIgnore.callCount.should.equal 0
@updateMerger.mergeUpdate.callCount.should.equal 0
done()
describe 'getting a delete :', ->
it 'should call deleteEntity in the collaberation manager', (done)->
path = "/delete/this"