Skip to content

Commit

Permalink
BAU: Refactors test to not use aws-sdk-mock
Browse files Browse the repository at this point in the history
I have removed the aws-sdk-mock package due to a vunerability, so
have refactored these test not to use this and instead use inbuilt
mocks native to jest.
  • Loading branch information
Ryan-Andrews99 committed Sep 10, 2024
1 parent 1d66774 commit 96914e0
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 99 deletions.
49 changes: 31 additions & 18 deletions backend/api/tests/handlers/logging/sqs-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import {SQSClient, SendMessageCommand} from "@aws-sdk/client-sqs";
const sendMessageMock = jest.fn();

jest.mock("@aws-sdk/client-sqs", () => {
return {
SQSClient: jest.fn(() => {
return {
send: sendMessageMock
};
}),
SendMessageCommand: jest.fn()
};
});
import {SendMessageCommand} from "@aws-sdk/client-sqs";
import {TEST_MESSAGE_ID} from "../constants";
import {constructTestApiGatewayEvent} from "../utils";
import {sendSQSMessageToTxMAHandler} from "./../../../src/handlers/logging/sqs-service";
import {TEST_SQS_QUEUE_URL} from "../../setup";
import {mockClient} from "aws-sdk-client-mock";
import "aws-sdk-client-mock-jest";

const mockSqsClient = mockClient(SQSClient);

const TEST_EVENT = {
event_name: "Hi there"
Expand All @@ -21,18 +29,20 @@ describe("sendSQSMessageToTxMAHandler tests", () => {
beforeEach(() => {
jest.spyOn(console, "log");
jest.clearAllMocks();
mockSqsClient.reset();
});

it("returns a 200 and the stringified success message when the sqs message sends successfully", async () => {
mockSqsClient.on(SendMessageCommand).resolves({MessageId: TEST_MESSAGE_ID});
sendMessageMock.mockResolvedValue({MessageId: TEST_MESSAGE_ID});

const response = await sendSQSMessageToTxMAHandler(TEST_HANDLER_EVENT);

expect(mockSqsClient).toHaveReceivedCommandWith(SendMessageCommand, {
QueueUrl: TEST_SQS_QUEUE_URL,
MessageBody: JSON.stringify(TEST_EVENT)
});
expect(sendMessageMock).toHaveBeenCalledWith(
new SendMessageCommand({
QueueUrl: TEST_SQS_QUEUE_URL,
MessageBody: JSON.stringify(TEST_EVENT)
})
);

expect(response).toStrictEqual({
statusCode: 200,
body: JSON.stringify("Message Send to SQS - Here is MessageId: " + TEST_MESSAGE_ID)
Expand All @@ -45,7 +55,7 @@ describe("sendSQSMessageToTxMAHandler tests", () => {

const response = await sendSQSMessageToTxMAHandler(event);

expect(mockSqsClient).not.toReceiveAnyCommand();
expect(sendMessageMock).not.toHaveBeenCalled();
expect(response).toStrictEqual({
statusCode: 200,
body: JSON.stringify("Error")
Expand All @@ -54,15 +64,18 @@ describe("sendSQSMessageToTxMAHandler tests", () => {

it("returns a 200 and the phrase error stringified and logs the error when the sqs client throws an error", async () => {
const sqsErr = "sqsErr";
mockSqsClient.on(SendMessageCommand).rejects(sqsErr);
sendMessageMock.mockRejectedValue(sqsErr);

const response = await sendSQSMessageToTxMAHandler(TEST_HANDLER_EVENT);

expect(mockSqsClient).toHaveReceivedCommandWith(SendMessageCommand, {
QueueUrl: TEST_SQS_QUEUE_URL,
MessageBody: JSON.stringify(TEST_EVENT)
});
expect(console.log).toHaveBeenCalledWith("Error", Error(sqsErr));
expect(sendMessageMock).toHaveBeenCalledWith(
new SendMessageCommand({
QueueUrl: TEST_SQS_QUEUE_URL,
MessageBody: JSON.stringify(TEST_EVENT)
})
);

expect(console.log).toHaveBeenCalledWith("Error", sqsErr);
expect(response).toStrictEqual({
statusCode: 200,
body: JSON.stringify("Error")
Expand Down
59 changes: 40 additions & 19 deletions backend/api/tests/handlers/step-functions/new-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import {SFNClient, StartSyncExecutionCommand, SyncExecutionStatus} from "@aws-sdk/client-sfn";
import {mockClient} from "aws-sdk-client-mock";
const sendCommandMock = jest.fn();
jest.mock("@aws-sdk/client-sfn", () => {
return {
SFNClient: jest.fn(() => {
return {
send: sendCommandMock
};
}),
StartSyncExecutionCommand: jest.fn(),
SyncExecutionStatus: {
FAILED: "FAILED",
SUCCESS: "SUCCESS",
TIMED_OUT: "TIMED_OUT"
}
};
});

import {StartSyncExecutionCommand, SyncExecutionStatus} from "@aws-sdk/client-sfn";
import {constructTestApiGatewayEvent, mockLambdaContext} from "../utils";
import {newClientHandler} from "../../../src/handlers/step-functions/new-client";
import {TEST_SERVICE_ID, TEST_SERVICE_NAME, TEST_USER_EMAIL} from "../constants";
import {TEST_STATE_MACHINE_ARN} from "../../setup";
import "aws-sdk-client-mock-jest";

const TEST_NEW_CLIENT = {
contactEmail: TEST_USER_EMAIL,
Expand All @@ -17,7 +32,6 @@ const TEST_NEW_CLIENT_EVENT = constructTestApiGatewayEvent({
body: JSON.stringify(TEST_NEW_CLIENT),
pathParameters: {}
});
const mockSfnClient = mockClient(SFNClient);

describe("newClientHandler tests", () => {
beforeEach(() => {
Expand All @@ -30,14 +44,17 @@ describe("newClientHandler tests", () => {
$metadata: {httpStatusCode: 200},
executionArn: TEST_STATE_MACHINE_ARN
};
mockSfnClient.on(StartSyncExecutionCommand).resolves(mockSfnResponse);

sendCommandMock.mockResolvedValue(mockSfnResponse);

const newClientResponse = await newClientHandler(TEST_NEW_CLIENT_EVENT, mockLambdaContext);

expect(mockSfnClient).toHaveReceivedCommandWith(StartSyncExecutionCommand, {
input: JSON.stringify(TEST_NEW_CLIENT),
stateMachineArn: TEST_STATE_MACHINE_ARN
});
expect(sendCommandMock).toHaveBeenCalledWith(
new StartSyncExecutionCommand({
input: JSON.stringify(TEST_NEW_CLIENT),
stateMachineArn: TEST_STATE_MACHINE_ARN
})
);

expect(newClientResponse).toStrictEqual({
statusCode: 200,
Expand All @@ -46,18 +63,20 @@ describe("newClientHandler tests", () => {
});

it("returns a 408 and time out result body if the function execution status is TIMED_OUT", async () => {
mockSfnClient.on(StartSyncExecutionCommand).resolves({
sendCommandMock.mockResolvedValue({
status: SyncExecutionStatus.TIMED_OUT,
$metadata: {httpStatusCode: 408},
executionArn: TEST_STATE_MACHINE_ARN
});

const newClientResponse = await newClientHandler(TEST_NEW_CLIENT_EVENT, mockLambdaContext);

expect(mockSfnClient).toHaveReceivedCommandWith(StartSyncExecutionCommand, {
input: JSON.stringify(TEST_NEW_CLIENT),
stateMachineArn: TEST_STATE_MACHINE_ARN
});
expect(sendCommandMock).toHaveBeenCalledWith(
new StartSyncExecutionCommand({
input: JSON.stringify(TEST_NEW_CLIENT),
stateMachineArn: TEST_STATE_MACHINE_ARN
})
);

expect(newClientResponse).toStrictEqual({
statusCode: 408,
Expand All @@ -69,7 +88,7 @@ describe("newClientHandler tests", () => {
const stateMachineError = "Invalid JSON";
const stateMachineErrorCause = "Payload";

mockSfnClient.on(StartSyncExecutionCommand).resolves({
sendCommandMock.mockResolvedValue({
status: SyncExecutionStatus.FAILED,
$metadata: {httpStatusCode: 400},
executionArn: TEST_STATE_MACHINE_ARN,
Expand All @@ -79,10 +98,12 @@ describe("newClientHandler tests", () => {

const newClientResponse = await newClientHandler(TEST_NEW_CLIENT_EVENT, mockLambdaContext);

expect(mockSfnClient).toHaveReceivedCommandWith(StartSyncExecutionCommand, {
input: JSON.stringify(TEST_NEW_CLIENT),
stateMachineArn: TEST_STATE_MACHINE_ARN
});
expect(sendCommandMock).toHaveBeenCalledWith(
new StartSyncExecutionCommand({
input: JSON.stringify(TEST_NEW_CLIENT),
stateMachineArn: TEST_STATE_MACHINE_ARN
})
);

expect(newClientResponse).toStrictEqual({
statusCode: 400,
Expand Down
59 changes: 39 additions & 20 deletions backend/api/tests/handlers/step-functions/new-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import {SFNClient, StartSyncExecutionCommand, SyncExecutionStatus} from "@aws-sdk/client-sfn";
import {mockClient} from "aws-sdk-client-mock";
const sendCommandMock = jest.fn();
jest.mock("@aws-sdk/client-sfn", () => {
return {
SFNClient: jest.fn(() => {
return {
send: sendCommandMock
};
}),
StartSyncExecutionCommand: jest.fn(),
SyncExecutionStatus: {
FAILED: "FAILED",
SUCCESS: "SUCCESS",
TIMED_OUT: "TIMED_OUT"
}
};
});

import {StartSyncExecutionCommand, SyncExecutionStatus} from "@aws-sdk/client-sfn";
import {constructTestApiGatewayEvent, mockLambdaContext} from "../utils";
import {newServiceHandler} from "../../../src/handlers/step-functions/new-service";
import {TEST_SERVICE_ID, TEST_SERVICE_NAME} from "../constants";
import {TEST_STATE_MACHINE_ARN} from "../../setup";
import "aws-sdk-client-mock-jest";

const TEST_NEW_SERVICE = {
service: {
Expand All @@ -16,7 +31,6 @@ const TEST_NEW_CLIENT_EVENT = constructTestApiGatewayEvent({
body: JSON.stringify(TEST_NEW_SERVICE),
pathParameters: {}
});
const mockSfnClient = mockClient(SFNClient);

describe("newServiceHandler tests", () => {
beforeEach(() => {
Expand All @@ -29,14 +43,16 @@ describe("newServiceHandler tests", () => {
$metadata: {httpStatusCode: 200},
executionArn: TEST_STATE_MACHINE_ARN
};
mockSfnClient.on(StartSyncExecutionCommand).resolves(mockSfnResponse);
sendCommandMock.mockResolvedValue(mockSfnResponse);

const newClientResponse = await newServiceHandler(TEST_NEW_CLIENT_EVENT, mockLambdaContext);

expect(mockSfnClient).toHaveReceivedCommandWith(StartSyncExecutionCommand, {
input: JSON.stringify(TEST_NEW_SERVICE),
stateMachineArn: TEST_STATE_MACHINE_ARN
});
expect(sendCommandMock).toHaveBeenCalledWith(
new StartSyncExecutionCommand({
input: JSON.stringify(TEST_NEW_SERVICE),
stateMachineArn: TEST_STATE_MACHINE_ARN
})
);

expect(newClientResponse).toStrictEqual({
statusCode: 200,
Expand All @@ -45,19 +61,20 @@ describe("newServiceHandler tests", () => {
});

it("returns a 408 and time out result body if the function execution status is TIMED_OUT", async () => {
mockSfnClient.on(StartSyncExecutionCommand).resolves({
sendCommandMock.mockResolvedValue({
status: SyncExecutionStatus.TIMED_OUT,
$metadata: {httpStatusCode: 408},
executionArn: TEST_STATE_MACHINE_ARN
});

const newClientResponse = await newServiceHandler(TEST_NEW_CLIENT_EVENT, mockLambdaContext);

expect(mockSfnClient).toHaveReceivedCommandWith(StartSyncExecutionCommand, {
input: JSON.stringify(TEST_NEW_SERVICE),
stateMachineArn: TEST_STATE_MACHINE_ARN
});

expect(sendCommandMock).toHaveBeenCalledWith(
new StartSyncExecutionCommand({
input: JSON.stringify(TEST_NEW_SERVICE),
stateMachineArn: TEST_STATE_MACHINE_ARN
})
);
expect(newClientResponse).toStrictEqual({
statusCode: 408,
body: "Function timed out"
Expand All @@ -68,7 +85,7 @@ describe("newServiceHandler tests", () => {
const stateMachineError = "Invalid JSON";
const stateMachineErrorCause = "Payload";

mockSfnClient.on(StartSyncExecutionCommand).resolves({
sendCommandMock.mockResolvedValue({
status: SyncExecutionStatus.FAILED,
$metadata: {httpStatusCode: 400},
executionArn: TEST_STATE_MACHINE_ARN,
Expand All @@ -78,10 +95,12 @@ describe("newServiceHandler tests", () => {

const newClientResponse = await newServiceHandler(TEST_NEW_CLIENT_EVENT, mockLambdaContext);

expect(mockSfnClient).toHaveReceivedCommandWith(StartSyncExecutionCommand, {
input: JSON.stringify(TEST_NEW_SERVICE),
stateMachineArn: TEST_STATE_MACHINE_ARN
});
expect(sendCommandMock).toHaveBeenCalledWith(
new StartSyncExecutionCommand({
input: JSON.stringify(TEST_NEW_SERVICE),
stateMachineArn: TEST_STATE_MACHINE_ARN
})
);

expect(newClientResponse).toStrictEqual({
statusCode: 400,
Expand Down
Loading

0 comments on commit 96914e0

Please sign in to comment.