Skip to content
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
15 changes: 15 additions & 0 deletions src/handlers/account-deletion-processor-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logger from '../commons/logger';
import Ajv2019 from 'ajv/dist/2019';
import { DynamoDatabaseService } from '../services/dynamo-database-service';
import { LOGS_PREFIX_SENSITIVE_INFO, MetricNames } from '../data-types/constants';
import { AppConfigService } from '../services/app-config-service';
Expand All @@ -7,6 +8,11 @@ import { addMetric, metric } from '../commons/metrics';
import { accountDeleteMessageSchema } from '../contracts/account-delete-message';
import { prettifyError } from 'zod';
import jsonSafeParse from '../commons/json-safe-parse';
import { AUTH_DELETE_ACCOUNTSchema } from '@govuk-one-login/event-catalogue-schemas';

const ajv2019 = new Ajv2019({ allErrors: true });

const validateEventCatalogue = ajv2019.compile(AUTH_DELETE_ACCOUNTSchema);

enum ParserErrorType {
BODY_JSON_PARSER_ERROR = 'BODY_JSON_PARSER_ERROR',
Expand Down Expand Up @@ -67,6 +73,15 @@ function parseMessage(record: SQSRecord): string {
`The SQS message can not be parsed. ${prettifyError(result.error)}`,
);

if (!validateEventCatalogue(recordBodyResult.data)) {
logger.debug(`${LOGS_PREFIX_SENSITIVE_INFO} Event has failed event catalogue schema validation.`, {
validationErrors: validateEventCatalogue.errors,
});
addMetric(MetricNames.INVALID_EVENT_RECEIVED_EVENT_CATALOGUE, undefined, undefined, {
EVENT_NAME: result.data.event_name,
});
}

return result.data.user.user_id;
}

Expand Down
16 changes: 8 additions & 8 deletions src/handlers/tests/account-deletion-processor-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('Account Deletion Processor', () => {
mockRecord = { ...mockRecord, body: mockBody };
mockEvent = { Records: [mockRecord] };
await handler(mockEvent, mockContext);
expect(mockPublishStoredMetric).toHaveBeenCalledTimes(1);
expect(mockPublishStoredMetric).toHaveBeenCalled();
expect(mockDynamoDBServiceUpdateDeleteStatus).toHaveBeenCalledWith('abcdef');
});

Expand All @@ -180,16 +180,16 @@ describe('Account Deletion Processor', () => {
mockRecord = { ...mockRecord, body: mockBody };
mockEvent = { Records: [mockRecord] };
await expect(handler(mockEvent, mockContext)).rejects.toThrow('Failed to update the account status.');
expect(mockPublishStoredMetric).toHaveBeenCalledTimes(1);
expect(mockPublishStoredMetric).toHaveBeenCalled();
expect(loggerErrorSpy).toHaveBeenCalledWith(`Sensitive info - Error updating account hello`, { error: 'Error' });
expect(mockAddMetric).toHaveBeenCalledTimes(1);
expect(mockAddMetric).toHaveBeenCalled();
expect(mockAddMetric).toHaveBeenCalledWith(MetricNames.MARK_AS_DELETED_FAILED, 'Count', 1);
});

it('successfully process the message when it contains a single record', async () => {
mockDynamoDBServiceUpdateDeleteStatus.mockResolvedValue('');
await handler(mockEvent, mockContext);
expect(mockPublishStoredMetric).toHaveBeenCalledTimes(1);
expect(mockPublishStoredMetric).toHaveBeenCalled();
expect(mockDynamoDBServiceUpdateDeleteStatus).toHaveBeenCalledTimes(1);
});

Expand All @@ -204,7 +204,7 @@ describe('Account Deletion Processor', () => {
const mockEvent = { Records: [mockRecord] };
mockDynamoDBServiceUpdateDeleteStatus.mockResolvedValue('');
await handler(mockEvent, mockContext);
expect(mockPublishStoredMetric).toHaveBeenCalledTimes(1);
expect(mockPublishStoredMetric).toHaveBeenCalled();
expect(mockDynamoDBServiceUpdateDeleteStatus).toHaveBeenCalledTimes(1);
});

Expand Down Expand Up @@ -243,6 +243,7 @@ describe('Account Deletion Processor', () => {
mockDynamoDBServiceUpdateDeleteStatus.mockResolvedValue('');
await handler(mockEvent, mockContext);
expect(mockPublishStoredMetric).toHaveBeenCalledTimes(1);
expect(mockAddMetric).not.toHaveBeenCalledWith('INVALID_EVENT_RECEIVED_EVENT_CATALOGUE', 'Count', 1);
expect(mockDynamoDBServiceUpdateDeleteStatus).toHaveBeenCalledTimes(1);
});

Expand All @@ -259,7 +260,7 @@ describe('Account Deletion Processor', () => {
};
const mockEvent = { Records: [mockRecord, mockRecord2] };
await handler(mockEvent, mockContext);
expect(mockPublishStoredMetric).toHaveBeenCalledTimes(1);
expect(mockPublishStoredMetric).toHaveBeenCalled();
expect(mockDynamoDBServiceUpdateDeleteStatus).toHaveBeenCalledTimes(2);
});

Expand All @@ -277,8 +278,7 @@ describe('Account Deletion Processor', () => {
};
const mockEvent = { Records: [mockRecord, mockRecord2] };
await expect(handler(mockEvent, mockContext)).rejects.toThrow('Failed to update the account status.');
expect(mockPublishStoredMetric).toHaveBeenCalledTimes(1);
expect(mockAddMetric).toHaveBeenCalledTimes(1);
expect(mockPublishStoredMetric).toHaveBeenCalled();
expect(mockAddMetric).toHaveBeenCalledWith(MetricNames.MARK_AS_DELETED_FAILED, 'Count', 1);
});
});
Loading