2019-05-29 05:21:06 -04:00
|
|
|
const sinon = require('sinon')
|
2020-04-16 08:01:40 -04:00
|
|
|
const { expect } = require('chai')
|
|
|
|
const timekeeper = require('timekeeper')
|
2019-05-29 05:21:06 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-04-23 07:51:06 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-04-16 08:01:40 -04:00
|
|
|
const MODULE_PATH =
|
|
|
|
'../../../../app/src/Features/Uploads/ProjectUploadManager.js'
|
2019-06-24 09:18:14 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
describe('ProjectUploadManager', function() {
|
|
|
|
beforeEach(function() {
|
2020-04-16 08:01:40 -04:00
|
|
|
this.now = Date.now()
|
|
|
|
timekeeper.freeze(this.now)
|
2020-04-23 07:51:06 -04:00
|
|
|
this.rootFolderId = new ObjectId()
|
|
|
|
this.ownerId = new ObjectId()
|
|
|
|
this.zipPath = '/path/to/zip/file-name.zip'
|
|
|
|
this.extractedZipPath = `/path/to/zip/file-name-${this.now}`
|
|
|
|
this.mainContent = 'Contents of main.tex'
|
|
|
|
this.projectName = 'My project*'
|
|
|
|
this.fixedProjectName = 'My project'
|
|
|
|
this.uniqueProjectName = 'My project (1)'
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project = {
|
2020-04-23 07:51:06 -04:00
|
|
|
_id: new ObjectId(),
|
|
|
|
rootFolder: [{ _id: this.rootFolderId }],
|
|
|
|
overleaf: { history: { id: 12345 } }
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-04-23 07:51:06 -04:00
|
|
|
this.doc = {
|
|
|
|
_id: new ObjectId(),
|
|
|
|
name: 'main.tex'
|
|
|
|
}
|
|
|
|
this.docFsPath = '/path/to/doc'
|
|
|
|
this.docLines = ['My thesis', 'by A. U. Thor']
|
|
|
|
this.file = {
|
|
|
|
_id: new ObjectId(),
|
|
|
|
name: 'image.png'
|
|
|
|
}
|
|
|
|
this.fileFsPath = '/path/to/file'
|
|
|
|
|
2020-04-16 08:01:40 -04:00
|
|
|
this.topLevelDestination = '/path/to/zip/file-extracted/nested'
|
2020-04-23 07:51:06 -04:00
|
|
|
this.newProjectVersion = 123
|
|
|
|
this.importEntries = [
|
|
|
|
{
|
|
|
|
type: 'doc',
|
|
|
|
projectPath: '/main.tex',
|
|
|
|
lines: this.docLines
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'file',
|
|
|
|
projectPath: `/${this.file.name}`,
|
|
|
|
fsPath: this.fileFsPath
|
|
|
|
}
|
|
|
|
]
|
|
|
|
this.docEntries = [
|
|
|
|
{
|
|
|
|
doc: this.doc,
|
|
|
|
path: `/${this.doc.name}`,
|
|
|
|
docLines: this.docLines.join('\n')
|
|
|
|
}
|
|
|
|
]
|
|
|
|
this.fileEntries = [
|
|
|
|
{ file: this.file, path: `/${this.file.name}`, url: this.fileStoreUrl }
|
|
|
|
]
|
2020-04-16 08:01:40 -04:00
|
|
|
|
|
|
|
this.fs = {
|
|
|
|
remove: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
this.ArchiveManager = {
|
|
|
|
promises: {
|
|
|
|
extractZipArchive: sinon.stub().resolves(),
|
2020-04-23 07:51:06 -04:00
|
|
|
findTopLevelDirectory: sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(this.extractedZipPath)
|
|
|
|
.resolves(this.topLevelDestination)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.Doc = sinon.stub().returns(this.doc)
|
|
|
|
this.DocstoreManager = {
|
|
|
|
promises: {
|
|
|
|
updateDoc: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.DocumentHelper = {
|
|
|
|
getTitleFromTexContent: sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(this.mainContent)
|
|
|
|
.returns(this.projectName)
|
|
|
|
}
|
|
|
|
this.DocumentUpdaterHandler = {
|
|
|
|
promises: {
|
|
|
|
updateProjectStructure: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.FileStoreHandler = {
|
|
|
|
promises: {
|
|
|
|
uploadFileFromDisk: sinon
|
|
|
|
.stub()
|
|
|
|
.resolves({ fileRef: this.file, url: this.fileStoreUrl })
|
2020-04-16 08:01:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.FileSystemImportManager = {
|
|
|
|
promises: {
|
2020-04-23 07:51:06 -04:00
|
|
|
importDir: sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(this.topLevelDestination)
|
|
|
|
.resolves(this.importEntries)
|
2020-04-16 08:01:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.ProjectCreationHandler = {
|
|
|
|
promises: {
|
|
|
|
createBlankProject: sinon.stub().resolves(this.project)
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 07:51:06 -04:00
|
|
|
this.ProjectEntityMongoUpdateHandler = {
|
|
|
|
promises: {
|
|
|
|
createNewFolderStructure: sinon.stub().resolves(this.newProjectVersion)
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 08:01:40 -04:00
|
|
|
this.ProjectRootDocManager = {
|
|
|
|
promises: {
|
|
|
|
setRootDocAutomatically: sinon.stub().resolves(),
|
|
|
|
findRootDocFileFromDirectory: sinon
|
|
|
|
.stub()
|
2020-04-23 07:51:06 -04:00
|
|
|
.resolves({ path: 'main.tex', content: this.mainContent }),
|
2020-04-16 08:01:40 -04:00
|
|
|
setRootDocFromName: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.ProjectDetailsHandler = {
|
2020-04-23 07:51:06 -04:00
|
|
|
fixProjectName: sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(this.projectName)
|
|
|
|
.returns(this.fixedProjectName),
|
2020-04-16 08:01:40 -04:00
|
|
|
promises: {
|
2020-04-23 07:51:06 -04:00
|
|
|
generateUniqueName: sinon.stub().resolves(this.uniqueProjectName)
|
2020-04-16 08:01:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.ProjectDeleter = {
|
|
|
|
promises: {
|
|
|
|
deleteProject: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 07:51:06 -04:00
|
|
|
this.TpdsProjectFlusher = {
|
|
|
|
promises: {
|
|
|
|
flushProjectToTpds: sinon.stub().resolves()
|
|
|
|
}
|
2020-04-16 08:01:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.ProjectUploadManager = SandboxedModule.require(MODULE_PATH, {
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
2020-04-16 08:01:40 -04:00
|
|
|
'fs-extra': this.fs,
|
|
|
|
'./ArchiveManager': this.ArchiveManager,
|
2020-04-23 07:51:06 -04:00
|
|
|
'../../models/Doc': { Doc: this.Doc },
|
|
|
|
'../Docstore/DocstoreManager': this.DocstoreManager,
|
|
|
|
'../Documents/DocumentHelper': this.DocumentHelper,
|
|
|
|
'../DocumentUpdater/DocumentUpdaterHandler': this
|
|
|
|
.DocumentUpdaterHandler,
|
|
|
|
'../FileStore/FileStoreHandler': this.FileStoreHandler,
|
|
|
|
'./FileSystemImportManager': this.FileSystemImportManager,
|
2020-04-16 08:01:40 -04:00
|
|
|
'../Project/ProjectCreationHandler': this.ProjectCreationHandler,
|
2020-04-23 07:51:06 -04:00
|
|
|
'../Project/ProjectEntityMongoUpdateHandler': this
|
|
|
|
.ProjectEntityMongoUpdateHandler,
|
2020-04-16 08:01:40 -04:00
|
|
|
'../Project/ProjectRootDocManager': this.ProjectRootDocManager,
|
|
|
|
'../Project/ProjectDetailsHandler': this.ProjectDetailsHandler,
|
|
|
|
'../Project/ProjectDeleter': this.ProjectDeleter,
|
2020-04-23 07:51:06 -04:00
|
|
|
'../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
2020-04-16 08:01:40 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-04-16 08:01:40 -04:00
|
|
|
afterEach(function() {
|
|
|
|
timekeeper.reset()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('createProjectFromZipArchive', function() {
|
|
|
|
describe('when the title can be read from the root document', function() {
|
2020-04-16 08:01:40 -04:00
|
|
|
beforeEach(async function() {
|
|
|
|
await this.ProjectUploadManager.promises.createProjectFromZipArchive(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.ownerId,
|
|
|
|
this.projectName,
|
|
|
|
this.zipPath
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should extract the archive', function() {
|
2020-04-16 08:01:40 -04:00
|
|
|
this.ArchiveManager.promises.extractZipArchive.should.have.been.calledWith(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.zipPath,
|
|
|
|
this.extractedZipPath
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
it('should create a project', function() {
|
|
|
|
this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
|
|
|
|
this.ownerId,
|
|
|
|
this.uniqueProjectName
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
it('should initialize the file tree', function() {
|
|
|
|
this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
|
|
|
|
this.project._id,
|
|
|
|
this.docEntries,
|
|
|
|
this.fileEntries
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
it('should notify document updater', function() {
|
|
|
|
this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
|
|
|
|
this.project._id,
|
|
|
|
this.project.overleaf.history.id,
|
|
|
|
this.ownerId,
|
|
|
|
{
|
|
|
|
newDocs: this.docEntries,
|
|
|
|
newFiles: this.fileEntries,
|
|
|
|
newProject: { version: this.newProjectVersion }
|
|
|
|
}
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
it('should flush the project to TPDS', function() {
|
|
|
|
this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
|
|
|
|
this.project._id
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should set the root document', function() {
|
2020-04-16 08:01:40 -04:00
|
|
|
this.ProjectRootDocManager.promises.setRootDocFromName.should.have.been.calledWith(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.project._id,
|
2020-04-16 08:01:40 -04:00
|
|
|
'main.tex'
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
it('should remove the destination directory afterwards', function() {
|
|
|
|
this.fs.remove.should.have.been.calledWith(this.extractedZipPath)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe("when the root document can't be determined", function() {
|
2020-04-16 08:01:40 -04:00
|
|
|
beforeEach(async function() {
|
|
|
|
this.ProjectRootDocManager.promises.findRootDocFileFromDirectory.resolves(
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
await this.ProjectUploadManager.promises.createProjectFromZipArchive(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.ownerId,
|
|
|
|
this.projectName,
|
|
|
|
this.zipPath
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should not try to set the root doc', function() {
|
2020-04-16 08:01:40 -04:00
|
|
|
this.ProjectRootDocManager.promises.setRootDocFromName.should.not.have
|
|
|
|
.been.called
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('createProjectFromZipArchiveWithName', function() {
|
2020-04-16 08:01:40 -04:00
|
|
|
beforeEach(async function() {
|
|
|
|
await this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.ownerId,
|
|
|
|
this.projectName,
|
|
|
|
this.zipPath
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
it('should extract the archive', function() {
|
|
|
|
this.ArchiveManager.promises.extractZipArchive.should.have.been.calledWith(
|
|
|
|
this.zipPath,
|
|
|
|
this.extractedZipPath
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
it('should create a project owned by the owner_id', function() {
|
2020-04-16 08:01:40 -04:00
|
|
|
this.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.ownerId,
|
|
|
|
this.uniqueProjectName
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should automatically set the root doc', function() {
|
2020-04-16 08:01:40 -04:00
|
|
|
this.ProjectRootDocManager.promises.setRootDocAutomatically.should.have.been.calledWith(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.project._id
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2019-06-27 06:10:46 -04:00
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
it('should initialize the file tree', function() {
|
|
|
|
this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
|
|
|
|
this.project._id,
|
|
|
|
this.docEntries,
|
|
|
|
this.fileEntries
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
2019-06-27 06:10:46 -04:00
|
|
|
})
|
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
it('should notify document updater', function() {
|
|
|
|
this.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
|
|
|
|
this.project._id,
|
|
|
|
this.project.overleaf.history.id,
|
|
|
|
this.ownerId,
|
|
|
|
{
|
|
|
|
newDocs: this.docEntries,
|
|
|
|
newFiles: this.fileEntries,
|
|
|
|
newProject: { version: this.newProjectVersion }
|
|
|
|
}
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
2019-06-27 06:10:46 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
it('should flush the project to TPDS', function() {
|
|
|
|
this.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
|
|
|
|
this.project._id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-04-16 08:01:40 -04:00
|
|
|
it('should remove the destination directory afterwards', function() {
|
2020-04-23 07:51:06 -04:00
|
|
|
this.fs.remove.should.have.been.calledWith(this.extractedZipPath)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-04-23 07:51:06 -04:00
|
|
|
describe('when initializing the folder structure fails', function() {
|
2020-04-16 08:01:40 -04:00
|
|
|
beforeEach(async function() {
|
2020-04-23 07:51:06 -04:00
|
|
|
this.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
|
2020-04-16 08:01:40 -04:00
|
|
|
await expect(
|
|
|
|
this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.ownerId,
|
|
|
|
this.projectName,
|
|
|
|
this.zipPath
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
|
|
|
).to.be.rejected
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-04-16 08:01:40 -04:00
|
|
|
it('should cleanup the blank project created', async function() {
|
|
|
|
this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.project._id
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2020-04-16 08:01:40 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-04-16 08:01:40 -04:00
|
|
|
describe('when setting automatically the root doc fails', function() {
|
|
|
|
beforeEach(async function() {
|
|
|
|
this.ProjectRootDocManager.promises.setRootDocAutomatically.rejects()
|
|
|
|
await expect(
|
|
|
|
this.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.ownerId,
|
|
|
|
this.projectName,
|
|
|
|
this.zipPath
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
|
|
|
).to.be.rejected
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-04-16 08:01:40 -04:00
|
|
|
it('should cleanup the blank project created', function() {
|
|
|
|
this.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
|
2020-04-23 07:51:06 -04:00
|
|
|
this.project._id
|
2020-04-16 08:01:40 -04:00
|
|
|
)
|
|
|
|
})
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|