diff --git a/test-resources/src/dequeue/dequeueHandler.ts b/test-resources/src/dequeue/dequeueHandler.ts index e8f9367c..236f69dd 100644 --- a/test-resources/src/dequeue/dequeueHandler.ts +++ b/test-resources/src/dequeue/dequeueHandler.ts @@ -53,7 +53,9 @@ export const lambdaHandlerConstructor = async ( continue; } const eventName = getEventResult.value.event_name; - const sessionId = getEventResult.value.user.session_id; + + //Refactor line below - shouldn't need to check if this is unknown in the handler. + const sessionId = getEventResult.value.user?.session_id ?? "UNKNOWN"; const { timestamp } = getEventResult.value; const timeToLiveInSeconds = getTimeToLiveInSeconds( diff --git a/test-resources/src/dequeue/getEvent.ts b/test-resources/src/dequeue/getEvent.ts index 4ad31ad8..83f8f6a4 100644 --- a/test-resources/src/dequeue/getEvent.ts +++ b/test-resources/src/dequeue/getEvent.ts @@ -25,28 +25,16 @@ export function getEvent(record: SQSRecord): Result { eventName: txmaEvent.event_name, }); } - - if (!txmaEvent.user) { - return errorResult({ - errorMessage: "Missing user", - eventName: txmaEvent.event_name, - }); - } - - const { session_id } = txmaEvent.user; + let session_id = txmaEvent.user?.session_id; if (!session_id) { - return errorResult({ - errorMessage: "Missing session_id", - eventName: txmaEvent.event_name, - }); - } - - if (!isValidUUID(session_id)) { - return errorResult({ - errorMessage: "session_id not valid", - eventName: txmaEvent.event_name, - sessionId: session_id, - }); + if (!allowedTxmaEventNamesWithoutSessionId.includes(txmaEvent.event_name)) { + return errorResult({ + errorMessage: "Missing session_id", + eventName: txmaEvent.event_name, + }); + } else { + session_id = "UNKNOWN"; + } } if (!txmaEvent.timestamp) { @@ -75,26 +63,10 @@ const isValidUUID = (input: string): boolean => { }; export const allowedTxmaEventNames = [ - "DCMAW_ABORT_APP", - "DCMAW_ABORT_WEB", - "DCMAW_CRI_4XXERROR", - "DCMAW_CRI_5XXERROR", - "DCMAW_REDIRECT_SUCCESS", - "DCMAW_REDIRECT_ABORT", - "DCMAW_MISSING_CONTEXT_AFTER_ABORT", - "DCMAW_MISSING_CONTEXT_AFTER_COMPLETION", - "DCMAW_PASSPORT_SELECTED", - "DCMAW_BRP_SELECTED", - "DCMAW_DRIVING_LICENCE_SELECTED", - "DCMAW_CRI_END", - "DCMAW_CRI_ABORT", - "DCMAW_APP_HANDOFF_START", - "DCMAW_HYBRID_BILLING_STARTED", - "DCMAW_IPROOV_BILLING_STARTED", - "DCMAW_READID_NFC_BILLING_STARTED", - "DCMAW_CRI_START", - "DCMAW_APP_END", - "AUTH_SESSION_TOO_OLD", - "BIOMETRIC_SESSION_OLDER_THAN_AUTH_SESSION", - "BIOMETRIC_SESSION_OPAQUEID_MISMATCH", + "DCMAW_ASYNC_CLIENT_CREDENTIALS_TOKEN_ISSUED", + "DCMAW_ASYNC_CRI_START", +]; + +const allowedTxmaEventNamesWithoutSessionId = [ + "DCMAW_ASYNC_CLIENT_CREDENTIALS_TOKEN_ISSUED", ]; diff --git a/test-resources/src/dequeue/tests/dequeueHandler.test.ts b/test-resources/src/dequeue/tests/dequeueHandler.test.ts index c470d5f3..7862bad1 100644 --- a/test-resources/src/dequeue/tests/dequeueHandler.test.ts +++ b/test-resources/src/dequeue/tests/dequeueHandler.test.ts @@ -16,12 +16,14 @@ import { invalidBodySQSRecord, invalidSessionId, invalidSessionIdSQSRecord, - missingSessionIdSQSRecord, + missingSessionIdInvalidSQSRecord, + missingSessionIdValidSQSRecord, missingTimestampSQSRecord, missingUserSQSRecord, notAllowedEventName, passingSQSRecord, putItemInputForPassingSQSRecord, + putItemInputForPassingSQSRecordWithoutSessionId, } from "./testData"; jest.useFakeTimers().setSystemTime(new Date("2025-01-08")); @@ -202,70 +204,75 @@ describe("Dequeue TxMA events", () => { }); }); - describe("Given user is missing", () => { - it("Logs an error message", async () => { - const event: SQSEvent = { - Records: [missingUserSQSRecord], - }; - - const result = await lambdaHandlerConstructor( - dependencies, - event, - buildLambdaContext(), - ); - - expect(mockLogger.getLogMessages().length).toEqual(4); - expect(mockLogger.getLogMessages()[1].logMessage.message).toStrictEqual( - "FAILED_TO_PROCESS_MESSAGES", - ); - expect(mockLogger.getLogMessages()[1].data).toStrictEqual({ - errorMessage: "Missing user", - eventName: JSON.parse(missingUserSQSRecord.body).event_name, - }); - expect(mockLogger.getLogMessages()[2].logMessage.message).toStrictEqual( - "PROCESSED_MESSAGES", - ); - expect(mockLogger.getLogMessages()[2].data).toStrictEqual({ - processedMessages: [], - }); - expect(result).toStrictEqual({ - batchItemFailures: [ - { itemIdentifier: missingUserSQSRecord.messageId }, - ], + describe("Given session_id is missing", () => { + describe("Given the registered event schema does not include a session_id", () => { + it("Writes to Dynamo using UNKNOWN as the sessionId", async () => { + const event: SQSEvent = { + Records: [missingSessionIdValidSQSRecord], + }; + + const result = await lambdaHandlerConstructor( + dependencies, + event, + buildLambdaContext(), + ); + + expect(mockLogger.getLogMessages().length).toEqual(3); + expect( + mockLogger.getLogMessages()[1].logMessage.message, + ).toStrictEqual("PROCESSED_MESSAGES"); + expect(mockLogger.getLogMessages()[1].data.processedMessages).toEqual( + [ + { + eventName: JSON.parse(missingSessionIdValidSQSRecord.body) + .event_name, + sessionId: "UNKNOWN", + }, + ], + ); + + expect(mockDbClient).toHaveReceivedCommandWith( + PutItemCommand, + putItemInputForPassingSQSRecordWithoutSessionId, + ); + expect(result).toStrictEqual({ + batchItemFailures: [], + }); }); }); - }); - describe("Given session_id is missing", () => { - it("Logs an error message", async () => { - const event: SQSEvent = { - Records: [missingSessionIdSQSRecord], - }; - - const result = await lambdaHandlerConstructor( - dependencies, - event, - buildLambdaContext(), - ); - - expect(mockLogger.getLogMessages().length).toEqual(4); - expect(mockLogger.getLogMessages()[1].logMessage.message).toStrictEqual( - "FAILED_TO_PROCESS_MESSAGES", - ); - expect(mockLogger.getLogMessages()[1].data).toStrictEqual({ - errorMessage: "Missing session_id", - eventName: JSON.parse(missingSessionIdSQSRecord.body).event_name, - }); - expect(mockLogger.getLogMessages()[2].logMessage.message).toStrictEqual( - "PROCESSED_MESSAGES", - ); - expect(mockLogger.getLogMessages()[2].data).toStrictEqual({ - processedMessages: [], - }); - expect(result).toStrictEqual({ - batchItemFailures: [ - { itemIdentifier: missingSessionIdSQSRecord.messageId }, - ], + describe("Given the registered event schema includes a session_id", () => { + it("Logs an error message", async () => { + const event: SQSEvent = { + Records: [missingSessionIdInvalidSQSRecord], + }; + + const result = await lambdaHandlerConstructor( + dependencies, + event, + buildLambdaContext(), + ); + + expect(mockLogger.getLogMessages().length).toEqual(4); + expect( + mockLogger.getLogMessages()[1].logMessage.message, + ).toStrictEqual("FAILED_TO_PROCESS_MESSAGES"); + expect(mockLogger.getLogMessages()[1].data).toStrictEqual({ + errorMessage: "Missing session_id", + eventName: JSON.parse(missingSessionIdInvalidSQSRecord.body) + .event_name, + }); + expect( + mockLogger.getLogMessages()[2].logMessage.message, + ).toStrictEqual("PROCESSED_MESSAGES"); + expect(mockLogger.getLogMessages()[2].data).toStrictEqual({ + processedMessages: [], + }); + expect(result).toStrictEqual({ + batchItemFailures: [ + { itemIdentifier: missingSessionIdInvalidSQSRecord.messageId }, + ], + }); }); }); }); diff --git a/test-resources/src/dequeue/tests/testData.ts b/test-resources/src/dequeue/tests/testData.ts index 34545ef7..ecc8e3ae 100644 --- a/test-resources/src/dequeue/tests/testData.ts +++ b/test-resources/src/dequeue/tests/testData.ts @@ -1,4 +1,6 @@ -const passingEventName = "DCMAW_APP_HANDOFF_START"; +const passingEventNameWithSessionId = "DCMAW_ASYNC_CRI_START"; +const passingEventNameWithoutSessionId = + "DCMAW_ASYNC_CLIENT_CREDENTIALS_TOKEN_ISSUED"; const passingSessionId = "49E7D76E-D5FE-4355-B8B4-E90ACA0887C2"; const failingSQSRecordMessageId = "54D7CA2F-BE1D-4D55-8F1C-9B3B501C9685"; @@ -6,7 +8,7 @@ export const passingSQSRecord = { messageId: "E8CA2168-36C2-4CAF-8CAC-9915B849E1E5", receiptHandle: "mockReceiptHandle", body: JSON.stringify({ - event_name: passingEventName, + event_name: passingEventNameWithSessionId, user: { session_id: passingSessionId, }, @@ -33,7 +35,7 @@ export const putItemInputForPassingSQSRecord = { }, eventBody: { S: JSON.stringify({ - event_name: passingEventName, + event_name: passingEventNameWithSessionId, user: { session_id: passingSessionId, }, @@ -44,6 +46,22 @@ export const putItemInputForPassingSQSRecord = { }, }; +export const putItemInputForPassingSQSRecordWithoutSessionId = { + Item: { + pk: { S: "SESSION#UNKNOWN" }, + sk: { + S: "TXMA#EVENT_NAME#DCMAW_ASYNC_CLIENT_CREDENTIALS_TOKEN_ISSUED#TIMESTAMP#mockTimestamp", + }, + eventBody: { + S: JSON.stringify({ + event_name: passingEventNameWithoutSessionId, + timestamp: "mockTimestamp", + }), + }, + timeToLiveInSeconds: { N: "1736298000" }, + }, +}; + export const invalidBodySQSRecord = { messageId: failingSQSRecordMessageId, receiptHandle: "mockReceiptHandle", @@ -111,7 +129,7 @@ export const missingUserSQSRecord = { messageId: failingSQSRecordMessageId, receiptHandle: "mockReceiptHandle", body: JSON.stringify({ - event_name: passingEventName, + event_name: passingEventNameWithSessionId, timestamp: "mockTimestamp", }), attributes: { @@ -127,11 +145,11 @@ export const missingUserSQSRecord = { awsRegion: "eu-west-2", }; -export const missingSessionIdSQSRecord = { +export const missingSessionIdInvalidSQSRecord = { messageId: failingSQSRecordMessageId, receiptHandle: "mockReceiptHandle", body: JSON.stringify({ - event_name: passingEventName, + event_name: passingEventNameWithSessionId, user: {}, timestamp: "mockTimestamp", }), @@ -148,12 +166,32 @@ export const missingSessionIdSQSRecord = { awsRegion: "eu-west-2", }; +export const missingSessionIdValidSQSRecord = { + messageId: failingSQSRecordMessageId, + receiptHandle: "mockReceiptHandle", + body: JSON.stringify({ + event_name: passingEventNameWithoutSessionId, + timestamp: "mockTimestamp", + }), + attributes: { + ApproximateReceiveCount: "1", + SentTimestamp: "1545082649183", + SenderId: "AIDAIENQZJOLO23YVJ4VO", + ApproximateFirstReceiveTimestamp: "1545082649185", + }, + messageAttributes: {}, + md5OfBody: "098f6bcd4621d373cade4e832627b4f6", + eventSource: "aws:sqs", + eventSourceARN: "arn:aws:sqs:eu-west-2:111122223333:my-queue", + awsRegion: "eu-west-2", +}; + export const invalidSessionId = "invalid-session-id"; export const invalidSessionIdSQSRecord = { messageId: failingSQSRecordMessageId, receiptHandle: "mockReceiptHandle", body: JSON.stringify({ - event_name: passingEventName, + event_name: passingEventNameWithSessionId, user: { session_id: invalidSessionId, }, @@ -176,7 +214,7 @@ export const missingTimestampSQSRecord = { messageId: failingSQSRecordMessageId, receiptHandle: "mockReceiptHandle", body: JSON.stringify({ - event_name: passingEventName, + event_name: passingEventNameWithSessionId, user: { session_id: passingSessionId, },