mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 14:13:43 +01:00
Update test files with vitest compat changes
GitOrigin-RevId: 494f906089d250268a5ff8c8a2150ff2692c37e2
This commit is contained in:
@@ -2,15 +2,12 @@
|
||||
// Fix any style issues and re-enable lint.
|
||||
/*
|
||||
* decaffeinate suggestions:
|
||||
* DS102: Remove unnecessary code created because of implicit returns
|
||||
* DS206: Consider reworking classes to avoid initClass
|
||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
import { vi } from 'vitest'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import { expect } from 'chai'
|
||||
|
||||
import esmock from 'esmock'
|
||||
import MockRequest from '../helpers/MockRequest.js'
|
||||
import MockResponse from '../helpers/MockResponse.js'
|
||||
import ArchiveErrors from '../../../../app/src/Features/Uploads/ArchiveErrors.js'
|
||||
@@ -19,12 +16,12 @@ const modulePath =
|
||||
'../../../../app/src/Features/Uploads/ProjectUploadController.mjs'
|
||||
|
||||
describe('ProjectUploadController', function () {
|
||||
beforeEach(async function () {
|
||||
beforeEach(async function (ctx) {
|
||||
let Timer
|
||||
this.req = new MockRequest()
|
||||
this.res = new MockResponse()
|
||||
this.user_id = 'user-id-123'
|
||||
this.metrics = {
|
||||
ctx.req = new MockRequest()
|
||||
ctx.res = new MockResponse()
|
||||
ctx.user_id = 'user-id-123'
|
||||
ctx.metrics = {
|
||||
Timer: (Timer = (function () {
|
||||
Timer = class Timer {
|
||||
static initClass() {
|
||||
@@ -35,262 +32,298 @@ describe('ProjectUploadController', function () {
|
||||
return Timer
|
||||
})()),
|
||||
}
|
||||
this.SessionManager = {
|
||||
getLoggedInUserId: sinon.stub().returns(this.user_id),
|
||||
ctx.SessionManager = {
|
||||
getLoggedInUserId: sinon.stub().returns(ctx.user_id),
|
||||
}
|
||||
this.ProjectLocator = {
|
||||
ctx.ProjectLocator = {
|
||||
promises: {},
|
||||
}
|
||||
this.EditorController = {
|
||||
ctx.EditorController = {
|
||||
promises: {},
|
||||
}
|
||||
|
||||
return (this.ProjectUploadController = await esmock.strict(modulePath, {
|
||||
multer: sinon.stub(),
|
||||
'@overleaf/settings': { path: {} },
|
||||
'../../../../app/src/Features/Uploads/ProjectUploadManager':
|
||||
(this.ProjectUploadManager = {}),
|
||||
'../../../../app/src/Features/Uploads/FileSystemImportManager':
|
||||
(this.FileSystemImportManager = {}),
|
||||
'@overleaf/metrics': this.metrics,
|
||||
'../../../../app/src/Features/Authentication/SessionManager':
|
||||
this.SessionManager,
|
||||
'../../../../app/src/Features/Uploads/ArchiveErrors': ArchiveErrors,
|
||||
'../../../../app/src/Features/Project/ProjectLocator':
|
||||
this.ProjectLocator,
|
||||
'../../../../app/src/Features/Editor/EditorController':
|
||||
this.EditorController,
|
||||
fs: (this.fs = {}),
|
||||
vi.doMock('multer', () => ({
|
||||
default: sinon.stub(),
|
||||
}))
|
||||
|
||||
vi.doMock('@overleaf/settings', () => ({
|
||||
default: { path: {} },
|
||||
}))
|
||||
|
||||
vi.doMock(
|
||||
'../../../../app/src/Features/Uploads/ProjectUploadManager',
|
||||
() => ({
|
||||
default: (ctx.ProjectUploadManager = {}),
|
||||
})
|
||||
)
|
||||
|
||||
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('fs', () => ({
|
||||
default: (ctx.fs = {}),
|
||||
}))
|
||||
|
||||
ctx.ProjectUploadController = (await import(modulePath)).default
|
||||
})
|
||||
|
||||
describe('uploadProject', function () {
|
||||
beforeEach(function () {
|
||||
this.path = '/path/to/file/on/disk.zip'
|
||||
this.name = 'filename.zip'
|
||||
this.req.file = {
|
||||
path: this.path,
|
||||
beforeEach(function (ctx) {
|
||||
ctx.path = '/path/to/file/on/disk.zip'
|
||||
ctx.fileName = 'filename.zip'
|
||||
ctx.req.file = {
|
||||
path: ctx.path,
|
||||
}
|
||||
this.req.body = {
|
||||
name: this.name,
|
||||
ctx.req.body = {
|
||||
name: ctx.fileName,
|
||||
}
|
||||
this.req.session = {
|
||||
ctx.req.session = {
|
||||
user: {
|
||||
_id: this.user_id,
|
||||
_id: ctx.user_id,
|
||||
},
|
||||
}
|
||||
this.project = { _id: (this.project_id = 'project-id-123') }
|
||||
ctx.project = { _id: (ctx.project_id = 'project-id-123') }
|
||||
|
||||
return (this.fs.unlink = sinon.stub())
|
||||
ctx.fs.unlink = sinon.stub()
|
||||
})
|
||||
|
||||
describe('successfully', function () {
|
||||
beforeEach(function () {
|
||||
this.ProjectUploadManager.createProjectFromZipArchive = sinon
|
||||
beforeEach(function (ctx) {
|
||||
ctx.ProjectUploadManager.createProjectFromZipArchive = sinon
|
||||
.stub()
|
||||
.callsArgWith(3, null, this.project)
|
||||
return this.ProjectUploadController.uploadProject(this.req, this.res)
|
||||
.callsArgWith(3, null, ctx.project)
|
||||
ctx.ProjectUploadController.uploadProject(ctx.req, ctx.res)
|
||||
})
|
||||
|
||||
it('should create a project owned by the logged in user', function () {
|
||||
return this.ProjectUploadManager.createProjectFromZipArchive
|
||||
.calledWith(this.user_id)
|
||||
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 () {
|
||||
return this.ProjectUploadManager.createProjectFromZipArchive
|
||||
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 () {
|
||||
return this.ProjectUploadManager.createProjectFromZipArchive
|
||||
.calledWith(sinon.match.any, sinon.match.any, this.path)
|
||||
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 () {
|
||||
return expect(this.res.body).to.deep.equal(
|
||||
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: this.project_id,
|
||||
project_id: ctx.project_id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('should record the time taken to do the upload', function () {
|
||||
return this.metrics.Timer.prototype.done.called.should.equal(true)
|
||||
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 () {
|
||||
return this.fs.unlink.calledWith(this.path).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 () {
|
||||
this.ProjectUploadManager.createProjectFromZipArchive = sinon
|
||||
beforeEach(function (ctx) {
|
||||
ctx.ProjectUploadManager.createProjectFromZipArchive = sinon
|
||||
.stub()
|
||||
.callsArgWith(3, new Error('Something went wrong'), this.project)
|
||||
return this.ProjectUploadController.uploadProject(this.req, this.res)
|
||||
.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 () {
|
||||
return expect(this.res.body).to.deep.equal(
|
||||
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' })
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when ProjectUploadManager.createProjectFromZipArchive reports the file as invalid', function () {
|
||||
beforeEach(function () {
|
||||
this.ProjectUploadManager.createProjectFromZipArchive = sinon
|
||||
beforeEach(function (ctx) {
|
||||
ctx.ProjectUploadManager.createProjectFromZipArchive = sinon
|
||||
.stub()
|
||||
.callsArgWith(
|
||||
3,
|
||||
new ArchiveErrors.ZipContentsTooLargeError(),
|
||||
this.project
|
||||
ctx.project
|
||||
)
|
||||
return this.ProjectUploadController.uploadProject(this.req, this.res)
|
||||
ctx.ProjectUploadController.uploadProject(ctx.req, ctx.res)
|
||||
})
|
||||
|
||||
it('should return the reported error to the FileUploader client', function () {
|
||||
expect(JSON.parse(this.res.body)).to.deep.equal({
|
||||
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 () {
|
||||
return expect(this.res.statusCode).to.equal(422)
|
||||
it("should return an 'unprocessable entity' status code", function (ctx) {
|
||||
expect(ctx.res.statusCode).to.equal(422)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('uploadFile', function () {
|
||||
beforeEach(function () {
|
||||
this.project_id = 'project-id-123'
|
||||
this.folder_id = 'folder-id-123'
|
||||
this.path = '/path/to/file/on/disk.png'
|
||||
this.name = 'filename.png'
|
||||
this.req.file = {
|
||||
path: this.path,
|
||||
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,
|
||||
}
|
||||
this.req.body = {
|
||||
name: this.name,
|
||||
ctx.req.body = {
|
||||
name: ctx.fileName,
|
||||
}
|
||||
this.req.session = {
|
||||
ctx.req.session = {
|
||||
user: {
|
||||
_id: this.user_id,
|
||||
_id: ctx.user_id,
|
||||
},
|
||||
}
|
||||
this.req.params = { Project_id: this.project_id }
|
||||
this.req.query = { folder_id: this.folder_id }
|
||||
return (this.fs.unlink = sinon.stub())
|
||||
ctx.req.params = { Project_id: ctx.project_id }
|
||||
ctx.req.query = { folder_id: ctx.folder_id }
|
||||
ctx.fs.unlink = sinon.stub()
|
||||
})
|
||||
|
||||
describe('successfully', function () {
|
||||
beforeEach(function () {
|
||||
this.entity = {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.entity = {
|
||||
_id: '1234',
|
||||
type: 'file',
|
||||
}
|
||||
this.FileSystemImportManager.addEntity = sinon
|
||||
ctx.FileSystemImportManager.addEntity = sinon
|
||||
.stub()
|
||||
.callsArgWith(6, null, this.entity)
|
||||
return this.ProjectUploadController.uploadFile(this.req, this.res)
|
||||
.callsArgWith(6, null, ctx.entity)
|
||||
ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res)
|
||||
})
|
||||
|
||||
it('should insert the file', function () {
|
||||
return this.FileSystemImportManager.addEntity
|
||||
it('should insert the file', function (ctx) {
|
||||
return ctx.FileSystemImportManager.addEntity
|
||||
.calledWith(
|
||||
this.user_id,
|
||||
this.project_id,
|
||||
this.folder_id,
|
||||
this.name,
|
||||
this.path
|
||||
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 () {
|
||||
return expect(this.res.body).to.deep.equal(
|
||||
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: this.entity._id,
|
||||
entity_id: ctx.entity._id,
|
||||
entity_type: 'file',
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('should time the request', function () {
|
||||
return this.metrics.Timer.prototype.done.called.should.equal(true)
|
||||
it('should time the request', function (ctx) {
|
||||
ctx.metrics.Timer.prototype.done.called.should.equal(true)
|
||||
})
|
||||
|
||||
it('should remove the uploaded file', function () {
|
||||
return this.fs.unlink.calledWith(this.path).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(function (done) {
|
||||
this.entity = {
|
||||
_id: '1234',
|
||||
type: 'file',
|
||||
}
|
||||
this.FileSystemImportManager.addEntity = sinon
|
||||
.stub()
|
||||
.callsArgWith(6, null, this.entity)
|
||||
this.ProjectLocator.promises.findElement = sinon.stub().resolves({
|
||||
path: { fileSystem: '/test' },
|
||||
beforeEach(function (ctx) {
|
||||
return 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)
|
||||
})
|
||||
this.EditorController.promises.mkdirp = sinon.stub().resolves({
|
||||
lastFolder: { _id: 'folder-id' },
|
||||
})
|
||||
this.req.body.relativePath = 'foo/bar/' + this.name
|
||||
this.res.json = data => {
|
||||
expect(data.success).to.be.true
|
||||
done()
|
||||
}
|
||||
this.ProjectUploadController.uploadFile(this.req, this.res)
|
||||
})
|
||||
|
||||
it('should insert the file', function () {
|
||||
this.ProjectLocator.promises.findElement.should.be.calledOnceWithExactly(
|
||||
it('should insert the file', function (ctx) {
|
||||
ctx.ProjectLocator.promises.findElement.should.be.calledOnceWithExactly(
|
||||
{
|
||||
project_id: this.project_id,
|
||||
element_id: this.folder_id,
|
||||
project_id: ctx.project_id,
|
||||
element_id: ctx.folder_id,
|
||||
type: 'folder',
|
||||
}
|
||||
)
|
||||
|
||||
this.EditorController.promises.mkdirp.should.be.calledWith(
|
||||
this.project_id,
|
||||
ctx.EditorController.promises.mkdirp.should.be.calledWith(
|
||||
ctx.project_id,
|
||||
'/test/foo/bar',
|
||||
this.user_id
|
||||
ctx.user_id
|
||||
)
|
||||
|
||||
this.FileSystemImportManager.addEntity.should.be.calledOnceWith(
|
||||
this.user_id,
|
||||
this.project_id,
|
||||
ctx.FileSystemImportManager.addEntity.should.be.calledOnceWith(
|
||||
ctx.user_id,
|
||||
ctx.project_id,
|
||||
'folder-id',
|
||||
this.name,
|
||||
this.path
|
||||
ctx.fileName,
|
||||
ctx.path
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when FileSystemImportManager.addEntity returns a generic error', function () {
|
||||
beforeEach(function () {
|
||||
this.FileSystemImportManager.addEntity = sinon
|
||||
beforeEach(function (ctx) {
|
||||
ctx.FileSystemImportManager.addEntity = sinon
|
||||
.stub()
|
||||
.callsArgWith(6, new Error('Sorry something went wrong'))
|
||||
return this.ProjectUploadController.uploadFile(this.req, this.res)
|
||||
ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res)
|
||||
})
|
||||
|
||||
it('should return an unsuccessful response to the FileUploader client', function () {
|
||||
return expect(this.res.body).to.deep.equal(
|
||||
it('should return an unsuccessful response to the FileUploader client', function (ctx) {
|
||||
expect(ctx.res.body).to.deep.equal(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
})
|
||||
@@ -299,15 +332,15 @@ describe('ProjectUploadController', function () {
|
||||
})
|
||||
|
||||
describe('when FileSystemImportManager.addEntity returns a too many files error', function () {
|
||||
beforeEach(function () {
|
||||
this.FileSystemImportManager.addEntity = sinon
|
||||
beforeEach(function (ctx) {
|
||||
ctx.FileSystemImportManager.addEntity = sinon
|
||||
.stub()
|
||||
.callsArgWith(6, new Error('project_has_too_many_files'))
|
||||
return this.ProjectUploadController.uploadFile(this.req, this.res)
|
||||
ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res)
|
||||
})
|
||||
|
||||
it('should return an unsuccessful response to the FileUploader client', function () {
|
||||
return expect(this.res.body).to.deep.equal(
|
||||
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',
|
||||
@@ -317,13 +350,13 @@ describe('ProjectUploadController', function () {
|
||||
})
|
||||
|
||||
describe('with an invalid filename', function () {
|
||||
beforeEach(function () {
|
||||
this.req.body.name = ''
|
||||
return this.ProjectUploadController.uploadFile(this.req, this.res)
|
||||
beforeEach(function (ctx) {
|
||||
ctx.req.body.name = ''
|
||||
ctx.ProjectUploadController.uploadFile(ctx.req, ctx.res)
|
||||
})
|
||||
|
||||
it('should return a a non success response', function () {
|
||||
return expect(this.res.body).to.deep.equal(
|
||||
it('should return a a non success response', function (ctx) {
|
||||
expect(ctx.res.body).to.deep.equal(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
error: 'invalid_filename',
|
||||
|
||||
Reference in New Issue
Block a user