Skip to content

Commit

Permalink
HAAR-1742 Added correlationId to audit event (#79)
Browse files Browse the repository at this point in the history
* WIP

* config.ts

* auditService.ts

* WIP. Complete, tested, working e2e

* HAAR-1742: update package-lock.json

* build fixes

* build fixes

* build fixes

* HAAR-1742 Added correlationId to audit message

* HAAR-1742 Undid package.json changes

---------

Co-authored-by: simonmitchell <[email protected]>
  • Loading branch information
ma226860 and simon-mitchell authored Nov 6, 2023
1 parent f46f5cc commit 8ee4b1a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
26 changes: 17 additions & 9 deletions server/routes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ describe('index.test.ts', () => {
sendAuditMessage: sendAuditMessageMock,
}))

jest.mock('uuid', () => ({
v1: jest.fn(() => 'mocked-uuid'),
}))

beforeEach(() => {
jest.clearAllMocks()
jest.resetAllMocks()
app = appWithAllRoutes({})
jest.spyOn(auditService, 'sendAuditMessage').mockResolvedValue({} as never)
})
Expand All @@ -34,14 +40,6 @@ describe('index.test.ts', () => {
})

it('GET /triggered-event', () => {
const mockPublishedEvent = {
action: 'TEST_EVENT',
who: 'user1',
subjectId: 'some user ID',
subjectType: 'USER_ID',
details: JSON.stringify({ testField: 'some value' }),
}

return request(app)
.get('/triggered-event')
.expect('Content-Type', /html/)
Expand All @@ -60,12 +58,22 @@ describe('index.test.ts', () => {
'<b>Subject Type:</b> The type of subject ID we are using. This is most commonly a user ID.',
)
expect(res.text).toContain('<b>Service:</b> Which service performed this action?')
expect(res.text).toContain('<b>Correlation ID:</b> An optional ID used to link multiple correlated events.')
expect(res.text).toContain(
'<b>Details:</b> Any additional details specific to this action that may be relevant can go here.',
)
})
.expect(() => {
expect(auditService.sendAuditMessage).toBeCalledWith(mockPublishedEvent)
expect(auditService.sendAuditMessage).toBeCalledWith(
expect.objectContaining({
action: 'TEST_EVENT',
who: 'user1',
subjectId: 'some user ID',
subjectType: 'USER_ID',
correlationId: expect.stringMatching('^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'),
details: JSON.stringify({ testField: 'some value' }),
}),
)
})
})
})
2 changes: 2 additions & 0 deletions server/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type RequestHandler, Router } from 'express'

import { v1 as uuidv1 } from 'uuid'
import asyncMiddleware from '../middleware/asyncMiddleware'
import type { Services } from '../services'
import auditService from '../services/auditService'
Expand All @@ -20,6 +21,7 @@ export default function routes(service: Services): Router {
who: username,
subjectId: 'some user ID',
subjectType: 'USER_ID',
correlationId: uuidv1(),
details: JSON.stringify({ testField: 'some value' }),
})
res.render('pages/triggeredEvent', {
Expand Down
3 changes: 3 additions & 0 deletions server/services/auditService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class AuditService {
who,
subjectId,
subjectType,
correlationId,
details,
}: {
action: string
who: string
subjectId?: string
subjectType?: string
correlationId?: string
details?: string
}) {
try {
Expand All @@ -31,6 +33,7 @@ class AuditService {
who,
subjectId,
subjectType,
correlationId,
service: config.apis.audit.serviceName,
details,
})
Expand Down
1 change: 1 addition & 0 deletions server/views/pages/triggeredEvent.njk
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<b>Subject ID:</b> The subject against which the action was performed. For example, if the action being
audited was a change of John's email address, then the subject ID is John's user ID.<br>
<b>Subject Type:</b> The type of subject ID we are using. This is most commonly a user ID.<br>
<b>Correlation ID:</b> An optional ID used to link multiple correlated events.<br>
<b>Service:</b> Which service performed this action?<br>
<b>Details:</b> Any additional details specific to this action that may be relevant can go here. This can be
anything but must be in the format of a stringifed JSON.<br>
Expand Down

0 comments on commit 8ee4b1a

Please sign in to comment.