| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670 |
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS206: Consider reworking classes to avoid initClass
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- import { expect, vi } from 'vitest'
- import sinon from 'sinon'
- import MockRequest from '../helpers/MockRequest.mjs'
- import MockResponse from '../helpers/MockResponse.mjs'
- import ArchiveErrors from '../../../../app/src/Features/Uploads/ArchiveErrors.mjs'
- import { FileTooLargeError } from '../../../../app/src/Features/Errors/Errors.js'
- const modulePath =
- '../../../../app/src/Features/Uploads/ProjectUploadController.mjs'
- describe('ProjectUploadController', function () {
- beforeEach(async function (ctx) {
- let Timer
- ctx.req = new MockRequest(vi)
- ctx.res = new MockResponse(vi)
- ctx.user_id = 'user-id-123'
- ctx.metrics = {
- Timer: (Timer = (function () {
- Timer = class Timer {
- static initClass() {
- this.prototype.done = sinon.stub()
- }
- }
- Timer.initClass()
- return Timer
- })()),
- }
- ctx.SessionManager = {
- getLoggedInUserId: sinon.stub().returns(ctx.user_id),
- }
- ctx.ProjectLocator = {
- promises: {},
- }
- ctx.EditorController = {
- promises: {},
- }
- ctx.ProjectOptionsHandler = {
- promises: {
- setCompiler: sinon.stub().resolves(),
- },
- }
- ctx.DocumentConversionManager = {
- promises: {
- convertDocumentToLaTeXZipArchive: sinon.stub(),
- },
- }
- vi.doMock('multer', () => ({
- default: sinon.stub(),
- }))
- vi.doMock('@overleaf/settings', () => ({
- default: { path: {} },
- }))
- vi.doMock(
- '../../../../app/src/Features/Uploads/ProjectUploadManager',
- () => ({
- default: (ctx.ProjectUploadManager = { promises: {} }),
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Uploads/FileSystemImportManager',
- () => ({
- default: (ctx.FileSystemImportManager = {}),
- })
- )
- vi.doMock('@overleaf/metrics', () => ({
- default: ctx.metrics,
- }))
- vi.doMock(
- '../../../../app/src/Features/Authentication/SessionManager',
- () => ({
- default: ctx.SessionManager,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Uploads/ArchiveErrors',
- () => ArchiveErrors
- )
- vi.doMock('../../../../app/src/Features/Project/ProjectLocator', () => ({
- default: ctx.ProjectLocator,
- }))
- vi.doMock('../../../../app/src/Features/Editor/EditorController', () => ({
- default: ctx.EditorController,
- }))
- vi.doMock(
- '../../../../app/src/Features/Project/ProjectOptionsHandler',
- () => ({
- default: ctx.ProjectOptionsHandler,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Uploads/DocumentConversionManager.mjs',
- () => ({
- default: ctx.DocumentConversionManager,
- })
- )
- vi.doMock('node:fs', () => ({
- default: (ctx.fs = {}),
- }))
- vi.doMock('node:fs/promises', () => ({
- default: (ctx.fsPromises = {}),
- }))
- ctx.ProjectUploadController = (await import(modulePath)).default
- })
- describe('uploadProject', function () {
- beforeEach(function (ctx) {
- ctx.path = '/path/to/file/on/disk.zip'
- ctx.fileName = 'filename.zip'
- ctx.req.file = {
- path: ctx.path,
- }
- ctx.req.body = {
- name: ctx.fileName,
- }
- ctx.req.session = {
- user: {
- _id: ctx.user_id,
- },
- }
- ctx.project = { _id: (ctx.project_id = 'project-id-123') }
- ctx.fs.unlink = sinon.stub()
- ctx.fsPromises.unlink = sinon.stub().resolves()
- })
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.ProjectUploadManager.createProjectFromZipArchive = sinon
- .stub()
- .callsArgWith(3, null, ctx.project)
- ctx.ProjectUploadController.uploadProject(ctx.req, ctx.res)
- })
- it('should create a project owned by the logged in user', function (ctx) {
- ctx.ProjectUploadManager.createProjectFromZipArchive
- .calledWith(ctx.user_id)
- .should.equal(true)
- })
- it('should create a project with the same name as the zip archive', function (ctx) {
- ctx.ProjectUploadManager.createProjectFromZipArchive
- .calledWith(sinon.match.any, 'filename', sinon.match.any)
- .should.equal(true)
- })
- it('should create a project from the zip archive', function (ctx) {
- ctx.ProjectUploadManager.createProjectFromZipArchive
- .calledWith(sinon.match.any, sinon.match.any, ctx.path)
- .should.equal(true)
- })
- it('should return a successful response to the FileUploader client', function (ctx) {
- expect(ctx.res.body).to.deep.equal(
- JSON.stringify({
- success: true,
- project_id: ctx.project_id,
- })
- )
- })
- it('should record the time taken to do the upload', function (ctx) {
- ctx.metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should remove the uploaded file', function (ctx) {
- ctx.fs.unlink.calledWith(ctx.path).should.equal(true)
- })
- })
- describe('when ProjectUploadManager.createProjectFromZipArchive fails', function () {
- beforeEach(function (ctx) {
- ctx.ProjectUploadManager.createProjectFromZipArchive = sinon
- .stub()
- .callsArgWith(3, new Error('Something went wrong'), ctx.project)
- ctx.ProjectUploadController.uploadProject(ctx.req, ctx.res)
- })
- it('should return a failed response to the FileUploader client', function (ctx) {
- expect(ctx.res.body).to.deep.equal(
- JSON.stringify({ success: false, error: 'upload_failed' })
- )
- })
- it('should remove the uploaded file', function (ctx) {
- ctx.fs.unlink.calledWith(ctx.path).should.equal(true)
- })
- })
- describe('when ProjectUploadManager.createProjectFromZipArchive reports the file as invalid', function () {
- beforeEach(function (ctx) {
- ctx.ProjectUploadManager.createProjectFromZipArchive = sinon
- .stub()
- .callsArgWith(
- 3,
- new ArchiveErrors.ZipContentsTooLargeError(),
- ctx.project
- )
- ctx.ProjectUploadController.uploadProject(ctx.req, ctx.res)
- })
- it('should return the reported error to the FileUploader client', function (ctx) {
- expect(JSON.parse(ctx.res.body)).to.deep.equal({
- success: false,
- error: 'zip_contents_too_large',
- })
- })
- it("should return an 'unprocessable entity' status code", function (ctx) {
- expect(ctx.res.statusCode).to.equal(422)
- })
- it('should remove the uploaded file', function (ctx) {
- ctx.fs.unlink.calledWith(ctx.path).should.equal(true)
- })
- })
- })
- describe('uploadFile', function () {
- beforeEach(function (ctx) {
- ctx.project_id = 'project-id-123'
- ctx.folder_id = 'folder-id-123'
- ctx.path = '/path/to/file/on/disk.png'
- ctx.fileName = 'filename.png'
- ctx.req.file = {
- path: ctx.path,
- }
- ctx.req.body = {
- name: ctx.fileName,
- }
- ctx.req.session = {
- user: {
- _id: ctx.user_id,
- },
- }
- ctx.req.params = { Project_id: ctx.project_id }
- ctx.req.query = { folder_id: ctx.folder_id }
- ctx.fs.unlink = sinon.stub()
- ctx.fsPromises.unlink = sinon.stub().resolves()
- })
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.entity = {
- _id: '1234',
- type: 'file',
- }
- ctx.FileSystemImportManager.addEntity = sinon
- .stub()
- .callsArgWith(6, null, ctx.entity)
- ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res)
- })
- it('should insert the file', function (ctx) {
- return ctx.FileSystemImportManager.addEntity
- .calledWith(
- ctx.user_id,
- ctx.project_id,
- ctx.folder_id,
- ctx.fileName,
- ctx.path
- )
- .should.equal(true)
- })
- it('should return a successful response to the FileUploader client', function (ctx) {
- expect(ctx.res.body).to.deep.equal(
- JSON.stringify({
- success: true,
- entity_id: ctx.entity._id,
- entity_type: 'file',
- })
- )
- })
- it('should time the request', function (ctx) {
- ctx.metrics.Timer.prototype.done.called.should.equal(true)
- })
- it('should remove the uploaded file', function (ctx) {
- ctx.fs.unlink.calledWith(ctx.path).should.equal(true)
- })
- })
- describe('with folder structure', function () {
- beforeEach(async function (ctx) {
- await new Promise(resolve => {
- ctx.entity = {
- _id: '1234',
- type: 'file',
- }
- ctx.FileSystemImportManager.addEntity = sinon
- .stub()
- .callsArgWith(6, null, ctx.entity)
- ctx.ProjectLocator.promises.findElement = sinon.stub().resolves({
- path: { fileSystem: '/test' },
- })
- ctx.EditorController.promises.mkdirp = sinon.stub().resolves({
- lastFolder: { _id: 'folder-id' },
- })
- ctx.req.body.relativePath = 'foo/bar/' + ctx.fileName
- ctx.res.json = data => {
- expect(data.success).to.be.true
- resolve()
- }
- ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res)
- })
- })
- it('should insert the file', function (ctx) {
- ctx.ProjectLocator.promises.findElement.should.be.calledOnceWithExactly(
- {
- project_id: ctx.project_id,
- element_id: ctx.folder_id,
- type: 'folder',
- }
- )
- ctx.EditorController.promises.mkdirp.should.be.calledWith(
- ctx.project_id,
- '/test/foo/bar',
- ctx.user_id
- )
- ctx.FileSystemImportManager.addEntity.should.be.calledOnceWith(
- ctx.user_id,
- ctx.project_id,
- 'folder-id',
- ctx.fileName,
- ctx.path
- )
- })
- })
- describe('when looking up the folder structure fails', function () {
- beforeEach(async function (ctx) {
- await new Promise(resolve => {
- ctx.error = new Error('woops')
- ctx.ProjectLocator.promises.findElement = sinon
- .stub()
- .rejects(ctx.error)
- ctx.req.body.relativePath = 'foo/bar/' + ctx.fileName
- ctx.next = error => {
- ctx.nextError = error
- resolve()
- }
- ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res, ctx.next)
- })
- })
- it('should unlink the file', function (ctx) {
- ctx.fsPromises.unlink.should.have.been.calledWith(ctx.path)
- })
- it('should call next with the error', function (ctx) {
- expect(ctx.nextError).to.equal(ctx.error)
- })
- })
- describe('when FileSystemImportManager.addEntity returns a generic error', function () {
- beforeEach(function (ctx) {
- ctx.FileSystemImportManager.addEntity = sinon
- .stub()
- .callsArgWith(6, new Error('Sorry something went wrong'))
- ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res)
- })
- it('should return an unsuccessful response to the FileUploader client', function (ctx) {
- expect(ctx.res.body).to.deep.equal(
- JSON.stringify({
- success: false,
- })
- )
- })
- it('should remove the uploaded file', function (ctx) {
- ctx.fs.unlink.calledWith(ctx.path).should.equal(true)
- })
- })
- describe('when FileSystemImportManager.addEntity returns a too many files error', function () {
- beforeEach(function (ctx) {
- ctx.FileSystemImportManager.addEntity = sinon
- .stub()
- .callsArgWith(6, new Error('project_has_too_many_files'))
- ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res)
- })
- it('should return an unsuccessful response to the FileUploader client', function (ctx) {
- expect(ctx.res.body).to.deep.equal(
- JSON.stringify({
- success: false,
- error: 'project_has_too_many_files',
- })
- )
- })
- it('should remove the uploaded file', function (ctx) {
- ctx.fs.unlink.calledWith(ctx.path).should.equal(true)
- })
- })
- describe('with an invalid filename', function () {
- beforeEach(function (ctx) {
- ctx.req.body.name = ''
- ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res)
- })
- it('should return a non success response', function (ctx) {
- expect(ctx.res.body).to.deep.equal(
- JSON.stringify({
- success: false,
- error: 'invalid_filename',
- })
- )
- })
- it('should remove the uploaded file', function (ctx) {
- ctx.fsPromises.unlink.calledWith(ctx.path).should.equal(true)
- })
- })
- describe('with a filename that is too long', function () {
- beforeEach(function (ctx) {
- ctx.req.body.name = 'a'.repeat(151)
- ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res)
- })
- it('should return a non success response', function (ctx) {
- expect(ctx.res.body).to.deep.equal(
- JSON.stringify({
- success: false,
- error: 'invalid_filename',
- })
- )
- })
- it('should remove the uploaded file', function (ctx) {
- ctx.fsPromises.unlink.calledWith(ctx.path).should.equal(true)
- })
- })
- })
- describe('importDocument', function () {
- beforeEach(async function (ctx) {
- ctx.req.file = {
- path: '/path/to/uploaded/file.docx',
- }
- ctx.req.body = {
- name: 'file.docx',
- }
- ctx.req.query = { type: 'docx' }
- ctx.archivePath = '/path/to/archive.zip'
- ctx.fsPromises.unlink = sinon.stub().resolves()
- })
- describe('with conversionType=docx', async function () {
- describe('successfully', async function () {
- beforeEach(async function (ctx) {
- ctx.DocumentConversionManager.promises.convertDocumentToLaTeXZipArchive =
- sinon.stub().resolves(ctx.archivePath)
- ctx.ProjectUploadManager.promises.createProjectFromZipArchive = sinon
- .stub()
- .resolves({
- _id: 'new-project-id',
- })
- await new Promise(resolve => {
- ctx.res.json = data => {
- expect(data.success).to.be.true
- expect(data.project_id).to.equal('new-project-id')
- resolve()
- }
- ctx.ProjectUploadController.importDocument(ctx.req, ctx.res)
- })
- })
- it('should call the DocumentConversionManager with file path and type', function (ctx) {
- expect(
- ctx.DocumentConversionManager.promises
- .convertDocumentToLaTeXZipArchive
- ).to.have.been.calledWith(ctx.req.file.path, ctx.user_id, 'docx')
- })
- it('should use the resulting archive to create a new project', function (ctx) {
- expect(
- ctx.ProjectUploadManager.promises.createProjectFromZipArchive
- ).to.have.been.calledWith(ctx.user_id, 'file', ctx.archivePath)
- })
- it('should set the compiler to lualatex', function (ctx) {
- expect(
- ctx.ProjectOptionsHandler.promises.setCompiler
- ).to.have.been.calledWith('new-project-id', 'lualatex')
- })
- it('should unlink the archive after creating the project', function (ctx) {
- expect(ctx.fsPromises.unlink).to.have.been.calledWith(ctx.archivePath)
- })
- it('should unlink the uploaded file', function (ctx) {
- expect(ctx.fsPromises.unlink).to.have.been.calledWith(
- ctx.req.file.path
- )
- })
- })
- })
- describe('with conversionType=markdown', async function () {
- beforeEach(async function (ctx) {
- ctx.req.file = {
- path: '/path/to/uploaded/file.md',
- }
- ctx.req.body = {
- name: 'file.md',
- }
- ctx.req.query = { type: 'markdown' }
- ctx.DocumentConversionManager.promises.convertDocumentToLaTeXZipArchive =
- sinon.stub().resolves(ctx.archivePath)
- ctx.ProjectUploadManager.promises.createProjectFromZipArchive = sinon
- .stub()
- .resolves({
- _id: 'new-project-id',
- })
- await new Promise(resolve => {
- ctx.res.json = data => {
- expect(data.success).to.be.true
- expect(data.project_id).to.equal('new-project-id')
- resolve()
- }
- ctx.ProjectUploadController.importDocument(ctx.req, ctx.res)
- })
- })
- it('should call the DocumentConversionManager with file path and markdown type', function (ctx) {
- expect(
- ctx.DocumentConversionManager.promises
- .convertDocumentToLaTeXZipArchive
- ).to.have.been.calledWith(ctx.req.file.path, ctx.user_id, 'markdown')
- })
- it('should use the resulting archive to create a new project', function (ctx) {
- expect(
- ctx.ProjectUploadManager.promises.createProjectFromZipArchive
- ).to.have.been.calledWith(ctx.user_id, 'file', ctx.archivePath)
- })
- it('should set the compiler to lualatex', function (ctx) {
- expect(
- ctx.ProjectOptionsHandler.promises.setCompiler
- ).to.have.been.calledWith('new-project-id', 'lualatex')
- })
- it('should unlink the archive after creating the project', function (ctx) {
- expect(ctx.fsPromises.unlink).to.have.been.calledWith(ctx.archivePath)
- })
- it('should unlink the uploaded file', function (ctx) {
- expect(ctx.fsPromises.unlink).to.have.been.calledWith(ctx.req.file.path)
- })
- })
- describe('with an invalid conversionType', async function () {
- beforeEach(async function (ctx) {
- ctx.req.query = { type: 'invalid' }
- await new Promise(resolve => {
- ctx.res.json = data => {
- expect(data).to.deep.equal({
- success: false,
- error: 'invalid_type',
- })
- resolve()
- }
- ctx.ProjectUploadController.importDocument(ctx.req, ctx.res)
- })
- })
- it('should return http 400', function (ctx) {
- expect(ctx.res.statusCode).to.equal(400)
- })
- it('should not call DocumentConversionManager', function (ctx) {
- expect(
- ctx.DocumentConversionManager.promises
- .convertDocumentToLaTeXZipArchive
- ).not.to.have.been.called
- })
- })
- describe('unsuccessfully', async function () {
- beforeEach(async function (ctx) {
- ctx.DocumentConversionManager.promises.convertDocumentToLaTeXZipArchive =
- sinon.stub().rejects(new Error('Conversion failed'))
- await new Promise(resolve => {
- ctx.res.json = data => {
- expect(data.success).to.be.false
- resolve()
- }
- ctx.ProjectUploadController.importDocument(ctx.req, ctx.res)
- })
- })
- it('should call the DocumentConversionManager to convert the file', function (ctx) {
- expect(
- ctx.DocumentConversionManager.promises
- .convertDocumentToLaTeXZipArchive
- ).to.have.been.calledWith(ctx.req.file.path, ctx.user_id, 'docx')
- })
- it('should unlink the uploaded file', function (ctx) {
- expect(ctx.fsPromises.unlink).to.have.been.calledWith(ctx.req.file.path)
- })
- it('should return http 500', function (ctx) {
- expect(ctx.res.statusCode).to.equal(500)
- })
- })
- describe('when the converted archive is too large', async function () {
- beforeEach(async function (ctx) {
- ctx.DocumentConversionManager.promises.convertDocumentToLaTeXZipArchive =
- sinon.stub().rejects(new FileTooLargeError('file too large'))
- await new Promise(resolve => {
- ctx.res.json = data => {
- expect(data).to.deep.equal({
- success: false,
- error: 'file_too_large',
- })
- resolve()
- }
- ctx.ProjectUploadController.importDocument(ctx.req, ctx.res)
- })
- })
- it('should return http 422', function (ctx) {
- expect(ctx.res.statusCode).to.equal(422)
- })
- it('should unlink the uploaded file', function (ctx) {
- expect(ctx.fsPromises.unlink).to.have.been.calledWith(ctx.req.file.path)
- })
- })
- })
- })
|