From 1e5853eb17fb76b6cfe459427dab1af0bc84579f Mon Sep 17 00:00:00 2001 From: aloftus23 Date: Thu, 5 Oct 2023 12:37:30 -0400 Subject: [PATCH 1/2] Create test for scanExecution lambda --- backend/env.yml | 2 + backend/src/tasks/scanExecution.ts | 18 +-------- backend/src/tasks/test/scanExecution.test.ts | 39 ++++++++++++++++++++ 3 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 backend/src/tasks/test/scanExecution.test.ts diff --git a/backend/env.yml b/backend/env.yml index 4a922725a..851e241ac 100644 --- a/backend/env.yml +++ b/backend/env.yml @@ -42,6 +42,7 @@ staging: PE_API_URL: ${ssm:/crossfeed/staging/PE_API_URL} REPORTS_BUCKET_NAME: cisa-crossfeed-staging-reports CLOUDWATCH_BUCKET_NAME: cisa-crossfeed-staging-cloudwatch + SQS_QUEUE_URL: { Ref: WorkerQueue } prod: DB_DIALECT: 'postgres' @@ -78,6 +79,7 @@ prod: PE_API_URL: ${ssm:/crossfeed/staging/PE_API_URL} REPORTS_BUCKET_NAME: cisa-crossfeed-prod-reports CLOUDWATCH_BUCKET_NAME: cisa-crossfeed-prod-cloudwatch + SQS_QUEUE_URL: { Ref: WorkerQueue } dev-vpc: securityGroupIds: diff --git a/backend/src/tasks/scanExecution.ts b/backend/src/tasks/scanExecution.ts index c3d959984..658a447d0 100644 --- a/backend/src/tasks/scanExecution.ts +++ b/backend/src/tasks/scanExecution.ts @@ -12,23 +12,9 @@ export const handler: Handler = async (event) => { console.log(commandOptions); - // Get the ARN of the SQS queue from the event - const sqsQueueArn: string | undefined = sqsRecord.eventSourceARN; - - if (!sqsQueueArn) { - throw new Error('SQS Queue ARN not found in event'); - } - - // Describe the SQS queue to get its URL - const sqsQueue = { - QueueUrl: sqsQueueArn // Use the ARN as the QueueUrl - }; - const queueAttributesResponse = await sqs - .getQueueAttributes(sqsQueue) - .promise(); - const sqsQueueUrl = queueAttributesResponse.Attributes?.QueueUrl; + const sqsQueueUrl = process.env.SQS_QUEUE_URL!; console.log(sqsQueueUrl); - if (!sqsQueueUrl) { + if (sqsQueueUrl) { throw new Error('SQS Queue URL not found'); } diff --git a/backend/src/tasks/test/scanExecution.test.ts b/backend/src/tasks/test/scanExecution.test.ts new file mode 100644 index 000000000..ea4942d33 --- /dev/null +++ b/backend/src/tasks/test/scanExecution.test.ts @@ -0,0 +1,39 @@ +import { handler } from '../scanExecution'; +import { SQSRecord } from 'aws-lambda'; + +// Mock the AWS SDK methods using aws-sdk-mock +jest.mock('aws-sdk', () => { + return { + ECS: jest.fn(() => ({ + runTask: jest.fn().mockReturnThis(), + promise: jest.fn() + })), + SQS: jest.fn(() => ({ + sendMessage: jest.fn().mockReturnThis(), + promise: jest.fn() + })) + }; +}); + +describe('Scan Execution', () => { + it('should handle the event', async () => { + const event = { + Records: [ + { + body: 'test command', + eventSourceARN: 'YourSQSQueueARN' + } as SQSRecord + ] + }; + + const context = {} as any; + const callback = () => void 0; + const result = await handler(event, context, callback); + + // Add your assertions here + expect(result.statusCode).toEqual(200); + expect(result.body).toContain( + 'Fargate task started and message sent to SQS queue' + ); + }); +}); From b30bc630362aa84d02795f79a703ae4188319961 Mon Sep 17 00:00:00 2001 From: aloftus23 Date: Thu, 5 Oct 2023 12:59:05 -0400 Subject: [PATCH 2/2] Fix formatting --- backend/src/tasks/test/scanExecution.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/backend/src/tasks/test/scanExecution.test.ts b/backend/src/tasks/test/scanExecution.test.ts index ea4942d33..d6dc10a27 100644 --- a/backend/src/tasks/test/scanExecution.test.ts +++ b/backend/src/tasks/test/scanExecution.test.ts @@ -21,14 +21,12 @@ describe('Scan Execution', () => { Records: [ { body: 'test command', - eventSourceARN: 'YourSQSQueueARN' + eventSourceARN: 'SQSQueueARN' } as SQSRecord ] }; - const context = {} as any; - const callback = () => void 0; - const result = await handler(event, context, callback); + const result = await handler(event, {} as any, () => void 0); // Add your assertions here expect(result.statusCode).toEqual(200);