Skip to content

Commit 534f0e8

Browse files
authored
Merge pull request #942 from govuk-one-login/FPAD-7912-remove-old-handling-account-deletions-processor
FPAD-7912: Remove handling for unwrapped deletion messages
2 parents 64d5bd5 + 328f0a9 commit 534f0e8

3 files changed

Lines changed: 35 additions & 41 deletions

File tree

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { z } from 'zod';
22

3-
export const accountDeleteMessageSchema = z.union([
4-
z.object({
5-
event_name: z.literal('AUTH_DELETE_ACCOUNT'),
6-
user: z.object({ user_id: z.string().optional() }),
3+
export const accountDeleteMessageSchema = z.object({
4+
event_name: z.literal('AUTH_DELETE_ACCOUNT'),
5+
user: z.object({
6+
user_id: z.string().trim().min(1, {
7+
message: 'String cannot be empty or just spaces',
8+
}),
79
}),
8-
z.object({
9-
event_name: z.literal('AUTH_DELETE_ACCOUNT'),
10-
user_id: z.string().optional(),
11-
}),
12-
]);
10+
});
1311

1412
export type AccountDeleteMessage = z.infer<typeof accountDeleteMessageSchema>;

src/handlers/account-deletion-processor-handler.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ import { accountDeleteMessageSchema } from '../contracts/account-delete-message'
88
import { prettifyError } from 'zod';
99
import jsonSafeParse from '../commons/json-safe-parse';
1010

11-
enum ErrorSeverity {
12-
error = 'error',
13-
warn = 'warn',
14-
}
15-
1611
enum ParserErrorType {
1712
BODY_JSON_PARSER_ERROR = 'BODY_JSON_PARSER_ERROR',
1813
BODY_FORMAT_PARSER_ERROR = 'BODY_FORMAT_PARSER_ERROR',
@@ -24,7 +19,6 @@ class ParserError extends Error {
2419
constructor(
2520
public readonly errorType: ParserErrorType,
2621
message?: string,
27-
public readonly severity: ErrorSeverity = ErrorSeverity.error,
2822
) {
2923
super(message ?? 'The SQS message can not be parsed.');
3024
this.name = 'ParserError';
@@ -60,7 +54,7 @@ export async function handler(event: SQSEvent, context: Context): Promise<void>
6054
* @param record - The record passed in from the event.
6155
* @returns - User ID or undefined
6256
*/
63-
function parseMessage(record: SQSRecord): string | undefined {
57+
function parseMessage(record: SQSRecord): string {
6458
// JSON parse record.body
6559
const recordBodyResult = jsonSafeParse(record.body);
6660
if (!recordBodyResult.success) throw new ParserError(ParserErrorType.BODY_JSON_PARSER_ERROR);
@@ -73,11 +67,7 @@ function parseMessage(record: SQSRecord): string | undefined {
7367
`The SQS message can not be parsed. ${prettifyError(result.error)}`,
7468
);
7569

76-
if ('user' in result.data) return result.data.user.user_id;
77-
78-
addMetric(MetricNames.DELETE_EVENT_UNWRAPPED);
79-
80-
return result.data.user_id;
70+
return result.data.user.user_id;
8171
}
8272

8373
/**
@@ -87,17 +77,10 @@ function parseMessage(record: SQSRecord): string | undefined {
8777
*/
8878
function getUserId(record: SQSRecord) {
8979
try {
90-
const userId = parseMessage(record);
91-
92-
if (userId === undefined)
93-
throw new ParserError(ParserErrorType.MISSING_USER_ID, 'Attribute missing: user_id.', ErrorSeverity.warn);
94-
if (userId.trim() === '')
95-
throw new ParserError(ParserErrorType.EMPTY_USER_ID, 'Attribute invalid: user_id is empty.', ErrorSeverity.warn);
96-
97-
return userId.trim();
80+
return parseMessage(record);
9881
} catch (error) {
9982
if (error instanceof ParserError) {
100-
logger[error.severity](error.message);
83+
logger.error(error.message);
10184
addMetric(MetricNames.DELETE_EVENT_PARSER_ERROR, undefined, undefined, {
10285
ERROR: error.errorType,
10386
});

src/handlers/tests/account-deletion-processor-handler.test.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const mockAddDimensions = Metrics.prototype.addDimensions as Mock;
2424
const mockSerializeMetrics = Metrics.prototype.serializeMetrics as Mock;
2525

2626
const loggerErrorSpy = vi.spyOn(logger, 'error');
27-
const loggerWarnSpy = vi.spyOn(logger, 'warn');
2827

2928
describe('Account Deletion Processor', () => {
3029
let mockEvent: SQSEvent;
@@ -110,46 +109,58 @@ describe('Account Deletion Processor', () => {
110109
mockRecord = { ...mockRecord, body: mockBody };
111110
mockEvent = { Records: [mockRecord] };
112111
await handler(mockEvent, mockContext);
113-
expect(loggerErrorSpy).toHaveBeenCalledWith('The SQS message can not be parsed. ✖ Invalid input');
112+
expect(loggerErrorSpy)
113+
.toHaveBeenCalledWith(`The SQS message can not be parsed. ✖ Invalid input: expected "AUTH_DELETE_ACCOUNT"
114+
→ at event_name
115+
✖ Invalid input: expected object, received undefined
116+
→ at user`);
114117
});
115118

116119
it("does not process the SQS Record when the message doesn't contain user id", async () => {
117120
const mockBody = JSON.stringify({ event_name: 'AUTH_DELETE_ACCOUNT' });
118121
mockRecord = { ...mockRecord, body: mockBody };
119122
mockEvent = { Records: [mockRecord] };
120123
await handler(mockEvent, mockContext);
121-
expect(loggerWarnSpy).toHaveBeenCalledWith('Attribute missing: user_id.');
124+
expect(loggerErrorSpy)
125+
.toHaveBeenCalledWith(`The SQS message can not be parsed. ✖ Invalid input: expected object, received undefined
126+
→ at user`);
122127
});
123128

124129
it('does not process the SQS Record when user_id is an empty string in the SNS Event', async () => {
125-
const mockBody = JSON.stringify({ event_name: 'AUTH_DELETE_ACCOUNT', user_id: '' });
130+
const mockBody = JSON.stringify({ event_name: 'AUTH_DELETE_ACCOUNT', user: { user_id: '' } });
126131
mockRecord = { ...mockRecord, body: mockBody };
127132
mockEvent = { Records: [mockRecord] };
128133
await handler(mockEvent, mockContext);
129-
expect(loggerWarnSpy).toHaveBeenCalledWith('Attribute invalid: user_id is empty.');
134+
expect(loggerErrorSpy)
135+
.toHaveBeenCalledWith(`The SQS message can not be parsed. ✖ String cannot be empty or just spaces
136+
→ at user.user_id`);
130137
});
131138

132139
it('does not process the SQS Record when user_id is a string with whitespaces in the SNS Event and tests the trim function', async () => {
133-
const mockBody = JSON.stringify({ event_name: 'AUTH_DELETE_ACCOUNT', user_id: ' ' });
140+
const mockBody = JSON.stringify({ event_name: 'AUTH_DELETE_ACCOUNT', user: { user_id: ' ' } });
134141
mockRecord = { ...mockRecord, body: mockBody };
135142
mockEvent = { Records: [mockRecord] };
136143
await handler(mockEvent, mockContext);
137-
expect(loggerWarnSpy).toHaveBeenCalledWith('Attribute invalid: user_id is empty.');
144+
expect(loggerErrorSpy)
145+
.toHaveBeenCalledWith(`The SQS message can not be parsed. ✖ String cannot be empty or just spaces
146+
→ at user.user_id`);
138147
});
139148

140149
it('does not process the SQS Record when user_id is not a string', async () => {
141-
const mockBody = JSON.stringify({ event_name: 'AUTH_DELETE_ACCOUNT', user_id: 123 });
150+
const mockBody = JSON.stringify({ event_name: 'AUTH_DELETE_ACCOUNT', user: { user_id: 123 } });
142151
mockRecord = { ...mockRecord, body: mockBody };
143152
mockEvent = { Records: [mockRecord] };
144153
await handler(mockEvent, mockContext);
145-
expect(loggerErrorSpy).toHaveBeenCalledWith(`The SQS message can not be parsed. ✖ Invalid input`);
154+
expect(loggerErrorSpy)
155+
.toHaveBeenCalledWith(`The SQS message can not be parsed. ✖ Invalid input: expected string, received number
156+
→ at user.user_id`);
146157
});
147158

148159
it('successfully processes the message when the user id passed contains trailing spaces', async () => {
149160
mockDynamoDBServiceUpdateDeleteStatus.mockReturnValueOnce(['1']);
150161
const mockBody = JSON.stringify({
151162
event_name: 'AUTH_DELETE_ACCOUNT',
152-
user_id: 'abcdef ',
163+
user: { user_id: 'abcdef ' },
153164
});
154165
mockRecord = { ...mockRecord, body: mockBody };
155166
mockEvent = { Records: [mockRecord] };
@@ -241,7 +252,9 @@ describe('Account Deletion Processor', () => {
241252
...mockRecord,
242253
body: JSON.stringify({
243254
event_name: 'AUTH_DELETE_ACCOUNT',
244-
user_id: 'other_user_id',
255+
user: {
256+
user_id: 'other_user_id',
257+
},
245258
}),
246259
};
247260
const mockEvent = { Records: [mockRecord, mockRecord2] };

0 commit comments

Comments
 (0)