Skip to content

Commit

Permalink
fix(apigateway): enabled property of ApiKeyProps is ignored (#18407)
Browse files Browse the repository at this point in the history
Using nullish coalescing so that it defaults to `true` only if `enabled` is `null` or `undefined`.

Fixes #18402.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
otaviomacedo authored Jan 14, 2022
1 parent 838ec83 commit c31f9b4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-apigateway/lib/api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class ApiKey extends ApiKeyBase {
const resource = new CfnApiKey(this, 'Resource', {
customerId: props.customerId,
description: props.description,
enabled: props.enabled || true,
enabled: props.enabled ?? true,
generateDistinctId: props.generateDistinctId,
name: this.physicalName,
stageKeys: this.renderStageKeys(props.resources),
Expand Down
61 changes: 54 additions & 7 deletions packages/@aws-cdk/aws-apigateway/test/api-key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,33 @@ describe('api key', () => {
// should have an api key with no props defined.
});


test('enabled flag is respected', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new apigateway.ApiKey(stack, 'my-api-key', {
enabled: false,
value: 'arandomstringwithmorethantwentycharacters',
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
Enabled: false,
Value: 'arandomstringwithmorethantwentycharacters',
});
});


test('specify props for apiKey', () => {
// GIVEN
const stack = new cdk.Stack();
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
const api = new apigateway.RestApi(stack, 'test-api', {
cloudWatchRole: false,
deploy: true,
deployOptions: { stageName: 'test' },
});
api.root.addMethod('GET'); // api must have atleast one method.

// WHEN
Expand Down Expand Up @@ -61,7 +84,11 @@ describe('api key', () => {
test('use an imported api key', () => {
// GIVEN
const stack = new cdk.Stack();
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
const api = new apigateway.RestApi(stack, 'test-api', {
cloudWatchRole: false,
deploy: true,
deployOptions: { stageName: 'test' },
});
api.root.addMethod('GET'); // api must have atleast one method.

// WHEN
Expand All @@ -83,7 +110,11 @@ describe('api key', () => {
// GIVEN
const stack = new cdk.Stack();
const user = new iam.User(stack, 'User');
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
const api = new apigateway.RestApi(stack, 'test-api', {
cloudWatchRole: false,
deploy: true,
deployOptions: { stageName: 'test' },
});
api.root.addMethod('GET'); // api must have atleast one method.

// WHEN
Expand Down Expand Up @@ -130,7 +161,11 @@ describe('api key', () => {
// GIVEN
const stack = new cdk.Stack();
const user = new iam.User(stack, 'User');
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
const api = new apigateway.RestApi(stack, 'test-api', {
cloudWatchRole: false,
deploy: true,
deployOptions: { stageName: 'test' },
});
api.root.addMethod('GET'); // api must have atleast one method.

// WHEN
Expand Down Expand Up @@ -182,7 +217,11 @@ describe('api key', () => {
// GIVEN
const stack = new cdk.Stack();
const user = new iam.User(stack, 'User');
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
const api = new apigateway.RestApi(stack, 'test-api', {
cloudWatchRole: false,
deploy: true,
deployOptions: { stageName: 'test' },
});
api.root.addMethod('GET'); // api must have atleast one method.

// WHEN
Expand Down Expand Up @@ -253,7 +292,11 @@ describe('api key', () => {
test('only api key is created when rate limiting properties are not provided', () => {
// GIVEN
const stack = new cdk.Stack();
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
const api = new apigateway.RestApi(stack, 'test-api', {
cloudWatchRole: false,
deploy: true,
deployOptions: { stageName: 'test' },
});
api.root.addMethod('GET'); // api must have atleast one method.

// WHEN
Expand Down Expand Up @@ -281,7 +324,11 @@ describe('api key', () => {
test('api key and usage plan are created and linked when rate limiting properties are provided', () => {
// GIVEN
const stack = new cdk.Stack();
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
const api = new apigateway.RestApi(stack, 'test-api', {
cloudWatchRole: false,
deploy: true,
deployOptions: { stageName: 'test' },
});
api.root.addMethod('GET'); // api must have atleast one method.

// WHEN
Expand Down

0 comments on commit c31f9b4

Please sign in to comment.