Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAAR-1742 Added correlationId to audit event #79

Merged
merged 11 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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