2014-02-12 05:23:40 -05:00
|
|
|
spies = require('chai-spies')
|
|
|
|
chai = require('chai').use(spies)
|
|
|
|
assert = require('chai').assert
|
|
|
|
should = chai.should()
|
|
|
|
modulePath = "../../../../app/js/Features/Project/ProjectLocator"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
2016-03-23 11:14:49 -04:00
|
|
|
Errors = require "../../../../app/js/Features/Errors/Errors"
|
2015-02-18 16:41:03 -05:00
|
|
|
expect = require("chai").expect
|
2014-02-12 05:23:40 -05:00
|
|
|
Project = class Project
|
|
|
|
|
|
|
|
project = _id : "1234566", rootFolder:[]
|
|
|
|
rootDoc = name:"rootDoc", _id:"das239djd"
|
|
|
|
doc1 = name:"otherDoc.txt", _id:"dsad2ddd"
|
|
|
|
doc2 = name:"docname.txt", _id:"dsad2ddddd"
|
|
|
|
file1 = name:"file1", _id:"dsa9lkdsad"
|
|
|
|
subSubFile = name:"subSubFile", _id:"d1d2dk"
|
|
|
|
subSubDoc = name:"subdoc.txt", _id:"321dmdwi"
|
|
|
|
secondSubFolder = name:"secondSubFolder", _id:"dsa3e23", docs:[subSubDoc], fileRefs:[subSubFile], folders:[]
|
2017-03-17 09:29:09 -04:00
|
|
|
subFolder = name:"subFolder", _id:"dsadsa93", folders:[secondSubFolder, null], docs:[], fileRefs:[]
|
2014-02-12 05:23:40 -05:00
|
|
|
subFolder1 = name:"subFolder1", _id:"123asdjoij"
|
|
|
|
|
|
|
|
rootFolder =
|
|
|
|
_id : "123sdskd"
|
2014-03-12 18:38:03 -04:00
|
|
|
docs:[doc1, doc2, null, rootDoc]
|
2014-02-12 05:23:40 -05:00
|
|
|
fileRefs:[file1]
|
|
|
|
folders:[subFolder1, subFolder]
|
|
|
|
|
|
|
|
project.rootFolder[0] = rootFolder
|
|
|
|
project.rootDoc_id = rootDoc._id
|
|
|
|
|
|
|
|
|
2016-03-03 12:19:03 -05:00
|
|
|
describe 'ProjectLocator', ->
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
Project.findById = (project_id, callback)=>
|
|
|
|
callback(null, project)
|
2018-02-12 11:03:11 -05:00
|
|
|
@ProjectGetter =
|
2016-02-29 11:03:22 -05:00
|
|
|
getProject: sinon.stub().callsArgWith(2, null, project)
|
2014-02-12 05:23:40 -05:00
|
|
|
@locator = SandboxedModule.require modulePath, requires:
|
|
|
|
'../../models/Project':{Project:Project}
|
|
|
|
'../../models/User':{User:@User}
|
2016-02-26 03:31:47 -05:00
|
|
|
"./ProjectGetter":@ProjectGetter
|
2014-02-12 05:23:40 -05:00
|
|
|
'logger-sharelatex':
|
|
|
|
log:->
|
|
|
|
err:->
|
|
|
|
warn: ->
|
|
|
|
|
|
|
|
describe 'finding a doc', ->
|
|
|
|
it 'finds one at the root level', (done)->
|
|
|
|
@locator.findElement {project_id:project._id, element_id:doc2._id, type:"docs"}, (err, foundElement, path, parentFolder)->
|
|
|
|
assert(!err?)
|
|
|
|
foundElement._id.should.equal doc2._id
|
|
|
|
path.fileSystem.should.equal "/#{doc2.name}"
|
|
|
|
parentFolder._id.should.equal project.rootFolder[0]._id
|
|
|
|
path.mongo.should.equal "rootFolder.0.docs.1"
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'when it is nested', (done)->
|
|
|
|
@locator.findElement {project_id:project._id, element_id:subSubDoc._id, type:"doc"}, (err, foundElement, path, parentFolder)->
|
|
|
|
assert(!err?)
|
|
|
|
should.equal foundElement._id, subSubDoc._id
|
|
|
|
path.fileSystem.should.equal "/#{subFolder.name}/#{secondSubFolder.name}/#{subSubDoc.name}"
|
|
|
|
parentFolder._id.should.equal secondSubFolder._id
|
|
|
|
path.mongo.should.equal "rootFolder.0.folders.1.folders.0.docs.0"
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should give error if element could not be found', (done)->
|
|
|
|
@locator.findElement {project_id:project._id, element_id:"ddsd432nj42", type:"docs"}, (err, foundElement, path, parentFolder)->
|
|
|
|
err.should.deep.equal new Errors.NotFoundError("entity not found")
|
|
|
|
done()
|
|
|
|
|
|
|
|
|
|
|
|
describe 'finding a folder', ->
|
|
|
|
it 'should return root folder when looking for root folder', (done)->
|
|
|
|
@locator.findElement {project_id:project._id, element_id:rootFolder._id, type:"folder"}, (err, foundElement, path, parentFolder)->
|
|
|
|
assert(!err)
|
|
|
|
foundElement._id.should.equal rootFolder._id
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'when at root', (done)->
|
|
|
|
@locator.findElement {project_id:project._id, element_id:subFolder._id, type:"folder"}, (err, foundElement, path, parentFolder)->
|
|
|
|
assert(!err)
|
|
|
|
foundElement._id.should.equal subFolder._id
|
|
|
|
path.fileSystem.should.equal "/#{subFolder.name}"
|
|
|
|
parentFolder._id.should.equal rootFolder._id
|
|
|
|
path.mongo.should.equal "rootFolder.0.folders.1"
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'when deeply nested', (done)->
|
|
|
|
@locator.findElement {project_id:project._id, element_id:secondSubFolder._id, type:"folder"}, (err, foundElement, path, parentFolder)->
|
|
|
|
assert(!err)
|
|
|
|
foundElement._id.should.equal secondSubFolder._id
|
|
|
|
path.fileSystem.should.equal "/#{subFolder.name}/#{secondSubFolder.name}"
|
|
|
|
parentFolder._id.should.equal subFolder._id
|
|
|
|
path.mongo.should.equal "rootFolder.0.folders.1.folders.0"
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'finding a file', ->
|
|
|
|
it 'when at root', (done)->
|
|
|
|
@locator.findElement {project_id:project._id, element_id:file1._id, type:"fileRefs"}, (err, foundElement, path, parentFolder)->
|
|
|
|
assert(!err)
|
|
|
|
foundElement._id.should.equal file1._id
|
|
|
|
path.fileSystem.should.equal "/#{file1.name}"
|
|
|
|
parentFolder._id.should.equal rootFolder._id
|
|
|
|
path.mongo.should.equal "rootFolder.0.fileRefs.0"
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'when deeply nested', (done)->
|
|
|
|
@locator.findElement {project_id:project._id, element_id:subSubFile._id, type:"fileRefs"}, (err, foundElement, path, parentFolder)->
|
|
|
|
assert(!err)
|
|
|
|
foundElement._id.should.equal subSubFile._id
|
|
|
|
path.fileSystem.should.equal "/#{subFolder.name}/#{secondSubFolder.name}/#{subSubFile.name}"
|
|
|
|
parentFolder._id.should.equal secondSubFolder._id
|
|
|
|
path.mongo.should.equal "rootFolder.0.folders.1.folders.0.fileRefs.0"
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'finding an element with wrong element type', ->
|
|
|
|
it 'should add an s onto the element type', (done)->
|
|
|
|
@locator.findElement {project_id:project._id, element_id:subSubDoc._id, type:"doc"}, (err, foundElement, path, parentFolder)->
|
|
|
|
assert(!err)
|
|
|
|
foundElement._id.should.equal subSubDoc._id
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should convert file to fileRefs', (done)->
|
|
|
|
@locator.findElement {project_id:project._id, element_id:file1._id, type:"fileRefs"}, (err, foundElement, path, parentFolder)->
|
|
|
|
assert(!err)
|
|
|
|
foundElement._id.should.equal file1._id
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'should be able to take actual project as well as id', ->
|
|
|
|
doc3 =
|
|
|
|
_id:"123dsdj3"
|
|
|
|
name:"doc3"
|
|
|
|
rootFolder2 =
|
|
|
|
_id : "123sddedskd"
|
|
|
|
docs:[doc3]
|
|
|
|
project2 =
|
|
|
|
_id : "1234566"
|
|
|
|
rootFolder:[rootFolder2]
|
|
|
|
it 'should find doc in project', (done)->
|
|
|
|
@locator.findElement {project:project2, element_id:doc3._id, type:"docs"}, (err, foundElement, path, parentFolder)->
|
|
|
|
assert(!err?)
|
|
|
|
foundElement._id.should.equal doc3._id
|
|
|
|
path.fileSystem.should.equal "/#{doc3.name}"
|
|
|
|
parentFolder._id.should.equal project2.rootFolder[0]._id
|
|
|
|
path.mongo.should.equal "rootFolder.0.docs.0"
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'finding root doc', ->
|
|
|
|
it 'should return root doc when passed project', (done)->
|
|
|
|
@locator.findRootDoc project, (err, doc)->
|
|
|
|
assert !err?
|
|
|
|
doc._id.should.equal rootDoc._id
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return root doc when passed project_id', (done)->
|
|
|
|
@locator.findRootDoc project._id, (err, doc)->
|
|
|
|
assert !err?
|
|
|
|
doc._id.should.equal rootDoc._id
|
|
|
|
done()
|
2018-02-12 11:03:11 -05:00
|
|
|
|
2015-09-01 12:25:13 -04:00
|
|
|
it 'should return null when the project has no rootDoc', (done) ->
|
|
|
|
project.rootDoc_id = null
|
|
|
|
@locator.findRootDoc project, (err, doc)->
|
|
|
|
assert !err?
|
|
|
|
expect(doc).to.equal null
|
2016-05-23 09:58:28 -04:00
|
|
|
done()
|
2018-02-12 11:03:11 -05:00
|
|
|
|
2016-05-23 09:58:28 -04:00
|
|
|
it 'should return null when the rootDoc_id no longer exists', (done) ->
|
|
|
|
project.rootDoc_id = "doesntexist"
|
|
|
|
@locator.findRootDoc project, (err, doc)->
|
|
|
|
assert !err?
|
|
|
|
expect(doc).to.equal null
|
2015-09-01 12:25:13 -04:00
|
|
|
done()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-11-11 09:44:44 -05:00
|
|
|
describe 'findElementByPath', ->
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
it 'should take a doc path and return the element for a root level document', (done)->
|
|
|
|
path = "#{doc1.name}"
|
2018-02-12 11:03:11 -05:00
|
|
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
element.should.deep.equal doc1
|
2016-09-19 09:32:58 -04:00
|
|
|
expect(type).to.equal "doc"
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should take a doc path and return the element for a root level document with a starting slash', (done)->
|
|
|
|
path = "/#{doc1.name}"
|
2018-02-12 11:03:11 -05:00
|
|
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
element.should.deep.equal doc1
|
2016-09-19 09:32:58 -04:00
|
|
|
expect(type).to.equal "doc"
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
2018-02-12 11:03:11 -05:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
it 'should take a doc path and return the element for a nested document', (done)->
|
|
|
|
path = "#{subFolder.name}/#{secondSubFolder.name}/#{subSubDoc.name}"
|
2018-02-12 11:03:11 -05:00
|
|
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
element.should.deep.equal subSubDoc
|
2016-09-19 09:32:58 -04:00
|
|
|
expect(type).to.equal "doc"
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should take a file path and return the element for a root level document', (done)->
|
|
|
|
path = "#{file1.name}"
|
2018-02-12 11:03:11 -05:00
|
|
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
element.should.deep.equal file1
|
2016-09-19 09:32:58 -04:00
|
|
|
expect(type).to.equal "file"
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should take a file path and return the element for a nested document', (done)->
|
|
|
|
path = "#{subFolder.name}/#{secondSubFolder.name}/#{subSubFile.name}"
|
2018-02-12 11:03:11 -05:00
|
|
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
element.should.deep.equal subSubFile
|
2016-09-19 09:32:58 -04:00
|
|
|
expect(type).to.equal "file"
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should take a file path and return the element for a nested document case insenstive', (done)->
|
|
|
|
path = "#{subFolder.name.toUpperCase()}/#{secondSubFolder.name.toUpperCase()}/#{subSubFile.name.toUpperCase()}"
|
2018-02-12 11:03:11 -05:00
|
|
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
element.should.deep.equal subSubFile
|
2016-09-19 09:32:58 -04:00
|
|
|
expect(type).to.equal "file"
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should take a file path and return the element for a nested folder', (done)->
|
|
|
|
path = "#{subFolder.name}/#{secondSubFolder.name}"
|
2018-02-12 11:03:11 -05:00
|
|
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
element.should.deep.equal secondSubFolder
|
2016-09-19 09:32:58 -04:00
|
|
|
expect(type).to.equal "folder"
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should take a file path and return the root folder', (done)->
|
2018-02-12 11:03:11 -05:00
|
|
|
path = "/"
|
|
|
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
element.should.deep.equal rootFolder
|
2016-09-19 09:32:58 -04:00
|
|
|
expect(type).to.equal "folder"
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return an error if the file can not be found inside know folder', (done)->
|
2018-02-12 11:03:11 -05:00
|
|
|
path = "#{subFolder.name}/#{secondSubFolder.name}/exist.txt"
|
|
|
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
err.should.not.equal undefined
|
|
|
|
assert.equal element, undefined
|
2016-09-19 09:32:58 -04:00
|
|
|
expect(type).to.be.undefined
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should return an error if the file can not be found inside unknown folder', (done)->
|
2018-02-12 11:03:11 -05:00
|
|
|
path = "this/does/not/exist.txt"
|
|
|
|
@locator.findElementByPath {project, path}, (err, element, type)->
|
2014-02-12 05:23:40 -05:00
|
|
|
err.should.not.equal undefined
|
|
|
|
assert.equal element, undefined
|
2016-09-19 09:32:58 -04:00
|
|
|
expect(type).to.be.undefined
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
2015-02-18 16:41:03 -05:00
|
|
|
|
2014-11-11 09:44:44 -05:00
|
|
|
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: []
|
|
|
|
]
|
|
|
|
|
|
|
|
it "should not call the callback more than once", (done)->
|
2018-02-12 11:03:11 -05:00
|
|
|
path = "#{@duplicateFolder.name}/#{@doc.name}"
|
|
|
|
@locator.findElementByPath {@project, path}, ->
|
2014-11-11 09:44:44 -05:00
|
|
|
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)->
|
2018-02-12 11:03:11 -05:00
|
|
|
path = "#{@duplicateFolder.name}/1/main.tex"
|
|
|
|
@locator.findElementByPath {@project, path}, ->
|
2014-11-11 09:44:44 -05:00
|
|
|
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"}]
|
|
|
|
]
|
|
|
|
|
|
|
|
it "should not crash with a null", (done)->
|
2018-02-12 11:03:11 -05:00
|
|
|
path = "/other.tex"
|
|
|
|
@locator.findElementByPath {@project, path}, (err, element)->
|
2014-11-11 09:44:44 -05:00
|
|
|
element.name.should.equal "other.tex"
|
|
|
|
done()
|
|
|
|
|
2015-02-18 16:41:03 -05:00
|
|
|
describe "with a null project", ->
|
|
|
|
beforeEach ->
|
2018-02-12 11:03:11 -05:00
|
|
|
@ProjectGetter =
|
|
|
|
getProject: sinon.stub().callsArg(2)
|
2015-02-18 16:41:03 -05:00
|
|
|
|
|
|
|
it "should not crash with a null", (done)->
|
2018-02-12 11:03:11 -05:00
|
|
|
path = "/other.tex"
|
|
|
|
@locator.findElementByPath {project_id: @project._id, path}, (err, element)->
|
2015-02-18 16:41:03 -05:00
|
|
|
expect(err).to.exist
|
2018-02-12 11:03:11 -05:00
|
|
|
done()
|
2014-11-11 09:44:44 -05:00
|
|
|
|
2018-02-12 11:03:11 -05:00
|
|
|
describe "with a project_id", ->
|
|
|
|
it 'should take a doc path and return the element for a root level document', (done)->
|
|
|
|
path = "#{doc1.name}"
|
|
|
|
@locator.findElementByPath {project_id: project._id, path}, (err, element, type)=>
|
|
|
|
@ProjectGetter.getProject
|
|
|
|
.calledWith(project._id, {rootFolder:true, rootDoc_id: true})
|
|
|
|
.should.equal true
|
|
|
|
element.should.deep.equal doc1
|
|
|
|
expect(type).to.equal "doc"
|
|
|
|
done()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2015-12-10 12:06:00 -05:00
|
|
|
describe 'findUsersProjectByName finding a project by user_id and project name', ()->
|
|
|
|
it 'should return the project from an array case insenstive', (done)->
|
2014-02-12 05:23:40 -05:00
|
|
|
user_id = "123jojoidns"
|
|
|
|
stubbedProject = {name:"findThis"}
|
2017-10-20 06:49:20 -04:00
|
|
|
projects = {
|
|
|
|
owned: [{name:"notThis"}, {name:"wellll"}, stubbedProject, {name:"Noooo"}]
|
|
|
|
}
|
2016-03-03 12:19:03 -05:00
|
|
|
@ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects)
|
2014-02-12 05:23:40 -05:00
|
|
|
@locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
|
|
|
|
project.should.equal stubbedProject
|
|
|
|
done()
|
|
|
|
|
2015-12-10 12:06:00 -05:00
|
|
|
it 'should return the project which is not archived', (done)->
|
|
|
|
user_id = "123jojoidns"
|
|
|
|
stubbedProject = {name:"findThis", _id:12331321}
|
2017-10-20 06:49:20 -04:00
|
|
|
projects = {
|
|
|
|
owned: [
|
|
|
|
{name:"notThis"},
|
|
|
|
{name:"wellll"},
|
|
|
|
{name:"findThis",archived:true},
|
|
|
|
stubbedProject,
|
|
|
|
{name:"findThis",archived:true},
|
|
|
|
{name:"Noooo"}
|
|
|
|
]
|
|
|
|
}
|
2016-03-03 12:19:03 -05:00
|
|
|
@ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects)
|
2015-12-10 12:06:00 -05:00
|
|
|
@locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
|
|
|
|
project._id.should.equal stubbedProject._id
|
|
|
|
done()
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
it 'should search collab projects as well', (done)->
|
|
|
|
user_id = "123jojoidns"
|
|
|
|
stubbedProject = {name:"findThis"}
|
2017-10-20 06:49:20 -04:00
|
|
|
projects = {
|
|
|
|
owned: [{name:"notThis"}, {name:"wellll"}, {name:"Noooo"}]
|
|
|
|
readAndWrite: [stubbedProject]
|
|
|
|
}
|
|
|
|
@ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects)
|
2014-02-12 05:23:40 -05:00
|
|
|
@locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
|
|
|
|
project.should.equal stubbedProject
|
|
|
|
done()
|
|
|
|
|