Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(apigateway): migrated tests to assertions #18562

Merged
merged 6 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 20 additions & 21 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,7 +12,7 @@ describe('api key', () => {
new apigateway.ApiKey(stack, 'my-api-key');

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

Expand All @@ -29,7 +28,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 +52,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 +75,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 +96,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 +124,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 +175,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 +231,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 +281,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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this isn't exactly "should have an api key with no props defined". We should be able to assert this (reference PR):

Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', Match.absent());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, makes sense. I'll fix it.

// 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 +305,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 +315,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 +342,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 +352,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 All @@ -51,7 +50,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 @@ -80,7 +79,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 All @@ -105,7 +104,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 @@ -154,7 +153,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 @@ -206,7 +205,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 @@ -260,7 +259,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 All @@ -285,9 +284,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 @@ -300,9 +299,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 @@ -332,7 +331,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 All @@ -357,9 +356,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 @@ -372,9 +371,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
Loading