Skip to content

Commit 8ee4b1a

Browse files
HAAR-1742 Added correlationId to audit event (#79)
* 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]>
1 parent f46f5cc commit 8ee4b1a

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

server/routes/index.test.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ describe('index.test.ts', () => {
1616
sendAuditMessage: sendAuditMessageMock,
1717
}))
1818

19+
jest.mock('uuid', () => ({
20+
v1: jest.fn(() => 'mocked-uuid'),
21+
}))
22+
1923
beforeEach(() => {
24+
jest.clearAllMocks()
25+
jest.resetAllMocks()
2026
app = appWithAllRoutes({})
2127
jest.spyOn(auditService, 'sendAuditMessage').mockResolvedValue({} as never)
2228
})
@@ -34,14 +40,6 @@ describe('index.test.ts', () => {
3440
})
3541

3642
it('GET /triggered-event', () => {
37-
const mockPublishedEvent = {
38-
action: 'TEST_EVENT',
39-
who: 'user1',
40-
subjectId: 'some user ID',
41-
subjectType: 'USER_ID',
42-
details: JSON.stringify({ testField: 'some value' }),
43-
}
44-
4543
return request(app)
4644
.get('/triggered-event')
4745
.expect('Content-Type', /html/)
@@ -60,12 +58,22 @@ describe('index.test.ts', () => {
6058
'<b>Subject Type:</b> The type of subject ID we are using. This is most commonly a user ID.',
6159
)
6260
expect(res.text).toContain('<b>Service:</b> Which service performed this action?')
61+
expect(res.text).toContain('<b>Correlation ID:</b> An optional ID used to link multiple correlated events.')
6362
expect(res.text).toContain(
6463
'<b>Details:</b> Any additional details specific to this action that may be relevant can go here.',
6564
)
6665
})
6766
.expect(() => {
68-
expect(auditService.sendAuditMessage).toBeCalledWith(mockPublishedEvent)
67+
expect(auditService.sendAuditMessage).toBeCalledWith(
68+
expect.objectContaining({
69+
action: 'TEST_EVENT',
70+
who: 'user1',
71+
subjectId: 'some user ID',
72+
subjectType: 'USER_ID',
73+
correlationId: expect.stringMatching('^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'),
74+
details: JSON.stringify({ testField: 'some value' }),
75+
}),
76+
)
6977
})
7078
})
7179
})

server/routes/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type RequestHandler, Router } from 'express'
22

3+
import { v1 as uuidv1 } from 'uuid'
34
import asyncMiddleware from '../middleware/asyncMiddleware'
45
import type { Services } from '../services'
56
import auditService from '../services/auditService'
@@ -20,6 +21,7 @@ export default function routes(service: Services): Router {
2021
who: username,
2122
subjectId: 'some user ID',
2223
subjectType: 'USER_ID',
24+
correlationId: uuidv1(),
2325
details: JSON.stringify({ testField: 'some value' }),
2426
})
2527
res.render('pages/triggeredEvent', {

server/services/auditService.ts

+3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ class AuditService {
1616
who,
1717
subjectId,
1818
subjectType,
19+
correlationId,
1920
details,
2021
}: {
2122
action: string
2223
who: string
2324
subjectId?: string
2425
subjectType?: string
26+
correlationId?: string
2527
details?: string
2628
}) {
2729
try {
@@ -31,6 +33,7 @@ class AuditService {
3133
who,
3234
subjectId,
3335
subjectType,
36+
correlationId,
3437
service: config.apis.audit.serviceName,
3538
details,
3639
})

server/views/pages/triggeredEvent.njk

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<b>Subject ID:</b> The subject against which the action was performed. For example, if the action being
2424
audited was a change of John's email address, then the subject ID is John's user ID.<br>
2525
<b>Subject Type:</b> The type of subject ID we are using. This is most commonly a user ID.<br>
26+
<b>Correlation ID:</b> An optional ID used to link multiple correlated events.<br>
2627
<b>Service:</b> Which service performed this action?<br>
2728
<b>Details:</b> Any additional details specific to this action that may be relevant can go here. This can be
2829
anything but must be in the format of a stringifed JSON.<br>

0 commit comments

Comments
 (0)