Skip to content

Commit 7ac57a0

Browse files
author
Dane Pilcher
committed
test: add unit tests for grantStreamRead
1 parent 6666bb3 commit 7ac57a0

2 files changed

Lines changed: 90 additions & 2 deletions

File tree

packages/amplify-graphql-model-transformer/src/__tests__/amplify-dynamodb-table-construct.test.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import {
66
} from '../resources/amplify-dynamodb-table/amplify-dynamodb-table-construct';
77
import { AttributeType, StreamViewType, TableEncryption } from 'aws-cdk-lib/aws-dynamodb';
88
import { Template } from 'aws-cdk-lib/assertions';
9+
import { Role, ArnPrincipal, PolicyDocument } from 'aws-cdk-lib/aws-iam';
10+
import { Key } from 'aws-cdk-lib/aws-kms';
11+
import { Match } from 'aws-cdk-lib/assertions';
912

1013
describe('Amplify DynamoDB Table Construct Tests', () => {
1114
it('render the default amplify dynamodb table in correct form', () => {
@@ -170,4 +173,90 @@ describe('Amplify DynamoDB Table Construct Tests', () => {
170173
isImported: true,
171174
});
172175
});
176+
177+
describe('grantStreamRead', () => {
178+
it('grants read access table stream', () => {
179+
const stack = new cdk.Stack();
180+
const table = new AmplifyDynamoDBTable(stack, 'MockTable', {
181+
customResourceServiceToken: 'mockResourceServiceToken',
182+
tableName: 'mockTableName',
183+
partitionKey: {
184+
name: 'id',
185+
type: AttributeType.STRING,
186+
},
187+
encryptionKey: new Key(stack, 'MockKey', {}),
188+
stream: StreamViewType.NEW_AND_OLD_IMAGES,
189+
});
190+
table.grantStreamRead(
191+
new Role(stack, 'MockRole', {
192+
assumedBy: new ArnPrincipal('mock_principal'),
193+
}),
194+
);
195+
const template = Template.fromStack(stack);
196+
template.hasResourceProperties('AWS::IAM::Policy', {
197+
PolicyDocument: Match.objectLike({
198+
Statement: Match.arrayWith([
199+
{
200+
Action: ['dynamodb:ListStreams', 'dynamodb:DescribeStream', 'dynamodb:GetRecords', 'dynamodb:GetShardIterator'],
201+
Effect: 'Allow',
202+
Resource: {
203+
'Fn::GetAtt': ['MockTable', 'TableStreamArn'],
204+
},
205+
},
206+
]),
207+
}),
208+
});
209+
});
210+
211+
it('throws when stream arn is undefined', () => {
212+
const stack = new cdk.Stack();
213+
const table = new AmplifyDynamoDBTable(stack, 'MockTable', {
214+
customResourceServiceToken: 'mockResourceServiceToken',
215+
tableName: 'mockTableName',
216+
partitionKey: {
217+
name: 'id',
218+
type: AttributeType.STRING,
219+
},
220+
isImported: true,
221+
});
222+
223+
expect(() =>
224+
table.grantStreamRead(
225+
new Role(stack, 'MockRole', {
226+
assumedBy: new ArnPrincipal('mock_principal'),
227+
}),
228+
),
229+
).toThrow('No stream ARNs found on the table Default/MockTable');
230+
});
231+
});
232+
233+
it('grants access to encryption key', () => {
234+
const stack = new cdk.Stack();
235+
const table = new AmplifyDynamoDBTable(stack, 'MockTable', {
236+
customResourceServiceToken: 'mockResourceServiceToken',
237+
tableName: 'mockTableName',
238+
partitionKey: {
239+
name: 'id',
240+
type: AttributeType.STRING,
241+
},
242+
encryptionKey: new Key(stack, 'MockKey', {}),
243+
stream: StreamViewType.NEW_AND_OLD_IMAGES,
244+
});
245+
table.grantStreamRead(
246+
new Role(stack, 'MockRole', {
247+
assumedBy: new ArnPrincipal('mock_principal'),
248+
}),
249+
);
250+
const template = Template.fromStack(stack);
251+
template.hasResourceProperties('AWS::IAM::Policy', {
252+
PolicyDocument: Match.objectLike({
253+
Statement: Match.arrayWith([
254+
Match.objectLike({
255+
Action: ['kms:Decrypt', 'kms:DescribeKey'],
256+
Effect: 'Allow',
257+
}),
258+
]),
259+
}),
260+
});
261+
});
173262
});

packages/amplify-graphql-model-transformer/src/__tests__/amplify-dynamodb-table-generator.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ describe('ModelTransformer:', () => {
156156
type Post @model @searchable {
157157
id: ID!
158158
title: String!
159-
}
160-
`;
159+
}`;
161160

162161
const out = testTransform({
163162
schema: validSchema,

0 commit comments

Comments
 (0)