|
6 | 6 | } from '../resources/amplify-dynamodb-table/amplify-dynamodb-table-construct'; |
7 | 7 | import { AttributeType, StreamViewType, TableEncryption } from 'aws-cdk-lib/aws-dynamodb'; |
8 | 8 | 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'; |
9 | 12 |
|
10 | 13 | describe('Amplify DynamoDB Table Construct Tests', () => { |
11 | 14 | it('render the default amplify dynamodb table in correct form', () => { |
@@ -170,4 +173,90 @@ describe('Amplify DynamoDB Table Construct Tests', () => { |
170 | 173 | isImported: true, |
171 | 174 | }); |
172 | 175 | }); |
| 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 | + }); |
173 | 262 | }); |
0 commit comments