2019-08-27 06:30:15 -04:00
|
|
|
const { expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
const modulePath = '../../../../app/src/Features/Project/ProjectLocator'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const Errors = require('../../../../app/src/Features/Errors/Errors')
|
|
|
|
|
|
|
|
const project = { _id: '1234566', rootFolder: [] }
|
2019-08-27 06:30:15 -04:00
|
|
|
class Project {}
|
2019-05-29 05:21:06 -04:00
|
|
|
const rootDoc = { name: 'rootDoc', _id: 'das239djd' }
|
|
|
|
const doc1 = { name: 'otherDoc.txt', _id: 'dsad2ddd' }
|
|
|
|
const doc2 = { name: 'docname.txt', _id: 'dsad2ddddd' }
|
|
|
|
const file1 = { name: 'file1', _id: 'dsa9lkdsad' }
|
|
|
|
const subSubFile = { name: 'subSubFile', _id: 'd1d2dk' }
|
|
|
|
const subSubDoc = { name: 'subdoc.txt', _id: '321dmdwi' }
|
|
|
|
const secondSubFolder = {
|
|
|
|
name: 'secondSubFolder',
|
|
|
|
_id: 'dsa3e23',
|
|
|
|
docs: [subSubDoc],
|
|
|
|
fileRefs: [subSubFile],
|
2021-04-27 03:52:58 -04:00
|
|
|
folders: [],
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const subFolder = {
|
|
|
|
name: 'subFolder',
|
|
|
|
_id: 'dsadsa93',
|
|
|
|
folders: [secondSubFolder, null],
|
|
|
|
docs: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
fileRefs: [],
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const subFolder1 = { name: 'subFolder1', _id: '123asdjoij' }
|
|
|
|
|
|
|
|
const rootFolder = {
|
|
|
|
_id: '123sdskd',
|
|
|
|
docs: [doc1, doc2, null, rootDoc],
|
|
|
|
fileRefs: [file1],
|
2021-04-27 03:52:58 -04:00
|
|
|
folders: [subFolder1, subFolder],
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
project.rootFolder[0] = rootFolder
|
|
|
|
project.rootDoc_id = rootDoc._id
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('ProjectLocator', function () {
|
|
|
|
beforeEach(function () {
|
2019-08-27 06:30:15 -04:00
|
|
|
Project.findById = (projectId, callback) => {
|
|
|
|
callback(null, project)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.ProjectGetter = {
|
2021-04-27 03:52:58 -04:00
|
|
|
getProject: sinon.stub().callsArgWith(2, null, project),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-09 05:40:11 -04:00
|
|
|
this.ProjectHelper = {
|
2019-08-27 06:38:17 -04:00
|
|
|
isArchived: sinon.stub(),
|
|
|
|
isTrashed: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
isArchivedOrTrashed: sinon.stub(),
|
2019-08-09 05:40:11 -04:00
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator = SandboxedModule.require(modulePath, {
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
|
|
|
'../../models/Project': { Project },
|
|
|
|
'../../models/User': { User: this.User },
|
|
|
|
'./ProjectGetter': this.ProjectGetter,
|
2021-04-27 03:52:58 -04:00
|
|
|
'./ProjectHelper': this.ProjectHelper,
|
|
|
|
},
|
2019-08-27 06:30:15 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('finding a doc', function () {
|
|
|
|
it('finds one at the root level', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project_id: project._id, element_id: doc2._id, type: 'docs' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
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')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('when it is nested', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project_id: project._id, element_id: subSubDoc._id, type: 'doc' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
expect(foundElement._id).to.equal(subSubDoc._id)
|
2019-05-29 05:21:06 -04:00
|
|
|
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')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should give error if element could not be found', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project_id: project._id, element_id: 'ddsd432nj42', type: 'docs' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-11-18 09:37:05 -05:00
|
|
|
expect(err).to.be.instanceOf(Errors.NotFoundError)
|
|
|
|
expect(err).to.have.property('message', 'entity not found')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('finding a folder', function () {
|
|
|
|
it('should return root folder when looking for root folder', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project_id: project._id, element_id: rootFolder._id, type: 'folder' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
foundElement._id.should.equal(rootFolder._id)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('when at root', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project_id: project._id, element_id: subFolder._id, type: 'folder' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
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')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('when deeply nested', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
project_id: project._id,
|
|
|
|
element_id: secondSubFolder._id,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'folder',
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
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')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('finding a file', function () {
|
|
|
|
it('when at root', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project_id: project._id, element_id: file1._id, type: 'fileRefs' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
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')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('when deeply nested', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
project_id: project._id,
|
|
|
|
element_id: subSubFile._id,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'fileRefs',
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
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')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('finding an element with wrong element type', function () {
|
|
|
|
it('should add an s onto the element type', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project_id: project._id, element_id: subSubDoc._id, type: 'doc' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
foundElement._id.should.equal(subSubDoc._id)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should convert file to fileRefs', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project_id: project._id, element_id: file1._id, type: 'fileRefs' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
foundElement._id.should.equal(file1._id)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('should be able to take actual project as well as id', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const doc3 = {
|
|
|
|
_id: '123dsdj3',
|
2021-04-27 03:52:58 -04:00
|
|
|
name: 'doc3',
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const rootFolder2 = {
|
|
|
|
_id: '123sddedskd',
|
2021-04-27 03:52:58 -04:00
|
|
|
docs: [doc3],
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const project2 = {
|
|
|
|
_id: '1234566',
|
2021-04-27 03:52:58 -04:00
|
|
|
rootFolder: [rootFolder2],
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should find doc in project', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project: project2, element_id: doc3._id, type: 'docs' },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, foundElement, path, parentFolder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
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')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('finding root doc', function () {
|
|
|
|
it('should return root doc when passed project', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findRootDoc(project, (err, doc) => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
doc._id.should.equal(rootDoc._id)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return root doc when passed project_id', function (done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findRootDoc(project._id, (err, doc) => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
doc._id.should.equal(rootDoc._id)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return null when the project has no rootDoc', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
project.rootDoc_id = null
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findRootDoc(project, (err, doc) => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(doc).to.equal(null)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return null when the rootDoc_id no longer exists', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
project.rootDoc_id = 'doesntexist'
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findRootDoc(project, (err, doc) => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(doc).to.equal(null)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('findElementByPath', function () {
|
|
|
|
it('should take a doc path and return the element for a root level document', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = `${doc1.name}`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-08-07 10:04:04 -04:00
|
|
|
{ project, path },
|
2022-09-21 08:01:56 -04:00
|
|
|
(err, element, type, folder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
element.should.deep.equal(doc1)
|
|
|
|
expect(type).to.equal('doc')
|
2022-09-21 08:01:56 -04:00
|
|
|
expect(folder).to.equal(rootFolder)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should take a doc path and return the element for a root level document with a starting slash', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = `/${doc1.name}`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-08-07 10:04:04 -04:00
|
|
|
{ project, path },
|
2022-09-21 08:01:56 -04:00
|
|
|
(err, element, type, folder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
element.should.deep.equal(doc1)
|
|
|
|
expect(type).to.equal('doc')
|
2022-09-21 08:01:56 -04:00
|
|
|
expect(folder).to.equal(rootFolder)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should take a doc path and return the element for a nested document', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = `${subFolder.name}/${secondSubFolder.name}/${subSubDoc.name}`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-08-07 10:04:04 -04:00
|
|
|
{ project, path },
|
2022-09-21 08:01:56 -04:00
|
|
|
(err, element, type, folder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
element.should.deep.equal(subSubDoc)
|
|
|
|
expect(type).to.equal('doc')
|
2022-09-21 08:01:56 -04:00
|
|
|
expect(folder).to.equal(secondSubFolder)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should take a file path and return the element for a root level document', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = `${file1.name}`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-08-07 10:04:04 -04:00
|
|
|
{ project, path },
|
2022-09-21 08:01:56 -04:00
|
|
|
(err, element, type, folder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
element.should.deep.equal(file1)
|
|
|
|
expect(type).to.equal('file')
|
2022-09-21 08:01:56 -04:00
|
|
|
expect(folder).to.equal(rootFolder)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should take a file path and return the element for a nested document', function (done) {
|
2020-12-15 05:23:54 -05:00
|
|
|
const path = `${subFolder.name}/${secondSubFolder.name}/${subSubFile.name}`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-08-07 10:04:04 -04:00
|
|
|
{ project, path },
|
2022-09-21 08:01:56 -04:00
|
|
|
(err, element, type, folder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
element.should.deep.equal(subSubFile)
|
|
|
|
expect(type).to.equal('file')
|
2022-09-21 08:01:56 -04:00
|
|
|
expect(folder).to.equal(secondSubFolder)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should take a file path and return the element for a nested document case insenstive', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = `${subFolder.name.toUpperCase()}/${secondSubFolder.name.toUpperCase()}/${subSubFile.name.toUpperCase()}`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-08-07 10:04:04 -04:00
|
|
|
{ project, path },
|
2022-09-21 08:01:56 -04:00
|
|
|
(err, element, type, folder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
element.should.deep.equal(subSubFile)
|
|
|
|
expect(type).to.equal('file')
|
2022-09-21 08:01:56 -04:00
|
|
|
expect(folder).to.equal(secondSubFolder)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2022-12-14 06:28:29 -05:00
|
|
|
it('should not return elements with a case-insensitive match when exactCaseMatch is true', function (done) {
|
|
|
|
const path = `${subFolder.name.toUpperCase()}/${secondSubFolder.name.toUpperCase()}/${subSubFile.name.toUpperCase()}`
|
|
|
|
this.locator.findElementByPath(
|
|
|
|
{ project, path, exactCaseMatch: true },
|
|
|
|
(err, element, type, folder) => {
|
|
|
|
err.should.not.equal(undefined)
|
|
|
|
expect(element).to.be.undefined
|
|
|
|
expect(type).to.be.undefined
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should take a file path and return the element for a nested folder', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = `${subFolder.name}/${secondSubFolder.name}`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-08-07 10:04:04 -04:00
|
|
|
{ project, path },
|
2022-09-21 08:01:56 -04:00
|
|
|
(err, element, type, folder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
element.should.deep.equal(secondSubFolder)
|
|
|
|
expect(type).to.equal('folder')
|
2022-09-21 08:01:56 -04:00
|
|
|
expect(folder).to.equal(subFolder)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should take a file path and return the root folder', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = '/'
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-08-07 10:04:04 -04:00
|
|
|
{ project, path },
|
2022-09-21 08:01:56 -04:00
|
|
|
(err, element, type, folder) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
element.should.deep.equal(rootFolder)
|
|
|
|
expect(type).to.equal('folder')
|
2022-09-21 08:01:56 -04:00
|
|
|
expect(folder).to.equal(null)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return an error if the file can not be found inside know folder', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = `${subFolder.name}/${secondSubFolder.name}/exist.txt`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-08-07 10:04:04 -04:00
|
|
|
{ project, path },
|
|
|
|
(err, element, type) => {
|
|
|
|
err.should.not.equal(undefined)
|
2019-08-27 06:30:15 -04:00
|
|
|
expect(element).to.be.undefined
|
2019-08-07 10:04:04 -04:00
|
|
|
expect(type).to.be.undefined
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return an error if the file can not be found inside unknown folder', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = 'this/does/not/exist.txt'
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-08-07 10:04:04 -04:00
|
|
|
{ project, path },
|
|
|
|
(err, element, type) => {
|
|
|
|
err.should.not.equal(undefined)
|
2019-08-27 06:30:15 -04:00
|
|
|
expect(element).to.be.undefined
|
2019-08-07 10:04:04 -04:00
|
|
|
expect(type).to.be.undefined
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('where duplicate folder exists', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.duplicateFolder = {
|
|
|
|
name: 'duplicate1',
|
|
|
|
_id: '1234',
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
name: '1',
|
|
|
|
docs: [{ name: 'main.tex', _id: '456' }],
|
|
|
|
folders: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
fileRefs: [],
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
],
|
|
|
|
docs: [(this.doc = { name: 'main.tex', _id: '456' })],
|
2021-04-27 03:52:58 -04:00
|
|
|
fileRefs: [],
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
this.project = {
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: [
|
|
|
|
{
|
|
|
|
folders: [this.duplicateFolder, this.duplicateFolder],
|
|
|
|
fileRefs: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
docs: [],
|
|
|
|
},
|
|
|
|
],
|
2019-08-27 06:30:15 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not call the callback more than once', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = `${this.duplicateFolder.name}/${this.doc.name}`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath({ project: this.project, path }, () =>
|
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
}) // mocha will throw exception if done called multiple times
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not call the callback more than once when the path is longer than 1 level below the duplicate level', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = `${this.duplicateFolder.name}/1/main.tex`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath({ project: this.project, path }, () =>
|
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}) // mocha will throw exception if done called multiple times
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with a null doc', function () {
|
|
|
|
beforeEach(function () {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.project = {
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: [
|
|
|
|
{
|
|
|
|
folders: [],
|
|
|
|
fileRefs: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
docs: [{ name: 'main.tex' }, null, { name: 'other.tex' }],
|
|
|
|
},
|
|
|
|
],
|
2019-08-27 06:30:15 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not crash with a null', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = '/other.tex'
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project: this.project, path },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, element) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
element.name.should.equal('other.tex')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with a null project', function () {
|
|
|
|
beforeEach(function () {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.ProjectGetter = { getProject: sinon.stub().callsArg(2) }
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not crash with a null', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = '/other.tex'
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project_id: project._id, path },
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, element) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(err).to.exist
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with a project_id', function () {
|
|
|
|
it('should take a doc path and return the element for a root level document', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = `${doc1.name}`
|
2019-08-27 06:30:15 -04:00
|
|
|
this.locator.findElementByPath(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ project_id: project._id, path },
|
|
|
|
(err, element, type) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectGetter.getProject
|
|
|
|
.calledWith(project._id, { rootFolder: true, rootDoc_id: true })
|
|
|
|
.should.equal(true)
|
|
|
|
element.should.deep.equal(doc1)
|
|
|
|
expect(type).to.equal('doc')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2022-09-12 10:59:16 -04:00
|
|
|
|
|
|
|
describe('findElementByMongoPath', function () {
|
|
|
|
it('traverses the file tree like Mongo would do', function () {
|
|
|
|
const element = this.locator.findElementByMongoPath(
|
|
|
|
project,
|
|
|
|
'rootFolder.0.folders.1.folders.0.fileRefs.0'
|
|
|
|
)
|
|
|
|
expect(element).to.equal(subSubFile)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws an error if no element is found', function () {
|
|
|
|
expect(() =>
|
|
|
|
this.locator.findElementByMongoPath(
|
|
|
|
project,
|
|
|
|
'rootolder.0.folders.0.folders.0.fileRefs.0'
|
|
|
|
)
|
|
|
|
).to.throw
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|