1 | const fs = require('fs');
|
2 | const debug = require('debug')('TEST');
|
3 | const { spawn } = require('child_process');
|
4 |
|
5 | describe('Test Scenarios', () => {
|
6 | describe('base case', () => {
|
7 | it('variable set - exit cleanly', (done) => {
|
8 | const p = spawn('node', ['./test/scenarios/base-case.js'], {
|
9 | stdio: 'pipe',
|
10 | env: {
|
11 | NODE_ENV: 'development',
|
12 | MONGO_URL: 'EXTERNAL',
|
13 | },
|
14 | });
|
15 | p.stdout.on('data', (data) => debug(data.toString()));
|
16 | p.on('exit', (code) => {
|
17 | if (code === 0) {
|
18 | done();
|
19 | } else {
|
20 | throw new Error(`test failed ${code}`);
|
21 | }
|
22 | });
|
23 | });
|
24 |
|
25 | it('NODE_ENV not set - exit cleanly', (done) => {
|
26 | const p = spawn('node', ['./test/scenarios/base-case.js'], {
|
27 | stdio: 'pipe',
|
28 | env: {
|
29 |
|
30 | MONGO_URL: 'EXTERNAL',
|
31 | },
|
32 | });
|
33 | p.stdout.on('data', (data) => debug(data.toString()));
|
34 | p.on('exit', (code) => {
|
35 | if (code === 0) {
|
36 | done();
|
37 | } else {
|
38 | throw new Error(`test failed ${code}`);
|
39 | }
|
40 | });
|
41 | });
|
42 |
|
43 | it('should error if missing the env variable', (done) => {
|
44 | const p = spawn('node', ['./test/scenarios/base-case.js'], {
|
45 | stdio: 'pipe',
|
46 | env: {
|
47 | NODE_ENV: 'development',
|
48 |
|
49 | },
|
50 | });
|
51 | p.stdout.on('data', (data) => debug(data.toString()));
|
52 | p.on('exit', (code) => {
|
53 | if (code !== 0) {
|
54 | done();
|
55 | } else {
|
56 | throw new Error(`test failed ${code}`);
|
57 | }
|
58 | });
|
59 | });
|
60 | });
|
61 |
|
62 | describe('base case with default', () => {
|
63 | it('should pass even thought env variable is not set', (done) => {
|
64 | const p = spawn('node', ['./test/scenarios/base-case-default.js'], {
|
65 | stdio: 'pipe',
|
66 | env: {
|
67 | NODE_ENV: 'development',
|
68 |
|
69 | },
|
70 | });
|
71 | p.stdout.on('data', (data) => debug(data.toString()));
|
72 | p.on('exit', (code) => {
|
73 | if (code === 0) {
|
74 | done();
|
75 | } else {
|
76 | throw new Error(`test failed ${code}`);
|
77 | }
|
78 | });
|
79 | });
|
80 |
|
81 | it('should error because of missing env in production', (done) => {
|
82 | const p = spawn('node', ['./test/scenarios/base-case-default.js'], {
|
83 | stdio: 'pipe',
|
84 | env: {
|
85 | NODE_ENV: 'production',
|
86 |
|
87 | },
|
88 | });
|
89 | p.stdout.on('data', (data) => debug(data.toString()));
|
90 | p.on('exit', (code) => {
|
91 | if (code !== 0) {
|
92 | done();
|
93 | } else {
|
94 | throw new Error(`test failed ${code}`);
|
95 | }
|
96 | });
|
97 | });
|
98 | });
|
99 |
|
100 | describe('base case optional true', () => {
|
101 | it('should not error if missing the env variable', (done) => {
|
102 | const p = spawn('node', ['./test/scenarios/base-case-optional.js'], {
|
103 | stdio: 'pipe',
|
104 | env: {
|
105 | NODE_ENV: 'development',
|
106 |
|
107 | },
|
108 | });
|
109 | p.stdout.on('data', (data) => debug(data.toString()));
|
110 | p.on('exit', (code) => {
|
111 | if (code === 0) {
|
112 | done();
|
113 | } else {
|
114 | throw new Error(`test failed ${code}`);
|
115 | }
|
116 | });
|
117 | });
|
118 | });
|
119 |
|
120 | describe('optional env true', () => {
|
121 | it('should not error if missing the env variable', (done) => {
|
122 | const p = spawn('node', ['./test/scenarios/base-case-optional-env.js'], {
|
123 | stdio: 'pipe',
|
124 | env: {
|
125 | NODE_ENV: 'development',
|
126 |
|
127 | },
|
128 | });
|
129 | p.stdout.on('data', (data) => debug(data.toString()));
|
130 | p.on('exit', (code) => {
|
131 | if (code === 0) {
|
132 | done();
|
133 | } else {
|
134 | throw new Error(`test failed ${code}`);
|
135 | }
|
136 | });
|
137 | });
|
138 | });
|
139 |
|
140 | describe('optional env fail', () => {
|
141 | it('should not error if missing the env variable', (done) => {
|
142 | const p = spawn('node', ['./test/scenarios/base-case-optional-env.js'], {
|
143 | stdio: 'pipe',
|
144 | env: {
|
145 | NODE_ENV: 'production',
|
146 |
|
147 | },
|
148 | });
|
149 | p.stdout.on('data', (data) => debug(data.toString()));
|
150 | p.on('exit', (code) => {
|
151 | if (code === 1) {
|
152 | done();
|
153 | } else {
|
154 | throw new Error(`test failed ${code}`);
|
155 | }
|
156 | });
|
157 | });
|
158 | });
|
159 |
|
160 | describe('test strict', () => {
|
161 | it('should pass because NODE_ENV is set', (done) => {
|
162 | const p = spawn('node', ['./test/scenarios/strict-test.js'], {
|
163 | stdio: 'pipe',
|
164 | env: {
|
165 | NODE_ENV: 'development',
|
166 |
|
167 | },
|
168 | });
|
169 | p.stdout.on('data', (data) => debug(data.toString()));
|
170 | p.on('exit', (code) => {
|
171 | if (code === 0) {
|
172 | done();
|
173 | } else {
|
174 | throw new Error(`test failed ${code}`);
|
175 | }
|
176 | });
|
177 | });
|
178 |
|
179 | it('should fail because NODE_ENV is not set', (done) => {
|
180 | const p = spawn('node', ['./test/scenarios/strict-test.js'], {
|
181 | stdio: 'pipe',
|
182 | env: {
|
183 |
|
184 |
|
185 | },
|
186 | });
|
187 | p.stdout.on('data', (data) => debug(data.toString()));
|
188 | p.on('exit', (code) => {
|
189 | if (code !== 0) {
|
190 | done();
|
191 | } else {
|
192 | throw new Error(`test failed ${code}`);
|
193 | }
|
194 | });
|
195 | });
|
196 | });
|
197 |
|
198 | describe('test secret file loading', () => {
|
199 | it('should exit status code zero with file present', (done) => {
|
200 | try {
|
201 | fs.mkdirSync('/run/secrets');
|
202 | } catch (err) {
|
203 | console.log(err.message);
|
204 | }
|
205 | fs.writeFileSync('/run/secrets/TEST_SECRET', 'MY_TEST_SECRET');
|
206 | const p = spawn('node', ['./test/scenarios/base-case-secret-file.js'], {
|
207 | stdio: 'pipe',
|
208 | env: {
|
209 | NODE_ENV: 'development',
|
210 | },
|
211 | });
|
212 | p.stdout.on('data', (data) => debug(data.toString()));
|
213 | p.on('exit', (code) => {
|
214 | fs.unlinkSync('/run/secrets/TEST_SECRET');
|
215 | if (code === 0) {
|
216 | done();
|
217 | } else {
|
218 | throw new Error(`test failed ${code}`);
|
219 | }
|
220 | });
|
221 | });
|
222 | it('should error file missing', (done) => {
|
223 | const p = spawn('node', ['./test/scenarios/base-case-secret-file.js'], {
|
224 | stdio: 'pipe',
|
225 | env: {
|
226 | NODE_ENV: 'development',
|
227 | },
|
228 | });
|
229 | p.stdout.on('data', (data) => debug(data.toString()));
|
230 | p.on('exit', (code) => {
|
231 | if (code !== 0) {
|
232 | done();
|
233 | } else {
|
234 | throw new Error(`test failed ${code}`);
|
235 | }
|
236 | });
|
237 | });
|
238 | });
|
239 |
|
240 | describe('test secret file loading', () => {
|
241 | it('should exit status code zero with file present', (done) => {
|
242 | fs.writeFileSync('/tmp/testfile', 'MY_TEST_SECRET');
|
243 | const p = spawn('node', ['./test/scenarios/base-case-secret-file.js'], {
|
244 | stdio: 'pipe',
|
245 | env: {
|
246 | NODE_ENV: 'development',
|
247 | TEST_SECRET_FILE: '/tmp/testfile',
|
248 | },
|
249 | });
|
250 | p.stdout.on('data', (data) => debug(data.toString()));
|
251 | p.on('exit', (code) => {
|
252 | fs.unlinkSync('/tmp/testfile');
|
253 | if (code === 0) {
|
254 | done();
|
255 | } else {
|
256 | throw new Error(`test failed ${code}`);
|
257 | }
|
258 | });
|
259 | });
|
260 | it('should error file missing', (done) => {
|
261 | const p = spawn('node', ['./test/scenarios/base-case-secret-file.js'], {
|
262 | stdio: 'pipe',
|
263 | env: {
|
264 | NODE_ENV: 'development',
|
265 | TEST_SECRET_FILE: '/tmp/testfile',
|
266 | },
|
267 | });
|
268 | p.stdout.on('data', (data) => debug(data.toString()));
|
269 | p.on('exit', (code) => {
|
270 | if (code !== 0) {
|
271 | done();
|
272 | } else {
|
273 | throw new Error(`test failed ${code}`);
|
274 | }
|
275 | });
|
276 | });
|
277 | });
|
278 | });
|