-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
notification.test.ts
188 lines (158 loc) · 5.58 KB
/
notification.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { Template } from '@aws-cdk/assertions';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as s3 from '../lib';
describe('notification', () => {
test('when notification is added a custom s3 bucket notification resource is provisioned', () => {
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'MyBucket');
bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC,
}),
});
Template.fromStack(stack).resourceCountIs('AWS::S3::Bucket', 1);
Template.fromStack(stack).hasResourceProperties('Custom::S3BucketNotifications', {
NotificationConfiguration: {
TopicConfigurations: [
{
Events: [
's3:ObjectCreated:*',
],
TopicArn: 'ARN',
},
],
},
});
});
test('can specify a custom role for the notifications handler of imported buckets', () => {
const stack = new cdk.Stack();
const importedRole = iam.Role.fromRoleArn(stack, 'role', 'arn:aws:iam::111111111111:role/DevsNotAllowedToTouch');
const bucket = s3.Bucket.fromBucketAttributes(stack, 'MyBucket', {
bucketName: 'foo-bar',
notificationsHandlerRole: importedRole,
});
bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC,
}),
});
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
Description: 'AWS CloudFormation handler for "Custom::S3BucketNotifications" resources (@aws-cdk/aws-s3)',
Role: 'arn:aws:iam::111111111111:role/DevsNotAllowedToTouch',
});
});
test('can specify prefix and suffix filter rules', () => {
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'MyBucket');
bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC,
}),
}, { prefix: 'images/', suffix: '.png' });
Template.fromStack(stack).hasResourceProperties('Custom::S3BucketNotifications', {
NotificationConfiguration: {
TopicConfigurations: [
{
Events: [
's3:ObjectCreated:*',
],
Filter: {
Key: {
FilterRules: [
{
Name: 'suffix',
Value: '.png',
},
{
Name: 'prefix',
Value: 'images/',
},
],
},
},
TopicArn: 'ARN',
},
],
},
});
});
test('the notification lambda handler must depend on the role to prevent executing too early', () => {
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'MyBucket');
bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC,
}),
});
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
Type: 'AWS::Lambda::Function',
Properties: {
Role: {
'Fn::GetAtt': [
'BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC',
'Arn',
],
},
},
DependsOn: ['BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleDefaultPolicy2CF63D36',
'BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC'],
});
});
test('throws with multiple prefix rules in a filter', () => {
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'MyBucket');
expect(() => bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC,
}),
}, { prefix: 'images/' }, { prefix: 'archive/' })).toThrow(/prefix rule/);
});
test('throws with multiple suffix rules in a filter', () => {
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'MyBucket');
expect(() => bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC,
}),
}, { suffix: '.png' }, { suffix: '.zip' })).toThrow(/suffix rule/);
});
test('EventBridge notification custom resource', () => {
// GIVEN
const stack = new cdk.Stack();
// WHEN
new s3.Bucket(stack, 'MyBucket', {
eventBridgeEnabled: true,
});
// THEN
Template.fromStack(stack).resourceCountIs('AWS::S3::Bucket', 1);
Template.fromStack(stack).hasResourceProperties('Custom::S3BucketNotifications', {
NotificationConfiguration: {
EventBridgeConfiguration: {},
},
});
});
test('check notifications handler runtime version', () => {
const stack = new cdk.Stack();
const importedRole = iam.Role.fromRoleArn(stack, 'role', 'arn:aws:iam::111111111111:role/DevsNotAllowedToTouch');
const bucket = s3.Bucket.fromBucketAttributes(stack, 'MyBucket', {
bucketName: 'foo-bar',
notificationsHandlerRole: importedRole,
});
bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC,
}),
});
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
Runtime: 'python3.9',
});
});
});