2014-02-12 05:23:40 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai').should()
|
|
|
|
modulePath = "../../../../app/js/Features/Project/ProjectDuplicator.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2014-05-06 07:36:00 -04:00
|
|
|
describe 'ProjectDuplicator', ->
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@level2folder =
|
|
|
|
name: "level2folderName"
|
|
|
|
_id:"level2folderId"
|
2017-03-17 09:21:30 -04:00
|
|
|
docs:[@doc2 = {_id: "doc2_id", name:"level2folderDocName"}, undefined]
|
2014-02-12 05:23:40 -05:00
|
|
|
folders:[]
|
|
|
|
fileRefs:[{name:"file2", _id:"file2"}]
|
|
|
|
@level1folder =
|
|
|
|
name:"level1folder"
|
|
|
|
_id:"level1folderId"
|
2014-05-06 07:36:00 -04:00
|
|
|
docs:[@doc1 = {_id: "doc1_id", name:"level1folderDocName"}]
|
2014-02-12 05:23:40 -05:00
|
|
|
folders:[@level2folder]
|
2017-03-17 09:21:30 -04:00
|
|
|
fileRefs:[{name:"file1", _id:"file1"}, null]
|
2014-02-12 05:23:40 -05:00
|
|
|
@rootFolder =
|
|
|
|
name:"rootFolder"
|
|
|
|
_id:"rootFolderId"
|
2014-05-06 07:36:00 -04:00
|
|
|
docs:[@doc0 = {_id: "doc0_id", name:"rootDocHere"}]
|
2017-03-17 09:21:30 -04:00
|
|
|
folders:[@level1folder, {}]
|
2014-02-12 05:23:40 -05:00
|
|
|
fileRefs:[{name:"file0", _id:"file0"}]
|
|
|
|
@project =
|
2016-03-10 07:42:21 -05:00
|
|
|
_id: @old_project_id = "this_is_the_old_project_id"
|
2014-02-12 05:23:40 -05:00
|
|
|
rootDoc_id: "rootDoc_id"
|
|
|
|
rootFolder:[@rootFolder]
|
|
|
|
compiler: "this_is_a_Compiler"
|
|
|
|
|
2014-05-06 07:36:00 -04:00
|
|
|
@docContents = [{
|
|
|
|
_id: @doc0._id
|
|
|
|
lines: @doc0_lines = ["zero"]
|
|
|
|
}, {
|
|
|
|
_id: @doc1._id
|
|
|
|
lines: @doc1_lines = ["one"]
|
|
|
|
}, {
|
|
|
|
_id: @doc2._id
|
|
|
|
lines: @doc2_lines = ["two"]
|
|
|
|
}]
|
|
|
|
@DocstoreManager =
|
|
|
|
getAllDocs: sinon.stub().callsArgWith(1, null, @docContents)
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
@owner = {_id:"this_is_the_owner"}
|
|
|
|
@stubbedNewProject =
|
2016-03-10 07:42:21 -05:00
|
|
|
_id: @new_project_id = "new_project_id"
|
2014-02-12 05:23:40 -05:00
|
|
|
readOnly_refs:[]
|
|
|
|
collaberator_refs:[]
|
|
|
|
rootFolder:[
|
|
|
|
{_id:"new_root_folder_id"}
|
|
|
|
]
|
|
|
|
@foundRootDoc = {_id:"rootDocId", name:"rootDocHere"}
|
|
|
|
|
|
|
|
@creationHandler =
|
|
|
|
createBlankProject : sinon.stub().callsArgWith(2, null, @stubbedNewProject)
|
|
|
|
|
|
|
|
@newFolder = {_id: "newFolderId"}
|
|
|
|
|
|
|
|
@locator =
|
2016-03-10 07:42:21 -05:00
|
|
|
findRootDoc : sinon.stub().callsArgWith(1, null, @foundRootDoc, {})
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
@projectOptionsHandler =
|
|
|
|
setCompiler : sinon.stub()
|
|
|
|
@entityHandler =
|
2017-11-23 10:18:43 -05:00
|
|
|
addDocWithProject: sinon.stub().callsArgWith(5, null, {name:"somDoc"})
|
2016-03-09 06:16:27 -05:00
|
|
|
copyFileFromExistingProjectWithProject: sinon.stub().callsArgWith(4)
|
2014-02-12 05:23:40 -05:00
|
|
|
setRootDoc: sinon.stub()
|
2016-03-09 06:16:27 -05:00
|
|
|
addFolderWithProject: sinon.stub().callsArgWith(3, null, @newFolder)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
@DocumentUpdaterHandler =
|
|
|
|
flushProjectToMongo: sinon.stub().callsArg(1)
|
|
|
|
|
|
|
|
@Project =
|
|
|
|
findById: sinon.stub().callsArgWith(1, null, @project)
|
|
|
|
|
2016-02-26 03:32:00 -05:00
|
|
|
@ProjectGetter =
|
2016-03-10 07:42:21 -05:00
|
|
|
getProject: sinon.stub()
|
|
|
|
|
|
|
|
@ProjectGetter.getProject.withArgs(@old_project_id, sinon.match.any).callsArgWith(2, null, @project)
|
|
|
|
@ProjectGetter.getProject.withArgs(@new_project_id, sinon.match.any).callsArgWith(2, null, @stubbedNewProject)
|
2016-02-26 03:32:00 -05:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
@duplicator = SandboxedModule.require modulePath, requires:
|
|
|
|
'../../models/Project':{Project:@Project}
|
|
|
|
"../DocumentUpdater/DocumentUpdaterHandler": @DocumentUpdaterHandler
|
|
|
|
'./ProjectCreationHandler': @creationHandler
|
|
|
|
'./ProjectEntityHandler': @entityHandler
|
|
|
|
'./ProjectLocator': @locator
|
|
|
|
'./ProjectOptionsHandler': @projectOptionsHandler
|
2014-05-06 07:36:00 -04:00
|
|
|
"../Docstore/DocstoreManager": @DocstoreManager
|
2016-02-26 03:32:00 -05:00
|
|
|
"./ProjectGetter":@ProjectGetter
|
2014-02-12 05:23:40 -05:00
|
|
|
'logger-sharelatex':{log:->}
|
|
|
|
|
|
|
|
it "should look up the original project", (done) ->
|
|
|
|
newProjectName = "someProj"
|
2016-03-10 07:42:21 -05:00
|
|
|
@duplicator.duplicate @owner, @old_project_id, newProjectName, (err, newProject)=>
|
|
|
|
@ProjectGetter.getProject.calledWith(@old_project_id).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should flush the original project to mongo", (done) ->
|
|
|
|
newProjectName = "someProj"
|
2016-03-10 07:42:21 -05:00
|
|
|
@duplicator.duplicate @owner, @old_project_id, newProjectName, (err, newProject)=>
|
|
|
|
@DocumentUpdaterHandler.flushProjectToMongo.calledWith(@old_project_id).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should create a blank project', (done)->
|
|
|
|
newProjectName = "someProj"
|
2016-03-10 07:42:21 -05:00
|
|
|
@duplicator.duplicate @owner, @old_project_id, newProjectName, (err, newProject)=>
|
2014-02-12 05:23:40 -05:00
|
|
|
newProject._id.should.equal @stubbedNewProject._id
|
|
|
|
@creationHandler.createBlankProject.calledWith(@owner._id, newProjectName).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should use the same compiler', (done)->
|
2017-11-23 10:23:29 -05:00
|
|
|
@entityHandler.addDocWithProject.callsArgWith(5, null, @rootFolder.docs[0], @owner._id)
|
2016-03-10 07:42:21 -05:00
|
|
|
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
|
2014-02-12 05:23:40 -05:00
|
|
|
@projectOptionsHandler.setCompiler.calledWith(@stubbedNewProject._id, @project.compiler).should.equal true
|
|
|
|
done()
|
|
|
|
|
2017-03-17 09:21:30 -04:00
|
|
|
it 'should use the same root doc', (done)->
|
2017-11-23 10:23:29 -05:00
|
|
|
@entityHandler.addDocWithProject.callsArgWith(5, null, @rootFolder.docs[0], @owner._id)
|
2016-03-10 07:42:21 -05:00
|
|
|
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
|
|
|
|
@entityHandler.setRootDoc.calledWith(@stubbedNewProject._id, @rootFolder.docs[0]._id).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
2014-05-06 07:36:00 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
it 'should not copy the collaberators or read only refs', (done)->
|
2016-03-10 07:42:21 -05:00
|
|
|
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
|
2014-02-12 05:23:40 -05:00
|
|
|
newProject.collaberator_refs.length.should.equal 0
|
|
|
|
newProject.readOnly_refs.length.should.equal 0
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should copy all the folders', (done)->
|
2016-03-10 07:42:21 -05:00
|
|
|
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
|
2016-03-09 06:16:27 -05:00
|
|
|
@entityHandler.addFolderWithProject.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @level1folder.name).should.equal true
|
|
|
|
@entityHandler.addFolderWithProject.calledWith(@stubbedNewProject, @newFolder._id, @level2folder.name).should.equal true
|
|
|
|
@entityHandler.addFolderWithProject.callCount.should.equal 2
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should copy all the docs', (done)->
|
2016-03-10 07:42:21 -05:00
|
|
|
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
|
|
|
|
@DocstoreManager.getAllDocs.calledWith(@old_project_id).should.equal true
|
2017-11-23 10:23:29 -05:00
|
|
|
@entityHandler.addDocWithProject
|
|
|
|
.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @doc0.name, @doc0_lines, @owner._id)
|
|
|
|
.should.equal true
|
|
|
|
@entityHandler.addDocWithProject
|
|
|
|
.calledWith(@stubbedNewProject, @newFolder._id, @doc1.name, @doc1_lines, @owner._id)
|
|
|
|
.should.equal true
|
|
|
|
@entityHandler.addDocWithProject
|
|
|
|
.calledWith(@stubbedNewProject, @newFolder._id, @doc2.name, @doc2_lines, @owner._id)
|
|
|
|
.should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should copy all the files', (done)->
|
2016-03-10 07:42:21 -05:00
|
|
|
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
|
2016-03-09 06:16:27 -05:00
|
|
|
@entityHandler.copyFileFromExistingProjectWithProject.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @project._id, @rootFolder.fileRefs[0]).should.equal true
|
|
|
|
@entityHandler.copyFileFromExistingProjectWithProject.calledWith(@stubbedNewProject, @newFolder._id, @project._id, @level1folder.fileRefs[0]).should.equal true
|
|
|
|
@entityHandler.copyFileFromExistingProjectWithProject.calledWith(@stubbedNewProject, @newFolder._id, @project._id, @level2folder.fileRefs[0]).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|