2021-02-17 06:41:06 -05:00
|
|
|
const { exec } = require('child_process')
|
|
|
|
const { promisify } = require('util')
|
|
|
|
const { expect } = require('chai')
|
2021-11-01 12:30:59 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
2021-02-17 06:41:06 -05:00
|
|
|
const { db, ObjectId } = require('../../../app/src/infrastructure/mongodb')
|
|
|
|
const User = require('./helpers/User').promises
|
|
|
|
|
|
|
|
async function getDeletedFiles(projectId) {
|
|
|
|
return (await db.projects.findOne({ _id: projectId })).deletedFiles
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setDeletedFiles(projectId, deletedFiles) {
|
|
|
|
await db.projects.updateOne({ _id: projectId }, { $set: { deletedFiles } })
|
|
|
|
}
|
|
|
|
|
|
|
|
async function unsetDeletedFiles(projectId) {
|
|
|
|
await db.projects.updateOne(
|
|
|
|
{ _id: projectId },
|
|
|
|
{ $unset: { deletedFiles: 1 } }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('BackFillDeletedFiles', function () {
|
2021-02-19 04:54:40 -05:00
|
|
|
let user, projectId1, projectId2, projectId3, projectId4, projectId5
|
2021-02-17 06:41:06 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('create projects', async function () {
|
2021-02-17 06:41:06 -05:00
|
|
|
user = new User()
|
|
|
|
await user.login()
|
|
|
|
|
|
|
|
projectId1 = ObjectId(await user.createProject('project1'))
|
|
|
|
projectId2 = ObjectId(await user.createProject('project2'))
|
|
|
|
projectId3 = ObjectId(await user.createProject('project3'))
|
|
|
|
projectId4 = ObjectId(await user.createProject('project4'))
|
2021-02-19 04:54:40 -05:00
|
|
|
projectId5 = ObjectId(await user.createProject('project5'))
|
2021-02-17 06:41:06 -05:00
|
|
|
})
|
|
|
|
|
2021-02-19 04:54:40 -05:00
|
|
|
let fileId1, fileId2, fileId3, fileId4
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('create files', function () {
|
2021-02-17 06:41:06 -05:00
|
|
|
// take a short cut and just allocate file ids
|
|
|
|
fileId1 = ObjectId()
|
|
|
|
fileId2 = ObjectId()
|
|
|
|
fileId3 = ObjectId()
|
2021-02-19 04:54:40 -05:00
|
|
|
fileId4 = ObjectId()
|
2021-02-17 06:41:06 -05:00
|
|
|
})
|
|
|
|
const otherFileDetails = {
|
|
|
|
name: 'universe.jpg',
|
|
|
|
linkedFileData: null,
|
|
|
|
hash: 'ed19e7d6779b47d8c63f6fa5a21954dcfb6cac00',
|
|
|
|
deletedAt: new Date(),
|
2021-04-27 03:52:58 -04:00
|
|
|
__v: 0,
|
2021-02-17 06:41:06 -05:00
|
|
|
}
|
2021-02-19 04:54:40 -05:00
|
|
|
let deletedFiles1, deletedFiles2, deletedFiles3
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('set deletedFiles details', async function () {
|
2021-02-17 06:41:06 -05:00
|
|
|
deletedFiles1 = [
|
|
|
|
{ _id: fileId1, ...otherFileDetails },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ _id: fileId2, ...otherFileDetails },
|
2021-02-17 06:41:06 -05:00
|
|
|
]
|
|
|
|
deletedFiles2 = [{ _id: fileId3, ...otherFileDetails }]
|
|
|
|
await setDeletedFiles(projectId1, deletedFiles1)
|
|
|
|
await setDeletedFiles(projectId2, deletedFiles2)
|
|
|
|
|
|
|
|
// a project without deletedFiles entries
|
|
|
|
await setDeletedFiles(projectId3, [])
|
|
|
|
// a project without deletedFiles array
|
|
|
|
await unsetDeletedFiles(projectId4)
|
2021-02-19 04:54:40 -05:00
|
|
|
// duplicate entry
|
|
|
|
deletedFiles3 = [
|
|
|
|
{ _id: fileId4, ...otherFileDetails },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ _id: fileId4, ...otherFileDetails },
|
2021-02-19 04:54:40 -05:00
|
|
|
]
|
|
|
|
await setDeletedFiles(projectId5, deletedFiles3)
|
2021-02-17 06:41:06 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
async function runScript(args = []) {
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
result = await promisify(exec)(
|
|
|
|
['LET_USER_DOUBLE_CHECK_INPUTS_FOR=1']
|
|
|
|
.concat(['node', 'scripts/back_fill_deleted_files'])
|
|
|
|
.concat(args)
|
|
|
|
.join(' ')
|
|
|
|
)
|
|
|
|
} catch (error) {
|
|
|
|
// dump details like exit code, stdErr and stdOut
|
|
|
|
logger.error({ error }, 'script failed')
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
const { stderr: stdErr, stdout: stdOut } = result
|
|
|
|
expect(stdOut).to.include(projectId1.toString())
|
|
|
|
expect(stdOut).to.include(projectId2.toString())
|
|
|
|
|
2021-02-19 04:54:40 -05:00
|
|
|
expect(stdErr).to.include(`Completed batch ending ${projectId5}`)
|
2021-02-17 06:41:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkAreFilesBackFilled() {
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should back fill file and set projectId', async function () {
|
2021-02-17 08:56:48 -05:00
|
|
|
const docs = await db.deletedFiles
|
|
|
|
.find({}, { sort: { _id: 1 } })
|
|
|
|
.toArray()
|
2021-02-17 06:41:06 -05:00
|
|
|
expect(docs).to.deep.equal([
|
|
|
|
{ _id: fileId1, projectId: projectId1, ...otherFileDetails },
|
|
|
|
{ _id: fileId2, projectId: projectId1, ...otherFileDetails },
|
2021-02-19 04:54:40 -05:00
|
|
|
{ _id: fileId3, projectId: projectId2, ...otherFileDetails },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ _id: fileId4, projectId: projectId5, ...otherFileDetails },
|
2021-02-17 06:41:06 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('back fill only', function () {
|
2021-02-17 06:41:06 -05:00
|
|
|
beforeEach('run script', runScript)
|
|
|
|
|
|
|
|
checkAreFilesBackFilled()
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should leave the deletedFiles as is', async function () {
|
2021-02-17 06:41:06 -05:00
|
|
|
expect(await getDeletedFiles(projectId1)).to.deep.equal(deletedFiles1)
|
|
|
|
expect(await getDeletedFiles(projectId2)).to.deep.equal(deletedFiles2)
|
2021-02-19 04:54:40 -05:00
|
|
|
expect(await getDeletedFiles(projectId5)).to.deep.equal(deletedFiles3)
|
2021-02-17 06:41:06 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('back fill and cleanup', function () {
|
|
|
|
beforeEach('run script with cleanup flag', async function () {
|
2021-02-17 06:41:06 -05:00
|
|
|
await runScript(['--perform-cleanup'])
|
|
|
|
})
|
|
|
|
|
|
|
|
checkAreFilesBackFilled()
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should cleanup the deletedFiles', async function () {
|
2021-02-17 06:41:06 -05:00
|
|
|
expect(await getDeletedFiles(projectId1)).to.deep.equal([])
|
|
|
|
expect(await getDeletedFiles(projectId2)).to.deep.equal([])
|
2021-02-19 04:54:40 -05:00
|
|
|
expect(await getDeletedFiles(projectId5)).to.deep.equal([])
|
2021-02-17 06:41:06 -05:00
|
|
|
})
|
|
|
|
})
|
2021-04-01 04:18:28 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('fix partial inserts and cleanup', function () {
|
|
|
|
beforeEach('simulate one missing insert', async function () {
|
2021-04-01 04:18:28 -04:00
|
|
|
await setDeletedFiles(projectId1, [deletedFiles1[0]])
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('run script with cleanup flag', async function () {
|
2021-04-01 04:18:28 -04:00
|
|
|
await runScript(['--perform-cleanup'])
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('add case for one missing file', async function () {
|
2021-04-01 04:18:28 -04:00
|
|
|
await setDeletedFiles(projectId1, deletedFiles1)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('add cases for no more files to insert', async function () {
|
2021-04-01 04:18:28 -04:00
|
|
|
await setDeletedFiles(projectId2, deletedFiles2)
|
|
|
|
await setDeletedFiles(projectId5, deletedFiles3)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach('fixing partial insert and cleanup', async function () {
|
2021-04-01 04:18:28 -04:00
|
|
|
await runScript(['--fix-partial-inserts', '--perform-cleanup'])
|
|
|
|
})
|
|
|
|
|
|
|
|
checkAreFilesBackFilled()
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should cleanup the deletedFiles', async function () {
|
2021-04-01 04:18:28 -04:00
|
|
|
expect(await getDeletedFiles(projectId1)).to.deep.equal([])
|
|
|
|
expect(await getDeletedFiles(projectId2)).to.deep.equal([])
|
|
|
|
expect(await getDeletedFiles(projectId5)).to.deep.equal([])
|
|
|
|
})
|
|
|
|
})
|
2021-02-17 06:41:06 -05:00
|
|
|
})
|