2019-05-29 05:21:06 -04:00
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const { assert } = require('chai')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const tk = require('timekeeper')
|
|
|
|
const Errors = require('../../../../app/src/Features/Errors/Errors')
|
|
|
|
const { ObjectId } = require('mongoose').Types
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
const MODULE_PATH =
|
|
|
|
'../../../../app/src/Features/Project/ProjectEntityMongoUpdateHandler'
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
describe('ProjectEntityMongoUpdateHandler', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
const projectId = '4eecb1c1bffa66588e0000a1'
|
|
|
|
const docId = '4eecb1c1bffa66588e0000a2'
|
|
|
|
const fileId = '4eecb1c1bffa66588e0000a3'
|
|
|
|
const folderId = '4eecaffcbffa66588e000008'
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.FolderModel = class Folder {
|
2019-05-29 05:21:06 -04:00
|
|
|
constructor(options) {
|
|
|
|
;({ name: this.name } = options)
|
|
|
|
this._id = 'folder_id'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.docName = 'doc-name'
|
|
|
|
this.fileName = 'something.jpg'
|
2019-11-04 04:50:15 -05:00
|
|
|
this.project = { _id: projectId, name: 'project name' }
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
|
|
|
|
tk.freeze(Date.now())
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject = SandboxedModule.require(MODULE_PATH, {
|
2019-07-15 06:33:47 -04:00
|
|
|
globals: {
|
|
|
|
console: console
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
|
|
|
'logger-sharelatex': (this.logger = {
|
|
|
|
log: sinon.stub(),
|
2019-07-01 09:48:09 -04:00
|
|
|
warn: sinon.stub(),
|
2019-05-29 05:21:06 -04:00
|
|
|
error: sinon.stub(),
|
|
|
|
err() {}
|
|
|
|
}),
|
|
|
|
'settings-sharelatex': (this.settings = {
|
|
|
|
maxEntitiesPerProject: 100
|
|
|
|
}),
|
|
|
|
'../Cooldown/CooldownManager': (this.CooldownManager = {}),
|
|
|
|
'../../models/Folder': {
|
|
|
|
Folder: this.FolderModel
|
|
|
|
},
|
|
|
|
'../../infrastructure/LockManager': (this.LockManager = {
|
|
|
|
runWithLock: sinon.spy((namespace, id, runner, callback) =>
|
|
|
|
runner(callback)
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
'../../models/Project': {
|
|
|
|
Project: (this.ProjectModel = {})
|
|
|
|
},
|
|
|
|
'./ProjectEntityHandler': (this.ProjectEntityHandler = {}),
|
|
|
|
'./ProjectLocator': (this.ProjectLocator = {}),
|
|
|
|
'./ProjectGetter': (this.ProjectGetter = {
|
|
|
|
getProjectWithoutLock: sinon.stub().yields(null, this.project)
|
2019-11-18 09:37:05 -05:00
|
|
|
}),
|
|
|
|
'../Errors/Errors': Errors
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-08-07 10:04:04 -04:00
|
|
|
afterEach(function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
tk.reset()
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
describe('addDoc', function() {
|
|
|
|
beforeEach(function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._confirmFolder = sinon.stub().yields(folderId)
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subject._putElement = sinon.stub()
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.doc = { _id: docId }
|
|
|
|
this.subject.addDoc(projectId, folderId, this.doc, this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('gets the project', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectGetter.getProjectWithoutLock
|
|
|
|
.calledWith(projectId, {
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: true,
|
|
|
|
name: true,
|
|
|
|
overleaf: true
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('checks the folder exists', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._confirmFolder
|
|
|
|
.calledWith(this.project, folderId)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('puts the element in mongo', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement
|
|
|
|
.calledWith(this.project, folderId, this.doc, 'doc', this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('addFile', function() {
|
|
|
|
beforeEach(function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._confirmFolder = sinon.stub().yields(folderId)
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subject._putElement = sinon.stub()
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.file = { _id: fileId }
|
|
|
|
this.subject.addFile(projectId, folderId, this.file, this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('gets the project', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectGetter.getProjectWithoutLock
|
|
|
|
.calledWith(projectId, {
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: true,
|
|
|
|
name: true,
|
|
|
|
overleaf: true
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('checks the folder exists', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._confirmFolder
|
|
|
|
.calledWith(this.project, folderId)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('puts the element in mongo', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement
|
|
|
|
.calledWith(this.project, folderId, this.file, 'file', this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('replaceFileWithNew', function() {
|
|
|
|
beforeEach(function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.file = { _id: fileId }
|
2019-05-29 05:21:06 -04:00
|
|
|
this.path = { mongo: 'file.png' }
|
|
|
|
this.newFile = { _id: 'new-file-id' }
|
|
|
|
this.newFile.linkedFileData = this.linkedFileData = { provider: 'url' }
|
|
|
|
this.newProject = 'new-project'
|
|
|
|
this.ProjectLocator.findElement = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.file, this.path)
|
|
|
|
this.ProjectModel.findOneAndUpdate = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.newProject)
|
|
|
|
this.ProjectModel.update = sinon.stub().yields()
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.replaceFileWithNew(
|
|
|
|
projectId,
|
|
|
|
fileId,
|
2019-05-29 05:21:06 -04:00
|
|
|
this.newFile,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('gets the project', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectGetter.getProjectWithoutLock
|
|
|
|
.calledWith(projectId, {
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: true,
|
|
|
|
name: true,
|
|
|
|
overleaf: true
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('finds the existing element', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectLocator.findElement
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith({
|
|
|
|
project: this.project,
|
2019-11-04 04:50:15 -05:00
|
|
|
element_id: fileId,
|
2019-05-29 05:21:06 -04:00
|
|
|
type: 'file'
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('inserts a deletedFile reference for the old file', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectModel.update
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
2019-11-04 04:50:15 -05:00
|
|
|
{ _id: projectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
$push: {
|
|
|
|
deletedFiles: {
|
2019-11-04 04:50:15 -05:00
|
|
|
_id: fileId,
|
2019-05-29 05:21:06 -04:00
|
|
|
name: this.file.name,
|
|
|
|
linkedFileData: this.file.linkedFileData,
|
|
|
|
hash: this.file.hash,
|
|
|
|
deletedAt: new Date()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('increments the project version and sets the rev and created_at', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectModel.findOneAndUpdate
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
2019-11-04 04:50:15 -05:00
|
|
|
{ _id: projectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
$inc: { version: 1, 'file.png.rev': 1 },
|
|
|
|
$set: {
|
|
|
|
'file.png._id': this.newFile._id,
|
|
|
|
'file.png.created': new Date(),
|
|
|
|
'file.png.linkedFileData': this.linkedFileData,
|
|
|
|
'file.png.hash': this.hash
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ new: true }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('calls the callback', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.callback
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(null, this.file, this.project, this.path, this.newProject)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('mkdirp', function() {
|
|
|
|
beforeEach(function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.parentFolderId = '1jnjknjk'
|
2019-05-29 05:21:06 -04:00
|
|
|
this.newFolder = { _id: 'newFolder_id_here' }
|
|
|
|
this.lastFolder = { _id: '123das', folders: [] }
|
|
|
|
|
|
|
|
this.rootFolder = { _id: 'rootFolderId' }
|
2019-11-04 04:50:15 -05:00
|
|
|
this.project = { _id: projectId, rootFolder: [this.rootFolder] }
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
this.ProjectGetter.getProjectWithOnlyFolders = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.project)
|
|
|
|
this.ProjectLocator.findElementByPath = function() {}
|
2019-11-18 09:37:05 -05:00
|
|
|
sinon
|
|
|
|
.stub(this.ProjectLocator, 'findElementByPath')
|
|
|
|
.callsFake((options, cb) => {
|
|
|
|
const { path } = options
|
|
|
|
this.parentFolder = { _id: 'parentFolder_id_here' }
|
|
|
|
const lastFolder = path.substring(path.lastIndexOf('/'))
|
|
|
|
if (lastFolder.indexOf('level1') === -1) {
|
|
|
|
cb(new Error('level1 is not the last folder'))
|
|
|
|
} else {
|
|
|
|
cb(null, this.parentFolder)
|
|
|
|
}
|
|
|
|
})
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.addFolder = {
|
|
|
|
withoutLock: (projectId, parentFolderId, folderName, callback) => {
|
|
|
|
return callback(null, { name: folderName }, this.parentFolderId)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the root folder if the path is just a slash', function(done) {
|
|
|
|
const path = '/'
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.mkdirp(projectId, path, {}, (err, folders, lastFolder) => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
lastFolder.should.deep.equal(this.rootFolder)
|
|
|
|
assert.equal(lastFolder.parentFolder_id, undefined)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should make just one folder', function(done) {
|
|
|
|
const path = '/differentFolder/'
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.mkdirp(projectId, path, {}, (err, folders, lastFolder) => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
folders.length.should.equal(1)
|
|
|
|
lastFolder.name.should.equal('differentFolder')
|
|
|
|
lastFolder.parentFolder_id.should.equal(this.parentFolderId)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should make the final folder in path if it doesnt exist with one level', function(done) {
|
|
|
|
const path = 'level1/level2'
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.mkdirp(projectId, path, {}, (err, folders, lastFolder) => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
folders.length.should.equal(1)
|
|
|
|
lastFolder.name.should.equal('level2')
|
|
|
|
lastFolder.parentFolder_id.should.equal(this.parentFolderId)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should make the final folder in path if it doesnt exist with mutliple levels', function(done) {
|
|
|
|
const path = 'level1/level2/level3'
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.mkdirp(projectId, path, {}, (err, folders, lastFolder) => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
folders.length.should.equal(2)
|
|
|
|
folders[0].name.should.equal('level2')
|
|
|
|
folders[0].parentFolder_id.should.equal(this.parentFolderId)
|
|
|
|
lastFolder.name.should.equal('level3')
|
|
|
|
lastFolder.parentFolder_id.should.equal(this.parentFolderId)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should work with slashes either side', function(done) {
|
|
|
|
const path = '/level1/level2/level3/'
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.mkdirp(projectId, path, {}, (err, folders, lastFolder) => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
folders.length.should.equal(2)
|
|
|
|
folders[0].name.should.equal('level2')
|
|
|
|
folders[0].parentFolder_id.should.equal(this.parentFolderId)
|
|
|
|
lastFolder.name.should.equal('level3')
|
|
|
|
lastFolder.parentFolder_id.should.equal(this.parentFolderId)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should use a case-insensitive match by default', function(done) {
|
|
|
|
const path = '/differentFolder/'
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.mkdirp(projectId, path, {}, (err, folders, lastFolder) => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectLocator.findElementByPath
|
|
|
|
.calledWithMatch({ exactCaseMatch: undefined })
|
|
|
|
.should.equal(true)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should use a case-sensitive match if exactCaseMatch option is set', function(done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const path = '/differentFolder/'
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.mkdirp(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
path,
|
|
|
|
{ exactCaseMatch: true },
|
|
|
|
(err, folders, lastFolder) => {
|
2019-11-04 04:50:15 -05:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectLocator.findElementByPath
|
|
|
|
.calledWithMatch({ exactCaseMatch: true })
|
|
|
|
.should.equal(true)
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('moveEntity', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.pathAfterMove = {
|
|
|
|
fileSystem: '/somewhere/else.txt'
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
|
|
|
|
this.ProjectEntityHandler.getAllEntitiesFromProject
|
|
|
|
.onFirstCall()
|
|
|
|
.yields(
|
|
|
|
null,
|
|
|
|
(this.oldDocs = ['old-doc']),
|
|
|
|
(this.oldFiles = ['old-file'])
|
|
|
|
)
|
|
|
|
this.ProjectEntityHandler.getAllEntitiesFromProject
|
|
|
|
.onSecondCall()
|
|
|
|
.yields(
|
|
|
|
null,
|
|
|
|
(this.newDocs = ['new-doc']),
|
|
|
|
(this.newFiles = ['new-file'])
|
|
|
|
)
|
|
|
|
|
|
|
|
this.doc = { lines: ['1234', '312343d'], rev: '1234' }
|
|
|
|
this.path = {
|
|
|
|
mongo: 'folders[0]',
|
|
|
|
fileSystem: '/old_folder/somewhere.txt'
|
|
|
|
}
|
|
|
|
this.newProject = 'new-project'
|
|
|
|
this.ProjectLocator.findElement = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs({
|
|
|
|
project: this.project,
|
|
|
|
element_id: this.docId,
|
|
|
|
type: 'docs'
|
|
|
|
})
|
|
|
|
.yields(null, this.doc, this.path)
|
|
|
|
|
|
|
|
this.subject._checkValidMove = sinon.stub().yields()
|
|
|
|
|
|
|
|
this.subject._removeElementFromMongoArray = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.newProject)
|
|
|
|
this.subject._putElement = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, { path: this.pathAfterMove }, this.newProject)
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.moveEntity(projectId, docId, folderId, 'docs', this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the project', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectGetter.getProjectWithoutLock
|
|
|
|
.calledWith(projectId, {
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: true,
|
|
|
|
name: true,
|
|
|
|
overleaf: true
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should find the doc to move', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectLocator.findElement
|
|
|
|
.calledWith({ element_id: docId, type: 'docs', project: this.project })
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should check this is a valid move', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._checkValidMove
|
|
|
|
.calledWith(this.project, 'docs', this.doc, this.path, folderId)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should put the element in the new folder', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement
|
|
|
|
.calledWith(this.project, folderId, this.doc, 'docs')
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove the element from its current position', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._removeElementFromMongoArray
|
|
|
|
.calledWith(this.ProjectModel, projectId, this.path.mongo, docId)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove the element from its current position after putting the element in the new folder', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._removeElementFromMongoArray
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledAfter(this.subject._putElement)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('calls the callback', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
const changes = {
|
|
|
|
oldDocs: this.oldDocs,
|
|
|
|
newDocs: this.newDocs,
|
|
|
|
oldFiles: this.oldFiles,
|
|
|
|
newFiles: this.newFiles,
|
|
|
|
newProject: this.newProject
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.callback
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
|
|
|
null,
|
|
|
|
this.project,
|
|
|
|
this.path.fileSystem,
|
|
|
|
this.pathAfterMove.fileSystem,
|
|
|
|
this.doc.rev,
|
|
|
|
changes
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('moveEntity must refuse to move the folder to a subfolder of itself', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.pathAfterMove = {
|
|
|
|
fileSystem: '/somewhere/else.txt'
|
|
|
|
}
|
|
|
|
|
|
|
|
this.doc = { lines: ['1234', '312343d'], rev: '1234' }
|
|
|
|
this.path = {
|
|
|
|
mongo: 'folders[0]',
|
|
|
|
fileSystem: '/old_folder/somewhere.txt'
|
|
|
|
}
|
|
|
|
this.newProject = 'new-project'
|
|
|
|
this.ProjectLocator.findElement = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs({
|
|
|
|
project: this.project,
|
|
|
|
element_id: this.docId,
|
|
|
|
type: 'docs'
|
|
|
|
})
|
|
|
|
.yields(null, this.doc, this.path)
|
|
|
|
|
|
|
|
// return an error when moving a folder to a subfolder of itself
|
|
|
|
this.subject._checkValidMove = sinon.stub().yields(new Error())
|
|
|
|
|
|
|
|
this.subject._removeElementFromMongoArray = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.project)
|
|
|
|
this.subject._putElement = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, { path: this.pathAfterMove }, this.newProject)
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.moveEntity(projectId, docId, folderId, 'docs', this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the project', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectGetter.getProjectWithoutLock
|
|
|
|
.calledWith(projectId, {
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: true,
|
|
|
|
name: true,
|
|
|
|
overleaf: true
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should find the doc to move', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectLocator.findElement
|
|
|
|
.calledWith({ element_id: docId, type: 'docs', project: this.project })
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should check this is an invalid move', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._checkValidMove
|
|
|
|
.calledWith(this.project, 'docs', this.doc, this.path, folderId)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not put the element in the new folder', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement.called.should.equal(false)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not remove the element from its current position', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._removeElementFromMongoArray.called.should.equal(false)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('calls the callback with an error', function() {
|
2019-11-18 09:37:05 -05:00
|
|
|
this.callback.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('deleteEntity', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.path = { mongo: 'mongo.path', fileSystem: '/file/system/path' }
|
2019-11-04 04:50:15 -05:00
|
|
|
this.doc = { _id: docId }
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectLocator.findElement = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.doc, this.path)
|
|
|
|
this.subject._removeElementFromMongoArray = sinon.stub().yields()
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.deleteEntity(projectId, docId, 'doc', this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the project', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectGetter.getProjectWithoutLock
|
|
|
|
.calledWith(projectId, {
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: true,
|
|
|
|
name: true,
|
|
|
|
overleaf: true
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should find the element', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectLocator.findElement
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith({
|
|
|
|
project: this.project,
|
|
|
|
element_id: this.doc._id,
|
|
|
|
type: 'doc'
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove the element from the database', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._removeElementFromMongoArray
|
|
|
|
.calledWith(this.ProjectModel, projectId, this.path.mongo, this.doc._id)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('calls the callbck', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.callback
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(null, this.doc, this.path, this.project)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('renameEntity', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.newName = 'new.tex'
|
|
|
|
this.path = { mongo: 'mongo.path', fileSystem: '/old.tex' }
|
|
|
|
|
|
|
|
this.project = {
|
2019-11-04 04:50:15 -05:00
|
|
|
_id: ObjectId(projectId),
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: [{ _id: ObjectId() }]
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.doc = { _id: docId, name: 'old.tex', rev: 1 }
|
|
|
|
this.folder = { _id: folderId }
|
2019-05-29 05:21:06 -04:00
|
|
|
this.newProject = 'new-project'
|
|
|
|
|
|
|
|
this.ProjectGetter.getProjectWithoutLock = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.project)
|
|
|
|
|
|
|
|
this.ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
|
|
|
|
this.ProjectEntityHandler.getAllEntitiesFromProject
|
|
|
|
.onFirstCall()
|
|
|
|
.yields(
|
|
|
|
null,
|
|
|
|
(this.oldDocs = ['old-doc']),
|
|
|
|
(this.oldFiles = ['old-file'])
|
|
|
|
)
|
|
|
|
this.ProjectEntityHandler.getAllEntitiesFromProject
|
|
|
|
.onSecondCall()
|
|
|
|
.yields(
|
|
|
|
null,
|
|
|
|
(this.newDocs = ['new-doc']),
|
|
|
|
(this.newFiles = ['new-file'])
|
|
|
|
)
|
|
|
|
|
|
|
|
this.ProjectLocator.findElement = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.doc, this.path, this.folder)
|
|
|
|
this.subject._checkValidElementName = sinon.stub().yields()
|
|
|
|
this.ProjectModel.findOneAndUpdate = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.newProject)
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.renameEntity(
|
|
|
|
projectId,
|
|
|
|
docId,
|
2019-05-29 05:21:06 -04:00
|
|
|
'doc',
|
|
|
|
this.newName,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the project', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectGetter.getProjectWithoutLock
|
|
|
|
.calledWith(projectId, {
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: true,
|
|
|
|
name: true,
|
|
|
|
overleaf: true
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should find the doc', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectLocator.findElement
|
|
|
|
.calledWith({ element_id: docId, type: 'doc', project: this.project })
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should check the new name is valid', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._checkValidElementName
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.folder, this.newName)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should update the doc name', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectModel.findOneAndUpdate
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
2019-11-04 04:50:15 -05:00
|
|
|
{ _id: projectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
{ $set: { 'mongo.path.name': this.newName }, $inc: { version: 1 } },
|
|
|
|
{ new: true }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('calls the callback', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
const changes = {
|
|
|
|
oldDocs: this.oldDocs,
|
|
|
|
newDocs: this.newDocs,
|
|
|
|
oldFiles: this.oldFiles,
|
|
|
|
newFiles: this.newFiles,
|
|
|
|
newProject: this.newProject
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.callback
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
|
|
|
null,
|
|
|
|
this.project,
|
|
|
|
'/old.tex',
|
|
|
|
'/new.tex',
|
|
|
|
this.doc.rev,
|
|
|
|
changes
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('addFolder', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.folderName = 'folder1234'
|
|
|
|
this.ProjectGetter.getProjectWithOnlyFolders = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.project)
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._confirmFolder = sinon.stub().yields(folderId)
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subject._putElement = sinon.stub().yields()
|
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject.addFolder(
|
|
|
|
projectId,
|
|
|
|
folderId,
|
2019-05-29 05:21:06 -04:00
|
|
|
this.folderName,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('gets the project', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectGetter.getProjectWithoutLock
|
|
|
|
.calledWith(projectId, {
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: true,
|
|
|
|
name: true,
|
|
|
|
overleaf: true
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('checks the parent folder exists', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._confirmFolder
|
|
|
|
.calledWith(this.project, folderId)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('puts the element in mongo', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
const folderMatcher = sinon.match(
|
|
|
|
folder => folder.name === this.folderName
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement
|
|
|
|
.calledWithMatch(this.project, folderId, folderMatcher, 'folder')
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('calls the callback', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
const folderMatcher = sinon.match(
|
|
|
|
folder => folder.name === this.folderName
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-04 04:50:15 -05:00
|
|
|
this.callback
|
|
|
|
.calledWithMatch(null, folderMatcher, folderId)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('_removeElementFromMongoArray ', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.mongoPath = 'folders[0].folders[5]'
|
|
|
|
this.id = '12344'
|
|
|
|
this.entityId = '5678'
|
|
|
|
this.ProjectModel.update = sinon.stub().yields()
|
|
|
|
this.ProjectModel.findOneAndUpdate = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.project)
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._removeElementFromMongoArray(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectModel,
|
|
|
|
this.id,
|
|
|
|
this.mongoPath,
|
|
|
|
this.entityId,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should pull', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectModel.findOneAndUpdate
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
|
|
|
{ _id: this.id },
|
|
|
|
{
|
|
|
|
$pull: { 'folders[0]': { _id: this.entityId } },
|
|
|
|
$inc: { version: 1 }
|
|
|
|
},
|
|
|
|
{ new: true }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should call the callback', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.callback.calledWith(null, this.project).should.equal(true)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('_countElements', function() {
|
|
|
|
beforeEach(function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.project = {
|
|
|
|
_id: projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: [
|
|
|
|
{
|
|
|
|
docs: [{ _id: 123 }, { _id: 345 }],
|
|
|
|
fileRefs: [{ _id: 123 }, { _id: 345 }, { _id: 456 }],
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
docs: [{ _id: 123 }, { _id: 345 }, { _id: 456 }],
|
|
|
|
fileRefs: {},
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
docs: [{ _id: 1234 }],
|
|
|
|
fileRefs: [{ _id: 23123 }, { _id: 123213 }, { _id: 2312 }],
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
docs: [{ _id: 321321 }, { _id: 123213 }],
|
|
|
|
fileRefs: [{ _id: 312321 }],
|
|
|
|
folders: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
docs: [{ _id: 123 }, { _id: 32131 }],
|
|
|
|
fileRefs: [],
|
|
|
|
folders: [
|
|
|
|
{
|
|
|
|
docs: [{ _id: 3123 }],
|
|
|
|
fileRefs: [
|
|
|
|
{ _id: 321321 },
|
|
|
|
{ _id: 321321 },
|
|
|
|
{ _id: 313122 }
|
|
|
|
],
|
|
|
|
folders: 0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2019-11-04 04:50:15 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the correct number', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
expect(this.subject._countElements(this.project)).to.equal(26)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should deal with null folders', function() {
|
|
|
|
this.project.rootFolder[0].folders[0].folders = undefined
|
2019-11-04 04:50:15 -05:00
|
|
|
expect(this.subject._countElements(this.project)).to.equal(17)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should deal with null docs', function() {
|
|
|
|
this.project.rootFolder[0].folders[0].docs = undefined
|
2019-11-04 04:50:15 -05:00
|
|
|
expect(this.subject._countElements(this.project)).to.equal(23)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should deal with null fileRefs', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
|
2019-11-04 04:50:15 -05:00
|
|
|
expect(this.subject._countElements(this.project)).to.equal(23)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('_putElement', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.project = {
|
2019-11-04 04:50:15 -05:00
|
|
|
_id: projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolder: [{ _id: ObjectId() }]
|
|
|
|
}
|
|
|
|
this.folder = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
name: 'someFolder',
|
|
|
|
docs: [{ name: 'another-doc.tex' }],
|
|
|
|
fileRefs: [{ name: 'another-file.tex' }],
|
|
|
|
folders: [{ name: 'another-folder' }]
|
|
|
|
}
|
|
|
|
this.doc = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
name: 'new.tex'
|
|
|
|
}
|
|
|
|
this.path = { mongo: 'mongo.path', fileSystem: '/file/system/old.tex' }
|
|
|
|
this.ProjectLocator.findElement = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.folder, this.path)
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectModel.findOneAndUpdate = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2019-11-04 04:50:15 -05:00
|
|
|
.yields(null, this.project)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('updating the project', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
it('should use the correct mongo path', function(done) {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
this.folder._id,
|
|
|
|
this.doc,
|
|
|
|
'docs',
|
|
|
|
err => {
|
2019-11-04 04:50:15 -05:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal(
|
|
|
|
this.project._id
|
|
|
|
)
|
|
|
|
assert.deepEqual(
|
|
|
|
this.ProjectModel.findOneAndUpdate.args[0][1].$push[
|
|
|
|
this.path.mongo + '.docs'
|
|
|
|
],
|
|
|
|
this.doc
|
|
|
|
)
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the project in the callback', function(done) {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
this.folder._id,
|
|
|
|
this.doc,
|
|
|
|
'docs',
|
|
|
|
(err, path, project) => {
|
2019-11-04 04:50:15 -05:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
assert.equal(project, this.project)
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add an s onto the type if not included', function(done) {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
this.folder._id,
|
|
|
|
this.doc,
|
|
|
|
'doc',
|
|
|
|
err => {
|
2019-11-04 04:50:15 -05:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
assert.deepEqual(
|
|
|
|
this.ProjectModel.findOneAndUpdate.args[0][1].$push[
|
|
|
|
this.path.mongo + '.docs'
|
|
|
|
],
|
|
|
|
this.doc
|
|
|
|
)
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not call update if element is null', function(done) {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
this.folder._id,
|
|
|
|
null,
|
|
|
|
'doc',
|
|
|
|
err => {
|
2019-11-04 04:50:15 -05:00
|
|
|
expect(err).to.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectModel.findOneAndUpdate.called.should.equal(false)
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should default to root folder insert', function(done) {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(this.project, null, this.doc, 'doc', err => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectLocator.findElement.args[0][0].element_id.should.equal(
|
|
|
|
this.project.rootFolder[0]._id
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should error if the element has no _id', function(done) {
|
|
|
|
const doc = { name: 'something' }
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
this.folder._id,
|
|
|
|
doc,
|
|
|
|
'doc',
|
|
|
|
err => {
|
2019-11-04 04:50:15 -05:00
|
|
|
expect(err).to.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectModel.findOneAndUpdate.called.should.equal(false)
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should error if element name contains invalid characters', function(done) {
|
|
|
|
const doc = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
name: 'something*bad'
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
this.folder._id,
|
|
|
|
doc,
|
|
|
|
'doc',
|
|
|
|
err => {
|
|
|
|
this.ProjectModel.findOneAndUpdate.called.should.equal(false)
|
2019-11-18 09:37:05 -05:00
|
|
|
expect(err).to.be.instanceOf(Errors.InvalidNameError)
|
|
|
|
expect(err).to.have.property('message', 'invalid element name')
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should error if element name is too long', function(done) {
|
|
|
|
const doc = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
name: new Array(200).join('long-') + 'something'
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
this.folder._id,
|
|
|
|
doc,
|
|
|
|
'doc',
|
|
|
|
err => {
|
|
|
|
this.ProjectModel.findOneAndUpdate.called.should.equal(false)
|
2019-11-18 09:37:05 -05:00
|
|
|
expect(err).to.be.instanceOf(Errors.InvalidNameError)
|
|
|
|
expect(err).to.have.property('message', 'path too long')
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should error if the folder name is too long', function(done) {
|
|
|
|
this.path = {
|
|
|
|
mongo: 'mongo.path',
|
|
|
|
fileSystem: new Array(200).join('subdir/') + 'foo'
|
|
|
|
}
|
|
|
|
this.ProjectLocator.findElement.callsArgWith(
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
this.folder,
|
|
|
|
this.path
|
|
|
|
)
|
|
|
|
const doc = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
name: 'something'
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
this.folder._id,
|
|
|
|
doc,
|
|
|
|
'doc',
|
|
|
|
err => {
|
|
|
|
this.ProjectModel.findOneAndUpdate.called.should.equal(false)
|
2019-11-18 09:37:05 -05:00
|
|
|
expect(err).to.be.instanceOf(Errors.InvalidNameError)
|
|
|
|
expect(err).to.have.property('message', 'path too long')
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should error if a document already exists with the same name', function(done) {
|
|
|
|
const doc = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
name: 'another-doc.tex'
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(this.project, this.folder, doc, 'doc', err => {
|
|
|
|
this.ProjectModel.findOneAndUpdate.called.should.equal(false)
|
2019-11-18 09:37:05 -05:00
|
|
|
expect(err).to.be.instanceOf(Errors.InvalidNameError)
|
|
|
|
expect(err).to.have.property('message', 'file already exists')
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should error if a file already exists with the same name', function(done) {
|
|
|
|
const doc = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
name: 'another-file.tex'
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(this.project, this.folder, doc, 'doc', err => {
|
|
|
|
this.ProjectModel.findOneAndUpdate.called.should.equal(false)
|
2019-11-18 09:37:05 -05:00
|
|
|
expect(err).to.be.instanceOf(Errors.InvalidNameError)
|
|
|
|
expect(err).to.have.property('message', 'file already exists')
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should error if a folder already exists with the same name', function(done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const doc = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
name: 'another-folder'
|
|
|
|
}
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._putElement(this.project, this.folder, doc, 'doc', err => {
|
|
|
|
this.ProjectModel.findOneAndUpdate.called.should.equal(false)
|
2019-11-18 09:37:05 -05:00
|
|
|
expect(err).to.be.instanceOf(Errors.InvalidNameError)
|
|
|
|
expect(err).to.have.property('message', 'file already exists')
|
2019-11-04 04:50:15 -05:00
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('_checkValidElementName', function() {
|
|
|
|
beforeEach(function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.folder = {
|
2019-05-29 05:21:06 -04:00
|
|
|
docs: [{ name: 'doc_name' }],
|
|
|
|
fileRefs: [{ name: 'file_name' }],
|
|
|
|
folders: [{ name: 'folder_name' }]
|
2019-11-04 04:50:15 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('returns an error if name matches any doc name', function() {
|
2019-11-18 09:37:05 -05:00
|
|
|
this.subject._checkValidElementName(this.folder, 'doc_name', err => {
|
|
|
|
expect(err).to.be.instanceOf(Errors.InvalidNameError)
|
|
|
|
expect(err).to.have.property('message', 'file already exists')
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('returns an error if name matches any file name', function() {
|
2019-11-18 09:37:05 -05:00
|
|
|
this.subject._checkValidElementName(this.folder, 'file_name', err => {
|
|
|
|
expect(err).to.be.instanceOf(Errors.InvalidNameError)
|
|
|
|
expect(err).to.have.property('message', 'file already exists')
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('returns an error if name matches any folder name', function() {
|
2019-11-18 09:37:05 -05:00
|
|
|
this.subject._checkValidElementName(this.folder, 'folder_name', err => {
|
|
|
|
expect(err).to.be.instanceOf(Errors.InvalidNameError)
|
|
|
|
expect(err).to.have.property('message', 'file already exists')
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('returns nothing if name is valid', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._checkValidElementName(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.folder,
|
|
|
|
'unique_name',
|
|
|
|
err => expect(err).to.be.undefined
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('_checkValidMove', function() {
|
|
|
|
beforeEach(function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.destFolder = { _id: folderId }
|
2019-05-29 05:21:06 -04:00
|
|
|
this.destFolderPath = { fileSystem: '/foo/bar' }
|
|
|
|
this.ProjectLocator.findElement = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.destFolder, this.destFolderPath)
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._checkValidElementName = sinon.stub().yields()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('checks the element name is valid', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.doc = { _id: docId, name: 'doc_name' }
|
|
|
|
this.subject._checkValidMove(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
'doc',
|
|
|
|
this.doc,
|
|
|
|
{ fileSystem: '/main.tex' },
|
|
|
|
this.destFolder._id,
|
|
|
|
err => {
|
|
|
|
expect(err).to.be.undefined
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._checkValidElementName
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.destFolder, this.doc.name)
|
|
|
|
.should.equal(true)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('returns an error if trying to move a folder inside itself', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
const folder = { name: 'folder_name' }
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._checkValidMove(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
'folder',
|
|
|
|
folder,
|
|
|
|
{ fileSystem: '/foo' },
|
|
|
|
this.destFolder._id,
|
|
|
|
err => {
|
2019-11-18 09:37:05 -05:00
|
|
|
expect(err).to.be.instanceOf(Errors.InvalidNameError)
|
|
|
|
expect(err).to.have.property(
|
|
|
|
'message',
|
|
|
|
'destination folder is a child folder of me'
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('_insertDeletedDocReference', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
this.doc = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
name: 'test.tex'
|
|
|
|
}
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.ProjectModel.update = sinon.stub().yields()
|
2019-11-04 04:50:15 -05:00
|
|
|
this.subject._insertDeletedDocReference(
|
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
this.doc,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should insert the doc into deletedDocs', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.ProjectModel.update
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
|
|
|
{
|
2019-11-04 04:50:15 -05:00
|
|
|
_id: projectId
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
$push: {
|
|
|
|
deletedDocs: {
|
|
|
|
_id: this.doc._id,
|
|
|
|
name: this.doc.name,
|
|
|
|
deletedAt: new Date()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should call the callback', function() {
|
2019-11-04 04:50:15 -05:00
|
|
|
this.callback.called.should.equal(true)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|