Skip to content

Commit e0b7d99

Browse files
authored
chore(apigateway): migrated tests to assertions (#18562)
---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 67f3b8d commit e0b7d99

26 files changed

+332
-328
lines changed

packages/@aws-cdk/aws-apigateway/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
},
8080
"license": "Apache-2.0",
8181
"devDependencies": {
82-
"@aws-cdk/assert-internal": "0.0.0",
82+
"@aws-cdk/assertions": "0.0.0",
8383
"@aws-cdk/cdk-build-tools": "0.0.0",
8484
"@aws-cdk/cdk-integ-tools": "0.0.0",
8585
"@aws-cdk/cfn2ts": "0.0.0",

packages/@aws-cdk/aws-apigateway/test/access-log.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import '@aws-cdk/assert-internal/jest';
21
import * as apigateway from '../lib';
32

43
describe('access log', () => {

packages/@aws-cdk/aws-apigateway/test/api-definition.test.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import '@aws-cdk/assert-internal/jest';
1+
import { Template } from '@aws-cdk/assertions';
22
import * as path from 'path';
3-
import { ResourcePart } from '@aws-cdk/assert-internal';
43
import * as s3 from '@aws-cdk/aws-s3';
54
import * as cdk from '@aws-cdk/core';
65
import * as cxapi from '@aws-cdk/cx-api';
@@ -84,12 +83,12 @@ describe('api definition', () => {
8483
apiDefinition: assetApiDefinition,
8584
});
8685

87-
expect(stack).toHaveResource('AWS::ApiGateway::RestApi', {
86+
Template.fromStack(stack).hasResource('AWS::ApiGateway::RestApi', {
8887
Metadata: {
8988
'aws:asset:path': 'asset.68497ac876de4e963fc8f7b5f1b28844c18ecc95e3f7c6e9e0bf250e03c037fb.yaml',
9089
'aws:asset:property': 'BodyS3Location',
9190
},
92-
}, ResourcePart.CompleteDefinition);
91+
});
9392

9493
});
9594
});

packages/@aws-cdk/aws-apigateway/test/api-key.test.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import '@aws-cdk/assert-internal/jest';
2-
import { ResourcePart } from '@aws-cdk/assert-internal';
1+
import { Match, Template } from '@aws-cdk/assertions';
32
import * as iam from '@aws-cdk/aws-iam';
43
import * as cdk from '@aws-cdk/core';
54
import * as apigateway from '../lib';
@@ -13,8 +12,9 @@ describe('api key', () => {
1312
new apigateway.ApiKey(stack, 'my-api-key');
1413

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

2020

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

3131
// THEN
32-
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
32+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', {
3333
Enabled: false,
3434
Value: 'arandomstringwithmorethantwentycharacters',
3535
});
@@ -53,7 +53,7 @@ describe('api key', () => {
5353
});
5454

5555
// THEN
56-
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
56+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', {
5757
CustomerId: 'test-customer',
5858
StageKeys: [
5959
{
@@ -76,7 +76,7 @@ describe('api key', () => {
7676
});
7777

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

9999
// THEN
100-
expect(stack).toHaveResourceLike('AWS::ApiGateway::UsagePlanKey', {
100+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::UsagePlanKey', {
101101
KeyId: 'KeyIdabc',
102102
KeyType: 'API_KEY',
103103
UsagePlanId: {
@@ -125,7 +125,7 @@ describe('api key', () => {
125125
apiKey.grantRead(user);
126126

127127
// THEN
128-
expect(stack).toHaveResource('AWS::IAM::Policy', {
128+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
129129
PolicyDocument: {
130130
Statement: [
131131
{
@@ -176,7 +176,7 @@ describe('api key', () => {
176176
apiKey.grantWrite(user);
177177

178178
// THEN
179-
expect(stack).toHaveResource('AWS::IAM::Policy', {
179+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
180180
PolicyDocument: {
181181
Statement: [
182182
{
@@ -232,7 +232,7 @@ describe('api key', () => {
232232
apiKey.grantReadWrite(user);
233233

234234
// THEN
235-
expect(stack).toHaveResource('AWS::IAM::Policy', {
235+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
236236
PolicyDocument: {
237237
Statement: [
238238
{
@@ -282,11 +282,11 @@ describe('api key', () => {
282282

283283
// THEN
284284
// should have an api key with no props defined.
285-
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', undefined, ResourcePart.CompleteDefinition);
285+
Template.fromStack(stack).hasResource('AWS::ApiGateway::ApiKey', Match.anyValue());
286286
// should not have a usage plan.
287-
expect(stack).not.toHaveResource('AWS::ApiGateway::UsagePlan');
287+
Template.fromStack(stack).resourceCountIs('AWS::ApiGateway::UsagePlan', 0);
288288
// should not have a usage plan key.
289-
expect(stack).not.toHaveResource('AWS::ApiGateway::UsagePlanKey');
289+
Template.fromStack(stack).resourceCountIs('AWS::ApiGateway::UsagePlanKey', 0);
290290
});
291291

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

308308
// THEN
309-
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
309+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', {
310310
CustomerId: 'test-customer',
311311
StageKeys: [
312312
{
@@ -316,9 +316,9 @@ describe('api key', () => {
316316
],
317317
});
318318
// should not have a usage plan.
319-
expect(stack).not.toHaveResource('AWS::ApiGateway::UsagePlan');
319+
Template.fromStack(stack).resourceCountIs('AWS::ApiGateway::UsagePlan', 0);
320320
// should not have a usage plan key.
321-
expect(stack).not.toHaveResource('AWS::ApiGateway::UsagePlanKey');
321+
Template.fromStack(stack).resourceCountIs('AWS::ApiGateway::UsagePlanKey', 0);
322322
});
323323

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

344344
// THEN
345345
// should have an api key
346-
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
346+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::ApiKey', {
347347
CustomerId: 'test-customer',
348348
StageKeys: [
349349
{
@@ -353,22 +353,22 @@ describe('api key', () => {
353353
],
354354
});
355355
// should have a usage plan with specified quota.
356-
expect(stack).toHaveResource('AWS::ApiGateway::UsagePlan', {
356+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::UsagePlan', {
357357
Quota: {
358358
Limit: 10000,
359359
Period: 'MONTH',
360360
},
361-
}, ResourcePart.Properties);
361+
});
362362
// should have a usage plan key linking the api key and usage plan
363-
expect(stack).toHaveResource('AWS::ApiGateway::UsagePlanKey', {
363+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::UsagePlanKey', {
364364
KeyId: {
365365
Ref: 'testapikey998028B6',
366366
},
367367
KeyType: 'API_KEY',
368368
UsagePlanId: {
369369
Ref: 'testapikeyUsagePlanResource66DB63D6',
370370
},
371-
}, ResourcePart.Properties);
371+
});
372372
});
373373
});
374374
});

packages/@aws-cdk/aws-apigateway/test/authorizers/cognito.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import '@aws-cdk/assert-internal/jest';
1+
import { Template } from '@aws-cdk/assertions';
22
import * as cognito from '@aws-cdk/aws-cognito';
33
import { Duration, Stack } from '@aws-cdk/core';
44
import { AuthorizationType, CognitoUserPoolsAuthorizer, RestApi } from '../../lib';
@@ -21,7 +21,7 @@ describe('Cognito Authorizer', () => {
2121
});
2222

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

5454
// THEN
55-
expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
55+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
5656
Type: 'COGNITO_USER_POOLS',
5757
Name: 'myauthorizer',
5858
RestApiId: stack.resolve(restApi.restApiId),

packages/@aws-cdk/aws-apigateway/test/authorizers/lambda.test.ts

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import '@aws-cdk/assert-internal/jest';
2-
import { ResourcePart } from '@aws-cdk/assert-internal';
1+
import { Match, Template } from '@aws-cdk/assertions';
32
import * as iam from '@aws-cdk/aws-iam';
43
import * as lambda from '@aws-cdk/aws-lambda';
54
import { Duration, Stack } from '@aws-cdk/core';
@@ -25,7 +24,7 @@ describe('lambda authorizer', () => {
2524
authorizationType: AuthorizationType.CUSTOM,
2625
});
2726

28-
expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
27+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
2928
Type: 'TOKEN',
3029
RestApiId: stack.resolve(restApi.restApiId),
3130
IdentitySource: 'method.request.header.Authorization',
@@ -71,7 +70,7 @@ describe('lambda authorizer', () => {
7170
},
7271
});
7372

74-
expect(stack).toHaveResource('AWS::Lambda::Permission', {
73+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Permission', {
7574
Action: 'lambda:InvokeFunction',
7675
Principal: 'apigateway.amazonaws.com',
7776
});
@@ -100,7 +99,7 @@ describe('lambda authorizer', () => {
10099
authorizationType: AuthorizationType.CUSTOM,
101100
});
102101

103-
expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
102+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
104103
Type: 'REQUEST',
105104
RestApiId: stack.resolve(restApi.restApiId),
106105
AuthorizerUri: {
@@ -145,7 +144,7 @@ describe('lambda authorizer', () => {
145144
},
146145
});
147146

148-
expect(stack).toHaveResource('AWS::Lambda::Permission', {
147+
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Permission', {
149148
Action: 'lambda:InvokeFunction',
150149
Principal: 'apigateway.amazonaws.com',
151150
});
@@ -194,7 +193,7 @@ describe('lambda authorizer', () => {
194193
authorizationType: AuthorizationType.CUSTOM,
195194
});
196195

197-
expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
196+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
198197
Type: 'TOKEN',
199198
RestApiId: stack.resolve(restApi.restApiId),
200199
IdentitySource: 'method.request.header.whoami',
@@ -266,7 +265,7 @@ describe('lambda authorizer', () => {
266265
authorizationType: AuthorizationType.CUSTOM,
267266
});
268267

269-
expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
268+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
270269
Type: 'REQUEST',
271270
RestApiId: stack.resolve(restApi.restApiId),
272271
IdentitySource: 'method.request.header.whoami',
@@ -340,7 +339,7 @@ describe('lambda authorizer', () => {
340339
authorizationType: AuthorizationType.CUSTOM,
341340
});
342341

343-
expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
342+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
344343
Type: 'TOKEN',
345344
RestApiId: stack.resolve(restApi.restApiId),
346345
AuthorizerUri: {
@@ -385,9 +384,9 @@ describe('lambda authorizer', () => {
385384
},
386385
});
387386

388-
expect(stack).toHaveResource('AWS::IAM::Role');
387+
Template.fromStack(stack).hasResource('AWS::IAM::Role', Match.anyValue());
389388

390-
expect(stack).toHaveResourceLike('AWS::IAM::Policy', {
389+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
391390
Roles: [
392391
stack.resolve(role.roleName),
393392
],
@@ -400,9 +399,9 @@ describe('lambda authorizer', () => {
400399
},
401400
],
402401
},
403-
}, ResourcePart.Properties);
402+
});
404403

405-
expect(stack).not.toHaveResource('AWS::Lambda::Permission');
404+
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 0);
406405
});
407406

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

435-
expect(stack).toHaveResource('AWS::ApiGateway::Authorizer', {
434+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
436435
Type: 'REQUEST',
437436
RestApiId: stack.resolve(restApi.restApiId),
438437
AuthorizerUri: {
@@ -477,9 +476,9 @@ describe('lambda authorizer', () => {
477476
},
478477
});
479478

480-
expect(stack).toHaveResource('AWS::IAM::Role');
479+
Template.fromStack(stack).hasResource('AWS::IAM::Role', Match.anyValue());
481480

482-
expect(stack).toHaveResourceLike('AWS::IAM::Policy', {
481+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
483482
Roles: [
484483
stack.resolve(role.roleName),
485484
],
@@ -492,9 +491,9 @@ describe('lambda authorizer', () => {
492491
},
493492
],
494493
},
495-
}, ResourcePart.Properties);
494+
});
496495

497-
expect(stack).not.toHaveResource('AWS::Lambda::Permission');
496+
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 0);
498497
});
499498

500499
test('token authorizer throws when not attached to a rest api', () => {

packages/@aws-cdk/aws-apigateway/test/base-path-mapping.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import '@aws-cdk/assert-internal/jest';
1+
import { Template } from '@aws-cdk/assertions';
22
import * as acm from '@aws-cdk/aws-certificatemanager';
33
import * as cdk from '@aws-cdk/core';
44
import * as apigw from '../lib';
@@ -22,7 +22,7 @@ describe('BasePathMapping', () => {
2222
});
2323

2424
// THEN
25-
expect(stack).toHaveResource('AWS::ApiGateway::BasePathMapping', {
25+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::BasePathMapping', {
2626
DomainName: { Ref: 'MyDomainE4943FBC' },
2727
RestApiId: { Ref: 'MyApi49610EDF' },
2828
});
@@ -47,7 +47,7 @@ describe('BasePathMapping', () => {
4747
});
4848

4949
// THEN
50-
expect(stack).toHaveResourceLike('AWS::ApiGateway::BasePathMapping', {
50+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::BasePathMapping', {
5151
BasePath: 'My_B45E-P4th',
5252
});
5353
});
@@ -100,7 +100,7 @@ describe('BasePathMapping', () => {
100100
});
101101

102102
// THEN
103-
expect(stack).toHaveResourceLike('AWS::ApiGateway::BasePathMapping', {
103+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::BasePathMapping', {
104104
Stage: { Ref: 'MyStage572B0482' },
105105
});
106106
});

0 commit comments

Comments
 (0)