From b02752be7c18a30241e9afec890a6793ca964ea5 Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Thu, 2 Nov 2023 02:58:44 +0100 Subject: [PATCH] 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* --- .../test-condition-with-ref.template.json | 2 +- .../lib/private/cfn-utils-provider/index.ts | 2 +- .../cfn-utils-provider-handler.test.ts | 74 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 packages/aws-cdk-lib/core/test/private/cfn-utils-provider-handler.test.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.condition-with-ref.js.snapshot/test-condition-with-ref.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.condition-with-ref.js.snapshot/test-condition-with-ref.template.json index 58e9598bae22b..9c90ab65c0719 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.condition-with-ref.js.snapshot/test-condition-with-ref.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.condition-with-ref.js.snapshot/test-condition-with-ref.template.json @@ -65,7 +65,7 @@ "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" }, - "S3Key": "1e14e895fcbdf65feb0a29e4aa74c6c92a6fb0e41f228bef7ab23627ed409cde.zip" + "S3Key": "3e61d858eaa7724170872a455b8f788d5fcf18adba89aadbe603a52dab59d917.zip" }, "Timeout": 900, "MemorySize": 128, diff --git a/packages/aws-cdk-lib/core/lib/private/cfn-utils-provider/index.ts b/packages/aws-cdk-lib/core/lib/private/cfn-utils-provider/index.ts index f082001f80159..3cfbd9ba583d3 100644 --- a/packages/aws-cdk-lib/core/lib/private/cfn-utils-provider/index.ts +++ b/packages/aws-cdk-lib/core/lib/private/cfn-utils-provider/index.ts @@ -13,7 +13,7 @@ export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent return cfnJsonStringifyHandler(event); } - throw new Error(`unexpected resource type "${event.ResourceType}`); + throw new Error(`unexpected resource type "${event.ResourceType}"`); } function cfnJsonHandler(event: AWSLambda.CloudFormationCustomResourceEvent) { diff --git a/packages/aws-cdk-lib/core/test/private/cfn-utils-provider-handler.test.ts b/packages/aws-cdk-lib/core/test/private/cfn-utils-provider-handler.test.ts new file mode 100644 index 0000000000000..26beaf733001d --- /dev/null +++ b/packages/aws-cdk-lib/core/test/private/cfn-utils-provider-handler.test.ts @@ -0,0 +1,74 @@ +import { CfnUtilsResourceType } from '../../lib/private/cfn-utils-provider/consts'; +import { handler } from '../../lib/private/cfn-utils-provider/index'; + +test('parses value as JSON', async () => { + // GIVEN + const event: Partial = { + ResourceType: CfnUtilsResourceType.CFN_JSON, + ResourceProperties: { + ServiceToken: 'Foo', + RepositoryName: 'MyRepo', + Value: JSON.stringify({ + test: 'Random', + }), + }, + }; + + // WHEN + const response = await invokeHandler(event); + + // THEN + expect(response).toEqual({ + Data: { + Value: { + test: 'Random', + }, + }, + }); +}); + +test('format JSON value as string', async () => { + // GIVEN + const event: Partial = { + ResourceType: CfnUtilsResourceType.CFN_JSON_STRINGIFY, + ResourceProperties: { + ServiceToken: 'Foo', + RepositoryName: 'MyRepo', + Value: { + test: 'Random', + }, + }, + }; + + // WHEN + const response = await invokeHandler(event); + + // THEN + expect(response).toEqual({ + Data: { + Value: JSON.stringify({ + test: 'Random', + }), + }, + }); +}); + +test('fails if wrong resource type', async () => { + // GIVEN + const event: Partial = { + ResourceType: 'Create', + ResourceProperties: { + ServiceToken: 'Foo', + RepositoryName: 'MyRepo', + }, + }; + + // WHEN + await expect(() => invokeHandler(event)).rejects.toThrow(/unexpected resource type "Create"/); +}); + +// helper function to get around TypeScript expecting a complete event object, +// even though our tests only need some of the fields +async function invokeHandler(event: Partial) { + return handler(event as AWSLambda.CloudFormationCustomResourceEvent); +} \ No newline at end of file