2020-05-01 10:00:34 -04:00
|
|
|
const { expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
const MODULE_PATH = '../../../../app/src/Features/Project/ProjectDuplicator.js'
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
describe('ProjectDuplicator', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.level2folder = {
|
|
|
|
name: 'level2folderName',
|
|
|
|
_id: 'level2folderId',
|
|
|
|
docs: [
|
|
|
|
(this.doc2 = { _id: 'doc2_id', name: 'level2folderDocName' }),
|
|
|
|
undefined
|
|
|
|
],
|
|
|
|
folders: [],
|
|
|
|
fileRefs: [{ name: 'file2', _id: 'file2' }]
|
|
|
|
}
|
|
|
|
this.level1folder = {
|
|
|
|
name: 'level1folder',
|
|
|
|
_id: 'level1folderId',
|
|
|
|
docs: [(this.doc1 = { _id: 'doc1_id', name: 'level1folderDocName' })],
|
|
|
|
folders: [this.level2folder],
|
|
|
|
fileRefs: [{ name: 'file1', _id: 'file1' }, null] // the null is intentional to test null docs/files
|
|
|
|
}
|
|
|
|
this.rootFolder = {
|
|
|
|
name: 'rootFolder',
|
|
|
|
_id: 'rootFolderId',
|
|
|
|
docs: [(this.doc0 = { _id: 'doc0_id', name: 'rootDocHere' })],
|
|
|
|
folders: [this.level1folder, {}],
|
|
|
|
fileRefs: [{ name: 'file0', _id: 'file0' }]
|
|
|
|
}
|
|
|
|
this.project = {
|
|
|
|
_id: (this.old_project_id = 'this_is_the_old_project_id'),
|
|
|
|
rootDoc_id: 'rootDoc_id',
|
|
|
|
rootFolder: [this.rootFolder],
|
|
|
|
compiler: 'this_is_a_Compiler'
|
|
|
|
}
|
|
|
|
|
|
|
|
this.docContents = [
|
|
|
|
{
|
|
|
|
_id: this.doc0._id,
|
|
|
|
lines: (this.doc0_lines = ['zero'])
|
|
|
|
},
|
|
|
|
{
|
|
|
|
_id: this.doc1._id,
|
|
|
|
lines: (this.doc1_lines = ['one'])
|
|
|
|
},
|
|
|
|
{
|
|
|
|
_id: this.doc2._id,
|
|
|
|
lines: (this.doc2_lines = ['two'])
|
|
|
|
}
|
|
|
|
]
|
|
|
|
this.DocstoreManager = {
|
2020-05-01 10:00:34 -04:00
|
|
|
promises: {
|
|
|
|
getAllDocs: sinon.stub().resolves(this.docContents)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.owner = { _id: 'this_is_the_owner' }
|
|
|
|
this.stubbedNewProject = {
|
|
|
|
_id: (this.new_project_id = 'new_project_id'),
|
|
|
|
readOnly_refs: [],
|
|
|
|
collaberator_refs: [],
|
|
|
|
rootFolder: [{ _id: 'new_root_folder_id' }]
|
|
|
|
}
|
|
|
|
this.foundRootDoc = { _id: 'rootDocId', name: 'rootDocHere' }
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
this.ProjectCreationHandler = {
|
|
|
|
promises: {
|
|
|
|
createBlankProject: sinon.stub().resolves(this.stubbedNewProject)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.newFolder = { _id: 'newFolderId' }
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
this.ProjectLocator = {
|
|
|
|
promises: {
|
|
|
|
findRootDoc: sinon
|
|
|
|
.stub()
|
|
|
|
.resolves({ element: this.foundRootDoc, path: {} })
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
this.ProjectOptionsHandler = {
|
|
|
|
promises: {
|
|
|
|
setCompiler: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectEntityUpdateHandler = {
|
|
|
|
addDoc: sinon.stub().callsArgWith(5, null, { name: 'somDoc' }),
|
|
|
|
copyFileFromExistingProjectWithProject: sinon.stub(),
|
|
|
|
setRootDoc: sinon.stub(),
|
|
|
|
addFolder: sinon.stub().callsArgWith(3, null, this.newFolder)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject
|
|
|
|
.withArgs(
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.any,
|
|
|
|
'BROKEN-FILE',
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.any
|
|
|
|
)
|
|
|
|
.callsArgWith(6, new Error('failed'))
|
|
|
|
this.ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject
|
|
|
|
.withArgs(
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.object,
|
|
|
|
sinon.match.any
|
|
|
|
)
|
|
|
|
.callsArg(6)
|
|
|
|
this.ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject
|
|
|
|
.withArgs(
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.any,
|
|
|
|
sinon.match.any,
|
|
|
|
null,
|
|
|
|
sinon.match.any
|
|
|
|
)
|
|
|
|
.callsArg(6)
|
|
|
|
|
|
|
|
this.DocumentUpdaterHandler = {
|
2020-05-01 10:00:34 -04:00
|
|
|
promises: {
|
|
|
|
flushProjectToMongo: sinon.stub().resolves()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.Project = {
|
2020-05-01 10:00:34 -04:00
|
|
|
promises: {
|
|
|
|
findById: sinon.stub().resolves(this.project)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
this.ProjectGetter = {
|
|
|
|
getProject: sinon.stub(),
|
|
|
|
promises: {
|
|
|
|
getProject: sinon.stub()
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
this.ProjectGetter.getProject
|
|
|
|
.withArgs(this.old_project_id, sinon.match.any)
|
|
|
|
.callsArgWith(2, null, this.project)
|
|
|
|
this.ProjectGetter.getProject
|
|
|
|
.withArgs(this.new_project_id, sinon.match.any)
|
|
|
|
.callsArgWith(2, null, this.stubbedNewProject)
|
2020-05-01 10:00:34 -04:00
|
|
|
this.ProjectGetter.promises.getProject
|
|
|
|
.withArgs(this.old_project_id, sinon.match.any)
|
|
|
|
.resolves(this.project)
|
|
|
|
this.ProjectGetter.promises.getProject
|
|
|
|
.withArgs(this.new_project_id, sinon.match.any)
|
|
|
|
.resolves(this.stubbedNewProject)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
this.ProjectDeleter = {
|
|
|
|
promises: {
|
|
|
|
deleteProject: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
this.ProjectDuplicator = SandboxedModule.require(MODULE_PATH, {
|
2019-07-15 06:33:47 -04:00
|
|
|
globals: {
|
|
|
|
console: console
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
|
|
|
'../../models/Project': { Project: this.Project },
|
|
|
|
'../DocumentUpdater/DocumentUpdaterHandler': this
|
|
|
|
.DocumentUpdaterHandler,
|
2020-05-01 10:00:34 -04:00
|
|
|
'./ProjectCreationHandler': this.ProjectCreationHandler,
|
2019-05-29 05:21:06 -04:00
|
|
|
'./ProjectEntityUpdateHandler': this.ProjectEntityUpdateHandler,
|
2020-05-01 10:00:34 -04:00
|
|
|
'./ProjectLocator': this.ProjectLocator,
|
2019-05-29 05:21:06 -04:00
|
|
|
'./ProjectDeleter': this.ProjectDeleter,
|
2020-05-01 10:00:34 -04:00
|
|
|
'./ProjectOptionsHandler': this.ProjectOptionsHandler,
|
2019-05-29 05:21:06 -04:00
|
|
|
'../Docstore/DocstoreManager': this.DocstoreManager,
|
|
|
|
'./ProjectGetter': this.ProjectGetter,
|
|
|
|
'logger-sharelatex': {
|
|
|
|
log() {},
|
2019-07-01 09:48:09 -04:00
|
|
|
warn() {},
|
2019-05-29 05:21:06 -04:00
|
|
|
err() {}
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 10:00:34 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the copy succeeds', function() {
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should look up the original project', async function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
const newProjectName = 'someProj'
|
2020-05-01 10:00:34 -04:00
|
|
|
await this.ProjectDuplicator.promises.duplicate(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.owner,
|
|
|
|
this.old_project_id,
|
2020-05-01 10:00:34 -04:00
|
|
|
newProjectName
|
|
|
|
)
|
|
|
|
this.ProjectGetter.promises.getProject.should.have.been.calledWith(
|
|
|
|
this.old_project_id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should flush the original project to mongo', async function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
const newProjectName = 'someProj'
|
2020-05-01 10:00:34 -04:00
|
|
|
await this.ProjectDuplicator.promises.duplicate(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.owner,
|
|
|
|
this.old_project_id,
|
2020-05-01 10:00:34 -04:00
|
|
|
newProjectName
|
|
|
|
)
|
|
|
|
this.DocumentUpdaterHandler.promises.flushProjectToMongo.should.have.been.calledWith(
|
|
|
|
this.old_project_id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should create a blank project', async function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
const newProjectName = 'someProj'
|
2020-05-01 10:00:34 -04:00
|
|
|
const newProject = await this.ProjectDuplicator.promises.duplicate(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.owner,
|
|
|
|
this.old_project_id,
|
2020-05-01 10:00:34 -04:00
|
|
|
newProjectName
|
|
|
|
)
|
|
|
|
newProject._id.should.equal(this.stubbedNewProject._id)
|
|
|
|
this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
|
|
|
|
this.owner._id,
|
|
|
|
newProjectName
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should use the same compiler', async function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectEntityUpdateHandler.addDoc.callsArgWith(
|
|
|
|
5,
|
|
|
|
null,
|
|
|
|
this.rootFolder.docs[0],
|
|
|
|
this.owner._id
|
|
|
|
)
|
2020-05-01 10:00:34 -04:00
|
|
|
await this.ProjectDuplicator.promises.duplicate(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.owner,
|
|
|
|
this.old_project_id,
|
2020-05-01 10:00:34 -04:00
|
|
|
''
|
|
|
|
)
|
|
|
|
this.ProjectOptionsHandler.promises.setCompiler.should.have.been.calledWith(
|
|
|
|
this.stubbedNewProject._id,
|
|
|
|
this.project.compiler
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should use the same root doc', async function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectEntityUpdateHandler.addDoc.callsArgWith(
|
|
|
|
5,
|
|
|
|
null,
|
|
|
|
this.rootFolder.docs[0],
|
|
|
|
this.owner._id
|
|
|
|
)
|
2020-05-01 10:00:34 -04:00
|
|
|
await this.ProjectDuplicator.promises.duplicate(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.owner,
|
|
|
|
this.old_project_id,
|
2020-05-01 10:00:34 -04:00
|
|
|
''
|
|
|
|
)
|
|
|
|
this.ProjectEntityUpdateHandler.setRootDoc.should.have.been.calledWith(
|
|
|
|
this.stubbedNewProject._id,
|
|
|
|
this.rootFolder.docs[0]._id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should not copy the collaberators or read only refs', async function() {
|
|
|
|
const newProject = await this.ProjectDuplicator.promises.duplicate(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.owner,
|
|
|
|
this.old_project_id,
|
2020-05-01 10:00:34 -04:00
|
|
|
''
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2020-05-01 10:00:34 -04:00
|
|
|
newProject.collaberator_refs.length.should.equal(0)
|
|
|
|
newProject.readOnly_refs.length.should.equal(0)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should copy all the folders', async function() {
|
|
|
|
await this.ProjectDuplicator.promises.duplicate(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.owner,
|
|
|
|
this.old_project_id,
|
2020-05-01 10:00:34 -04:00
|
|
|
''
|
|
|
|
)
|
|
|
|
this.ProjectEntityUpdateHandler.addFolder.should.have.been.calledWith(
|
|
|
|
this.new_project_id,
|
|
|
|
this.stubbedNewProject.rootFolder[0]._id,
|
|
|
|
this.level1folder.name
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2020-05-01 10:00:34 -04:00
|
|
|
this.ProjectEntityUpdateHandler.addFolder.should.have.been.calledWith(
|
|
|
|
this.new_project_id,
|
|
|
|
this.newFolder._id,
|
|
|
|
this.level2folder.name
|
|
|
|
)
|
|
|
|
this.ProjectEntityUpdateHandler.addFolder.callCount.should.equal(2)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should copy all the docs', async function() {
|
|
|
|
await this.ProjectDuplicator.promises.duplicate(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.owner,
|
|
|
|
this.old_project_id,
|
2020-05-01 10:00:34 -04:00
|
|
|
''
|
|
|
|
)
|
|
|
|
this.DocstoreManager.promises.getAllDocs.should.have.been.calledWith(
|
|
|
|
this.old_project_id
|
|
|
|
)
|
|
|
|
this.ProjectEntityUpdateHandler.addDoc.should.have.been.calledWith(
|
|
|
|
this.new_project_id,
|
|
|
|
this.stubbedNewProject.rootFolder[0]._id,
|
|
|
|
this.doc0.name,
|
|
|
|
this.doc0_lines,
|
|
|
|
this.owner._id
|
|
|
|
)
|
|
|
|
this.ProjectEntityUpdateHandler.addDoc.should.have.been.calledWith(
|
|
|
|
this.new_project_id,
|
|
|
|
this.newFolder._id,
|
|
|
|
this.doc1.name,
|
|
|
|
this.doc1_lines,
|
|
|
|
this.owner._id
|
|
|
|
)
|
|
|
|
this.ProjectEntityUpdateHandler.addDoc.should.have.been.calledWith(
|
|
|
|
this.new_project_id,
|
|
|
|
this.newFolder._id,
|
|
|
|
this.doc2.name,
|
|
|
|
this.doc2_lines,
|
|
|
|
this.owner._id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should copy all the files', async function() {
|
|
|
|
await this.ProjectDuplicator.promises.duplicate(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.owner,
|
|
|
|
this.old_project_id,
|
2020-05-01 10:00:34 -04:00
|
|
|
''
|
|
|
|
)
|
|
|
|
this.ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject.should.have.been.calledWith(
|
|
|
|
this.stubbedNewProject._id,
|
|
|
|
this.stubbedNewProject,
|
|
|
|
this.stubbedNewProject.rootFolder[0]._id,
|
|
|
|
this.project._id,
|
|
|
|
this.rootFolder.fileRefs[0],
|
|
|
|
this.owner._id
|
|
|
|
)
|
|
|
|
this.ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject.should.have.been.calledWith(
|
|
|
|
this.stubbedNewProject._id,
|
|
|
|
this.stubbedNewProject,
|
|
|
|
this.newFolder._id,
|
|
|
|
this.project._id,
|
|
|
|
this.level1folder.fileRefs[0],
|
|
|
|
this.owner._id
|
|
|
|
)
|
|
|
|
this.ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject.should.have.been.calledWith(
|
|
|
|
this.stubbedNewProject._id,
|
|
|
|
this.stubbedNewProject,
|
|
|
|
this.newFolder._id,
|
|
|
|
this.project._id,
|
|
|
|
this.level2folder.fileRefs[0],
|
|
|
|
this.owner._id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('when there is an error', function() {
|
2020-05-01 10:00:34 -04:00
|
|
|
beforeEach(async function() {
|
|
|
|
this.rootFolder.fileRefs = [
|
2019-05-29 05:21:06 -04:00
|
|
|
{ name: 'file0', _id: 'file0' },
|
|
|
|
'BROKEN-FILE',
|
|
|
|
{ name: 'file1', _id: 'file1' },
|
|
|
|
{ name: 'file2', _id: 'file2' }
|
2020-05-01 10:00:34 -04:00
|
|
|
]
|
|
|
|
await expect(
|
|
|
|
this.ProjectDuplicator.promises.duplicate(
|
|
|
|
this.owner,
|
|
|
|
this.old_project_id,
|
|
|
|
''
|
|
|
|
)
|
|
|
|
).to.be.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should delete the broken cloned project', function() {
|
|
|
|
this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
|
|
|
|
this.stubbedNewProject._id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-01 10:00:34 -04:00
|
|
|
it('should not delete the original project', function() {
|
|
|
|
this.ProjectDeleter.promises.deleteProject.should.not.have.been.calledWith(
|
|
|
|
this.old_project_id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|