Skip to content

Commit

Permalink
DCMAW-10799: Allow events without sessionId in schema to be processed
Browse files Browse the repository at this point in the history
  • Loading branch information
sandymay-dd committed Jan 10, 2025
1 parent ebd109c commit a631272
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 114 deletions.
4 changes: 3 additions & 1 deletion test-resources/src/dequeue/dequeueHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
58 changes: 15 additions & 43 deletions test-resources/src/dequeue/getEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,16 @@ export function getEvent(record: SQSRecord): Result<TxmaEvent> {
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) {
Expand Down Expand Up @@ -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",
];
131 changes: 69 additions & 62 deletions test-resources/src/dequeue/tests/dequeueHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import {
invalidBodySQSRecord,
invalidSessionId,
invalidSessionIdSQSRecord,
missingSessionIdSQSRecord,
missingSessionIdInvalidSQSRecord,
missingSessionIdValidSQSRecord,
missingTimestampSQSRecord,
missingUserSQSRecord,

Check failure on line 22 in test-resources/src/dequeue/tests/dequeueHandler.test.ts

View workflow job for this annotation

GitHub Actions / Run CI checks

'missingUserSQSRecord' is defined but never used
notAllowedEventName,
passingSQSRecord,
putItemInputForPassingSQSRecord,
putItemInputForPassingSQSRecordWithoutSessionId,
} from "./testData";

jest.useFakeTimers().setSystemTime(new Date("2025-01-08"));
Expand Down Expand Up @@ -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 },
],
});
});
});
});
Expand Down
54 changes: 46 additions & 8 deletions test-resources/src/dequeue/tests/testData.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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";

export const passingSQSRecord = {
messageId: "E8CA2168-36C2-4CAF-8CAC-9915B849E1E5",
receiptHandle: "mockReceiptHandle",
body: JSON.stringify({
event_name: passingEventName,
event_name: passingEventNameWithSessionId,
user: {
session_id: passingSessionId,
},
Expand All @@ -33,7 +35,7 @@ export const putItemInputForPassingSQSRecord = {
},
eventBody: {
S: JSON.stringify({
event_name: passingEventName,
event_name: passingEventNameWithSessionId,
user: {
session_id: passingSessionId,
},
Expand All @@ -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",
Expand Down Expand Up @@ -111,7 +129,7 @@ export const missingUserSQSRecord = {
messageId: failingSQSRecordMessageId,
receiptHandle: "mockReceiptHandle",
body: JSON.stringify({
event_name: passingEventName,
event_name: passingEventNameWithSessionId,
timestamp: "mockTimestamp",
}),
attributes: {
Expand All @@ -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",
}),
Expand All @@ -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,
},
Expand All @@ -176,7 +214,7 @@ export const missingTimestampSQSRecord = {
messageId: failingSQSRecordMessageId,
receiptHandle: "mockReceiptHandle",
body: JSON.stringify({
event_name: passingEventName,
event_name: passingEventNameWithSessionId,
user: {
session_id: passingSessionId,
},
Expand Down

0 comments on commit a631272

Please sign in to comment.