Skip to content

Commit d988105

Browse files
authored
chore(cloudfront): create unit tests to mock edge function handler (#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*
1 parent 1310a26 commit d988105

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

packages/aws-cdk-lib/aws-cloudfront/test/experimental/edge-function.test.ts

+84
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,49 @@ import * as iam from '../../../aws-iam';
55
import * as lambda from '../../../aws-lambda';
66
import * as cdk from '../../../core';
77
import * as cloudfront from '../../lib';
8+
import { handler } from '../../lib/experimental/edge-function/index';
89

910
let app: cdk.App;
1011
let stack: cdk.Stack;
1112

13+
type RequestType = 'Create' | 'Update' | 'Delete';
14+
15+
const mockSSM = {
16+
getParameter: jest.fn().mockResolvedValue({
17+
Parameter: { Value: 'arn:aws:lambda:us-west-2:123456789012:function:edge-function' },
18+
}),
19+
};
20+
21+
jest.mock('@aws-sdk/client-ssm', () => {
22+
return {
23+
SSM: jest.fn().mockImplementation(() => {
24+
return mockSSM;
25+
}),
26+
};
27+
});
28+
29+
const eventCommon = {
30+
ServiceToken: 'token',
31+
ResponseURL: 'https://localhost',
32+
StackId: 'stackId',
33+
RequestId: 'requestId',
34+
LogicalResourceId: 'logicalResourceId',
35+
PhysicalResourceId: 'physicalResourceId',
36+
ResourceProperties: {
37+
Region: 'us-west-2',
38+
ParameterName: 'edge-function-arn',
39+
},
40+
};
41+
1242
beforeEach(() => {
1343
app = new cdk.App();
1444
stack = new cdk.Stack(app, 'Stack', {
1545
env: { account: '111111111111', region: 'testregion' },
1646
});
1747
});
1848

49+
afterAll(() => { jest.resetAllMocks(); });
50+
1951
describe('stacks', () => {
2052
test('creates a custom resource and supporting resources in main stack', () => {
2153
new cloudfront.experimental.EdgeFunction(stack, 'MyFn', defaultEdgeFunctionProps());
@@ -227,6 +259,58 @@ describe('stacks', () => {
227259
});
228260
});
229261

262+
describe('handler', () => {
263+
afterEach(() => {
264+
jest.restoreAllMocks();
265+
mockSSM.getParameter.mockClear();
266+
});
267+
268+
test('create event', async () => {
269+
// GIVEN
270+
const event = {
271+
...eventCommon,
272+
RequestType: 'Create' as RequestType,
273+
};
274+
275+
// WHEN
276+
const response = await handler(event);
277+
278+
// THEN
279+
expect(mockSSM.getParameter).toBeCalledWith({ Name: 'edge-function-arn' });
280+
expect(response).toEqual({ Data: { FunctionArn: 'arn:aws:lambda:us-west-2:123456789012:function:edge-function' } });
281+
});
282+
283+
test('update event', async () => {
284+
// GIVEN
285+
const event = {
286+
...eventCommon,
287+
RequestType: 'Update' as RequestType,
288+
};
289+
290+
// WHEN
291+
const response = await handler(event);
292+
293+
// THEN
294+
expect(mockSSM.getParameter).toBeCalledWith({ Name: 'edge-function-arn' });
295+
expect(response).toEqual({ Data: { FunctionArn: 'arn:aws:lambda:us-west-2:123456789012:function:edge-function' } });
296+
});
297+
298+
test('delete event', async () => {
299+
// GIVEN
300+
const event = {
301+
...eventCommon,
302+
RequestType: 'Delete' as RequestType,
303+
};
304+
305+
// WHEN
306+
const response = await handler(event);
307+
308+
// THEN
309+
expect(mockSSM.getParameter).not.toHaveBeenCalled();
310+
expect(response).toBe(undefined);
311+
});
312+
});
313+
230314
test('addAlias() creates alias in function stack', () => {
231315
const fn = new cloudfront.experimental.EdgeFunction(stack, 'MyFn', defaultEdgeFunctionProps());
232316

0 commit comments

Comments
 (0)