| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- const { expect } = require('chai')
- const sinon = require('sinon')
- const SandboxedModule = require('sandboxed-module')
- const { ObjectId } = require('mongodb-legacy')
- const MODULE_PATH = '../../../../app/src/Features/Project/ProjectDuplicator.js'
- describe('ProjectDuplicator', function () {
- beforeEach(function () {
- this.doc0 = { _id: 'doc0_id', name: 'rootDocHere' }
- this.doc1 = { _id: 'doc1_id', name: 'level1folderDocName' }
- this.doc2 = { _id: 'doc2_id', name: 'level2folderDocName' }
- this.doc0Lines = ['zero']
- this.doc1Lines = ['one']
- this.doc2Lines = ['two']
- this.file0 = { name: 'file0', _id: 'file0' }
- this.file1 = { name: 'file1', _id: 'file1' }
- this.file2 = {
- name: 'file2',
- _id: 'file2',
- created: '2024-07-05T14:18:31.401+00:00',
- linkedFileData: { provider: 'url' },
- hash: '123456',
- }
- this.level2folder = {
- name: 'level2folderName',
- _id: 'level2folderId',
- docs: [this.doc2, undefined],
- folders: [],
- fileRefs: [this.file2],
- }
- this.level1folder = {
- name: 'level1folder',
- _id: 'level1folderId',
- docs: [this.doc1],
- folders: [this.level2folder],
- fileRefs: [this.file1, null], // the null is intentional to test null docs/files
- }
- this.rootFolder = {
- name: 'rootFolder',
- _id: 'rootFolderId',
- docs: [this.doc0],
- folders: [this.level1folder, {}],
- fileRefs: [this.file0],
- }
- this.project = {
- _id: 'this_is_the_old_project_id',
- rootDoc_id: this.doc0._id,
- rootFolder: [this.rootFolder],
- compiler: 'this_is_a_Compiler',
- }
- this.doc0Path = '/rootDocHere'
- this.doc1Path = '/level1folder/level1folderDocName'
- this.doc2Path = '/level1folder/level2folderName/level2folderDocName'
- this.file0Path = '/file0'
- this.file1Path = '/level1folder/file1'
- this.file2Path = '/level1folder/level2folderName/file2'
- this.docContents = [
- { _id: this.doc0._id, lines: this.doc0Lines },
- { _id: this.doc1._id, lines: this.doc1Lines },
- { _id: this.doc2._id, lines: this.doc2Lines },
- ]
- this.rootDoc = this.doc0
- this.rootDocPath = '/rootDocHere'
- this.owner = { _id: 'this_is_the_owner' }
- this.newBlankProject = {
- _id: 'new_project_id',
- overleaf: { history: { id: 339123 } },
- readOnly_refs: [],
- collaberator_refs: [],
- rootFolder: [{ _id: 'new_root_folder_id' }],
- }
- this.newFolder = { _id: 'newFolderId' }
- this.filestoreUrl = 'filestore-url'
- this.newProjectVersion = 2
- this.newDocId = new ObjectId()
- this.newFileId = new ObjectId()
- this.newDoc0 = { ...this.doc0, _id: this.newDocId }
- this.newDoc1 = { ...this.doc1, _id: this.newDocId }
- this.newDoc2 = { ...this.doc2, _id: this.newDocId }
- this.newFile0 = { ...this.file0, _id: this.newFileId }
- this.newFile1 = { ...this.file1, _id: this.newFileId }
- this.newFile2 = { ...this.file2, _id: this.newFileId }
- this.docEntries = [
- {
- path: this.doc0Path,
- doc: this.newDoc0,
- docLines: this.doc0Lines.join('\n'),
- },
- {
- path: this.doc1Path,
- doc: this.newDoc1,
- docLines: this.doc1Lines.join('\n'),
- },
- {
- path: this.doc2Path,
- doc: this.newDoc2,
- docLines: this.doc2Lines.join('\n'),
- },
- ]
- this.fileEntries = [
- {
- path: this.file0Path,
- file: this.newFile0,
- url: this.filestoreUrl,
- },
- {
- path: this.file1Path,
- file: this.newFile1,
- url: this.filestoreUrl,
- },
- {
- path: this.file2Path,
- file: this.newFile2,
- url: this.filestoreUrl,
- },
- ]
- this.Doc = sinon
- .stub()
- .callsFake(props => ({ _id: this.newDocId, ...props }))
- this.File = sinon
- .stub()
- .callsFake(props => ({ _id: this.newFileId, ...props }))
- this.DocstoreManager = {
- promises: {
- updateDoc: sinon.stub().resolves(),
- getAllDocs: sinon.stub().resolves(this.docContents),
- },
- }
- this.DocumentUpdaterHandler = {
- promises: {
- flushProjectToMongo: sinon.stub().resolves(),
- updateProjectStructure: sinon.stub().resolves(),
- },
- }
- this.FileStoreHandler = {
- promises: {
- copyFile: sinon.stub().resolves(this.filestoreUrl),
- },
- }
- this.TagsHandler = {
- promises: {
- addProjectToTags: sinon.stub().resolves({
- _id: 'project-1',
- }),
- countTagsForProject: sinon.stub().resolves(1),
- },
- }
- this.ProjectCreationHandler = {
- promises: {
- createBlankProject: sinon.stub().resolves(this.newBlankProject),
- },
- }
- this.ProjectDeleter = {
- promises: {
- deleteProject: sinon.stub().resolves(),
- },
- }
- this.ProjectEntityMongoUpdateHandler = {
- promises: {
- createNewFolderStructure: sinon.stub().resolves(this.newProjectVersion),
- },
- }
- this.ProjectEntityUpdateHandler = {
- isPathValidForRootDoc: sinon.stub().returns(true),
- promises: {
- setRootDoc: sinon.stub().resolves(),
- },
- }
- this.ProjectGetter = {
- promises: {
- getProject: sinon
- .stub()
- .withArgs(this.project._id)
- .resolves(this.project),
- },
- }
- this.ProjectLocator = {
- promises: {
- findRootDoc: sinon.stub().resolves({
- element: this.rootDoc,
- path: { fileSystem: this.rootDocPath },
- }),
- findElementByPath: sinon
- .stub()
- .withArgs({
- project_id: this.newBlankProject._id,
- path: this.rootDocPath,
- exactCaseMatch: true,
- })
- .resolves({ element: this.doc0 }),
- },
- }
- this.ProjectOptionsHandler = {
- promises: {
- setCompiler: sinon.stub().resolves(),
- },
- }
- this.TpdsProjectFlusher = {
- promises: {
- flushProjectToTpds: sinon.stub().resolves(),
- },
- }
- this.ProjectDuplicator = SandboxedModule.require(MODULE_PATH, {
- requires: {
- '../../models/Doc': { Doc: this.Doc },
- '../../models/File': { File: this.File },
- '../Docstore/DocstoreManager': this.DocstoreManager,
- '../DocumentUpdater/DocumentUpdaterHandler':
- this.DocumentUpdaterHandler,
- '../FileStore/FileStoreHandler': this.FileStoreHandler,
- './ProjectCreationHandler': this.ProjectCreationHandler,
- './ProjectDeleter': this.ProjectDeleter,
- './ProjectEntityMongoUpdateHandler':
- this.ProjectEntityMongoUpdateHandler,
- './ProjectEntityUpdateHandler': this.ProjectEntityUpdateHandler,
- './ProjectGetter': this.ProjectGetter,
- './ProjectLocator': this.ProjectLocator,
- './ProjectOptionsHandler': this.ProjectOptionsHandler,
- '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
- '../Tags/TagsHandler': this.TagsHandler,
- },
- })
- })
- describe('when the copy succeeds', function () {
- beforeEach(async function () {
- this.newProjectName = 'New project name'
- this.newProject = await this.ProjectDuplicator.promises.duplicate(
- this.owner,
- this.project._id,
- this.newProjectName
- )
- })
- it('should flush the original project to mongo', function () {
- this.DocumentUpdaterHandler.promises.flushProjectToMongo.should.have.been.calledWith(
- this.project._id
- )
- })
- it('should copy docs to docstore', function () {
- for (const docLines of [this.doc0Lines, this.doc1Lines, this.doc2Lines]) {
- this.DocstoreManager.promises.updateDoc.should.have.been.calledWith(
- this.newProject._id.toString(),
- this.newDocId.toString(),
- docLines,
- 0,
- {}
- )
- }
- })
- it('should copy files to the filestore', function () {
- for (const file of [this.file0, this.file1, this.file2]) {
- this.FileStoreHandler.promises.copyFile.should.have.been.calledWith(
- this.project._id,
- file._id,
- this.newProject._id,
- this.newFileId
- )
- }
- })
- it('should create a blank project', function () {
- this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
- this.owner._id,
- this.newProjectName
- )
- this.newProject._id.should.equal(this.newBlankProject._id)
- })
- it('should use the same compiler', function () {
- this.ProjectOptionsHandler.promises.setCompiler.should.have.been.calledWith(
- this.newProject._id,
- this.project.compiler
- )
- })
- it('should use the same root doc', function () {
- this.ProjectEntityUpdateHandler.promises.setRootDoc.should.have.been.calledWith(
- this.newProject._id,
- this.rootFolder.docs[0]._id
- )
- })
- it('should not copy the collaborators or read only refs', function () {
- this.newProject.collaberator_refs.length.should.equal(0)
- this.newProject.readOnly_refs.length.should.equal(0)
- })
- it('should copy all documents and files', function () {
- this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
- this.newProject._id,
- this.docEntries,
- this.fileEntries
- )
- })
- it('should notify document updater of changes', function () {
- this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
- this.newProject._id,
- this.newProject.overleaf.history.id,
- this.owner._id,
- {
- newDocs: this.docEntries,
- newFiles: this.fileEntries,
- newProject: { version: this.newProjectVersion },
- },
- null
- )
- })
- it('should flush the project to TPDS', function () {
- this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
- this.newProject._id
- )
- })
- })
- describe('without a root doc', function () {
- beforeEach(async function () {
- this.ProjectLocator.promises.findRootDoc.resolves({
- element: null,
- path: null,
- })
- this.newProject = await this.ProjectDuplicator.promises.duplicate(
- this.owner,
- this.project._id,
- 'Copy of project'
- )
- })
- it('should not set the root doc on the copy', function () {
- this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
- .called
- })
- })
- describe('with an invalid root doc', function () {
- beforeEach(async function () {
- this.ProjectEntityUpdateHandler.isPathValidForRootDoc.returns(false)
- this.newProject = await this.ProjectDuplicator.promises.duplicate(
- this.owner,
- this.project._id,
- 'Copy of project'
- )
- })
- it('should not set the root doc on the copy', function () {
- this.ProjectEntityUpdateHandler.promises.setRootDoc.should.not.have.been
- .called
- })
- })
- describe('when there is an error', function () {
- beforeEach(async function () {
- this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
- await expect(
- this.ProjectDuplicator.promises.duplicate(
- this.owner,
- this.project._id,
- ''
- )
- ).to.be.rejected
- })
- it('should delete the broken cloned project', function () {
- this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
- this.newBlankProject._id
- )
- })
- it('should not delete the original project', function () {
- this.ProjectDeleter.promises.deleteProject.should.not.have.been.calledWith(
- this.project._id
- )
- })
- })
- })
|