-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathNotificationConfiguration.spec.js
More file actions
363 lines (325 loc) · 14.2 KB
/
NotificationConfiguration.spec.js
File metadata and controls
363 lines (325 loc) · 14.2 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
const assert = require('assert');
const { parseString } = require('xml2js');
const NotificationConfiguration =
require('../../../lib/models/NotificationConfiguration').default;
const { supportedNotificationEvents } = require('../../../lib/constants');
function checkError(parsedXml, err, errMessage, cb) {
const config = new NotificationConfiguration(parsedXml).
getValidatedNotificationConfiguration();
expect(config.error.is[err]).toBeTruthy();
expect(config.error.description).toEqual(errMessage);
cb();
}
function generateEvent(testParams) {
const event = [];
if (testParams.key === 'Event') {
if (Array.isArray(testParams.value)) {
testParams.value.forEach(v => {
event.push(`${event}<Event>${v}</Event>`);
});
} else {
event.push(`<Event>${testParams.value}</Event>`);
}
} else {
event.push('<Event>s3:ObjectCreated:*</Event>');
}
return event.join('');
}
function generateFilter(testParams) {
let filter = '';
if (testParams.key === 'Filter') {
filter = `<Filter>${testParams.value}</Filter>`;
}
if (testParams.key === 'S3Key') {
filter = `<Filter><S3Key>${testParams.value}</S3Key></Filter>`;
}
if (testParams.key === 'FilterRule') {
if (Array.isArray(testParams.value)) {
testParams.value.forEach(v => {
filter = `${filter}<Filter><S3Key><FilterRule>${v}` +
'</FilterRule></S3Key></Filter>';
});
} else {
filter = `<Filter><S3Key><FilterRule>${testParams.value}` +
'</FilterRule></S3Key></Filter>';
}
}
return filter;
}
function generateXml(testParams) {
const id = testParams.key === 'Id' ? `<Id>${testParams.value}</Id>` : '<Id>queue-id</Id>';
const arn = testParams.key === 'QueueArn' ?
`<Queue>${testParams.value}</Queue>` :
'<Queue>arn:scality:bucketnotif:::target</Queue>';
const event = generateEvent(testParams);
const filter = generateFilter(testParams);
let queueConfig = `<QueueConfiguration>${id}${arn}${event}${filter}` +
'</QueueConfiguration>';
if (testParams.key === 'QueueConfiguration') {
if (testParams.value === 'double') {
queueConfig = `${queueConfig}${queueConfig}`;
} else {
queueConfig = testParams.value;
}
}
const notification = testParams.key === 'NotificationConfiguration' ? '' :
`<NotificationConfiguration>${queueConfig}</NotificationConfiguration>`;
return notification;
}
function generateParsedXml(testParams, cb) {
const xml = generateXml(testParams);
parseString(xml, (err, parsedXml) => {
assert.equal(err, null, 'Error parsing xml');
cb(parsedXml);
});
}
const failTests = [
{
name: 'fail with empty configuration',
params: { key: 'NotificationConfiguration' },
error: 'MalformedXML',
errorMessage: 'request xml is undefined or empty',
},
{
name: 'fail with invalid id',
params: { key: 'Id', value: 'a'.repeat(256) },
error: 'InvalidArgument',
errorMessage: 'queue configuration ID is greater than 255 characters long',
},
{
name: 'fail with repeated id',
params: { key: 'QueueConfiguration', value: 'double' },
error: 'InvalidRequest',
errorMessage: 'queue configuration ID must be unique',
},
{
name: 'fail with empty QueueArn',
params: { key: 'QueueArn', value: '' },
error: 'MalformedXML',
errorMessage: 'each queue configuration must contain a queue arn',
},
{
name: 'fail with invalid QueueArn',
params: { key: 'QueueArn', value: 'arn:scality:bucketnotif:target' },
error: 'MalformedXML',
errorMessage: 'queue arn is invalid',
},
{
name: 'fail with invalid QueueArn partition',
params: { key: 'QueueArn', value: 'arn:aws:bucketnotif:::target' },
error: 'MalformedXML',
errorMessage: 'queue arn is invalid',
},
{
name: 'fail with empty event',
params: { key: 'Event', value: '' },
error: 'MalformedXML',
errorMessage: 'each queue configuration must contain an event',
},
{
name: 'fail with invalid event',
params: { key: 'Event', value: 's3:BucketCreated:Put' },
error: 'MalformedXML',
errorMessage: 'event array contains invalid or unsupported event',
},
{
name: 'fail with unsupported event',
params: { key: 'Event', value: 's3:Replication:OperationNotTracked' },
error: 'MalformedXML',
errorMessage: 'event array contains invalid or unsupported event',
},
{
name: 'fail with filter that does not contain S3Key',
params: { key: 'Filter', value: '<FilterRule><Name>Prefix</Name><Value>logs/</Value></FilterRule>' },
error: 'MalformedXML',
errorMessage: 'if included, queue configuration filter must contain S3Key',
},
{
name: 'fail with filter that does not contain a rule',
params: { key: 'S3Key', value: '<Name>Prefix</Name><Value>logs/</Value>' },
error: 'MalformedXML',
errorMessage: 'if included, queue configuration filter must contain a rule',
},
{
name: 'fail with filter rule that does not contain name and value',
params: { key: 'FilterRule', value: '<Value>noname</Value>' },
error: 'MalformedXML',
errorMessage: 'each included filter must contain a name and value',
},
{
name: 'fail with invalid name in filter rule',
params: { key: 'FilterRule', value: '<Name>Invalid</Name><Value>logs/</Value>' },
error: 'MalformedXML',
errorMessage: 'filter Name must be one of Prefix or Suffix',
},
];
const passTests = [
{
name: 'pass with empty QueueConfiguration',
params: { key: 'QueueConfiguration', value: '[]' },
},
{
name: 'pass with multiple events in one queue configuration',
params: {
key: 'Event', value: ['s3:ObjectCreated:Put', 's3:ObjectCreated:Copy'],
},
},
{
name: 'pass with multiple filter rules',
params: {
key: 'FilterRule',
value: ['<Name>Prefix</Name><Value>logs/</Value>', '<Name>Suffix</Name><Value>.pdf</Value>'] },
},
{
name: 'pass with no id',
params: { key: 'Id', value: '' },
},
{
name: 'pass with basic config', params: {},
},
];
describe('NotificationConfiguration class getValidatedNotificationConfiguration',
() => {
it('should return MalformedXML error if request xml is empty', done => {
const errMessage = 'request xml is undefined or empty';
checkError('', 'MalformedXML', errMessage, done);
});
failTests.forEach(test => {
it(`should ${test.name}`, done => {
generateParsedXml(test.params, xml => {
checkError(xml, test.error, test.errorMessage, done);
});
});
});
passTests.forEach(test => {
it(`should ${test.name}`, done => {
generateParsedXml(test.params, xml => {
const config = new NotificationConfiguration(xml).
getValidatedNotificationConfiguration();
assert.ifError(config.error);
done();
});
});
});
});
describe('NotificationConfiguration.restrictSupportedNotificationBasedOnLifecycle', () => {
// Store original events to restore after each test
let originalEvents;
beforeEach(() => {
// Create a copy of the original supported events
originalEvents = new Set(supportedNotificationEvents);
});
afterEach(() => {
// Restore original supported events
supportedNotificationEvents.clear();
originalEvents.forEach(event => supportedNotificationEvents.add(event));
});
it('should keep all restore and lifecycle events when both Transition and Expiration rules are supported', () => {
const supportedLifecycleRules = ['Transition', 'Expiration'];
NotificationConfiguration.restrictSupportedNotificationBasedOnLifecycle(supportedLifecycleRules);
// Verify all lifecycle events remain
expect(supportedNotificationEvents.has('s3:ObjectRestore:*')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:ObjectRestore:Post')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:ObjectRestore:Completed')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:ObjectRestore:Delete')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:LifecycleTransition')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:LifecycleExpiration:*')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:LifecycleExpiration:DeleteMarkerCreated')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:LifecycleExpiration:Delete')).toBeTruthy();
// Verify other events remain
expect(supportedNotificationEvents.has('s3:ObjectCreated:*')).toBeTruthy();
});
it('should remove ObjectRestore and LifecycleTransition events when no Transition rules', () => {
const supportedLifecycleRules = ['Expiration'];
NotificationConfiguration.restrictSupportedNotificationBasedOnLifecycle(supportedLifecycleRules);
// Verify Transition-related events are removed
expect(supportedNotificationEvents.has('s3:ObjectRestore:*')).toBeFalsy();
expect(supportedNotificationEvents.has('s3:ObjectRestore:Post')).toBeFalsy();
expect(supportedNotificationEvents.has('s3:ObjectRestore:Completed')).toBeFalsy();
expect(supportedNotificationEvents.has('s3:ObjectRestore:Delete')).toBeFalsy();
expect(supportedNotificationEvents.has('s3:LifecycleTransition')).toBeFalsy();
// Verify Expiration events remain
expect(supportedNotificationEvents.has('s3:LifecycleExpiration:*')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:LifecycleExpiration:DeleteMarkerCreated')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:LifecycleExpiration:Delete')).toBeTruthy();
// Verify other events remain
expect(supportedNotificationEvents.has('s3:ObjectCreated:*')).toBeTruthy();
});
it('should remove LifecycleExpiration events when no Expiration rules', () => {
const supportedLifecycleRules = ['Transition'];
NotificationConfiguration.restrictSupportedNotificationBasedOnLifecycle(supportedLifecycleRules);
// Verify Expiration-related events are removed
expect(supportedNotificationEvents.has('s3:LifecycleExpiration:*')).toBeFalsy();
expect(supportedNotificationEvents.has('s3:LifecycleExpiration:DeleteMarkerCreated')).toBeFalsy();
expect(supportedNotificationEvents.has('s3:LifecycleExpiration:Delete')).toBeFalsy();
// Verify Transition events remain
expect(supportedNotificationEvents.has('s3:ObjectRestore:*')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:ObjectRestore:Post')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:ObjectRestore:Completed')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:ObjectRestore:Delete')).toBeTruthy();
expect(supportedNotificationEvents.has('s3:LifecycleTransition')).toBeTruthy();
// Verify other events remain
expect(supportedNotificationEvents.has('s3:ObjectCreated:*')).toBeTruthy();
});
});
describe('NotificationConfiguration.getConfigXML - XML escaping for special characters', () => {
const specialCharacters = ['&', '<', '>', '"', "'"];
specialCharacters.forEach(char => {
it(`should escape \`${char}\` in notification ID and generate valid XML`, done => {
const config = {
queueConfig: [{
id: `test-id${char}value`,
queueArn: 'arn:scality:bucketnotif:::target',
events: ['s3:ObjectCreated:*'],
filterRules: [],
}],
};
const xml = NotificationConfiguration.getConfigXML(config);
parseString(xml, (err, result) => {
assert.ifError(err);
const queueConfig = result.NotificationConfiguration.QueueConfiguration[0];
assert.strictEqual(queueConfig.Id[0], `test-id${char}value`);
done();
});
});
it(`should escape \`${char}\` in queue ARN`, done => {
const config = {
queueConfig: [{
id: 'test-id',
queueArn: `arn:scality:bucketnotif:::queue${char}name`,
events: ['s3:ObjectCreated:*'],
filterRules: [],
}],
};
const xml = NotificationConfiguration.getConfigXML(config);
parseString(xml, (err, result) => {
assert.ifError(err);
const queueConfig = result.NotificationConfiguration.QueueConfiguration[0];
assert.strictEqual(queueConfig.Queue[0], `arn:scality:bucketnotif:::queue${char}name`);
done();
});
});
it(`should escape \`${char}\` in filter rule name and value`, done => {
const config = {
queueConfig: [{
id: 'test-id',
queueArn: 'arn:scality:bucketnotif:::target',
events: ['s3:ObjectCreated:*'],
filterRules: [{
name: `Prefix${char}Name`,
value: `logs/${char}path`,
}],
}],
};
const xml = NotificationConfiguration.getConfigXML(config);
parseString(xml, (err, result) => {
assert.ifError(err);
const queueConfig = result.NotificationConfiguration.QueueConfiguration[0];
const filterRule = queueConfig.Filter[0].S3Key[0].FilterRule[0];
assert.strictEqual(filterRule.Name[0], `Prefix${char}Name`);
assert.strictEqual(filterRule.Value[0], `logs/${char}path`);
done();
});
});
});
});