-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathBucketLoggingStatus.spec.js
More file actions
334 lines (277 loc) · 15.4 KB
/
BucketLoggingStatus.spec.js
File metadata and controls
334 lines (277 loc) · 15.4 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
const assert = require('assert');
const BucketLoggingStatus = require('../../../lib/models/BucketLoggingStatus').default;
const { parseString } = require('xml2js');
describe('BucketLoggingStatus', () => {
describe('Constructor', () => {
it('should initialize with undefined when no parameters provided', () => {
const config = new BucketLoggingStatus();
assert.strictEqual(config.getLoggingEnabled(), undefined);
});
it('should initialize with LoggingEnabled when provided', () => {
const loggingEnabled = {
TargetBucket: 'my-bucket',
TargetPrefix: 'logs/',
};
const config = new BucketLoggingStatus(loggingEnabled);
assert.deepStrictEqual(config.getLoggingEnabled(), loggingEnabled);
});
});
describe('toXML', () => {
it('should generate XML without LoggingEnabled when not configured', done => {
const config = new BucketLoggingStatus();
const xml = config.toXML();
// Parse the XML and check that BucketLoggingStatus is empty (no LoggingEnabled)
parseString(xml, { explicitArray: false }, (err, result) => {
assert.ifError(err);
assert(result.BucketLoggingStatus);
// Should not have LoggingEnabled property
assert.strictEqual(result.BucketLoggingStatus.LoggingEnabled, undefined);
done();
});
});
it('should generate XML with LoggingEnabled when configured', done => {
const loggingEnabled = {
TargetBucket: 'my-log-bucket',
TargetPrefix: 'logs/2025/',
};
const config = new BucketLoggingStatus(loggingEnabled);
const xml = config.toXML();
parseString(xml, { explicitArray: false }, (err, result) => {
assert.ifError(err);
assert(result.BucketLoggingStatus);
const logging = result.BucketLoggingStatus.LoggingEnabled;
assert(logging);
assert.strictEqual(logging.TargetBucket, 'my-log-bucket');
assert.strictEqual(logging.TargetPrefix, 'logs/2025/');
done();
});
});
});
describe('fromXML', () => {
describe('Success cases', () => {
it('should parse XML with LoggingEnabled', () => {
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01">' +
'<LoggingEnabled>' +
'<TargetBucket>loggingbucket</TargetBucket>' +
'<TargetPrefix>my-app-logs/2025/</TargetPrefix>' +
'</LoggingEnabled>' +
'</BucketLoggingStatus>';
const result = BucketLoggingStatus.fromXML(xml);
assert.strictEqual(result.error, undefined);
assert(result.res instanceof BucketLoggingStatus);
const loggingEnabled = result.res.getLoggingEnabled();
assert.strictEqual(loggingEnabled.TargetBucket, 'loggingbucket');
assert.strictEqual(loggingEnabled.TargetPrefix, 'my-app-logs/2025/');
});
it('should parse XML without LoggingEnabled (empty config)', () => {
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01">' +
'</BucketLoggingStatus>';
const result = BucketLoggingStatus.fromXML(xml);
assert.strictEqual(result.error, undefined);
assert(result.res instanceof BucketLoggingStatus);
assert.strictEqual(result.res.getLoggingEnabled(), undefined);
});
it('should parse XML without LoggingEnabled (self closing)', () => {
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01" />';
const result = BucketLoggingStatus.fromXML(xml);
assert.strictEqual(result.error, undefined);
assert(result.res instanceof BucketLoggingStatus);
assert.strictEqual(result.res.getLoggingEnabled(), undefined);
});
it('should parse XML with empty TargetPrefix', () => {
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01">' +
'<LoggingEnabled>' +
'<TargetBucket>loggingbucket</TargetBucket>' +
'<TargetPrefix></TargetPrefix>' +
'</LoggingEnabled>' +
'</BucketLoggingStatus>';
const result = BucketLoggingStatus.fromXML(xml);
assert.strictEqual(result.error, undefined);
assert(result.res instanceof BucketLoggingStatus);
const loggingEnabled = result.res.getLoggingEnabled();
assert.strictEqual(loggingEnabled.TargetBucket, 'loggingbucket');
assert.strictEqual(loggingEnabled.TargetPrefix, '');
});
});
describe('Error cases - MalformedXML', () => {
it('should return error for invalid XML', () => {
const xml = 'not valid xml';
const result = BucketLoggingStatus.fromXML(xml);
assert(result.error);
assert.strictEqual(result.error.arsenalError.MalformedXML, true);
assert.strictEqual(result.res, undefined);
});
it('should return error for empty string', () => {
const xml = '';
const result = BucketLoggingStatus.fromXML(xml);
assert(result.error);
assert.strictEqual(result.error.arsenalError.MalformedXML, true);
assert.strictEqual(result.error.details, 'request xml is undefined or empty');
assert.strictEqual(result.res, undefined);
});
it('should return error for XML without BucketLoggingStatus root tag', () => {
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<WrongTag></WrongTag>';
const result = BucketLoggingStatus.fromXML(xml);
assert(result.error);
assert.strictEqual(result.error.arsenalError.MalformedXML, true);
assert.strictEqual(result.error.details, 'missing BucketLoggingStatus root');
assert.strictEqual(result.res, undefined);
});
it('should return error when TargetBucket is missing', () => {
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01">' +
'<LoggingEnabled>' +
'<TargetPrefix>logs/</TargetPrefix>' +
'</LoggingEnabled>' +
'</BucketLoggingStatus>';
const result = BucketLoggingStatus.fromXML(xml);
assert(result.error);
assert.strictEqual(result.error.arsenalError.MalformedXML, true);
assert.strictEqual(result.error.details, 'missing TargetBucket field in LoggingEnabled');
assert.strictEqual(result.res, undefined);
});
it('should return error when TargetPrefix is missing', () => {
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01">' +
'<LoggingEnabled>' +
'<TargetBucket>bucket</TargetBucket>' +
'</LoggingEnabled>' +
'</BucketLoggingStatus>';
const result = BucketLoggingStatus.fromXML(xml);
assert(result.error);
assert.strictEqual(result.error.arsenalError.MalformedXML, true);
assert.strictEqual(result.error.details, 'missing TargetPrefix field in LoggingEnabled');
assert.strictEqual(result.res, undefined);
});
it('should return error when TargetBucket length is less than 3', () => {
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01">' +
'<LoggingEnabled>' +
'<TargetBucket>ab</TargetBucket>' +
'<TargetPrefix>logs/</TargetPrefix>' +
'</LoggingEnabled>' +
'</BucketLoggingStatus>';
const result = BucketLoggingStatus.fromXML(xml);
assert(result.error);
assert.strictEqual(result.error.arsenalError.InvalidBucketName, true);
assert.strictEqual(result.error.details, 'TargetBucket field length < 3');
assert.strictEqual(result.res, undefined);
});
it('should return error when TargetBucket length is greater than 255', () => {
const longBucketName = 'a'.repeat(256);
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01">' +
'<LoggingEnabled>' +
`<TargetBucket>${longBucketName}</TargetBucket>` +
'<TargetPrefix>logs/</TargetPrefix>' +
'</LoggingEnabled>' +
'</BucketLoggingStatus>';
const result = BucketLoggingStatus.fromXML(xml);
assert(result.error);
assert.strictEqual(result.error.arsenalError.InvalidBucketName, true);
assert.strictEqual(result.error.details, 'TargetBucket field length > 255');
assert.strictEqual(result.res, undefined);
});
it('should return error when TargetPrefix length is greater than 800', () => {
const longPrefix = 'a'.repeat(801);
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01">' +
'<LoggingEnabled>' +
'<TargetBucket>bucket</TargetBucket>' +
`<TargetPrefix>${longPrefix}</TargetPrefix>` +
'</LoggingEnabled>' +
'</BucketLoggingStatus>';
const result = BucketLoggingStatus.fromXML(xml);
assert(result.error);
assert.strictEqual(result.error.arsenalError.InvalidArgument, true);
assert.strictEqual(result.error.details, 'TargetPrefix field length > 800');
assert.strictEqual(result.res, undefined);
});
});
describe('Error cases - NotImplemented', () => {
it('should return NotImplemented error when TargetGrants is present', () => {
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01">' +
'<LoggingEnabled>' +
'<TargetBucket>bucket</TargetBucket>' +
'<TargetPrefix>logs/</TargetPrefix>' +
'<TargetGrants><Grant><Grantee><ID>user123</ID></Grantee>' +
'<Permission>READ</Permission></Grant></TargetGrants>' +
'</LoggingEnabled>' +
'</BucketLoggingStatus>';
const result = BucketLoggingStatus.fromXML(xml);
assert(result.error);
assert.strictEqual(result.error.arsenalError.NotImplemented, true);
assert.strictEqual(result.error.details,
'TargetGrants field in LoggingEnabled is not implemented');
assert.strictEqual(result.res, undefined);
});
it('should return NotImplemented error when TargetObjectKeyFormat is present', () => {
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01">' +
'<LoggingEnabled>' +
'<TargetBucket>bucket</TargetBucket>' +
'<TargetPrefix>logs/</TargetPrefix>' +
'<TargetObjectKeyFormat><SimplePrefix></SimplePrefix></TargetObjectKeyFormat>' +
'</LoggingEnabled>' +
'</BucketLoggingStatus>';
const result = BucketLoggingStatus.fromXML(xml);
assert(result.error);
assert.strictEqual(result.error.arsenalError.NotImplemented, true);
assert.strictEqual(result.error.details,
'TargetObjectKeyFormat field in LoggingEnabled is not implemented');
assert.strictEqual(result.res, undefined);
});
});
describe('Round-trip conversions', () => {
it('should successfully round-trip with LoggingEnabled', () => {
const loggingEnabled = {
TargetBucket: 'test-bucket',
TargetPrefix: 'app/logs/2025/',
};
const config1 = new BucketLoggingStatus(loggingEnabled);
const xml = config1.toXML();
const result = BucketLoggingStatus.fromXML(xml);
assert.strictEqual(result.error, undefined);
assert(result.res instanceof BucketLoggingStatus);
const parsed = result.res.getLoggingEnabled();
assert.strictEqual(parsed.TargetBucket, loggingEnabled.TargetBucket);
assert.strictEqual(parsed.TargetPrefix, loggingEnabled.TargetPrefix);
});
it('should successfully round-trip without LoggingEnabled', () => {
const config1 = new BucketLoggingStatus();
const xml = config1.toXML();
const result = BucketLoggingStatus.fromXML(xml);
assert.strictEqual(result.error, undefined);
assert(result.res instanceof BucketLoggingStatus);
assert.strictEqual(result.res.getLoggingEnabled(), undefined);
});
});
describe('XML escaping for special characters', () => {
const specialCharacters = ['&', '<', '>', '"', "'"];
specialCharacters.forEach(char =>
it(`should escape \`${char}\` in TargetPrefix and generate valid XML`, done => {
const loggingEnabled = {
TargetBucket: 'test-bucket',
TargetPrefix: `logs/app${char}env/`,
};
const config = new BucketLoggingStatus(loggingEnabled);
const xml = config.toXML();
// Verify the XML is valid and the character roundtrips by parsing it
parseString(xml, { explicitArray: false }, (err, result) => {
assert.ifError(err);
assert(result.BucketLoggingStatus);
const logging = result.BucketLoggingStatus.LoggingEnabled;
assert.strictEqual(logging.TargetPrefix, `logs/app${char}env/`);
done();
});
})
);
});
});
});