forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLI.spec.js
330 lines (306 loc) · 9.66 KB
/
CLI.spec.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
'use strict';
const commander = require('../lib/cli/utils/commander').default;
const definitions = require('../lib/cli/definitions/parse-server').default;
const liveQueryDefinitions = require('../lib/cli/definitions/parse-live-query-server').default;
const path = require('path');
const { spawn } = require('child_process');
const testDefinitions = {
arg0: 'PROGRAM_ARG_0',
arg1: {
env: 'PROGRAM_ARG_1',
required: true,
},
arg2: {
env: 'PROGRAM_ARG_2',
action: function (value) {
const intValue = parseInt(value);
if (!Number.isInteger(intValue)) {
throw 'arg2 is invalid';
}
return intValue;
},
},
arg3: {},
arg4: {
default: 'arg4Value',
},
};
describe('commander additions', () => {
afterEach(done => {
commander.options = [];
delete commander.arg0;
delete commander.arg1;
delete commander.arg2;
delete commander.arg3;
delete commander.arg4;
done();
});
it('should load properly definitions from args', done => {
commander.loadDefinitions(testDefinitions);
commander.parse([
'node',
'./CLI.spec.js',
'--arg0',
'arg0Value',
'--arg1',
'arg1Value',
'--arg2',
'2',
'--arg3',
'some',
]);
expect(commander.arg0).toEqual('arg0Value');
expect(commander.arg1).toEqual('arg1Value');
expect(commander.arg2).toEqual(2);
expect(commander.arg3).toEqual('some');
expect(commander.arg4).toEqual('arg4Value');
done();
});
it('should load properly definitions from env', done => {
commander.loadDefinitions(testDefinitions);
commander.parse([], {
PROGRAM_ARG_0: 'arg0ENVValue',
PROGRAM_ARG_1: 'arg1ENVValue',
PROGRAM_ARG_2: '3',
});
expect(commander.arg0).toEqual('arg0ENVValue');
expect(commander.arg1).toEqual('arg1ENVValue');
expect(commander.arg2).toEqual(3);
expect(commander.arg4).toEqual('arg4Value');
done();
});
it('should load properly use args over env', () => {
commander.loadDefinitions(testDefinitions);
commander.parse(['node', './CLI.spec.js', '--arg0', 'arg0Value', '--arg4', ''], {
PROGRAM_ARG_0: 'arg0ENVValue',
PROGRAM_ARG_1: 'arg1ENVValue',
PROGRAM_ARG_2: '4',
PROGRAM_ARG_4: 'arg4ENVValue',
});
expect(commander.arg0).toEqual('arg0Value');
expect(commander.arg1).toEqual('arg1ENVValue');
expect(commander.arg2).toEqual(4);
expect(commander.arg4).toEqual('');
});
it('should fail in action as port is invalid', done => {
commander.loadDefinitions(testDefinitions);
expect(() => {
commander.parse(['node', './CLI.spec.js', '--arg0', 'arg0Value'], {
PROGRAM_ARG_0: 'arg0ENVValue',
PROGRAM_ARG_1: 'arg1ENVValue',
PROGRAM_ARG_2: 'hello',
});
}).toThrow('arg2 is invalid');
done();
});
it('should not override config.json', done => {
spyOn(console, 'log').and.callFake(() => {});
commander.loadDefinitions(testDefinitions);
commander.parse(
['node', './CLI.spec.js', '--arg0', 'arg0Value', './spec/configs/CLIConfig.json'],
{
PROGRAM_ARG_0: 'arg0ENVValue',
PROGRAM_ARG_1: 'arg1ENVValue',
}
);
const options = commander.getOptions();
expect(options.arg2).toBe(8888);
expect(options.arg3).toBe('hello'); //config value
expect(options.arg4).toBe('/1');
done();
});
it('should fail with invalid values in JSON', done => {
commander.loadDefinitions(testDefinitions);
expect(() => {
commander.parse(
['node', './CLI.spec.js', '--arg0', 'arg0Value', './spec/configs/CLIConfigFail.json'],
{
PROGRAM_ARG_0: 'arg0ENVValue',
PROGRAM_ARG_1: 'arg1ENVValue',
}
);
}).toThrow('arg2 is invalid');
done();
});
it('should fail when too many apps are set', done => {
commander.loadDefinitions(testDefinitions);
expect(() => {
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigFailTooManyApps.json']);
}).toThrow('Multiple apps are not supported');
done();
});
it('should load config from apps', done => {
spyOn(console, 'log').and.callFake(() => {});
commander.loadDefinitions(testDefinitions);
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigApps.json']);
const options = commander.getOptions();
expect(options.arg1).toBe('my_app');
expect(options.arg2).toBe(8888);
expect(options.arg3).toBe('hello'); //config value
expect(options.arg4).toBe('/1');
done();
});
it('should fail when passing an invalid arguement', done => {
commander.loadDefinitions(testDefinitions);
expect(() => {
commander.parse(['node', './CLI.spec.js', './spec/configs/CLIConfigUnknownArg.json']);
}).toThrow('error: unknown option myArg');
done();
});
});
describe('definitions', () => {
it('should have valid types', () => {
for (const key in definitions) {
const definition = definitions[key];
expect(typeof definition).toBe('object');
if (typeof definition.env !== 'undefined') {
expect(typeof definition.env).toBe('string');
}
expect(typeof definition.help).toBe('string');
if (typeof definition.required !== 'undefined') {
expect(typeof definition.required).toBe('boolean');
}
if (typeof definition.action !== 'undefined') {
expect(typeof definition.action).toBe('function');
}
}
});
it('should throw when using deprecated facebookAppIds', () => {
expect(() => {
definitions.facebookAppIds.action();
}).toThrow();
});
});
describe('LiveQuery definitions', () => {
it('should have valid types', () => {
for (const key in liveQueryDefinitions) {
const definition = liveQueryDefinitions[key];
expect(typeof definition).toBe('object');
if (typeof definition.env !== 'undefined') {
expect(typeof definition.env).toBe('string');
}
expect(typeof definition.help).toBe('string', `help for ${key} should be a string`);
if (typeof definition.required !== 'undefined') {
expect(typeof definition.required).toBe('boolean');
}
if (typeof definition.action !== 'undefined') {
expect(typeof definition.action).toBe('function');
}
}
});
});
describe('execution', () => {
const binPath = path.resolve(__dirname, '../bin/parse-server');
let childProcess;
let aggregatedData;
function handleStdout(childProcess, done, aggregatedData, requiredData) {
childProcess.stdout.on('data', data => {
data = data.toString();
aggregatedData.push(data);
if (requiredData.every(required => aggregatedData.some(aggregated => aggregated.includes(required)))) {
done();
}
});
}
function handleStderr(childProcess, done) {
childProcess.stderr.on('data', data => {
data = data.toString();
if (!data.includes('[DEP0040] DeprecationWarning')) {
done.fail(data);
}
});
}
function handleError(childProcess, done) {
childProcess.on('error', err => {
done.fail(err);
});
}
beforeEach(() => {
aggregatedData = [];
});
afterEach(done => {
if (childProcess) {
childProcess.on('close', () => {
childProcess = undefined;
done();
});
childProcess.kill();
}
});
it_id('a0ab74b4-f805-4e03-b31d-b5cd59e64495')(it)('should start Parse Server', done => {
const env = { ...process.env };
env.NODE_OPTIONS = '--dns-result-order=ipv4first --trace-deprecation';
childProcess = spawn(
binPath,
['--appId', 'test', '--masterKey', 'test', '--databaseURI', databaseURI, '--port', '1339'],
{ env }
);
handleStdout(childProcess, done, aggregatedData, ['parse-server running on']);
handleStderr(childProcess, done);
handleError(childProcess, done);
});
it_id('d7165081-b133-4cba-901b-19128ce41301')(it)('should start Parse Server with GraphQL', async done => {
const env = { ...process.env };
env.NODE_OPTIONS = '--dns-result-order=ipv4first --trace-deprecation';
childProcess = spawn(
binPath,
[
'--appId',
'test',
'--masterKey',
'test',
'--databaseURI',
databaseURI,
'--port',
'1340',
'--mountGraphQL',
],
{ env }
);
handleStdout(childProcess, done, aggregatedData, [
'parse-server running on',
'GraphQL running on',
]);
handleStderr(childProcess, done);
handleError(childProcess, done);
});
it_id('2769cdb4-ce8a-484d-8a91-635b5894ba7e')(it)('should start Parse Server with GraphQL and Playground', async done => {
const env = { ...process.env };
env.NODE_OPTIONS = '--dns-result-order=ipv4first --trace-deprecation';
childProcess = spawn(
binPath,
[
'--appId',
'test',
'--masterKey',
'test',
'--databaseURI',
databaseURI,
'--port',
'1341',
'--mountGraphQL',
'--mountPlayground',
],
{ env }
);
handleStdout(childProcess, done, aggregatedData, [
'parse-server running on',
'Playground running on',
'GraphQL running on',
]);
handleStderr(childProcess, done);
handleError(childProcess, done);
});
it_id('23caddd7-bfea-4869-8bd4-0f2cd283c8bd')(it)('can start Parse Server with auth via CLI', done => {
const env = { ...process.env };
env.NODE_OPTIONS = '--dns-result-order=ipv4first --trace-deprecation';
childProcess = spawn(
binPath,
['--databaseURI', databaseURI, './spec/configs/CLIConfigAuth.json'],
{ env }
);
handleStdout(childProcess, done, aggregatedData, ['parse-server running on']);
handleStderr(childProcess, done);
handleError(childProcess, done);
});
});