| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- import { vi, expect } from 'vitest'
- import sinon from 'sinon'
- import timekeeper from 'timekeeper'
- import mongodb from 'mongodb-legacy'
- const { ObjectId } = mongodb
- const MODULE_PATH =
- '../../../../app/src/Features/Uploads/ProjectUploadManager.mjs'
- describe('ProjectUploadManager', function () {
- beforeEach(async function (ctx) {
- ctx.now = Date.now()
- timekeeper.freeze(ctx.now)
- ctx.rootFolderId = new ObjectId()
- ctx.ownerId = new ObjectId()
- ctx.zipPath = '/path/to/zip/file-name.zip'
- ctx.extractedZipPath = `/path/to/zip/file-name-${ctx.now}`
- ctx.mainContent = 'Contents of main.tex'
- ctx.projectName = 'My project*'
- ctx.fixedProjectName = 'My project'
- ctx.uniqueProjectName = 'My project (1)'
- ctx.project = {
- _id: new ObjectId(),
- rootFolder: [{ _id: ctx.rootFolderId }],
- overleaf: { history: { id: 12345 } },
- }
- ctx.doc = {
- _id: new ObjectId(),
- name: 'main.tex',
- }
- ctx.docFsPath = '/path/to/doc'
- ctx.docLines = ['My thesis', 'by A. U. Thor']
- ctx.file = {
- _id: new ObjectId(),
- name: 'image.png',
- }
- ctx.fileFsPath = '/path/to/file'
- ctx.topLevelDestination = '/path/to/zip/file-extracted/nested'
- ctx.newProjectVersion = 123
- ctx.importEntries = [
- {
- type: 'doc',
- projectPath: '/main.tex',
- lines: ctx.docLines,
- },
- {
- type: 'file',
- projectPath: `/${ctx.file.name}`,
- fsPath: ctx.fileFsPath,
- },
- ]
- ctx.docEntries = [
- {
- doc: ctx.doc,
- path: `/${ctx.doc.name}`,
- docLines: ctx.docLines.join('\n'),
- },
- ]
- ctx.fileEntries = [
- {
- file: ctx.file,
- path: `/${ctx.file.name}`,
- createdBlob: true,
- },
- ]
- ctx.fs = {
- promises: {
- rm: sinon.stub().resolves(),
- },
- }
- ctx.ArchiveManager = {
- promises: {
- extractZipArchive: sinon.stub().resolves(),
- findTopLevelDirectory: sinon
- .stub()
- .withArgs(ctx.extractedZipPath)
- .resolves(ctx.topLevelDestination),
- },
- }
- ctx.Doc = sinon.stub().returns(ctx.doc)
- ctx.DocstoreManager = {
- promises: {
- updateDoc: sinon.stub().resolves(),
- },
- }
- ctx.DocumentHelper = {
- getTitleFromTexContent: sinon
- .stub()
- .withArgs(ctx.mainContent)
- .returns(ctx.projectName),
- }
- ctx.DocumentUpdaterHandler = {
- promises: {
- updateProjectStructure: sinon.stub().resolves(),
- },
- }
- ctx.FileStoreHandler = {
- promises: {
- uploadFileFromDiskWithHistoryId: sinon.stub().resolves({
- fileRef: ctx.file,
- createdBlob: true,
- }),
- },
- }
- ctx.FileSystemImportManager = {
- promises: {
- importDir: sinon
- .stub()
- .withArgs(ctx.topLevelDestination)
- .resolves(ctx.importEntries),
- },
- }
- ctx.ProjectCreationHandler = {
- promises: {
- createBlankProject: sinon.stub().resolves(ctx.project),
- },
- }
- ctx.ProjectEntityMongoUpdateHandler = {
- promises: {
- createNewFolderStructure: sinon.stub().resolves(ctx.newProjectVersion),
- },
- }
- ctx.ProjectRootDocManager = {
- promises: {
- setRootDocAutomatically: sinon.stub().resolves(),
- findRootDocFileFromDirectory: sinon
- .stub()
- .resolves({ path: 'main.tex', content: ctx.mainContent }),
- setRootDocFromName: sinon.stub().resolves(),
- },
- }
- ctx.ProjectDetailsHandler = {
- fixProjectName: sinon
- .stub()
- .withArgs(ctx.projectName)
- .returns(ctx.fixedProjectName),
- promises: {
- generateUniqueName: sinon.stub().resolves(ctx.uniqueProjectName),
- },
- }
- ctx.ProjectDeleter = {
- promises: {
- deleteProject: sinon.stub().resolves(),
- },
- }
- ctx.TpdsProjectFlusher = {
- promises: {
- flushProjectToTpds: sinon.stub().resolves(),
- },
- }
- vi.doMock('fs', () => ({
- default: ctx.fs,
- }))
- vi.doMock('../../../../app/src/Features/Uploads/ArchiveManager', () => ({
- default: ctx.ArchiveManager,
- }))
- vi.doMock('../../../../app/src/models/Doc', () => ({
- Doc: ctx.Doc,
- }))
- vi.doMock('../../../../app/src/Features/Docstore/DocstoreManager', () => ({
- default: ctx.DocstoreManager,
- }))
- vi.doMock('../../../../app/src/Features/Documents/DocumentHelper', () => ({
- default: ctx.DocumentHelper,
- }))
- vi.doMock(
- '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler',
- () => ({
- default: ctx.DocumentUpdaterHandler,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/FileStore/FileStoreHandler',
- () => ({
- default: ctx.FileStoreHandler,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Uploads/FileSystemImportManager',
- () => ({
- default: ctx.FileSystemImportManager,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Project/ProjectCreationHandler',
- () => ({
- default: ctx.ProjectCreationHandler,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Project/ProjectEntityMongoUpdateHandler',
- () => ({
- default: ctx.ProjectEntityMongoUpdateHandler,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Project/ProjectRootDocManager',
- () => ({
- default: ctx.ProjectRootDocManager,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Project/ProjectDetailsHandler',
- () => ({
- default: ctx.ProjectDetailsHandler,
- })
- )
- vi.doMock('../../../../app/src/Features/Project/ProjectDeleter', () => ({
- default: ctx.ProjectDeleter,
- }))
- vi.doMock(
- '../../../../app/src/Features/ThirdPartyDataStore/TpdsProjectFlusher',
- () => ({
- default: ctx.TpdsProjectFlusher,
- })
- )
- ctx.ProjectUploadManager = (await import(MODULE_PATH)).default
- })
- afterEach(function () {
- timekeeper.reset()
- })
- describe('createProjectFromZipArchive', function () {
- describe('when the title can be read from the root document', function () {
- beforeEach(async function (ctx) {
- await ctx.ProjectUploadManager.promises.createProjectFromZipArchive(
- ctx.ownerId,
- ctx.projectName,
- ctx.zipPath
- )
- })
- it('should extract the archive', function (ctx) {
- ctx.ArchiveManager.promises.extractZipArchive.should.have.been.calledWith(
- ctx.zipPath,
- ctx.extractedZipPath
- )
- })
- it('should create a project', function (ctx) {
- ctx.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
- ctx.ownerId,
- ctx.uniqueProjectName
- )
- })
- it('should initialize the file tree', function (ctx) {
- ctx.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
- ctx.project._id,
- ctx.docEntries,
- ctx.fileEntries
- )
- })
- it('should notify document updater', function (ctx) {
- ctx.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
- ctx.project._id,
- ctx.project.overleaf.history.id,
- ctx.ownerId,
- {
- newDocs: ctx.docEntries,
- newFiles: ctx.fileEntries,
- newProject: { version: ctx.newProjectVersion },
- },
- null
- )
- })
- it('should flush the project to TPDS', function (ctx) {
- ctx.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
- ctx.project._id
- )
- })
- it('should set the root document', function (ctx) {
- ctx.ProjectRootDocManager.promises.setRootDocFromName.should.have.been.calledWith(
- ctx.project._id,
- 'main.tex'
- )
- })
- it('should remove the destination directory afterwards', function (ctx) {
- ctx.fs.promises.rm.should.have.been.calledWith(ctx.extractedZipPath, {
- recursive: true,
- force: true,
- })
- })
- })
- describe("when the root document can't be determined", function () {
- beforeEach(async function (ctx) {
- ctx.ProjectRootDocManager.promises.findRootDocFileFromDirectory.resolves(
- {}
- )
- await ctx.ProjectUploadManager.promises.createProjectFromZipArchive(
- ctx.ownerId,
- ctx.projectName,
- ctx.zipPath
- )
- })
- it('should not try to set the root doc', function (ctx) {
- ctx.ProjectRootDocManager.promises.setRootDocFromName.should.not.have
- .been.called
- })
- })
- })
- describe('createProjectFromZipArchiveWithName', function () {
- beforeEach(async function (ctx) {
- await ctx.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
- ctx.ownerId,
- ctx.projectName,
- ctx.zipPath
- )
- })
- it('should extract the archive', function (ctx) {
- ctx.ArchiveManager.promises.extractZipArchive.should.have.been.calledWith(
- ctx.zipPath,
- ctx.extractedZipPath
- )
- })
- it('should create a project owned by the owner_id', function (ctx) {
- ctx.ProjectCreationHandler.promises.createBlankProject.should.have.been.calledWith(
- ctx.ownerId,
- ctx.uniqueProjectName
- )
- })
- it('should automatically set the root doc', function (ctx) {
- ctx.ProjectRootDocManager.promises.setRootDocAutomatically.should.have.been.calledWith(
- ctx.project._id
- )
- })
- it('should initialize the file tree', function (ctx) {
- ctx.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.should.have.been.calledWith(
- ctx.project._id,
- ctx.docEntries,
- ctx.fileEntries
- )
- })
- it('should notify document updater', function (ctx) {
- ctx.DocumentUpdaterHandler.promises.updateProjectStructure.should.have.been.calledWith(
- ctx.project._id,
- ctx.project.overleaf.history.id,
- ctx.ownerId,
- {
- newDocs: ctx.docEntries,
- newFiles: ctx.fileEntries,
- newProject: { version: ctx.newProjectVersion },
- },
- null
- )
- })
- it('should flush the project to TPDS', function (ctx) {
- ctx.TpdsProjectFlusher.promises.flushProjectToTpds.should.have.been.calledWith(
- ctx.project._id
- )
- })
- it('should remove the destination directory afterwards', function (ctx) {
- ctx.fs.promises.rm.should.have.been.calledWith(ctx.extractedZipPath, {
- recursive: true,
- force: true,
- })
- })
- describe('when initializing the folder structure fails', function () {
- beforeEach(async function (ctx) {
- ctx.ProjectEntityMongoUpdateHandler.promises.createNewFolderStructure.rejects()
- await expect(
- ctx.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
- ctx.ownerId,
- ctx.projectName,
- ctx.zipPath
- )
- ).to.be.rejected
- })
- it('should cleanup the blank project created', async function (ctx) {
- ctx.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
- ctx.project._id
- )
- })
- })
- describe('when setting automatically the root doc fails', function () {
- beforeEach(async function (ctx) {
- ctx.ProjectRootDocManager.promises.setRootDocAutomatically.rejects()
- await expect(
- ctx.ProjectUploadManager.promises.createProjectFromZipArchiveWithName(
- ctx.ownerId,
- ctx.projectName,
- ctx.zipPath
- )
- ).to.be.rejected
- })
- it('should cleanup the blank project created', function (ctx) {
- ctx.ProjectDeleter.promises.deleteProject.should.have.been.calledWith(
- ctx.project._id
- )
- })
- })
- })
- })
|