Skip to content

Commit

Permalink
chore(apigateway): migrated tests to assertions (aws#18562)
Browse files Browse the repository at this point in the history
----

*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 and TikiTDO committed Feb 21, 2022
1 parent 8aea5c9 commit ad86acd
Show file tree
Hide file tree
Showing 26 changed files with 332 additions and 328 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-apigateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
},
"license": "Apache-2.0",
"devDependencies": {
"@aws-cdk/assert-internal": "0.0.0",
"@aws-cdk/assertions": "0.0.0",
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/cdk-integ-tools": "0.0.0",
"@aws-cdk/cfn2ts": "0.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/@aws-cdk/aws-apigateway/test/access-log.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import '@aws-cdk/assert-internal/jest';
import * as apigateway from '../lib';

describe('access log', () => {
Expand Down
7 changes: 3 additions & 4 deletions packages/@aws-cdk/aws-apigateway/test/api-definition.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import '@aws-cdk/assert-internal/jest';
import { Template } from '@aws-cdk/assertions';
import * as path from 'path';
import { ResourcePart } from '@aws-cdk/assert-internal';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
Expand Down Expand Up @@ -84,12 +83,12 @@ describe('api definition', () => {
apiDefinition: assetApiDefinition,
});

expect(stack).toHaveResource('AWS::ApiGateway::RestApi', {
Template.fromStack(stack).hasResource('AWS::ApiGateway::RestApi', {
Metadata: {
'aws:asset:path': 'asset.68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fb.yaml',
'aws:asset:property': 'BodyS3Location',
},
}, ResourcePart.CompleteDefinition);
});

});
});
Expand Down
44 changes: 22 additions & 22 deletions packages/@aws-cdk/aws-apigateway/test/api-key.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import '@aws-cdk/assert-internal/jest';
import { ResourcePart } from '@aws-cdk/assert-internal';
import { Match, Template } from '@aws-cdk/assertions';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as apigateway from '../lib';
Expand All @@ -13,8 +12,9 @@ describe('api key', () => {
new apigateway.ApiKey(stack, 'my-api-key');

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', undefined, ResourcePart.CompleteDefinition);
// should have an api key with no props defined.
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', {
Enabled: true,
});
});


Expand All @@ -29,7 +29,7 @@ describe('api key', () => {
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', {
Enabled: false,
Value: 'arandomstringwithmorethantwentycharacters',
});
Expand All @@ -53,7 +53,7 @@ describe('api key', () => {
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', {
CustomerId: 'test-customer',
StageKeys: [
{
Expand All @@ -76,7 +76,7 @@ describe('api key', () => {
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', {
Description: 'The most secret api key',
});
});
Expand All @@ -97,7 +97,7 @@ describe('api key', () => {
usagePlan.addApiKey(importedKey);

// THEN
expect(stack).toHaveResourceLike('AWS::ApiGateway::UsagePlanKey', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::UsagePlanKey', {
KeyId: 'KeyIdabc',
KeyType: 'API_KEY',
UsagePlanId: {
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('api key', () => {
apiKey.grantRead(user);

// THEN
expect(stack).toHaveResource('AWS::IAM::Policy', {
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Expand Down Expand Up @@ -176,7 +176,7 @@ describe('api key', () => {
apiKey.grantWrite(user);

// THEN
expect(stack).toHaveResource('AWS::IAM::Policy', {
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Expand Down Expand Up @@ -232,7 +232,7 @@ describe('api key', () => {
apiKey.grantReadWrite(user);

// THEN
expect(stack).toHaveResource('AWS::IAM::Policy', {
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Expand Down Expand Up @@ -282,11 +282,11 @@ describe('api key', () => {

// THEN
// should have an api key with no props defined.
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', undefined, ResourcePart.CompleteDefinition);
Template.fromStack(stack).hasResource('AWS::ApiGateway::ApiKey', Match.anyValue());
// should not have a usage plan.
expect(stack).not.toHaveResource('AWS::ApiGateway::UsagePlan');
Template.fromStack(stack).resourceCountIs('AWS::ApiGateway::UsagePlan', 0);
// should not have a usage plan key.
expect(stack).not.toHaveResource('AWS::ApiGateway::UsagePlanKey');
Template.fromStack(stack).resourceCountIs('AWS::ApiGateway::UsagePlanKey', 0);
});

test('only api key is created when rate limiting properties are not provided', () => {
Expand All @@ -306,7 +306,7 @@ describe('api key', () => {
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', {
CustomerId: 'test-customer',
StageKeys: [
{
Expand All @@ -316,9 +316,9 @@ describe('api key', () => {
],
});
// should not have a usage plan.
expect(stack).not.toHaveResource('AWS::ApiGateway::UsagePlan');
Template.fromStack(stack).resourceCountIs('AWS::ApiGateway::UsagePlan', 0);
// should not have a usage plan key.
expect(stack).not.toHaveResource('AWS::ApiGateway::UsagePlanKey');
Template.fromStack(stack).resourceCountIs('AWS::ApiGateway::UsagePlanKey', 0);
});

test('api key and usage plan are created and linked when rate limiting properties are provided', () => {
Expand All @@ -343,7 +343,7 @@ describe('api key', () => {

// THEN
// should have an api key
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', {
CustomerId: 'test-customer',
StageKeys: [
{
Expand All @@ -353,22 +353,22 @@ describe('api key', () => {
],
});
// should have a usage plan with specified quota.
expect(stack).toHaveResource('AWS::ApiGateway::UsagePlan', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::UsagePlan', {
Quota: {
Limit: 10000,
Period: 'MONTH',
},
}, ResourcePart.Properties);
});
// should have a usage plan key linking the api key and usage plan
expect(stack).toHaveResource('AWS::ApiGateway::UsagePlanKey', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::UsagePlanKey', {
KeyId: {
Ref: 'testapikey998028B6',
},
KeyType: 'API_KEY',
UsagePlanId: {
Ref: 'testapikeyUsagePlanResource66DB63D6',
},
}, ResourcePart.Properties);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '@aws-cdk/assert-internal/jest';
import { Template } from '@aws-cdk/assertions';
import * as cognito from '@aws-cdk/aws-cognito';
import { Duration, Stack } from '@aws-cdk/core';
import { AuthorizationType, CognitoUserPoolsAuthorizer, RestApi } from '../../lib';
Expand All @@ -21,7 +21,7 @@ describe('Cognito Authorizer', () => {
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
Type: 'COGNITO_USER_POOLS',
RestApiId: stack.resolve(restApi.restApiId),
IdentitySource: 'method.request.header.Authorization',
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('Cognito Authorizer', () => {
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
Type: 'COGNITO_USER_POOLS',
Name: 'myauthorizer',
RestApiId: stack.resolve(restApi.restApiId),
Expand Down
35 changes: 17 additions & 18 deletions packages/@aws-cdk/aws-apigateway/test/authorizers/lambda.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import '@aws-cdk/assert-internal/jest';
import { ResourcePart } from '@aws-cdk/assert-internal';
import { Match, Template } from '@aws-cdk/assertions';
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import { Duration, Stack } from '@aws-cdk/core';
Expand All @@ -25,7 +24,7 @@ describe('lambda authorizer', () => {
authorizationType: AuthorizationType.CUSTOM,
});

expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
Type: 'TOKEN',
RestApiId: stack.resolve(restApi.restApiId),
IdentitySource: 'method.request.header.Authorization',
Expand Down Expand Up @@ -71,7 +70,7 @@ describe('lambda authorizer', () => {
},
});

expect(stack).toHaveResource('AWS::Lambda::Permission', {
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Permission', {
Action: 'lambda:InvokeFunction',
Principal: 'apigateway.amazonaws.com',
});
Expand Down Expand Up @@ -100,7 +99,7 @@ describe('lambda authorizer', () => {
authorizationType: AuthorizationType.CUSTOM,
});

expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
Type: 'REQUEST',
RestApiId: stack.resolve(restApi.restApiId),
AuthorizerUri: {
Expand Down Expand Up @@ -145,7 +144,7 @@ describe('lambda authorizer', () => {
},
});

expect(stack).toHaveResource('AWS::Lambda::Permission', {
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Permission', {
Action: 'lambda:InvokeFunction',
Principal: 'apigateway.amazonaws.com',
});
Expand Down Expand Up @@ -194,7 +193,7 @@ describe('lambda authorizer', () => {
authorizationType: AuthorizationType.CUSTOM,
});

expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
Type: 'TOKEN',
RestApiId: stack.resolve(restApi.restApiId),
IdentitySource: 'method.request.header.whoami',
Expand Down Expand Up @@ -266,7 +265,7 @@ describe('lambda authorizer', () => {
authorizationType: AuthorizationType.CUSTOM,
});

expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
Type: 'REQUEST',
RestApiId: stack.resolve(restApi.restApiId),
IdentitySource: 'method.request.header.whoami',
Expand Down Expand Up @@ -340,7 +339,7 @@ describe('lambda authorizer', () => {
authorizationType: AuthorizationType.CUSTOM,
});

expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
Type: 'TOKEN',
RestApiId: stack.resolve(restApi.restApiId),
AuthorizerUri: {
Expand Down Expand Up @@ -385,9 +384,9 @@ describe('lambda authorizer', () => {
},
});

expect(stack).toHaveResource('AWS::IAM::Role');
Template.fromStack(stack).hasResource('AWS::IAM::Role', Match.anyValue());

expect(stack).toHaveResourceLike('AWS::IAM::Policy', {
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
Roles: [
stack.resolve(role.roleName),
],
Expand All @@ -400,9 +399,9 @@ describe('lambda authorizer', () => {
},
],
},
}, ResourcePart.Properties);
});

expect(stack).not.toHaveResource('AWS::Lambda::Permission');
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 0);
});

test('request authorizer with assume role', () => {
Expand Down Expand Up @@ -432,7 +431,7 @@ describe('lambda authorizer', () => {
authorizationType: AuthorizationType.CUSTOM,
});

expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
Type: 'REQUEST',
RestApiId: stack.resolve(restApi.restApiId),
AuthorizerUri: {
Expand Down Expand Up @@ -477,9 +476,9 @@ describe('lambda authorizer', () => {
},
});

expect(stack).toHaveResource('AWS::IAM::Role');
Template.fromStack(stack).hasResource('AWS::IAM::Role', Match.anyValue());

expect(stack).toHaveResourceLike('AWS::IAM::Policy', {
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
Roles: [
stack.resolve(role.roleName),
],
Expand All @@ -492,9 +491,9 @@ describe('lambda authorizer', () => {
},
],
},
}, ResourcePart.Properties);
});

expect(stack).not.toHaveResource('AWS::Lambda::Permission');
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 0);
});

test('token authorizer throws when not attached to a rest api', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '@aws-cdk/assert-internal/jest';
import { Template } from '@aws-cdk/assertions';
import * as acm from '@aws-cdk/aws-certificatemanager';
import * as cdk from '@aws-cdk/core';
import * as apigw from '../lib';
Expand All @@ -22,7 +22,7 @@ describe('BasePathMapping', () => {
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::BasePathMapping', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::BasePathMapping', {
DomainName: { Ref: 'MyDomainE4943FBC' },
RestApiId: { Ref: 'MyApi49610EDF' },
});
Expand All @@ -47,7 +47,7 @@ describe('BasePathMapping', () => {
});

// THEN
expect(stack).toHaveResourceLike('AWS::ApiGateway::BasePathMapping', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::BasePathMapping', {
BasePath: 'My_B45E-P4th',
});
});
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('BasePathMapping', () => {
});

// THEN
expect(stack).toHaveResourceLike('AWS::ApiGateway::BasePathMapping', {
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::BasePathMapping', {
Stage: { Ref: 'MyStage572B0482' },
});
});
Expand Down

0 comments on commit ad86acd

Please sign in to comment.