Skip to content

Commit 67f3b8d

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

20 files changed

+119
-126
lines changed

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

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

packages/@aws-cdk/aws-iam/test/access-key.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 { App, Stack } from '@aws-cdk/core';
33
import { AccessKey, AccessKeyStatus, User } from '../lib';
44

@@ -13,7 +13,7 @@ describe('IAM Access keys', () => {
1313
new AccessKey(stack, 'MyAccessKey', { user });
1414

1515
// THEN
16-
expect(stack).toMatchTemplate({
16+
Template.fromStack(stack).templateMatches({
1717
Resources: {
1818
MyUserDC45028B: {
1919
Type: 'AWS::IAM::User',
@@ -38,7 +38,7 @@ describe('IAM Access keys', () => {
3838
new AccessKey(stack, 'MyAccessKey', { user, status: AccessKeyStatus.ACTIVE });
3939

4040
// THEN
41-
expect(stack).toHaveResourceLike('AWS::IAM::AccessKey', { Status: 'Active' });
41+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::AccessKey', { Status: 'Active' });
4242
});
4343

4444
test('inactive status is specified with correct capitalization', () => {
@@ -54,7 +54,7 @@ describe('IAM Access keys', () => {
5454
});
5555

5656
// THEN
57-
expect(stack).toHaveResourceLike('AWS::IAM::AccessKey', {
57+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::AccessKey', {
5858
Status: 'Inactive',
5959
});
6060
});

packages/@aws-cdk/aws-iam/test/auto-cross-stack-refs.test.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { SynthUtils } from '@aws-cdk/assert-internal';
2-
import '@aws-cdk/assert-internal/jest';
1+
import { Template } from '@aws-cdk/assertions';
32
import * as cdk from '@aws-cdk/core';
43
import * as iam from '../lib';
54

@@ -25,7 +24,7 @@ describe('automatic cross-stack references', () => {
2524
//
2625

2726
// THEN
28-
expect(stackWithUser).toMatchTemplate({
27+
Template.fromStack(stackWithUser).templateMatches({
2928
Resources: {
3029
User00B015A1: {
3130
Type: 'AWS::IAM::User',
@@ -35,7 +34,7 @@ describe('automatic cross-stack references', () => {
3534
},
3635
},
3736
});
38-
expect(stackWithGroup).toMatchTemplate({
37+
Template.fromStack(stackWithGroup).templateMatches({
3938
Outputs: {
4039
ExportsOutputRefGroupC77FDACD8CF7DD5B: {
4140
Value: { Ref: 'GroupC77FDACD' },
@@ -59,6 +58,6 @@ describe('automatic cross-stack references', () => {
5958
group.addUser(user);
6059

6160
// THEN
62-
expect(() => SynthUtils.synthesize(stack1)).toThrow(/Cannot reference across apps/);
61+
expect(() => cdk.App.of(stack1)!.synth()).toThrow(/Cannot reference across apps/);
6362
});
6463
});

packages/@aws-cdk/aws-iam/test/cross-account.test.ts

+6-6
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 cdk from '@aws-cdk/core';
33
import * as constructs from 'constructs';
44
import * as iam from '../lib';
@@ -191,7 +191,7 @@ function doGrant(resource: FakeResource, principal: iam.IPrincipal) {
191191
}
192192

193193
function assertTrustCreated(stack: cdk.Stack, principal: any) {
194-
expect(stack).toHaveResource('Test::Fake::Resource', {
194+
Template.fromStack(stack).hasResourceProperties('Test::Fake::Resource', {
195195
ResourcePolicy: {
196196
Statement: [
197197
{
@@ -207,17 +207,17 @@ function assertTrustCreated(stack: cdk.Stack, principal: any) {
207207
}
208208

209209
function noTrustCreated(stack: cdk.Stack) {
210-
expect(stack).not.toHaveResourceLike('Test::Fake::Resource', {
210+
expect(Template.fromStack(stack).findResources('Test::Fake::Resource', {
211211
ResourcePolicy: {
212212
Statement: [
213213
{},
214214
],
215215
},
216-
});
216+
})).toEqual({});
217217
}
218218

219219
function assertPolicyCreated(stack: cdk.Stack) {
220-
expect(stack).toHaveResource('AWS::IAM::Policy', {
220+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
221221
PolicyDocument: {
222222
Statement: [
223223
{
@@ -232,5 +232,5 @@ function assertPolicyCreated(stack: cdk.Stack) {
232232
}
233233

234234
function noPolicyCreated(stack: cdk.Stack) {
235-
expect(stack).not.toHaveResource('AWS::IAM::Policy');
235+
Template.fromStack(stack).resourceCountIs('AWS::IAM::Policy', 0);
236236
}

packages/@aws-cdk/aws-iam/test/escape-hatch.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// tests for the L1 escape hatches (overrides). those are in the IAM module
22
// because we want to verify them end-to-end, as a complement to the unit
33
// tests in the @aws-cdk/core module
4-
import '@aws-cdk/assert-internal/jest';
4+
import { Template } from '@aws-cdk/assertions';
55
import { Stack } from '@aws-cdk/core';
66
import * as iam from '../lib';
77

@@ -17,7 +17,7 @@ describe('IAM escape hatches', () => {
1717
const cfn = user.node.findChild('Resource') as iam.CfnUser;
1818
cfn.addPropertyOverride('UserName', 'OverriddenUserName');
1919

20-
expect(stack).toMatchTemplate({
20+
Template.fromStack(stack).templateMatches({
2121
'Resources': {
2222
'user2C2B57AE': {
2323
'Type': 'AWS::IAM::User',
@@ -39,7 +39,7 @@ describe('IAM escape hatches', () => {
3939
cfn.addPropertyOverride('Hello.World', 'Boom');
4040

4141
// THEN
42-
expect(stack).toMatchTemplate({
42+
Template.fromStack(stack).templateMatches({
4343
'Resources': {
4444
'user2C2B57AE': {
4545
'Type': 'AWS::IAM::User',
@@ -69,7 +69,7 @@ describe('IAM escape hatches', () => {
6969
cfn.addOverride('UpdatePolicy.UseOnlineResharding.Type', 'None');
7070

7171
// THEN
72-
expect(stack).toMatchTemplate({
72+
Template.fromStack(stack).templateMatches({
7373
'Resources': {
7474
'user2C2B57AE': {
7575
'Type': 'AWS::IAM::User',

packages/@aws-cdk/aws-iam/test/grant.test.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { ResourcePart } from '@aws-cdk/assert-internal';
2-
import '@aws-cdk/assert-internal/jest';
1+
import { Template } from '@aws-cdk/assertions';
32
import { CfnResource, Resource, Stack } from '@aws-cdk/core';
43
import { Construct } from 'constructs';
54
import * as iam from '../lib';
@@ -109,9 +108,9 @@ function applyGrantWithDependencyTo(principal: iam.IPrincipal) {
109108
}
110109

111110
function expectDependencyOn(id: string) {
112-
expect(stack).toHaveResource('CDK::Test::SomeResource', (props: any) => {
111+
Template.fromStack(stack).hasResource('CDK::Test::SomeResource', (props: any) => {
113112
return (props?.DependsOn ?? []).includes(id);
114-
}, ResourcePart.CompleteDefinition);
113+
});
115114
}
116115

117116
class FakeResourceWithPolicy extends Resource implements iam.IResourceWithPolicy {

packages/@aws-cdk/aws-iam/test/group.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 { App, Stack } from '@aws-cdk/core';
33
import { Group, ManagedPolicy, User } from '../lib';
44

@@ -8,7 +8,7 @@ describe('IAM groups', () => {
88
const stack = new Stack(app, 'MyStack');
99
new Group(stack, 'MyGroup');
1010

11-
expect(stack).toMatchTemplate({
11+
Template.fromStack(stack).templateMatches({
1212
Resources: { MyGroupCBA54B1B: { Type: 'AWS::IAM::Group' } },
1313
});
1414
});
@@ -22,7 +22,7 @@ describe('IAM groups', () => {
2222
user1.addToGroup(group);
2323
group.addUser(user2);
2424

25-
expect(stack).toMatchTemplate({
25+
Template.fromStack(stack).templateMatches({
2626
Resources:
2727
{
2828
MyGroupCBA54B1B: { Type: 'AWS::IAM::Group' },
@@ -50,7 +50,7 @@ describe('IAM groups', () => {
5050
});
5151

5252
// THEN
53-
expect(stack).toHaveResource('AWS::IAM::Group', {
53+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Group', {
5454
ManagedPolicyArns: [
5555
{ 'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':iam::aws:policy/asdf']] },
5656
],

packages/@aws-cdk/aws-iam/test/immutable-role.test.ts

+6-6
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 { Stack } from '@aws-cdk/core';
33
import * as iam from '../lib';
44

@@ -33,7 +33,7 @@ describe('ImmutableRole', () => {
3333

3434
immutableRole.attachInlinePolicy(policy);
3535

36-
expect(stack).toHaveResource('AWS::IAM::Policy', {
36+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
3737
'PolicyDocument': {
3838
'Statement': [
3939
{
@@ -58,7 +58,7 @@ describe('ImmutableRole', () => {
5858

5959
immutableRole.addManagedPolicy({ managedPolicyArn: 'Arn2' });
6060

61-
expect(stack).toHaveResourceLike('AWS::IAM::Role', {
61+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', {
6262
'ManagedPolicyArns': [
6363
'Arn1',
6464
],
@@ -76,7 +76,7 @@ describe('ImmutableRole', () => {
7676
actions: ['s3:*'],
7777
}));
7878

79-
expect(stack).toHaveResource('AWS::IAM::Policy', {
79+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
8080
'PolicyDocument': {
8181
'Version': '2012-10-17',
8282
'Statement': [
@@ -98,7 +98,7 @@ describe('ImmutableRole', () => {
9898
resourceArns: ['*'],
9999
});
100100

101-
expect(stack).not.toHaveResourceLike('AWS::IAM::Policy', {
101+
expect(Template.fromStack(stack).findResources('AWS::IAM::Policy', {
102102
'PolicyDocument': {
103103
'Statement': [
104104
{
@@ -108,7 +108,7 @@ describe('ImmutableRole', () => {
108108
},
109109
],
110110
},
111-
});
111+
})).toEqual({});
112112
});
113113

114114
// this pattern is used here:

packages/@aws-cdk/aws-iam/test/lazy-role.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 cdk from '@aws-cdk/core';
33
import * as iam from '../lib';
44

@@ -13,7 +13,7 @@ describe('IAM lazy role', () => {
1313
});
1414

1515
// THEN
16-
expect(stack).not.toHaveResource('AWS::IAM::Role');
16+
Template.fromStack(stack).resourceCountIs('AWS::IAM::Role', 0);
1717
});
1818

1919
test('creates the resource when a property is read', () => {
@@ -27,7 +27,7 @@ describe('IAM lazy role', () => {
2727

2828
// THEN
2929
expect(roleArn).not.toBeNull();
30-
expect(stack).toHaveResource('AWS::IAM::Role', {
30+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', {
3131
AssumeRolePolicyDocument: {
3232
Version: '2012-10-17',
3333
Statement: [{

packages/@aws-cdk/aws-iam/test/managed-policy.test.ts

+12-12
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 cdk from '@aws-cdk/core';
33
import { Group, ManagedPolicy, PolicyDocument, PolicyStatement, Role, ServicePrincipal, User } from '../lib';
44

@@ -49,7 +49,7 @@ describe('managed policy', () => {
4949
const group = new Group(stack, 'MyGroup');
5050
group.addManagedPolicy(policy);
5151

52-
expect(stack).toMatchTemplate({
52+
Template.fromStack(stack).templateMatches({
5353
Resources: {
5454
MyManagedPolicy9F3720AE: {
5555
Type: 'AWS::IAM::ManagedPolicy',
@@ -89,7 +89,7 @@ describe('managed policy', () => {
8989
}),
9090
});
9191

92-
expect(stack).toMatchTemplate({
92+
Template.fromStack(stack).templateMatches({
9393
Resources: {
9494
MyManagedPolicy9F3720AE: {
9595
Type: 'AWS::IAM::ManagedPolicy',
@@ -120,7 +120,7 @@ describe('managed policy', () => {
120120
statements: [new PolicyStatement({ resources: ['arn'], actions: ['sns:Subscribe'] })],
121121
});
122122

123-
expect(stack).toMatchTemplate({
123+
Template.fromStack(stack).templateMatches({
124124
Resources: {
125125
MyManagedPolicy9F3720AE: {
126126
Type: 'AWS::IAM::ManagedPolicy',
@@ -148,7 +148,7 @@ describe('managed policy', () => {
148148
const group = new Group(stack, 'MyGroup');
149149
group.addManagedPolicy(policy);
150150

151-
expect(stack).toMatchTemplate({
151+
Template.fromStack(stack).templateMatches({
152152
Resources: {
153153
MyManagedPolicy9F3720AE: {
154154
Type: 'AWS::IAM::ManagedPolicy',
@@ -192,7 +192,7 @@ describe('managed policy', () => {
192192
statements: [new PolicyStatement({ resources: ['*'], actions: ['dynamodb:PutItem'] })],
193193
});
194194

195-
expect(stack).toMatchTemplate({
195+
Template.fromStack(stack).templateMatches({
196196
Resources: {
197197
User1E278A736: { Type: 'AWS::IAM::User' },
198198
Group1BEBD4686: { Type: 'AWS::IAM::Group' },
@@ -248,7 +248,7 @@ describe('managed policy', () => {
248248
p.attachToRole(role);
249249
p.attachToRole(role);
250250

251-
expect(stack).toMatchTemplate({
251+
Template.fromStack(stack).templateMatches({
252252
Resources: {
253253
MyManagedPolicy9F3720AE: {
254254
Type: 'AWS::IAM::ManagedPolicy',
@@ -295,7 +295,7 @@ describe('managed policy', () => {
295295
p.attachToRole(new Role(stack, 'Role1', { assumedBy: new ServicePrincipal('test.service') }));
296296
p.addStatements(new PolicyStatement({ resources: ['*'], actions: ['dynamodb:GetItem'] }));
297297

298-
expect(stack).toMatchTemplate({
298+
Template.fromStack(stack).templateMatches({
299299
Resources: {
300300
MyManagedPolicy9F3720AE: {
301301
Type: 'AWS::IAM::ManagedPolicy',
@@ -346,7 +346,7 @@ describe('managed policy', () => {
346346

347347
policy.addStatements(new PolicyStatement({ resources: ['*'], actions: ['*'] }));
348348

349-
expect(stack).toMatchTemplate({
349+
Template.fromStack(stack).templateMatches({
350350
Resources: {
351351
MyManagedPolicy9F3720AE: {
352352
Type: 'AWS::IAM::ManagedPolicy',
@@ -390,7 +390,7 @@ describe('managed policy', () => {
390390
group.addManagedPolicy(policy);
391391
role.addManagedPolicy(policy);
392392

393-
expect(stack).toMatchTemplate({
393+
Template.fromStack(stack).templateMatches({
394394
Resources: {
395395
MyUserDC45028B: {
396396
Type: 'AWS::IAM::User',
@@ -466,7 +466,7 @@ describe('managed policy', () => {
466466
group.addManagedPolicy(policy);
467467
role.addManagedPolicy(policy);
468468

469-
expect(stack).toMatchTemplate({
469+
Template.fromStack(stack).templateMatches({
470470
Resources: {
471471
MyUserDC45028B: {
472472
Type: 'AWS::IAM::User',
@@ -594,7 +594,7 @@ describe('managed policy', () => {
594594
value: mp.managedPolicyArn,
595595
});
596596

597-
expect(stack2).toMatchTemplate({
597+
Template.fromStack(stack2).templateMatches({
598598
Outputs: {
599599
Output: {
600600
Value: {

0 commit comments

Comments
 (0)