/
test-config-vault.js
365 lines (258 loc) · 8.9 KB
/
test-config-vault.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
const fs = require('fs')
const crypto = require('crypto')
const sinon = require('sinon')
const t = require('tap')
const dotenv = require('../lib/main')
const testPath = 'tests/.env'
const dotenvKey = 'dotenv://:key_ddcaa26504cd70a6fef9801901c3981538563a1767c297cb8416e8a38c62fe00@dotenvx.com/vault/.env.vault?environment=development'
let envStub
let logStub
t.beforeEach(() => {
process.env.DOTENV_KEY = ''
envStub = sinon.stub(process.env, 'DOTENV_KEY').value(dotenvKey)
})
t.afterEach(() => {
envStub.restore()
if (logStub) {
logStub.restore()
}
})
t.test('logs when no path is set', ct => {
ct.plan(1)
logStub = sinon.stub(console, 'log')
dotenv.config()
ct.ok(logStub.called)
})
t.test('logs', ct => {
ct.plan(1)
logStub = sinon.stub(console, 'log')
dotenv.config({ path: testPath })
ct.ok(logStub.called)
})
t.test('logs when testPath calls to .env.vault directly (interpret what the user meant)', ct => {
ct.plan(1)
logStub = sinon.stub(console, 'log')
dotenv.config({ path: `${testPath}.vault` })
ct.ok(logStub.called)
})
t.test('warns if DOTENV_KEY exists but .env.vault does not exist', ct => {
ct.plan(1)
logStub = sinon.stub(console, 'log')
const existsSync = sinon.stub(fs, 'existsSync').returns(false) // make .env.vault not exist
dotenv.config({ path: testPath })
ct.ok(logStub.called)
existsSync.restore()
ct.end()
})
t.test('warns if DOTENV_KEY exists but .env.vault does not exist (set as array)', ct => {
ct.plan(1)
logStub = sinon.stub(console, 'log')
const existsSync = sinon.stub(fs, 'existsSync').returns(false) // make .env.vault not exist
dotenv.config({ path: [testPath] })
ct.ok(logStub.called)
existsSync.restore()
ct.end()
})
t.test('returns parsed object', ct => {
ct.plan(1)
const env = dotenv.config({ path: testPath })
ct.same(env.parsed, { ALPHA: 'zeta' })
ct.end()
})
t.test('returns parsed object (set path as array)', ct => {
ct.plan(1)
const env = dotenv.config({ path: [testPath] })
ct.same(env.parsed, { ALPHA: 'zeta' })
ct.end()
})
t.test('returns parsed object (set path as mulit-array)', ct => {
ct.plan(1)
const env = dotenv.config({ path: ['tests/.env.local', 'tests/.env'] })
ct.same(env.parsed, { ALPHA: 'zeta' })
ct.end()
})
t.test('returns parsed object (set path as array with .vault extension)', ct => {
ct.plan(1)
const env = dotenv.config({ path: [`${testPath}.vault`] })
ct.same(env.parsed, { ALPHA: 'zeta' })
ct.end()
})
t.test('throws not found if .env.vault is empty', ct => {
ct.plan(2)
const readFileSync = sinon.stub(fs, 'readFileSync').returns('') // empty file
try {
dotenv.config({ path: testPath })
} catch (e) {
ct.equal(e.message, 'NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment DOTENV_VAULT_DEVELOPMENT in your .env.vault file.')
ct.equal(e.code, 'NOT_FOUND_DOTENV_ENVIRONMENT')
}
readFileSync.restore()
ct.end()
})
t.test('throws missing data when somehow parsed badly', ct => {
ct.plan(2)
const configDotenvStub = sinon.stub(dotenv, 'configDotenv').returns({ parsed: undefined })
try {
dotenv.config({ path: testPath })
} catch (e) {
ct.equal(e.message, 'MISSING_DATA: Cannot parse tests/.env.vault for an unknown reason')
ct.equal(e.code, 'MISSING_DATA')
}
configDotenvStub.restore()
ct.end()
})
t.test('throws error when invalid formed DOTENV_KEY', ct => {
envStub.restore()
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('invalid-format-non-uri-format')
ct.plan(2)
try {
dotenv.config({ path: testPath })
} catch (e) {
ct.equal(e.message, 'INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=development')
ct.equal(e.code, 'INVALID_DOTENV_KEY')
}
ct.end()
})
t.test('throws error when invalid formed DOTENV_KEY that otherwise is not caught', ct => {
ct.plan(1)
const urlStub = sinon.stub(global, 'URL')
urlStub.callsFake(() => {
throw new Error('uncaught error')
})
try {
dotenv.config({ path: testPath })
} catch (e) {
ct.equal(e.message, 'uncaught error')
}
urlStub.restore()
ct.end()
})
t.test('throws error when DOTENV_KEY missing password', ct => {
envStub.restore()
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('dotenv://username@dotenvx.com/vault/.env.vault?environment=development')
ct.plan(2)
try {
dotenv.config({ path: testPath })
} catch (e) {
ct.equal(e.message, 'INVALID_DOTENV_KEY: Missing key part')
ct.equal(e.code, 'INVALID_DOTENV_KEY')
}
ct.end()
})
t.test('throws error when DOTENV_KEY missing environment', ct => {
envStub.restore()
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('dotenv://:key_ddcaa26504cd70a6fef9801901c3981538563a1767c297cb8416e8a38c62fe00@dotenvx.com/vault/.env.vault')
ct.plan(2)
try {
dotenv.config({ path: testPath })
} catch (e) {
ct.equal(e.message, 'INVALID_DOTENV_KEY: Missing environment part')
ct.equal(e.code, 'INVALID_DOTENV_KEY')
}
ct.end()
})
t.test('when DOTENV_KEY is empty string falls back to .env file', ct => {
envStub.restore()
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('')
ct.plan(1)
const result = dotenv.config({ path: testPath })
ct.equal(result.parsed.BASIC, 'basic')
ct.end()
})
t.test('does not write over keys already in process.env by default', ct => {
ct.plan(2)
const existing = 'bar'
process.env.ALPHA = existing
const result = dotenv.config({ path: testPath })
ct.equal(result.parsed.ALPHA, 'zeta')
ct.equal(process.env.ALPHA, 'bar')
})
t.test('does write over keys already in process.env if override turned on', ct => {
ct.plan(2)
const existing = 'bar'
process.env.ALPHA = existing
const result = dotenv.config({ path: testPath, override: true })
ct.equal(result.parsed.ALPHA, 'zeta')
ct.equal(process.env.ALPHA, 'zeta')
})
t.test('when DOTENV_KEY is passed as an option it successfully decrypts and injects', ct => {
envStub.restore()
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('')
ct.plan(2)
const result = dotenv.config({ path: testPath, DOTENV_KEY: dotenvKey })
ct.equal(result.parsed.ALPHA, 'zeta')
ct.equal(process.env.ALPHA, 'zeta')
ct.end()
})
t.test('can write to a different object rather than process.env', ct => {
ct.plan(3)
process.env.ALPHA = 'other' // reset process.env
logStub = sinon.stub(console, 'log')
const myObject = {}
const result = dotenv.config({ path: testPath, processEnv: myObject })
ct.equal(result.parsed.ALPHA, 'zeta')
ct.equal(process.env.ALPHA, 'other')
ct.equal(myObject.ALPHA, 'zeta')
})
t.test('logs when debug and override are turned on', ct => {
ct.plan(1)
logStub = sinon.stub(console, 'log')
dotenv.config({ path: testPath, override: true, debug: true })
ct.ok(logStub.called)
})
t.test('logs when debug is on and override is false', ct => {
ct.plan(1)
logStub = sinon.stub(console, 'log')
dotenv.config({ path: testPath, override: false, debug: true })
ct.ok(logStub.called)
})
t.test('raises an INVALID_DOTENV_KEY if key RangeError', ct => {
envStub.restore()
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('dotenv://:key_ddcaa26504cd70a@dotenvx.com/vault/.env.vault?environment=development')
ct.plan(2)
try {
dotenv.config({ path: testPath })
} catch (e) {
ct.equal(e.message, 'INVALID_DOTENV_KEY: It must be 64 characters long (or more)')
ct.equal(e.code, 'INVALID_DOTENV_KEY')
}
ct.end()
})
t.test('raises an DECRYPTION_FAILED if key fails to decrypt payload', ct => {
envStub.restore()
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('dotenv://:key_2c4d267b8c3865f921311612e69273666cc76c008acb577d3e22bc3046fba386@dotenvx.com/vault/.env.vault?environment=development')
ct.plan(2)
try {
dotenv.config({ path: testPath })
} catch (e) {
ct.equal(e.message, 'DECRYPTION_FAILED: Please check your DOTENV_KEY')
ct.equal(e.code, 'DECRYPTION_FAILED')
}
ct.end()
})
t.test('raises an DECRYPTION_FAILED if both (comma separated) keys fail to decrypt', ct => {
envStub.restore()
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('dotenv://:key_2c4d267b8c3865f921311612e69273666cc76c008acb577d3e22bc3046fba386@dotenvx.com/vault/.env.vault?environment=development,dotenv://:key_c04959b64473e43dd60c56a536ef8481388528b16759736d89515c25eec69247@dotenvx.com/vault/.env.vault?environment=development')
ct.plan(2)
try {
dotenv.config({ path: testPath })
} catch (e) {
ct.equal(e.message, 'DECRYPTION_FAILED: Please check your DOTENV_KEY')
ct.equal(e.code, 'DECRYPTION_FAILED')
}
ct.end()
})
t.test('raises error if some other uncaught decryption error', ct => {
ct.plan(1)
const decipherStub = sinon.stub(crypto, 'createDecipheriv')
decipherStub.callsFake(() => {
throw new Error('uncaught error')
})
try {
dotenv.config({ path: testPath })
} catch (e) {
ct.equal(e.message, 'uncaught error')
}
decipherStub.restore()
ct.end()
})