mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-23 09:09:36 +02:00
[WEB + CLSI] Download as markdown GitOrigin-RevId: 181eddf2513e9c5edacbab37e93f9cac2191ee1a
411 lines
13 KiB
JavaScript
411 lines
13 KiB
JavaScript
import { vi } from 'vitest'
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
// Fix any style issues and re-enable lint.
|
|
/*
|
|
* decaffeinate suggestions:
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
*/
|
|
import sinon from 'sinon'
|
|
import MockRequest from '../helpers/MockRequest.mjs'
|
|
import MockResponse from '../helpers/MockResponse.mjs'
|
|
const modulePath =
|
|
'../../../../app/src/Features/Downloads/ProjectDownloadsController.mjs'
|
|
|
|
describe('ProjectDownloadsController', function () {
|
|
beforeEach(async function (ctx) {
|
|
ctx.project_id = 'project-id-123'
|
|
ctx.req = new MockRequest(vi)
|
|
ctx.res = new MockResponse(vi)
|
|
ctx.next = sinon.stub()
|
|
ctx.DocumentUpdaterHandler = sinon.stub()
|
|
|
|
vi.doMock(
|
|
'../../../../app/src/Features/Downloads/ProjectZipStreamManager.mjs',
|
|
() => ({
|
|
default: (ctx.ProjectZipStreamManager = {}),
|
|
})
|
|
)
|
|
|
|
vi.doMock('../../../../app/src/Features/Project/ProjectGetter.mjs', () => ({
|
|
default: (ctx.ProjectGetter = {
|
|
promises: {
|
|
getProject: sinon.stub(),
|
|
},
|
|
}),
|
|
}))
|
|
|
|
vi.doMock(
|
|
'../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler.mjs',
|
|
() => ({
|
|
default: ctx.DocumentUpdaterHandler,
|
|
})
|
|
)
|
|
|
|
vi.doMock(
|
|
'../../../../app/src/Features/Project/ProjectAuditLogHandler.mjs',
|
|
() => ({
|
|
default: (ctx.ProjectAuditLogHandler = {
|
|
addEntryInBackground: sinon.stub(),
|
|
}),
|
|
})
|
|
)
|
|
|
|
vi.doMock(
|
|
'../../../../app/src/Features/Authentication/SessionManager.mjs',
|
|
() => ({
|
|
default: (ctx.SessionManager = {
|
|
getLoggedInUserId: sinon
|
|
.stub()
|
|
.callsFake(session => session?.user?._id),
|
|
}),
|
|
})
|
|
)
|
|
|
|
vi.doMock(
|
|
'../../../../app/src/Features/Uploads/DocumentConversionManager.mjs',
|
|
() => ({
|
|
default: (ctx.DocumentConversionManager = {
|
|
promises: {
|
|
convertProjectToDocument: sinon.stub(),
|
|
},
|
|
}),
|
|
})
|
|
)
|
|
|
|
vi.doMock('node:stream/promises', () => ({
|
|
pipeline: (ctx.pipeline = sinon.stub().resolves()),
|
|
}))
|
|
|
|
ctx.ProjectDownloadsController = (await import(modulePath)).default
|
|
})
|
|
|
|
describe('downloadProject', function () {
|
|
beforeEach(function (ctx) {
|
|
ctx.stream = { pipe: sinon.stub() }
|
|
ctx.ProjectZipStreamManager.createZipStreamForProject = sinon
|
|
.stub()
|
|
.callsArgWith(1, null, ctx.stream)
|
|
ctx.req.params = { Project_id: ctx.project_id }
|
|
ctx.req.ip = '192.168.1.1'
|
|
ctx.req.session = {
|
|
user: {
|
|
_id: 'user-id-123',
|
|
email: 'user@example.com',
|
|
},
|
|
}
|
|
ctx.project_name = 'project name with accênts and % special characters'
|
|
ctx.ProjectGetter.getProject = sinon
|
|
.stub()
|
|
.callsArgWith(2, null, { name: ctx.project_name })
|
|
ctx.DocumentUpdaterHandler.flushProjectToMongo = sinon
|
|
.stub()
|
|
.callsArgWith(1)
|
|
ctx.Metrics.inc = sinon.stub()
|
|
return ctx.ProjectDownloadsController.downloadProject(
|
|
ctx.req,
|
|
ctx.res,
|
|
ctx.next
|
|
)
|
|
})
|
|
|
|
it('should create a zip from the project', function (ctx) {
|
|
return ctx.ProjectZipStreamManager.createZipStreamForProject
|
|
.calledWith(ctx.project_id)
|
|
.should.equal(true)
|
|
})
|
|
|
|
it('should stream the zip to the request', function (ctx) {
|
|
return ctx.stream.pipe.calledWith(ctx.res).should.equal(true)
|
|
})
|
|
|
|
it('should set the correct content type on the request', function (ctx) {
|
|
expect(ctx.res.contentType).toHaveBeenCalledWith('application/zip')
|
|
})
|
|
|
|
it('should flush the project to mongo', function (ctx) {
|
|
return ctx.DocumentUpdaterHandler.flushProjectToMongo
|
|
.calledWith(ctx.project_id)
|
|
.should.equal(true)
|
|
})
|
|
|
|
it("should look up the project's name", function (ctx) {
|
|
return ctx.ProjectGetter.getProject
|
|
.calledWith(ctx.project_id, { name: true })
|
|
.should.equal(true)
|
|
})
|
|
|
|
it('should name the downloaded file after the project but sanitise special characters', function (ctx) {
|
|
ctx.res.headers.should.deep.equal({
|
|
'Content-Disposition': `attachment; filename="project_name_with_accênts_and___special_characters.zip"`,
|
|
'Content-Type': 'application/zip',
|
|
'X-Accel-Buffering': 'no',
|
|
'X-Content-Type-Options': 'nosniff',
|
|
})
|
|
})
|
|
|
|
it('should record the action via Metrics', function (ctx) {
|
|
return ctx.Metrics.inc.calledWith('zip-downloads').should.equal(true)
|
|
})
|
|
|
|
it('should add an audit log entry', function (ctx) {
|
|
return ctx.ProjectAuditLogHandler.addEntryInBackground
|
|
.calledWith(
|
|
ctx.project_id,
|
|
'project-downloaded',
|
|
ctx.req.session.user._id,
|
|
ctx.req.ip
|
|
)
|
|
.should.equal(true)
|
|
})
|
|
})
|
|
|
|
describe('downloadMultipleProjects', function () {
|
|
beforeEach(function (ctx) {
|
|
ctx.stream = { pipe: sinon.stub() }
|
|
ctx.ProjectZipStreamManager.createZipStreamForMultipleProjects = sinon
|
|
.stub()
|
|
.callsArgWith(1, null, ctx.stream)
|
|
ctx.project_ids = ['project-1', 'project-2']
|
|
ctx.req.query = { project_ids: ctx.project_ids.join(',') }
|
|
ctx.req.ip = '192.168.1.1'
|
|
ctx.req.session = {
|
|
user: {
|
|
_id: 'user-id-123',
|
|
email: 'user@example.com',
|
|
},
|
|
}
|
|
ctx.DocumentUpdaterHandler.flushMultipleProjectsToMongo = sinon
|
|
.stub()
|
|
.callsArgWith(1)
|
|
ctx.Metrics.inc = sinon.stub()
|
|
return ctx.ProjectDownloadsController.downloadMultipleProjects(
|
|
ctx.req,
|
|
ctx.res,
|
|
ctx.next
|
|
)
|
|
})
|
|
|
|
it('should create a zip from the project', function (ctx) {
|
|
return ctx.ProjectZipStreamManager.createZipStreamForMultipleProjects
|
|
.calledWith(ctx.project_ids)
|
|
.should.equal(true)
|
|
})
|
|
|
|
it('should stream the zip to the request', function (ctx) {
|
|
return ctx.stream.pipe.calledWith(ctx.res).should.equal(true)
|
|
})
|
|
|
|
it('should set the correct content type on the request', function (ctx) {
|
|
expect(ctx.res.contentType).toHaveBeenCalledWith('application/zip')
|
|
})
|
|
|
|
it('should flush the projects to mongo', function (ctx) {
|
|
return ctx.DocumentUpdaterHandler.flushMultipleProjectsToMongo
|
|
.calledWith(ctx.project_ids)
|
|
.should.equal(true)
|
|
})
|
|
|
|
it('should name the downloaded file after the project', function (ctx) {
|
|
ctx.res.headers.should.deep.equal({
|
|
'Content-Disposition':
|
|
'attachment; filename="Overleaf Projects (2 items).zip"',
|
|
'Content-Type': 'application/zip',
|
|
'X-Accel-Buffering': 'no',
|
|
'X-Content-Type-Options': 'nosniff',
|
|
})
|
|
})
|
|
|
|
it('should record the action via Metrics', function (ctx) {
|
|
return ctx.Metrics.inc
|
|
.calledWith('zip-downloads-multiple')
|
|
.should.equal(true)
|
|
})
|
|
|
|
it('should add an audit log entry for each project', function (ctx) {
|
|
ctx.ProjectAuditLogHandler.addEntryInBackground.callCount.should.equal(
|
|
ctx.project_ids.length
|
|
)
|
|
for (const projectId of ctx.project_ids) {
|
|
ctx.ProjectAuditLogHandler.addEntryInBackground
|
|
.calledWith(
|
|
projectId,
|
|
'project-downloaded',
|
|
ctx.req.session.user._id,
|
|
ctx.req.ip
|
|
)
|
|
.should.equal(true)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('exportProjectConversion', function () {
|
|
describe('when an unsupported type is requested', function () {
|
|
beforeEach(async function (ctx) {
|
|
ctx.req.params = { Project_id: 'test-project-id', type: 'unsupported' }
|
|
ctx.req.session = { user: { _id: 'test-user-id' } }
|
|
|
|
await ctx.ProjectDownloadsController.exportProjectConversion(
|
|
ctx.req,
|
|
ctx.res,
|
|
ctx.next
|
|
)
|
|
})
|
|
|
|
it('should return 400', function (ctx) {
|
|
expect(ctx.res.statusCode).to.equal(400)
|
|
})
|
|
|
|
it('should not call the conversion manager', function (ctx) {
|
|
sinon.assert.notCalled(
|
|
ctx.DocumentConversionManager.promises.convertProjectToDocument
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('with a supported type', function () {
|
|
beforeEach(async function (ctx) {
|
|
ctx.projectId = 'test-project-id'
|
|
ctx.userId = 'test-user-id'
|
|
ctx.projectName = 'My Test Project'
|
|
ctx.exportStream = { pipe: sinon.stub() }
|
|
ctx.contentLength = 9876
|
|
|
|
ctx.req.params = { Project_id: ctx.projectId, type: 'docx' }
|
|
ctx.req.session = { user: { _id: ctx.userId } }
|
|
ctx.req.ip = '192.168.1.1'
|
|
|
|
ctx.res.attachment = sinon.stub().returns(ctx.res)
|
|
|
|
ctx.SessionManager.getLoggedInUserId.returns(ctx.userId)
|
|
ctx.ProjectGetter.promises.getProject.resolves({
|
|
name: ctx.projectName,
|
|
})
|
|
ctx.DocumentConversionManager.promises.convertProjectToDocument.resolves(
|
|
{
|
|
stream: ctx.exportStream,
|
|
contentLength: ctx.contentLength,
|
|
}
|
|
)
|
|
|
|
await ctx.ProjectDownloadsController.exportProjectConversion(
|
|
ctx.req,
|
|
ctx.res,
|
|
ctx.next
|
|
)
|
|
})
|
|
|
|
it('should call convertProjectToDocument with the docx type', function (ctx) {
|
|
sinon.assert.calledWith(
|
|
ctx.DocumentConversionManager.promises.convertProjectToDocument,
|
|
ctx.projectId,
|
|
ctx.userId,
|
|
'docx'
|
|
)
|
|
})
|
|
|
|
it('should set the Content-Length header', function (ctx) {
|
|
expect(ctx.res.headers['Content-Length']).to.equal(ctx.contentLength)
|
|
})
|
|
|
|
it('should set the attachment filename with safe project name', function (ctx) {
|
|
sinon.assert.calledWith(ctx.res.attachment, 'My_Test_Project.docx')
|
|
})
|
|
|
|
it('should set the X-Content-Type-Options header', function (ctx) {
|
|
expect(ctx.res.headers['X-Content-Type-Options']).to.equal('nosniff')
|
|
})
|
|
|
|
it('should set the X-Accel-Buffering header', function (ctx) {
|
|
expect(ctx.res.headers['X-Accel-Buffering']).to.equal('no')
|
|
})
|
|
|
|
it('should add an audit log entry', function (ctx) {
|
|
sinon.assert.calledWith(
|
|
ctx.ProjectAuditLogHandler.addEntryInBackground,
|
|
ctx.projectId,
|
|
'project-exported-docx',
|
|
ctx.userId,
|
|
ctx.req.ip
|
|
)
|
|
})
|
|
|
|
it('should record the action via Metrics', function (ctx) {
|
|
ctx.Metrics.inc
|
|
.calledWith('document-exports', 1, { type: 'docx' })
|
|
.should.equal(true)
|
|
})
|
|
|
|
it('should stream the document to the response', function (ctx) {
|
|
sinon.assert.calledWith(ctx.pipeline, ctx.exportStream, ctx.res)
|
|
})
|
|
})
|
|
|
|
describe('with type=markdown', function () {
|
|
beforeEach(async function (ctx) {
|
|
ctx.projectId = 'test-project-id'
|
|
ctx.userId = 'test-user-id'
|
|
ctx.projectName = 'My Test Project'
|
|
ctx.exportStream = { pipe: sinon.stub() }
|
|
ctx.contentLength = 9876
|
|
|
|
ctx.req.params = { Project_id: ctx.projectId, type: 'markdown' }
|
|
ctx.req.session = { user: { _id: ctx.userId } }
|
|
ctx.req.ip = '192.168.1.1'
|
|
|
|
ctx.res.attachment = sinon.stub().returns(ctx.res)
|
|
|
|
ctx.SessionManager.getLoggedInUserId.returns(ctx.userId)
|
|
ctx.ProjectGetter.promises.getProject.resolves({
|
|
name: ctx.projectName,
|
|
})
|
|
ctx.DocumentConversionManager.promises.convertProjectToDocument.resolves(
|
|
{
|
|
stream: ctx.exportStream,
|
|
contentLength: ctx.contentLength,
|
|
}
|
|
)
|
|
|
|
await ctx.ProjectDownloadsController.exportProjectConversion(
|
|
ctx.req,
|
|
ctx.res,
|
|
ctx.next
|
|
)
|
|
})
|
|
|
|
it('should call convertProjectToDocument with the markdown type', function (ctx) {
|
|
sinon.assert.calledWith(
|
|
ctx.DocumentConversionManager.promises.convertProjectToDocument,
|
|
ctx.projectId,
|
|
ctx.userId,
|
|
'markdown'
|
|
)
|
|
})
|
|
|
|
it('should set the attachment filename with .zip extension', function (ctx) {
|
|
sinon.assert.calledWith(ctx.res.attachment, 'My_Test_Project.zip')
|
|
})
|
|
|
|
it('should add an audit log entry for markdown export', function (ctx) {
|
|
sinon.assert.calledWith(
|
|
ctx.ProjectAuditLogHandler.addEntryInBackground,
|
|
ctx.projectId,
|
|
'project-exported-markdown',
|
|
ctx.userId,
|
|
ctx.req.ip
|
|
)
|
|
})
|
|
|
|
it('should record the action via Metrics with markdown type', function (ctx) {
|
|
ctx.Metrics.inc
|
|
.calledWith('document-exports', 1, { type: 'markdown' })
|
|
.should.equal(true)
|
|
})
|
|
|
|
it('should stream the document to the response', function (ctx) {
|
|
sinon.assert.calledWith(ctx.pipeline, ctx.exportStream, ctx.res)
|
|
})
|
|
})
|
|
})
|
|
})
|