Skip to content

Commit

Permalink
Added more test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonife committed Feb 26, 2024
1 parent 050ee57 commit 240e8f8
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion packages/aws-cdk-lib/aws-lambda-event-sources/test/s3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,60 @@ describe('S3EventSource', () => {
'SourceArn': 'arn:aws:s3:::some-bucket-not-in-this-account',
});
});
});

test('Test bucket account is referenced intrinsicly', () => {
// GIVEN
const stack = new cdk.Stack();
const fn = new TestFunction(stack, 'Fn');
const bucket = new s3.Bucket(stack, 'B');

// WHEN
fn.addEventSource(new sources.S3EventSource(bucket, {
events: [s3.EventType.OBJECT_CREATED, s3.EventType.OBJECT_REMOVED],
filters: [
{ prefix: 'prefix/' },
{ suffix: '.png' },
],
}));

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Permission', {
'Principal': 's3.amazonaws.com',
'SourceAccount': {
'Ref': 'AWS::AccountId',
},
'SourceArn': {
'Fn::GetAtt': ['B08E7C7AF', 'Arn'],
},
});
});

test('Default to stack account if bucket account doesnt exist', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'stack');
const fn = new TestFunction(stack, 'Fn');

let accountB = '';
//WHEN
const foreignBucket =
s3.Bucket.fromBucketAttributes(stack, 'ImportedBucket', {
bucketArn: 'arn:aws:s3:::some-bucket-not-in-this-account',
// The account the bucket really lives in
account: accountB,
});

// This will generate the IAM bindings
fn.addEventSource(new sources.S3EventSource(foreignBucket as s3.Bucket,
{ events: [s3.EventType.OBJECT_CREATED] }));

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Permission', {
'Principal': 's3.amazonaws.com',
'SourceAccount': {
'Ref': 'AWS::AccountId',
},
'SourceArn': 'arn:aws:s3:::some-bucket-not-in-this-account',
});
});
});

0 comments on commit 240e8f8

Please sign in to comment.