Files
overleaf-cep/services/web/test/unit/src/User/UserAuditLogHandlerTests.js
T
Timothée Alby 3babf23444 Merge pull request #3091 from overleaf/jel-log-default-email-change
Update audit log for default email changes

GitOrigin-RevId: c7b4e4e888aa5ffd976062d72f660ded303f0885
2020-08-13 02:06:40 +00:00

134 lines
3.6 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(function() {
this.dbUpdate = this.UserMock.expects('updateOne')
.chain('exec')
.resolves({ nModified: 1 })
})
it('writes a log', async function() {
await this.UserAuditLogHandler.promises.addEntry(
this.userId,
this.action.operation,
this.action.initiatorId,
this.action.ip,
this.action.info
)
this.UserMock.verify()
})
it('updates the log for password reset operation witout a initiatorId', async function() {
await expect(
this.UserAuditLogHandler.promises.addEntry(
this.userId,
'reset-password',
undefined,
this.action.ip,
this.action.info
)
)
this.UserMock.verify()
})
})
describe('errors', function() {
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
})
})
describe('missing parameters', function() {
it('throws an error when no operation', async function() {
await expect(
this.UserAuditLogHandler.promises.addEntry(
this.userId,
undefined,
this.action.initiatorId,
this.action.ip,
this.action.info
)
).to.be.rejected
})
it('throws an error when no IP', async function() {
await expect(
this.UserAuditLogHandler.promises.addEntry(
this.userId,
this.action.operation,
this.action.initiatorId,
undefined,
this.action.info
)
).to.be.rejected
})
it('throws an error when no initiatorId and not a password reset operation', async function() {
await expect(
this.UserAuditLogHandler.promises.addEntry(
this.userId,
this.action.operation,
undefined,
this.action.ip,
this.action.info
)
).to.be.rejected
})
})
})
})
})