2014-02-12 05:23:40 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
|
|
|
require('chai').should()
|
2017-05-11 06:29:57 -04:00
|
|
|
expect = require('chai').expect
|
2014-02-12 05:23:40 -05:00
|
|
|
modulePath = require('path').join __dirname, '../../../../app/js/Features/ThirdPartyDataStore/TpdsUpdateHandler.js'
|
|
|
|
|
2014-10-15 09:11:02 -04:00
|
|
|
describe 'TpdsUpdateHandler', ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
|
|
|
@requestQueuer = {}
|
|
|
|
@updateMerger =
|
2014-10-16 06:43:54 -04:00
|
|
|
deleteUpdate: (user_id, path, source, cb)->cb()
|
2016-02-04 09:26:50 -05:00
|
|
|
mergeUpdate:(user_id, project_id, path, update, source, cb)->cb()
|
2014-02-12 05:23:40 -05:00
|
|
|
@editorController = {}
|
|
|
|
@project_id = "dsjajilknaksdn"
|
|
|
|
@project = {_id:@project_id, name:"projectNameHere"}
|
|
|
|
@projectLocator = findUsersProjectByName:sinon.stub().callsArgWith(2, null, @project)
|
|
|
|
@projectCreationHandler =
|
|
|
|
createBlankProject : sinon.stub().callsArgWith(2, null, @project)
|
|
|
|
@projectDeleter = {markAsDeletedByExternalSource:sinon.stub().callsArgWith(1)}
|
|
|
|
@rootDocManager = setRootDocAutomatically:sinon.stub()
|
2014-10-27 10:39:20 -04:00
|
|
|
@FileTypeManager =
|
|
|
|
shouldIgnore: sinon.stub().callsArgWith(1, null, false)
|
2017-05-11 06:29:57 -04:00
|
|
|
@CooldownManager =
|
|
|
|
isProjectOnCooldown: sinon.stub().callsArgWith(1, null, false)
|
2014-02-12 05:23:40 -05:00
|
|
|
@handler = SandboxedModule.require modulePath, requires:
|
|
|
|
'./UpdateMerger': @updateMerger
|
|
|
|
'./Editor/EditorController': @editorController
|
|
|
|
'../Project/ProjectLocator': @projectLocator
|
|
|
|
'../Project/ProjectCreationHandler':@projectCreationHandler
|
|
|
|
'../Project/ProjectDeleter': @projectDeleter
|
|
|
|
"../Project/ProjectRootDocManager" : @rootDocManager
|
2014-10-27 10:39:20 -04:00
|
|
|
'../Uploads/FileTypeManager': @FileTypeManager
|
2017-05-11 06:29:57 -04:00
|
|
|
'../Cooldown/CooldownManager': @CooldownManager
|
2014-02-12 05:23:40 -05:00
|
|
|
'logger-sharelatex': log:->
|
|
|
|
@user_id = "dsad29jlkjas"
|
2014-10-15 09:11:02 -04:00
|
|
|
@source = "dropbox"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe 'getting an update', ->
|
|
|
|
it 'should send the update to the update merger', (done)->
|
|
|
|
path = "/path/here"
|
|
|
|
update = {}
|
|
|
|
@updateMerger.mergeUpdate = sinon.stub()
|
2016-02-04 09:26:50 -05:00
|
|
|
@updateMerger.mergeUpdate.withArgs(@user_id, @project_id, path, update, @source).callsArg(5)
|
2014-10-16 06:43:54 -04:00
|
|
|
@handler.newUpdate @user_id, @project.name, path, update, @source, =>
|
2014-02-12 05:23:40 -05:00
|
|
|
@projectCreationHandler.createBlankProject.called.should.equal false
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should create a new project if one does not already exit', (done)->
|
|
|
|
@projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
|
|
|
|
path = "/"
|
2014-10-16 06:43:54 -04:00
|
|
|
@handler.newUpdate @user_id, @project.name, path, {}, @source, =>
|
2014-02-12 05:23:40 -05:00
|
|
|
@projectCreationHandler.createBlankProject.calledWith(@user_id, @project.name).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should set the root doc automatically if a new project is created', (done)->
|
|
|
|
@projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
|
|
|
|
@handler._rootDocTimeoutLength = 0
|
|
|
|
path = "/"
|
2014-10-16 06:43:54 -04:00
|
|
|
@handler.newUpdate @user_id, @project.name, path, {}, @source, =>
|
2014-02-12 05:23:40 -05:00
|
|
|
setTimeout (=>
|
|
|
|
@rootDocManager.setRootDocAutomatically.calledWith(@project._id).should.equal true
|
|
|
|
done()
|
|
|
|
), 1
|
|
|
|
|
2014-10-27 10:39:20 -04:00
|
|
|
it 'should not update files that should be ignored', (done) ->
|
|
|
|
@FileTypeManager.shouldIgnore = sinon.stub().callsArgWith(1, null, true)
|
|
|
|
@projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
|
|
|
|
path = "/.gitignore"
|
|
|
|
@updateMerger.mergeUpdate = sinon.stub()
|
|
|
|
@handler.newUpdate @user_id, @project.name, path, {}, @source, =>
|
|
|
|
@updateMerger.mergeUpdate.called.should.equal false
|
|
|
|
done()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-05-11 06:29:57 -04:00
|
|
|
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()
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
describe 'getting a delete :', ->
|
|
|
|
it 'should call deleteEntity in the collaberation manager', (done)->
|
|
|
|
path = "/delete/this"
|
|
|
|
update = {}
|
2014-10-16 06:43:54 -04:00
|
|
|
@updateMerger.deleteUpdate = sinon.stub().callsArg(3)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-10-16 06:43:54 -04:00
|
|
|
@handler.deleteUpdate @user_id, @project.name, path, @source, =>
|
2014-02-12 05:23:40 -05:00
|
|
|
@projectDeleter.markAsDeletedByExternalSource.calledWith(@project._id).should.equal false
|
2014-10-16 06:43:54 -04:00
|
|
|
@updateMerger.deleteUpdate.calledWith(@project_id, path, @source).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should mark the project as deleted by external source if path is a single slash', (done)->
|
|
|
|
path = "/"
|
2014-10-16 06:43:54 -04:00
|
|
|
@handler.deleteUpdate @user_id, @project.name, path, @source, =>
|
2014-02-12 05:23:40 -05:00
|
|
|
@projectDeleter.markAsDeletedByExternalSource.calledWith(@project._id).should.equal true
|
|
|
|
done()
|
|
|
|
|