2024-10-14 06:41:11 -04:00
|
|
|
import { exec } from 'child_process'
|
|
|
|
import { promisify } from 'util'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import logger from '@overleaf/logger'
|
|
|
|
import { db, ObjectId } from '../../../app/src/infrastructure/mongodb.js'
|
|
|
|
import UserHelper from './helpers/User.js'
|
|
|
|
|
|
|
|
const User = UserHelper.promises
|
2021-04-06 07:13:28 -04:00
|
|
|
|
|
|
|
async function getDeletedDocs(projectId) {
|
|
|
|
return (await db.projects.findOne({ _id: projectId })).deletedDocs
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setDeletedDocs(projectId, deletedDocs) {
|
|
|
|
await db.projects.updateOne({ _id: projectId }, { $set: { deletedDocs } })
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('BackFillDocNameForDeletedDocs', function () {
|
2021-04-06 07:13:28 -04:00
|
|
|
let user, projectId1, projectId2, docId1, docId2, docId3
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('create projects', async function () {
|
2021-04-06 07:13:28 -04:00
|
|
|
user = new User()
|
|
|
|
await user.login()
|
|
|
|
|
2023-12-18 05:54:01 -05:00
|
|
|
projectId1 = new ObjectId(await user.createProject('project1'))
|
|
|
|
projectId2 = new ObjectId(await user.createProject('project2'))
|
2021-04-06 07:13:28 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('create docs', async function () {
|
2023-12-18 05:54:01 -05:00
|
|
|
docId1 = new ObjectId(
|
2021-04-06 07:13:28 -04:00
|
|
|
await user.createDocInProject(projectId1, null, 'doc1.tex')
|
|
|
|
)
|
2023-12-18 05:54:01 -05:00
|
|
|
docId2 = new ObjectId(
|
2021-04-06 07:13:28 -04:00
|
|
|
await user.createDocInProject(projectId1, null, 'doc2.tex')
|
|
|
|
)
|
2023-12-18 05:54:01 -05:00
|
|
|
docId3 = new ObjectId(
|
2021-04-06 07:13:28 -04:00
|
|
|
await user.createDocInProject(projectId2, null, 'doc3.tex')
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('deleted docs', async function () {
|
2021-04-06 07:13:28 -04:00
|
|
|
await user.deleteItemInProject(projectId1, 'doc', docId1)
|
|
|
|
await user.deleteItemInProject(projectId1, 'doc', docId2)
|
|
|
|
await user.deleteItemInProject(projectId2, 'doc', docId3)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('insert doc stubs into docs collection', async function () {
|
2021-04-06 07:13:28 -04:00
|
|
|
await db.docs.insertMany([
|
|
|
|
{ _id: docId1, deleted: true },
|
|
|
|
{ _id: docId2, deleted: true },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ _id: docId3, deleted: true },
|
2021-04-06 07:13:28 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
let deletedDocs1, deletedDocs2
|
|
|
|
let deletedAt1, deletedAt2, deletedAt3
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('set deletedDocs details', async function () {
|
2021-04-06 07:13:28 -04:00
|
|
|
deletedAt1 = new Date()
|
|
|
|
deletedAt2 = new Date()
|
|
|
|
deletedAt3 = new Date()
|
|
|
|
deletedDocs1 = [
|
|
|
|
{ _id: docId1, name: 'doc1.tex', deletedAt: deletedAt1 },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ _id: docId2, name: 'doc2.tex', deletedAt: deletedAt2 },
|
2021-04-06 07:13:28 -04:00
|
|
|
]
|
|
|
|
deletedDocs2 = [{ _id: docId3, name: 'doc3.tex', deletedAt: deletedAt3 }]
|
|
|
|
await setDeletedDocs(projectId1, deletedDocs1)
|
|
|
|
await setDeletedDocs(projectId2, deletedDocs2)
|
|
|
|
})
|
|
|
|
|
|
|
|
async function runScript(args = []) {
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
result = await promisify(exec)(
|
|
|
|
['LET_USER_DOUBLE_CHECK_INPUTS_FOR=1']
|
2024-10-18 07:04:57 -04:00
|
|
|
.concat(['node', 'scripts/back_fill_doc_name_for_deleted_docs.mjs'])
|
2021-04-06 07:13:28 -04:00
|
|
|
.concat(args)
|
|
|
|
.join(' ')
|
|
|
|
)
|
|
|
|
} catch (error) {
|
|
|
|
// dump details like exit code, stdErr and stdOut
|
|
|
|
logger.error({ error }, 'script failed')
|
|
|
|
throw error
|
|
|
|
}
|
2022-06-08 08:48:58 -04:00
|
|
|
const { stderr: stdErr } = result
|
2021-04-06 07:13:28 -04:00
|
|
|
|
|
|
|
expect(stdErr).to.include(`Completed batch ending ${projectId2}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkDocsBackFilled() {
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should back fill names and deletedAt dates into docs', async function () {
|
2021-04-06 07:13:28 -04:00
|
|
|
const docs = await db.docs.find({}).toArray()
|
|
|
|
expect(docs).to.deep.equal([
|
|
|
|
{ _id: docId1, deleted: true, name: 'doc1.tex', deletedAt: deletedAt1 },
|
|
|
|
{ _id: docId2, deleted: true, name: 'doc2.tex', deletedAt: deletedAt2 },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ _id: docId3, deleted: true, name: 'doc3.tex', deletedAt: deletedAt3 },
|
2021-04-06 07:13:28 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('back fill only', function () {
|
2021-04-06 07:13:28 -04:00
|
|
|
beforeEach('run script', runScript)
|
|
|
|
|
|
|
|
checkDocsBackFilled()
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should leave the deletedDocs as is', async function () {
|
2021-04-06 07:13:28 -04:00
|
|
|
expect(await getDeletedDocs(projectId1)).to.deep.equal(deletedDocs1)
|
|
|
|
expect(await getDeletedDocs(projectId2)).to.deep.equal(deletedDocs2)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('back fill and cleanup', function () {
|
|
|
|
beforeEach('run script with cleanup flag', async function () {
|
2021-04-06 07:13:28 -04:00
|
|
|
await runScript(['--perform-cleanup'])
|
|
|
|
})
|
|
|
|
|
|
|
|
checkDocsBackFilled()
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should cleanup the deletedDocs', async function () {
|
2021-04-06 07:13:28 -04:00
|
|
|
expect(await getDeletedDocs(projectId1)).to.deep.equal([])
|
|
|
|
expect(await getDeletedDocs(projectId2)).to.deep.equal([])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|