-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
368 lines (310 loc) · 8.46 KB
/
test.js
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
364
365
366
367
368
const assert = require('assert');
const { Secrets } = require('./index');
const FIXTURES = [
{
name: 'secret1',
description: 'testing objects',
secrets: {
key: '[email protected]',
secret: 'pumpkins',
},
},
{
name: 'secret2',
description: 'testing string',
secrets: 'pumpkins',
},
{
name: 'secret3',
description: 'testing number',
secrets: 4,
},
{
name: 'secret4',
description: 'testing boolean',
secrets: true,
},
{
name: 'secret5',
description: 'testing array',
secrets: [1, 2, 3],
},
{
name: 'secret6',
description: 'testing mixed',
secrets: [1, 'peach', null, ['apple'], { yes: true }],
},
{
name: 'nested/secret/1',
description: 'testing nested number',
secrets: 5,
},
{
name: 'nested/secret/2',
description: 'tested nested array',
secrets: ['apple', 'peach', 'banana'],
},
{
name: 'nested/secret/auth',
description: 'tested nested object',
secrets: {
username: '[email protected]',
password: 'p3@ches',
},
},
];
const RESULT = {};
describe('Secrets', function () {
this.timeout(4000);
let secrets, secretCache;
before(function () {
// build the expected result
RESULT.secret1 = FIXTURES[0].secrets;
RESULT.secret2 = FIXTURES[1].secrets;
RESULT.secret3 = FIXTURES[2].secrets;
RESULT.secret4 = FIXTURES[3].secrets;
RESULT.secret5 = FIXTURES[4].secrets;
RESULT.secret6 = FIXTURES[5].secrets;
RESULT.nested = {
secret: {
1: FIXTURES[6].secrets,
2: FIXTURES[7].secrets,
auth: FIXTURES[8].secrets,
},
};
secrets = new Secrets({
env: 'unit-testing',
region: 'us-east-1',
namespace: `__secrets-${new Date().getTime()}__`,
});
});
// cleanup test data
after(function (done) {
this.timeout(0);
let promises = [];
for (let item of FIXTURES) {
let id = `${secrets.namespace}${secrets.delimiter}${secrets.env}${secrets.delimiter}${item.name}`;
let promise = secrets.deleteSecret(id, true);
assert.ok(promise);
assert.ok(promise instanceof Promise);
promises.push(promise);
}
Promise.all(promises)
.then((res) => {
assert.ok(res);
assert.ok(res.length === FIXTURES.length);
for (let item of res) {
assert.ok(item);
assert.ok('Name' in item);
assert.ok('ARN' in item);
assert.ok('DeletionDate' in item);
}
done();
})
.catch(done);
});
it('should should parse secret list into config object', function () {
let args = FIXTURES.map((item) => {
return {
Name: `${secrets.namespace}/${secrets.env}/${item.name}`,
SecretString: JSON.stringify(item.secrets),
};
});
let res = secrets._parseSecrets(args);
assert.deepStrictEqual(res, RESULT);
});
it('should create some secrets', function (done) {
let promises = [];
const items = JSON.parse(JSON.stringify(FIXTURES)); // deep clone this
for (let item of items) {
let promise = secrets.createSecret(item);
assert.ok(promise);
assert.ok(promise instanceof Promise);
promises.push(promise);
}
Promise.all(promises)
.then((res) => {
assert.ok(res);
secretCache = res;
assert.ok(secretCache.length === promises.length);
for (let secret of secretCache) {
assert.ok(secret);
assert.ok('Name' in secret);
assert.ok('ARN' in secret);
assert.ok('VersionId' in secret);
}
done();
})
.catch(done);
});
it('should retrieve all secrets individually', function (done) {
const promises = secretCache.map((item) => {
return secrets.getSecret({
id: item.Name,
raw: true,
});
});
Promise.all(promises)
.then((res) => {
assert.ok(res);
assert.ok(res.length === secretCache.length);
for (let secret of res) {
assert.ok(secret);
assert.ok('Name' in secret);
assert.ok('ARN' in secret);
assert.ok('VersionId' in secret);
assert.ok('SecretString' in secret);
assert.ok('CreatedDate' in secret);
}
done();
})
.catch(done);
});
it('should synchronously retrieve a single secret string', function () {
let secret,
id = `${secrets.namespace}/${secrets.env}/secret2`;
try {
secret = secrets.getSecretSync({ id: id });
} catch (err) {
assert.fail(err.message);
}
assert.ok(secret);
assert.ok(Object.prototype.toString.call(secret) === '[object String]');
assert.strictEqual(secret, RESULT.secret2);
});
it('should synchronously retrieve a single secret and return a secret object', function () {
let secret,
id = `${secrets.namespace}/${secrets.env}/secret1`;
try {
secret = secrets.getSecretSync({ id: id });
} catch (err) {
assert.fail(err.message);
}
assert.ok(secret);
assert.ok(Object.prototype.toString.call(secret) === '[object Object]');
assert.ok('key' in secret);
assert.ok('secret' in secret);
assert.deepStrictEqual(secret, RESULT.secret1);
});
it('should synchronously retrieve a single secret mixed array', function () {
let secret,
id = `${secrets.namespace}/${secrets.env}/secret6`;
try {
secret = secrets.getSecretSync({ id: id });
} catch (err) {
assert.fail(err.message);
}
assert.ok(secret);
assert.ok(Object.prototype.toString.call(secret) === '[object Array]');
assert.deepStrictEqual(secret, RESULT.secret6);
});
it('should synchronously retrieve a single secret and return the raw AWS response', function () {
let secret,
item = secretCache[Math.floor(Math.random() * secretCache.length)];
try {
secret = secrets.getSecretSync({ id: item.Name, raw: true });
} catch (err) {
assert.fail(err.message);
}
assert.ok(secret);
assert.ok('Name' in secret);
assert.ok('ARN' in secret);
assert.ok('VersionId' in secret);
assert.ok('SecretString' in secret);
assert.ok('CreatedDate' in secret);
});
it('retrieve a list of all secrets', async function () {
this.timeout(4000);
this.retries(3);
const regex = new RegExp(`^${secrets.namespace}/${secrets.env}/.+`);
let list;
try {
list = await secrets.listSecrets();
} catch (err) {
assert.fail(err.message);
}
assert.ok(list);
assert.ok(Array.isArray(list));
// console.log(list);
// console.log(list.length, secretCache.length);
assert.strictEqual(
list.length,
secretCache.length,
`The list should have length of ${secretCache.length} but only has ${list.length} items.`
);
for (let secret of list) {
assert.ok('Name' in secret);
assert.ok('ARN' in secret);
assert.ok(regex.test(secret.Name));
}
});
it('should generate a config with all test secrets', async function () {
// test should't take this long but could depending on how long the
// secrets take to retrieve from AWS.
this.timeout(4000);
this.retries(3);
let config;
try {
config = await secrets.config();
} catch (err) {
assert.fail(err.message);
}
assert.ok(config);
assert.deepStrictEqual(config, RESULT);
});
it('should synchronously generate config with all test secrets', function () {
// test should't take this long but could depending on how long the
// secrets take to retrieve from AWS.
this.timeout(4000);
let config;
try {
config = secrets.configSync();
} catch (err) {
assert.fail(err.message);
}
assert.ok(config);
assert.deepStrictEqual(config, RESULT);
});
it.skip('should not synchronously generate config since buffer is too small', function () {
this.timeout(4000);
// get all the same test secrets but with a max buffer of 1
const maxBufSecrets = new Secrets({
env: secrets.env,
region: secrets.region,
namespace: secrets.namespace,
maxBuffer: .1,
});
// The buffer is about 304 Bytes so this should fail but doesn't :/
assert.throws(() => {
const err = maxBufSecrets.configSync();
return err;
}, Error, 'Max buffer it too small to get all secrets');
});
it.skip('should produce a rate limit error, wait and try again', async function () {
const iterations = 500;
this.timeout(1000 * iterations);
let result,
promises = [];
for (let index = 0; index < iterations; index++) {
// const name = `${secrets.namespace}/${secrets.env}/${FIXTURES[0].name}`;
promises.push(secrets.listSecrets());
}
try {
result = await Promise.allSettled(promises);
} catch (err) {
assert.fail(err.message);
}
result.forEach((res, i) => {
assert.ok('value' in res);
assert.ok('status' in res);
assert.ok(res.status === 'fulfilled');
assert.ok(
Object.prototype.toString.call(res.value) === '[object Array]'
);
// if (res.status !== 'fulfilled') {
// console.log( require('util').inspect(res, {depth:10, colors:true}) );
// }
});
assert.ok(result);
});
});