diff --git a/services/web/app/coffee/Features/Project/ProjectLocator.coffee b/services/web/app/coffee/Features/Project/ProjectLocator.coffee index a0fd455e90..bfdb34142d 100644 --- a/services/web/app/coffee/Features/Project/ProjectLocator.coffee +++ b/services/web/app/coffee/Features/Project/ProjectLocator.coffee @@ -69,13 +69,13 @@ module.exports = return cb null, haystackFolder needleFolderName = foldersList[level] found = false - _.each haystackFolder.folders, (folder)-> + for folder in haystackFolder.folders if folder.name.toLowerCase() == needleFolderName.toLowerCase() found = true if level == foldersList.length-1 - cb null, folder + return cb null, folder else - getParentFolder(folder, foldersList, ++level, cb) + return getParentFolder(folder, foldersList, level+1, cb) if !found cb("not found project_or_id: #{project_or_id} search path: #{needlePath}, folder #{foldersList[level]} could not be found") @@ -84,7 +84,7 @@ module.exports = return cb null, folder enteties = _.union folder.fileRefs, folder.docs, folder.folders result = _.find enteties, (entity)-> - entity.name.toLowerCase() == entityName.toLowerCase() + entity?.name.toLowerCase() == entityName.toLowerCase() if result? cb null, result else diff --git a/services/web/test/UnitTests/coffee/Project/ProjectLocatorTests.coffee b/services/web/test/UnitTests/coffee/Project/ProjectLocatorTests.coffee index 7881f11bf8..08c4da865a 100644 --- a/services/web/test/UnitTests/coffee/Project/ProjectLocatorTests.coffee +++ b/services/web/test/UnitTests/coffee/Project/ProjectLocatorTests.coffee @@ -160,7 +160,7 @@ describe 'project model', -> doc._id.should.equal rootDoc._id done() - describe 'finding an entity by path', (done)-> + describe 'findElementByPath', -> it 'should take a doc path and return the element for a root level document', (done)-> path = "#{doc1.name}" @@ -221,6 +221,53 @@ describe 'project model', -> assert.equal element, undefined done() + describe "where duplicate folder exists", -> + + beforeEach -> + @duplicateFolder = {name:"duplicate1", _id:"1234", folders:[{ + name: "1" + docs:[{name:"main.tex", _id:"456"}] + folders: [] + fileRefs: [] + }], docs:[@doc = {name:"main.tex", _id:"456"}], fileRefs:[]} + @project = + rootFolder:[ + folders: [@duplicateFolder, @duplicateFolder] + fileRefs: [] + docs: [] + ] + Project.getProject = sinon.stub() + Project.getProject.callsArgWith(2, null, @project) + + + it "should not call the callback more than once", (done)-> + @locator.findElementByPath project._id, "#{@duplicateFolder.name}/#{@doc.name}", -> + done() #mocha will throw exception if done called multiple times + + + it "should not call the callback more than once when the path is longer than 1 level below the duplicate level", (done)-> + @locator.findElementByPath project._id, "#{@duplicateFolder.name}/1/main.tex", -> + done() #mocha will throw exception if done called multiple times + + describe "with a null doc", -> + beforeEach -> + @project = + rootFolder:[ + folders: [] + fileRefs: [] + docs: [{name:"main.tex"}, null, {name:"other.tex"}] + ] + Project.getProject = sinon.stub() + Project.getProject.callsArgWith(2, null, @project) + + it "should not crash with a null", (done)-> + callback = sinon.stub() + @locator.findElementByPath project._id, "/other.tex", (err, element)-> + element.name.should.equal "other.tex" + done() + + + describe 'finding a project by user_id and project name', ()-> it 'should return the projet from an array case insenstive', (done)->