From 929b9996d397bdbeae7132b0899550ed3af919a5 Mon Sep 17 00:00:00 2001 From: Hayden Faulds Date: Wed, 1 Nov 2017 18:21:05 +0000 Subject: [PATCH] version doc renames --- .../Features/Editor/EditorController.coffee | 4 +-- .../Editor/EditorHttpController.coffee | 4 ++- .../Project/ProjectEntityHandler.coffee | 33 +++++++++++-------- .../Editor/EditorControllerTests.coffee | 10 +++--- .../Editor/EditorHttpControllerTests.coffee | 13 +++++--- .../Project/ProjectEntityHandlerTests.coffee | 29 ++++++++++++---- 6 files changed, 59 insertions(+), 34 deletions(-) diff --git a/services/web/app/coffee/Features/Editor/EditorController.coffee b/services/web/app/coffee/Features/Editor/EditorController.coffee index b748085daf..5026f62595 100644 --- a/services/web/app/coffee/Features/Editor/EditorController.coffee +++ b/services/web/app/coffee/Features/Editor/EditorController.coffee @@ -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) diff --git a/services/web/app/coffee/Features/Editor/EditorHttpController.coffee b/services/web/app/coffee/Features/Editor/EditorHttpController.coffee index 058799a495..63eeef70e9 100644 --- a/services/web/app/coffee/Features/Editor/EditorHttpController.coffee +++ b/services/web/app/coffee/Features/Editor/EditorHttpController.coffee @@ -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 diff --git a/services/web/app/coffee/Features/Project/ProjectEntityHandler.coffee b/services/web/app/coffee/Features/Project/ProjectEntityHandler.coffee index 6489b2bbea..03849762a2 100644 --- a/services/web/app/coffee/Features/Project/ProjectEntityHandler.coffee +++ b/services/web/app/coffee/Features/Project/ProjectEntityHandler.coffee @@ -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) diff --git a/services/web/test/UnitTests/coffee/Editor/EditorControllerTests.coffee b/services/web/test/UnitTests/coffee/Editor/EditorControllerTests.coffee index 1c69b375b8..2d9e344c8e 100644 --- a/services/web/test/UnitTests/coffee/Editor/EditorControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Editor/EditorControllerTests.coffee @@ -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() diff --git a/services/web/test/UnitTests/coffee/Editor/EditorHttpControllerTests.coffee b/services/web/test/UnitTests/coffee/Editor/EditorHttpControllerTests.coffee index 2bde0cdab2..2567230a36 100644 --- a/services/web/test/UnitTests/coffee/Editor/EditorHttpControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Editor/EditorHttpControllerTests.coffee @@ -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 = diff --git a/services/web/test/UnitTests/coffee/Project/ProjectEntityHandlerTests.coffee b/services/web/test/UnitTests/coffee/Project/ProjectEntityHandlerTests.coffee index 0bf3460cbb..9263ebed9f 100644 --- a/services/web/test/UnitTests/coffee/Project/ProjectEntityHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Project/ProjectEntityHandlerTests.coffee @@ -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()