|
|
@@ -14,6 +14,7 @@ describe 'ProjectEntityHandler', ->
|
|
|
doc_id = '4eecb1c1bffa66588e0000a2'
|
|
|
folder_id = "4eecaffcbffa66588e000008"
|
|
|
rootFolderId = "4eecaffcbffa66588e000007"
|
|
|
+ userId = 1234
|
|
|
|
|
|
beforeEach ->
|
|
|
@FileStoreHandler =
|
|
|
@@ -235,10 +236,18 @@ describe 'ProjectEntityHandler', ->
|
|
|
@pathAfterMove = {
|
|
|
fileSystem: "/somewhere/else.txt"
|
|
|
}
|
|
|
- @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArg(3)
|
|
|
+ @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArgWith(3, null, @project)
|
|
|
@ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, path: @pathAfterMove)
|
|
|
@ProjectGetter.getProject.callsArgWith(2, null, @project)
|
|
|
- @tpdsUpdateSender.moveEntity = sinon.stub().callsArg(1)
|
|
|
+ @tpdsUpdateSender.moveEntity = sinon.stub()
|
|
|
+ @documentUpdaterHandler.updateProjectStructure = sinon.stub().callsArg(6)
|
|
|
+ @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
|
|
|
+ @ProjectEntityHandler.getAllEntitiesFromProject
|
|
|
+ .onFirstCall()
|
|
|
+ .callsArgWith(1, null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
|
|
|
+ @ProjectEntityHandler.getAllEntitiesFromProject
|
|
|
+ .onSecondCall()
|
|
|
+ .callsArgWith(1, null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
|
|
|
|
|
|
describe "moving a doc", ->
|
|
|
beforeEach (done) ->
|
|
|
@@ -246,14 +255,26 @@ describe 'ProjectEntityHandler', ->
|
|
|
@doc = {lines:["1234","312343d"], rev: "1234"}
|
|
|
@path = {
|
|
|
mongo:"folders[0]"
|
|
|
- fileSystem:"/somewhere.txt"
|
|
|
+ fileSystem:"/old_folder/somewhere.txt"
|
|
|
}
|
|
|
- @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, @path)
|
|
|
- @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", done
|
|
|
+ @destFolder = { name: "folder" }
|
|
|
+ @destFolderPath = {
|
|
|
+ mongo: "folders[0]"
|
|
|
+ fileSystem: "/dest_folder"
|
|
|
+ }
|
|
|
+ @projectLocator.findElement = sinon.stub()
|
|
|
+ @projectLocator.findElement.withArgs({project: @project, element_id: @docId, type: 'docs'})
|
|
|
+ .callsArgWith(1, null, @doc, @path)
|
|
|
+ @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", userId, done
|
|
|
|
|
|
- it 'should find the project then element', ->
|
|
|
+ it 'should find the doc to move', ->
|
|
|
@projectLocator.findElement.calledWith({element_id: @docId, type: "docs", project: @project }).should.equal true
|
|
|
|
|
|
+ it "should should send the update to the doc updater", ->
|
|
|
+ @documentUpdaterHandler.updateProjectStructure
|
|
|
+ .calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
it 'should remove the element from its current position', ->
|
|
|
@ProjectEntityHandler._removeElementFromMongoArray
|
|
|
.calledWith(@ProjectModel, project_id, @path.mongo ).should.equal true
|
|
|
@@ -278,28 +299,19 @@ describe 'ProjectEntityHandler', ->
|
|
|
@move_to_folder_id = "folder-to-move-to"
|
|
|
@folder = { name: "folder" }
|
|
|
@folder_to_move_to = { name: "folder to move to" }
|
|
|
- @path = {
|
|
|
- mongo: "folders[0]"
|
|
|
- fileSystem: "/somewhere.txt"
|
|
|
- }
|
|
|
- @pathToMoveTo = {
|
|
|
- mongo: "folders[0]"
|
|
|
- fileSystem: "/somewhere.txt"
|
|
|
- }
|
|
|
- @projectLocator.findElement = (options, callback) =>
|
|
|
- if options.element_id == @folder_id
|
|
|
- callback(null, @folder, @path)
|
|
|
- else if options.element_id == @move_to_folder_id
|
|
|
- callback(null, @folder_to_move_to, @pathToMoveTo)
|
|
|
- else
|
|
|
- console.log "UNKNOWN ID", options
|
|
|
- sinon.spy @projectLocator, "findElement"
|
|
|
+ @path = { mongo:"folders[0]" }
|
|
|
+ @pathToMoveTo = { mongo: "folders[0]" }
|
|
|
+ @projectLocator.findElement = sinon.stub()
|
|
|
+ @projectLocator.findElement.withArgs({project: @project, element_id: @folder_id, type: 'folder'})
|
|
|
+ .callsArgWith(1, null, @folder, @path)
|
|
|
+ @projectLocator.findElement.withArgs({project: @project, element_id: @move_to_folder_id, type: 'folder'})
|
|
|
+ .callsArgWith(1, null, @folder_to_move_to, @pathToMoveTo)
|
|
|
|
|
|
describe "when the destination folder is outside the moving folder", ->
|
|
|
beforeEach (done) ->
|
|
|
- @path.fileSystem = "/one/directory"
|
|
|
- @pathToMoveTo.fileSystem = "/another/directory"
|
|
|
- @ProjectEntityHandler.moveEntity project_id, @folder_id, @move_to_folder_id, "folder", done
|
|
|
+ @path.fileSystem = "/one/src_dir"
|
|
|
+ @pathToMoveTo.fileSystem = "/two/dest_dir"
|
|
|
+ @ProjectEntityHandler.moveEntity project_id, @folder_id, @move_to_folder_id, "folder", userId, done
|
|
|
|
|
|
it 'should find the project then element', ->
|
|
|
@projectLocator.findElement
|
|
|
@@ -310,6 +322,11 @@ describe 'ProjectEntityHandler', ->
|
|
|
})
|
|
|
.should.equal true
|
|
|
|
|
|
+ it "should should send the update to the doc updater", ->
|
|
|
+ @documentUpdaterHandler.updateProjectStructure
|
|
|
+ .calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
it 'should remove the element from its current position', ->
|
|
|
@ProjectEntityHandler._removeElementFromMongoArray
|
|
|
.calledWith(
|
|
|
@@ -345,9 +362,9 @@ describe 'ProjectEntityHandler', ->
|
|
|
@path.fileSystem = "/one/two"
|
|
|
@pathToMoveTo.fileSystem = "/one/two/three"
|
|
|
@callback = sinon.stub()
|
|
|
- @ProjectEntityHandler.moveEntity project_id, @folder_id, @move_to_folder_id, "folder", @callback
|
|
|
+ @ProjectEntityHandler.moveEntity project_id, @folder_id, @move_to_folder_id, "folder", userId, @callback
|
|
|
|
|
|
- it 'should find the folder we are moving to as well element', ->
|
|
|
+ it 'should find the folder we are moving to element', ->
|
|
|
@projectLocator.findElement
|
|
|
.calledWith({
|
|
|
element_id: @move_to_folder_id,
|
|
|
@@ -361,23 +378,29 @@ describe 'ProjectEntityHandler', ->
|
|
|
.calledWith(new Error("destination folder is a child folder of me"))
|
|
|
.should.equal true
|
|
|
|
|
|
- describe 'removing element from mongo array', ->
|
|
|
- it 'should call update with log the path', (done)->
|
|
|
- mongoPath = "folders[0].folders[5]"
|
|
|
- id = "12344"
|
|
|
- firstUpdate = true
|
|
|
- model =
|
|
|
- update: (conditions, update, opts, callback)->
|
|
|
- if firstUpdate
|
|
|
- conditions._id.should.equal id
|
|
|
- update.$unset[mongoPath].should.equal 1
|
|
|
- firstUpdate = false
|
|
|
- callback()
|
|
|
- else
|
|
|
- conditions._id.should.equal id
|
|
|
- assert.deepEqual update, { '$pull': { 'folders[0]': null } }
|
|
|
- done()
|
|
|
- @ProjectEntityHandler._removeElementFromMongoArray model, id, mongoPath, ->
|
|
|
+ describe '_removeElementFromMongoArray ', ->
|
|
|
+ beforeEach ->
|
|
|
+ @mongoPath = "folders[0].folders[5]"
|
|
|
+ @id = "12344"
|
|
|
+ @project = 'a project'
|
|
|
+ @ProjectModel.update = sinon.stub().callsArg(3)
|
|
|
+ @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
|
|
|
+ @ProjectEntityHandler._removeElementFromMongoArray @ProjectModel, @id, @mongoPath, @callback
|
|
|
+
|
|
|
+ it 'should unset', ->
|
|
|
+ update = { '$unset': { } }
|
|
|
+ update['$unset'][@mongoPath] = 1
|
|
|
+ @ProjectModel.update
|
|
|
+ .calledWith({ _id: @id }, update, {})
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it 'should pull', ->
|
|
|
+ @ProjectModel.findOneAndUpdate
|
|
|
+ .calledWith({ _id: @id }, { '$pull': { 'folders[0]': null } }, {'new': true})
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it 'should call the callback', ->
|
|
|
+ @callback.calledWith(null, @project).should.equal true
|
|
|
|
|
|
describe 'getDoc', ->
|
|
|
beforeEach ->
|
|
|
@@ -976,20 +999,36 @@ describe 'ProjectEntityHandler', ->
|
|
|
@entityType = "doc"
|
|
|
@newName = "new.tex"
|
|
|
@path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
|
|
|
+
|
|
|
+ @ProjectGetter.getProject.callsArgWith(2, null, @project)
|
|
|
+ @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
|
|
|
+ @ProjectEntityHandler.getAllEntitiesFromProject
|
|
|
+ .onFirstCall()
|
|
|
+ .callsArgWith(1, null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
|
|
|
+ @ProjectEntityHandler.getAllEntitiesFromProject
|
|
|
+ .onSecondCall()
|
|
|
+ .callsArgWith(1, null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
|
|
|
+
|
|
|
@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()
|
|
|
- @ProjectGetter.getProject.callsArgWith(2, null, @project)
|
|
|
+ @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
|
|
|
+ @documentUpdaterHandler.updateProjectStructure = sinon.stub().callsArg(6)
|
|
|
|
|
|
- it "should update the name in mongo", (done)->
|
|
|
+ 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, @oldFiles, @newFiles)
|
|
|
+ .should.equal true
|
|
|
+ done()
|
|
|
|
|
|
- @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
|
|
|
- @ProjectModel.update.calledWith({_id : @project_id}, {"$set":{"mongo.path.name":@newName}}).should.equal true
|
|
|
+ it "should update the name in mongo", (done)->
|
|
|
+ @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, =>
|
|
|
- @tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
|
|
|
+ @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()
|
|
|
|
|
|
describe "_insertDeletedDocReference", ->
|
|
|
@@ -1085,27 +1124,28 @@ describe 'ProjectEntityHandler', ->
|
|
|
@path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
|
|
|
@ProjectGetter.getProject.callsArgWith(2, null, @project)
|
|
|
@projectLocator.findElement.callsArgWith(1, null, @folder, @path)
|
|
|
- @ProjectUpdateStub.callsArgWith(3)
|
|
|
-
|
|
|
+ @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
|
|
|
|
|
|
describe "updating the project", ->
|
|
|
-
|
|
|
-
|
|
|
it "should use the correct mongo path", (done)->
|
|
|
@ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err)=>
|
|
|
- @ProjectModel.update.args[0][0]._id.should.equal @project._id
|
|
|
- assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
|
|
|
+ @ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal @project._id
|
|
|
+ assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
|
|
|
+ done()
|
|
|
+
|
|
|
+ it "should return the project in the callback", (done)->
|
|
|
+ @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err, path, project)=>
|
|
|
+ expect(project).to.equal @project
|
|
|
done()
|
|
|
|
|
|
it "should add an s onto the type if not included", (done)->
|
|
|
@ProjectEntityHandler._putElement @project, @folder._id, @doc, "doc", (err)=>
|
|
|
- assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
|
|
|
+ assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
|
|
|
done()
|
|
|
|
|
|
-
|
|
|
- it "should not call update if elemenet is null", (done)->
|
|
|
+ it "should not call update if element is null", (done)->
|
|
|
@ProjectEntityHandler._putElement @project, @folder._id, null, "doc", (err)=>
|
|
|
- @ProjectModel.update.called.should.equal false
|
|
|
+ @ProjectModel.findOneAndUpdate.called.should.equal false
|
|
|
done()
|
|
|
|
|
|
it "should default to root folder insert", (done)->
|
|
|
@@ -1117,11 +1157,9 @@ describe 'ProjectEntityHandler', ->
|
|
|
doc =
|
|
|
name:"something"
|
|
|
@ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
|
|
|
- @ProjectModel.update.called.should.equal false
|
|
|
+ @ProjectModel.findOneAndUpdate.called.should.equal false
|
|
|
done()
|
|
|
|
|
|
-
|
|
|
-
|
|
|
describe "_countElements", ->
|
|
|
|
|
|
beforeEach ->
|