overleaf/services/web/test/unit/src/Project/ProjectAuditLogHandlerTests.js
Eric Mc Sween 06de9233b8 Merge pull request #2255 from overleaf/em-audit-log
Project audit logs

GitOrigin-RevId: 439add2959be140c4f56ce9b41b9f59d432c494d
2019-10-23 12:59:04 +00:00

80 lines
2.2 KiB
JavaScript

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
})
})
})
})