version doc renames

This commit is contained in:
Hayden Faulds 2017-11-01 18:21:05 +00:00
parent 599f2cb3ae
commit 929b9996d3
6 changed files with 59 additions and 34 deletions

View file

@ -150,11 +150,11 @@ module.exports = EditorController =
logger.log project_id:project_id, "recived message to delete project"
ProjectDeleter.deleteProject project_id, callback
renameEntity: (project_id, entity_id, entityType, newName, callback)->
renameEntity: (project_id, entity_id, entityType, newName, userId, callback)->
newName = sanitize.escape(newName)
Metrics.inc "editor.rename-entity"
logger.log entity_id:entity_id, entity_id:entity_id, entity_id:entity_id, "reciving new name for entity for project"
ProjectEntityHandler.renameEntity project_id, entity_id, entityType, newName, ->
ProjectEntityHandler.renameEntity project_id, entity_id, entityType, newName, userId, (err) ->
if err?
logger.err err:err, project_id:project_id, entity_id:entity_id, entityType:entityType, newName:newName, "error renaming entity"
return callback(err)

View file

@ -12,6 +12,7 @@ CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler")
CollaboratorsInviteHandler = require("../Collaborators/CollaboratorsInviteHandler")
PrivilegeLevels = require "../Authorization/PrivilegeLevels"
TokenAccessHandler = require '../TokenAccess/TokenAccessHandler'
AuthenticationController = require "../Authentication/AuthenticationController"
module.exports = EditorHttpController =
joinProject: (req, res, next) ->
@ -112,9 +113,10 @@ module.exports = EditorHttpController =
entity_id = req.params.entity_id
entity_type = req.params.entity_type
name = req.body.name
user_id = AuthenticationController.getLoggedInUserId(req)
if !EditorHttpController._nameIsAcceptableLength(name)
return res.sendStatus 400
EditorController.renameEntity project_id, entity_id, entity_type, name, (error) ->
EditorController.renameEntity project_id, entity_id, entity_type, name, user_id, (error) ->
return next(error) if error?
res.sendStatus 204

View file

@ -417,25 +417,30 @@ module.exports = ProjectEntityHandler =
callback null
renameEntity: (project_id, entity_id, entityType, newName, callback)->
renameEntity: (project_id, entity_id, entityType, newName, userId, callback)->
logger.log(entity_id: entity_id, project_id: project_id, ('renaming '+entityType))
if !entityType?
logger.err err: "No entityType set", project_id: project_id, entity_id: entity_id
return callback("No entityType set")
entityType = entityType.toLowerCase()
ProjectGetter.getProject project_id, {rootFolder:true, name:true}, (err, project)=>
projectLocator.findElement {project:project, element_id:entity_id, type:entityType}, (err, entity, path, folder)=>
if err?
return callback err
conditons = {_id:project_id}
update = "$set":{}
namePath = path.mongo+".name"
update["$set"][namePath] = newName
endPath = path.fileSystem.replace(entity.name, newName)
tpdsUpdateSender.moveEntity({project_id:project_id, startPath:path.fileSystem, endPath:endPath, project_name:project.name, rev:entity.rev})
Project.update conditons, update, {}, (err)->
if callback?
callback err
ProjectGetter.getProject project_id, {rootFolder:true, name:true}, (error, project)=>
return callback(error) if error?
ProjectEntityHandler.getAllEntitiesFromProject project, (error, oldDocs) =>
return callback(error) if error?
projectLocator.findElement {project:project, element_id:entity_id, type:entityType}, (error, entity, path)=>
return callback(error) if error?
endPath = path.fileSystem.replace(entity.name, newName)
conditions = {_id:project_id}
update = "$set":{}
namePath = path.mongo+".name"
update["$set"][namePath] = newName
tpdsUpdateSender.moveEntity({project_id:project_id, startPath:path.fileSystem, endPath:endPath, project_name:project.name, rev:entity.rev})
Project.findOneAndUpdate conditions, update, { "new": true}, (error, newProject) ->
return callback(error) if error?
ProjectEntityHandler.getAllEntitiesFromProject newProject, (error, newDocs) =>
return callback(error) if error?
documentUpdaterHandler = require('../../Features/DocumentUpdater/DocumentUpdaterHandler')
documentUpdaterHandler.updateProjectStructure project_id, userId, oldDocs, newDocs, callback
_cleanUpEntity: (project, entity, entityType, callback = (error) ->) ->
if(entityType.indexOf("file") != -1)

View file

@ -489,23 +489,21 @@ describe "EditorController", ->
describe "renameEntity", ->
beforeEach ->
@err = "errro"
@entity_id = "entity_id_here"
@entityType = "doc"
@newName = "bobsfile.tex"
@ProjectEntityHandler.renameEntity = sinon.stub().callsArgWith(4, @err)
@ProjectEntityHandler.renameEntity = sinon.stub().callsArg(5)
@EditorRealTimeController.emitToRoom = sinon.stub()
it "should call the project handler", (done)->
@EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, =>
@ProjectEntityHandler.renameEntity.calledWith(@project_id, @entity_id, @entityType, @newName).should.equal true
@EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, @user_id, =>
@ProjectEntityHandler.renameEntity.calledWith(@project_id, @entity_id, @entityType, @newName, @user_id).should.equal true
done()
it "should emit the update to the room", (done)->
@EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, =>
@EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, @user_id, =>
@EditorRealTimeController.emitToRoom.calledWith(@project_id, 'reciveEntityRename', @entity_id, @newName).should.equal true
done()

View file

@ -19,6 +19,7 @@ describe "EditorHttpController", ->
"../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
"../Collaborators/CollaboratorsInviteHandler": @CollaboratorsInviteHandler = {}
"../TokenAccess/TokenAccessHandler": @TokenAccessHandler = {}
"../Authentication/AuthenticationController": @AuthenticationController = {}
@project_id = "mock-project-id"
@doc_id = "mock-doc-id"
@ -264,12 +265,14 @@ describe "EditorHttpController", ->
entity_type: @entity_type = "entity-type"
@req.body =
name: @name = "new-name"
@EditorController.renameEntity = sinon.stub().callsArg(4)
@userId = 1234
@AuthenticationController.getLoggedInUserId = sinon.stub().returns(@userId)
@EditorController.renameEntity = sinon.stub().callsArg(5)
@EditorHttpController.renameEntity @req, @res
it "should call EditorController.renameEntity", ->
@EditorController.renameEntity
.calledWith(@project_id, @entity_id, @entity_type, @name)
.calledWith(@project_id, @entity_id, @entity_type, @name, @userId)
.should.equal true
it "should send back a success response", ->
@ -283,6 +286,8 @@ describe "EditorHttpController", ->
entity_type: @entity_type = "entity-type"
@req.body =
name: @name = "EDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOTEDMUBEEBKBXUUUZERMNSXFFWIBHGSDAWGMRIQWJBXGWSBVWSIKLFPRBYSJEKMFHTRZBHVKJSRGKTBHMJRXPHORFHAKRNPZGGYIOT"
@userId = 1234
@AuthenticationController.getLoggedInUserId = sinon.stub().returns(@userId)
@EditorController.renameEntity = sinon.stub().callsArg(4)
@EditorHttpController.renameEntity @req, @res
@ -290,7 +295,6 @@ describe "EditorHttpController", ->
@res.sendStatus.calledWith(400).should.equal true
describe "rename entity with 0 length name", ->
beforeEach ->
@req.params =
Project_id: @project_id
@ -298,13 +302,14 @@ describe "EditorHttpController", ->
entity_type: @entity_type = "entity-type"
@req.body =
name: @name = ""
@userId = 1234
@AuthenticationController.getLoggedInUserId = sinon.stub().returns(@userId)
@EditorController.renameEntity = sinon.stub().callsArg(4)
@EditorHttpController.renameEntity @req, @res
it "should send back a bad request status code", ->
@res.sendStatus.calledWith(400).should.equal true
describe "moveEntity", ->
beforeEach ->
@req.params =

View file

@ -976,19 +976,34 @@ describe 'ProjectEntityHandler', ->
@entityType = "doc"
@newName = "new.tex"
@path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
@projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
@ProjectModel.update = sinon.stub().callsArgWith(3)
@tpdsUpdateSender.moveEntity = sinon.stub()
@userId = 1234
@ProjectGetter.getProject.callsArgWith(2, null, @project)
@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
@ProjectEntityHandler.getAllEntitiesFromProject
.onFirstCall().callsArgWith(1, null, @oldDocs = [])
@ProjectEntityHandler.getAllEntitiesFromProject
.onSecondCall().callsArgWith(1, null, @newDocs = [])
@projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
@tpdsUpdateSender.moveEntity = sinon.stub()
@ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
@documentUpdaterHandler.updateProjectStructure = sinon.stub().callsArg(4)
it "should should send the old and new project structure to the doc updater", (done) ->
@ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, @userId, =>
@documentUpdaterHandler.updateProjectStructure.calledWith(
@project_id, @userId, @oldDocs, @newDocs,
).should.equal true
done()
it "should update the name in mongo", (done)->
@ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
@ProjectModel.update.calledWith({_id : @project_id}, {"$set":{"mongo.path.name":@newName}}).should.equal true
@ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, @userId, =>
@ProjectModel.findOneAndUpdate.calledWith({_id: @project_id}, {"$set":{"mongo.path.name": @newName}}, {"new": true}).should.equal true
done()
it "should send the update to the tpds", (done)->
@ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
@ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, @userId, =>
@tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
done()