-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest.js
More file actions
152 lines (134 loc) · 4.93 KB
/
test.js
File metadata and controls
152 lines (134 loc) · 4.93 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
import path from 'node:path';
import process from 'node:process';
import test from 'ava';
import camelcase from 'camelcase';
import envy from './index.js';
const fixture = (dir, file = '.env') => {
return envy(path.join('fixture', dir, file));
};
const monkey = (object, prop) => {
const original = Object.getOwnPropertyDescriptor(object, prop);
return () => {
delete object[prop];
if (original) {
Object.defineProperty(object, prop, original);
}
};
};
test('returns camelcased keys', (t) => {
const env = fixture('normal');
const keys = Object.keys(env);
t.is(keys.length, 2);
for (const key of keys) {
t.is(key, camelcase(key));
}
t.deepEqual(env, {
myKey : 'my val',
dog : 'woof'
});
});
test('returns excess vars in .env', (t) => {
t.deepEqual(fixture('excess-env-entry'), {
myKey : 'my val',
dog : 'woof',
extra : 'awesome'
});
});
test('returns empty collection of vars', (t) => {
t.deepEqual(fixture('empty-example'), {});
});
test('can parse complex values', (t) => {
// Complex values are those which have a high risk of confusing the parser,
// such as those using reserved characters.
t.deepEqual(fixture('complex-values'), {
fakeSecret : 'UG9pbnRsZXNzIHRvIGRlY29kZQ==',
equation : '5=10/2',
number : '3',
sentence : 'Hi there!'
});
});
test('requires keys for environment variables', (t) => {
const error = t.throws(() => {
fixture('missing-key');
}, { instanceOf : Error });
const filepath = path.join('fixture', 'missing-key', '.env');
t.is(error.message, `Missing key before "=" for variable in ${filepath}`);
});
test('requires env files to be hidden', (t) => {
const bad = 'env';
const good = '.' + bad;
const error = t.throws(() => {
fixture('normal', bad);
}, { instanceOf : Error });
t.is(error.message, `Filepath must be hidden. Fix: mv '${bad}' '${good}'`);
});
test('requires secure file permissions on .env', (t) => {
const error = t.throws(() => {
fixture('unsafe-perm-env-640');
}, { instanceOf : Error });
const filepath = path.join('fixture', 'unsafe-perm-env-640', '.env');
t.is(error.message, `File permissions are unsafe. Fix: chmod 600 '${filepath}'`);
});
test('requires secure file permissions on .env in windows land', (t) => {
const restore = monkey(process, 'platform');
Object.defineProperty(process, 'platform', {
configurable : true,
value : 'win32'
});
const error = t.throws(() => {
fixture('unsafe-perm-windows-env-777');
}, { instanceOf : Error });
const filepath = path.join('fixture', 'unsafe-perm-windows-env-777', '.env');
t.is(error.message, `File permissions are unsafe. Make them 555 '${filepath}'`);
restore();
});
test('requires secure file permissions on .env.example', (t) => {
const error = t.throws(() => {
fixture('unsafe-perm-example-602');
}, { instanceOf : Error });
const filepath = path.join('fixture', 'unsafe-perm-example-602', '.env.example');
t.is(error.message, `File must not be writable by others. Fix: chmod o-w '${filepath}'`);
});
test('does not modify process.env', (t) => {
const oldEnvDescriptor = Object.getOwnPropertyDescriptor(process, 'env');
const oldEnv = process.env;
const envCopy = Object.create(
Object.getPrototypeOf(oldEnv),
Object.getOwnPropertyDescriptors(oldEnv)
);
t.deepEqual(fixture('normal'), {
myKey : 'my val',
dog : 'woof'
});
t.is(process.env, oldEnv);
t.deepEqual(process.env, envCopy);
t.deepEqual(Object.getOwnPropertyDescriptor(process, 'env'), oldEnvDescriptor);
});
test('disallows values in .env.example', (t) => {
const error = t.throws(() => {
fixture('example-has-values');
}, { instanceOf : Error });
const envPath = path.join('fixture', 'example-has-values', '.env');
const examplePath = envPath + '.example';
t.is(error.message, `No values are allowed in ${examplePath}, put them in ${envPath} instead`);
});
test('friendly error if .env.example is not a file', (t) => {
const error = t.throws(() => {
fixture('example-not-file');
}, { instanceOf : Error });
const filepath = path.join('fixture', 'example-not-file', '.env.example');
t.is(error.message, `Filepath must be a file: ${filepath}`);
});
test('friendly error if .env is not a file', (t) => {
const error = t.throws(() => {
fixture('env-not-file');
}, { instanceOf : Error });
const filepath = path.join('fixture', 'env-not-file', '.env');
t.is(error.message, `Filepath must be a file: ${filepath}`);
});
test('requires all vars from .env.example', (t) => {
const error = t.throws(() => {
fixture('missing-env-entry');
}, { instanceOf : Error });
t.is(error.message, 'Environment variables are missing: MISSING, EMPTY');
});