Merge pull request #9617 from overleaf/msm-audit-log-collections

Move project/user audit logs to their own collections

GitOrigin-RevId: f6f89b3e2815c0fe5691a79eceb35b77b3c370d8
This commit is contained in:
Miguel Serrano
2022-09-29 11:47:50 +02:00
committed by Copybot
parent 9c7aa36e7e
commit 694cb665da
19 changed files with 298 additions and 156 deletions
@@ -1,80 +0,0 @@
const sinon = require('sinon')
const { expect } = require('chai')
const { ObjectId } = require('mongodb')
const SandboxedModule = require('sandboxed-module')
const { Project } = require('../helpers/models/Project')
const MODULE_PATH =
'../../../../app/src/Features/Project/ProjectAuditLogHandler'
describe('ProjectAuditLogHandler', function () {
beforeEach(function () {
this.projectId = ObjectId()
this.userId = ObjectId()
this.ProjectMock = sinon.mock(Project)
this.ProjectAuditLogHandler = SandboxedModule.require(MODULE_PATH, {
requires: {
'../../models/Project': { Project },
},
})
})
afterEach(function () {
this.ProjectMock.restore()
})
describe('addEntry', function () {
describe('success', function () {
beforeEach(async function () {
this.dbUpdate = this.ProjectMock.expects('updateOne').withArgs(
{ _id: this.projectId },
{
$push: {
auditLog: {
$each: [
{
operation: 'translate',
initiatorId: this.userId,
info: { destinationLanguage: 'tagalog' },
timestamp: sinon.match.typeOf('date'),
},
],
$slice: -200,
},
},
}
)
this.dbUpdate.chain('exec').resolves({ nModified: 1 })
this.operationId = await this.ProjectAuditLogHandler.promises.addEntry(
this.projectId,
'translate',
this.userId,
{ destinationLanguage: 'tagalog' }
)
})
it('writes a log', async function () {
this.ProjectMock.verify()
})
})
describe('when the project does not exist', function () {
beforeEach(function () {
this.ProjectMock.expects('updateOne')
.chain('exec')
.resolves({ nModified: 0 })
})
it('throws an error', async function () {
await expect(
this.ProjectAuditLogHandler.promises.addEntry(
this.projectId,
'translate',
this.userId,
{ destinationLanguage: 'tagalog' }
)
).to.be.rejected
})
})
})
})
@@ -140,7 +140,9 @@ describe('ProjectDeleter', function () {
destroyProject: sinon.stub().resolves(),
},
}
this.ProjectAuditLogEntry = {
deleteMany: sinon.stub().returns({ exec: sinon.stub().resolves() }),
}
this.ProjectDeleter = SandboxedModule.require(modulePath, {
requires: {
'../../infrastructure/Features': this.Features,
@@ -160,6 +162,9 @@ describe('ProjectDeleter', function () {
'./ProjectDetailsHandler': this.ProjectDetailsHandler,
'../../infrastructure/mongodb': { db: this.db, ObjectId },
'../History/HistoryManager': this.HistoryManager,
'../../models/ProjectAuditLogEntry': {
ProjectAuditLogEntry: this.ProjectAuditLogEntry,
},
},
})
})
@@ -500,6 +505,12 @@ describe('ProjectDeleter', function () {
this.ChatApiHandler.promises.destroyProject
).to.have.been.calledWith(this.deletedProjects[0].project._id)
})
it('should delete audit logs', async function () {
expect(this.ProjectAuditLogEntry.deleteMany).to.have.been.calledWith({
projectId: this.deletedProjects[0].project._id,
})
})
})
describe('when history-v1 is not available', function () {