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

chore(cloudfront): create unit tests to mock edge function handler #27768

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Changes from all 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
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
Loading