-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSecretsManager.test.js
76 lines (62 loc) · 2.11 KB
/
SecretsManager.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
const Test = require('thunk-test')
const assert = require('assert')
const AwsCredentials = require('./internal/AwsCredentials')
const SecretsManager = require('./SecretsManager')
const test = new Test('SecretsManager', async function () {
const awsCreds = await AwsCredentials('default').catch(error => {
if (error.code == 'ENOENT') {
const accessKeyId = process.env.AWS_ACCESS_KEY_ID
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY
if (accessKeyId == null || secretAccessKey == null) {
throw new Error('No AWS credential file or environment variables')
}
return { accessKeyId, secretAccessKey }
}
throw error
})
awsCreds.region = 'us-west-1'
const secretsManager = new SecretsManager({ ...awsCreds })
const mySecret = {
name: `test-secret-${Math.trunc(Math.random() * 1e6)}`,
value: 'helloworld',
}
let didRerunTooSoon = false
try {
const result0 = await secretsManager.createSecret(mySecret.name, mySecret.value)
assert.equal(result0.Name, mySecret.name)
mySecret.value = 'helloworld2'
// should update
const result1 = await secretsManager.putSecret(mySecret.name, mySecret.value)
assert.equal(result1.Name, mySecret.name)
} catch (error) {
if (error.message.includes('scheduled for deletion')) {
didRerunTooSoon = true
} else {
throw error
}
}
try {
const result0 = await secretsManager.getSecretValue(mySecret.name)
assert.equal(result0.Name, mySecret.name)
assert.equal(result0.SecretString, mySecret.value)
const result1 = await secretsManager.getSecretString(mySecret.name)
assert.equal(result1, mySecret.value)
} catch (error) {
if (error.message.includes('marked for deletion')) {
didRerunTooSoon = true
} else {
throw error
}
}
{
const result = await secretsManager.deleteSecret(mySecret.name, mySecret.value)
assert.equal(result.Name, mySecret.name)
}
if (didRerunTooSoon) {
throw new Error('Reran test too soon, please try in a few seconds')
}
}).case()
if (process.argv[1] == __filename) {
test()
}
module.exports = test