Files
Verso/services/web/test/unit/src/User/UserAuditLogHandlerTests.js
T
Jessica Lawshe 8f773318c1 Merge pull request #3023 from overleaf/jel-add-user-audit-log
User audit log

GitOrigin-RevId: 687448d5da5d783c6db0fadb53c020cc9c3876b9
2020-07-22 02:06:16 +00:00

82 lines
2.0 KiB
JavaScript

const sinon = require('sinon')
const { expect } = require('chai')
const { ObjectId } = require('mongodb')
const SandboxedModule = require('sandboxed-module')
const { User } = require('../helpers/models/User')
const MODULE_PATH = '../../../../app/src/Features/User/UserAuditLogHandler'
describe('UserAuditLogHandler', function() {
beforeEach(function() {
this.userId = ObjectId()
this.initiatorId = ObjectId()
this.action = {
operation: 'clear-sessions',
initiatorId: this.initiatorId,
info: {
sessions: [
{
ip_address: '0:0:0:0',
session_created: '2020-07-15T16:07:57.652Z'
}
]
},
ip: '0:0:0:0'
}
this.UserMock = sinon.mock(User)
this.UserAuditLogHandler = SandboxedModule.require(MODULE_PATH, {
globals: {
console: console
},
requires: {
'../../models/User': { User }
}
})
})
afterEach(function() {
this.UserMock.restore()
})
describe('addEntry', function() {
describe('success', function() {
beforeEach(async function() {
this.dbUpdate = this.UserMock.expects('updateOne')
.chain('exec')
.resolves({ nModified: 1 })
await this.UserAuditLogHandler.promises.addEntry(
this.userId,
this.action.operation,
this.action.initiatorId,
this.action.ip,
this.action.info
)
})
it('writes a log', async function() {
this.UserMock.verify()
})
})
describe('when the user does not exist', function() {
beforeEach(function() {
this.UserMock.expects('updateOne')
.chain('exec')
.resolves({ nModified: 0 })
})
it('throws an error', async function() {
await expect(
this.UserAuditLogHandler.promises.addEntry(
this.userId,
this.action.operation,
this.action.initiatorId,
this.action.ip,
this.action.info
)
).to.be.rejected
})
})
})
})