Skip to content

Commit

Permalink
test(core): unit tests for cfn utils provider handler (#27759)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
lpizzinidev committed Nov 2, 2023
1 parent d449cfd commit b02752b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "1e14e895fcbdf65feb0a29e4aa74c6c92a6fb0e41f228bef7ab23627ed409cde.zip"
"S3Key": "3e61d858eaa7724170872a455b8f788d5fcf18adba89aadbe603a52dab59d917.zip"
},
"Timeout": 900,
"MemorySize": 128,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AWSLambda.CloudFormationCustomResourceEvent> = {
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<AWSLambda.CloudFormationCustomResourceEvent> = {
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<AWSLambda.CloudFormationCustomResourceEvent> = {
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<AWSLambda.CloudFormationCustomResourceEvent>) {
return handler(event as AWSLambda.CloudFormationCustomResourceEvent);
}

0 comments on commit b02752b

Please sign in to comment.