Skip to content

Commit

Permalink
chore(cloudfront): create unit tests to mock edge function handler (#…
Browse files Browse the repository at this point in the history
…27768)

This PR adds three unit tests that mock functionality found in the Lambda handler for edge functions. Specifically, this PR is adding one unit test for a create event, one unit test for an update event, and one unit test for a delete event to verify that the handler only executes for create and update events.

Closes #27724

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
colifran authored Oct 31, 2023
1 parent 1310a26 commit d988105
Showing 1 changed file with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,49 @@ import * as iam from '../../../aws-iam';
import * as lambda from '../../../aws-lambda';
import * as cdk from '../../../core';
import * as cloudfront from '../../lib';
import { handler } from '../../lib/experimental/edge-function/index';

let app: cdk.App;
let stack: cdk.Stack;

type RequestType = 'Create' | 'Update' | 'Delete';

const mockSSM = {
getParameter: jest.fn().mockResolvedValue({
Parameter: { Value: 'arn:aws:lambda:us-west-2:123456789012:function:edge-function' },
}),
};

jest.mock('@aws-sdk/client-ssm', () => {
return {
SSM: jest.fn().mockImplementation(() => {
return mockSSM;
}),
};
});

const eventCommon = {
ServiceToken: 'token',
ResponseURL: 'https://localhost',
StackId: 'stackId',
RequestId: 'requestId',
LogicalResourceId: 'logicalResourceId',
PhysicalResourceId: 'physicalResourceId',
ResourceProperties: {
Region: 'us-west-2',
ParameterName: 'edge-function-arn',
},
};

beforeEach(() => {
app = new cdk.App();
stack = new cdk.Stack(app, 'Stack', {
env: { account: '111111111111', region: 'testregion' },
});
});

afterAll(() => { jest.resetAllMocks(); });

describe('stacks', () => {
test('creates a custom resource and supporting resources in main stack', () => {
new cloudfront.experimental.EdgeFunction(stack, 'MyFn', defaultEdgeFunctionProps());
Expand Down Expand Up @@ -227,6 +259,58 @@ describe('stacks', () => {
});
});

describe('handler', () => {
afterEach(() => {
jest.restoreAllMocks();
mockSSM.getParameter.mockClear();
});

test('create event', async () => {
// GIVEN
const event = {
...eventCommon,
RequestType: 'Create' as RequestType,
};

// WHEN
const response = await handler(event);

// THEN
expect(mockSSM.getParameter).toBeCalledWith({ Name: 'edge-function-arn' });
expect(response).toEqual({ Data: { FunctionArn: 'arn:aws:lambda:us-west-2:123456789012:function:edge-function' } });
});

test('update event', async () => {
// GIVEN
const event = {
...eventCommon,
RequestType: 'Update' as RequestType,
};

// WHEN
const response = await handler(event);

// THEN
expect(mockSSM.getParameter).toBeCalledWith({ Name: 'edge-function-arn' });
expect(response).toEqual({ Data: { FunctionArn: 'arn:aws:lambda:us-west-2:123456789012:function:edge-function' } });
});

test('delete event', async () => {
// GIVEN
const event = {
...eventCommon,
RequestType: 'Delete' as RequestType,
};

// WHEN
const response = await handler(event);

// THEN
expect(mockSSM.getParameter).not.toHaveBeenCalled();
expect(response).toBe(undefined);
});
});

test('addAlias() creates alias in function stack', () => {
const fn = new cloudfront.experimental.EdgeFunction(stack, 'MyFn', defaultEdgeFunctionProps());

Expand Down

0 comments on commit d988105

Please sign in to comment.