Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create test for scanExecution lambda #2289

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Fixed Show fixed Hide fixed

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