Skip to content

Commit b02752b

Browse files
authored
test(core): unit tests for cfn utils provider handler (#27759)
Adds unit test coverage for cfn utils provider handler. Closes #27729. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d449cfd commit b02752b

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.condition-with-ref.js.snapshot/test-condition-with-ref.template.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"S3Bucket": {
6666
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
6767
},
68-
"S3Key": "1e14e895fcbdf65feb0a29e4aa74c6c92a6fb0e41f228bef7ab23627ed409cde.zip"
68+
"S3Key": "3e61d858eaa7724170872a455b8f788d5fcf18adba89aadbe603a52dab59d917.zip"
6969
},
7070
"Timeout": 900,
7171
"MemorySize": 128,

packages/aws-cdk-lib/core/lib/private/cfn-utils-provider/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent
1313
return cfnJsonStringifyHandler(event);
1414
}
1515

16-
throw new Error(`unexpected resource type "${event.ResourceType}`);
16+
throw new Error(`unexpected resource type "${event.ResourceType}"`);
1717
}
1818

1919
function cfnJsonHandler(event: AWSLambda.CloudFormationCustomResourceEvent) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { CfnUtilsResourceType } from '../../lib/private/cfn-utils-provider/consts';
2+
import { handler } from '../../lib/private/cfn-utils-provider/index';
3+
4+
test('parses value as JSON', async () => {
5+
// GIVEN
6+
const event: Partial<AWSLambda.CloudFormationCustomResourceEvent> = {
7+
ResourceType: CfnUtilsResourceType.CFN_JSON,
8+
ResourceProperties: {
9+
ServiceToken: 'Foo',
10+
RepositoryName: 'MyRepo',
11+
Value: JSON.stringify({
12+
test: 'Random',
13+
}),
14+
},
15+
};
16+
17+
// WHEN
18+
const response = await invokeHandler(event);
19+
20+
// THEN
21+
expect(response).toEqual({
22+
Data: {
23+
Value: {
24+
test: 'Random',
25+
},
26+
},
27+
});
28+
});
29+
30+
test('format JSON value as string', async () => {
31+
// GIVEN
32+
const event: Partial<AWSLambda.CloudFormationCustomResourceEvent> = {
33+
ResourceType: CfnUtilsResourceType.CFN_JSON_STRINGIFY,
34+
ResourceProperties: {
35+
ServiceToken: 'Foo',
36+
RepositoryName: 'MyRepo',
37+
Value: {
38+
test: 'Random',
39+
},
40+
},
41+
};
42+
43+
// WHEN
44+
const response = await invokeHandler(event);
45+
46+
// THEN
47+
expect(response).toEqual({
48+
Data: {
49+
Value: JSON.stringify({
50+
test: 'Random',
51+
}),
52+
},
53+
});
54+
});
55+
56+
test('fails if wrong resource type', async () => {
57+
// GIVEN
58+
const event: Partial<AWSLambda.CloudFormationCustomResourceEvent> = {
59+
ResourceType: 'Create',
60+
ResourceProperties: {
61+
ServiceToken: 'Foo',
62+
RepositoryName: 'MyRepo',
63+
},
64+
};
65+
66+
// WHEN
67+
await expect(() => invokeHandler(event)).rejects.toThrow(/unexpected resource type "Create"/);
68+
});
69+
70+
// helper function to get around TypeScript expecting a complete event object,
71+
// even though our tests only need some of the fields
72+
async function invokeHandler(event: Partial<AWSLambda.CloudFormationCustomResourceEvent>) {
73+
return handler(event as AWSLambda.CloudFormationCustomResourceEvent);
74+
}

0 commit comments

Comments
 (0)