Skip to content

Commit

Permalink
Create test for scanExecution lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Oct 5, 2023
1 parent 0863530 commit 1e5853e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
2 changes: 2 additions & 0 deletions backend/env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 2 additions & 16 deletions backend/src/tasks/scanExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
39 changes: 39 additions & 0 deletions backend/src/tasks/test/scanExecution.test.ts
Original file line number Diff line number Diff line change
@@ -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);

Check warning

Code scanning / CodeQL

Superfluous trailing arguments Warning test

Superfluous arguments passed to
function handler
.

// Add your assertions here
expect(result.statusCode).toEqual(200);
expect(result.body).toContain(
'Fargate task started and message sent to SQS queue'
);
});
});

0 comments on commit 1e5853e

Please sign in to comment.