forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
217 lines (197 loc) · 6.57 KB
/
config.js
File metadata and controls
217 lines (197 loc) · 6.57 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
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
const assert = require('assert');
const {
getFromCli,
getFromDefault,
getFromEnv,
} = require('../../lib/config/config-utils');
const Config = require('../../lib/config');
function configHasError(args, errorFindFunction) {
const config = new Config(args, {});
const validations = config.getValidations();
assert(validations.errors);
const found = validations.errors.find(errorFindFunction);
assert(found);
}
describe('lib/config/from-default', function () {
it('provides expected values', function () {
const conf = getFromDefault();
assert.equal(conf.port, 80, 'default port');
assert(conf.dbPath !== '$HOME/sqlpad/db', 'dbPath should change');
});
});
describe('lib/config/from-env', function () {
it('provides expected values', function () {
const conf = getFromEnv({ SQLPAD_PORT: 8000 });
assert.equal(conf.port, 8000, 'conf.port');
});
});
describe('lib/config/from-cli', function () {
it('provides expected values', function () {
const conf = getFromCli({
keyPath: 'key/path',
certPath: 'cert/path',
});
assert.equal(conf.keyPath, 'key/path', 'keyPath');
assert.equal(conf.certPath, 'cert/path', 'certPath');
});
});
describe('lib/config', function () {
it('Error: Unknown session store', function () {
configHasError({ sessionStore: 'not-real-store' }, (error) =>
error.includes('SQLPAD_SESSION_STORE must be one of')
);
});
it('Error: Unknown query result store', function () {
configHasError({ queryResultStore: 'not-real-store' }, (error) =>
error.includes('SQLPAD_QUERY_RESULT_STORE must be one of')
);
});
it('Error: redis store requires redis URI', function () {
configHasError({ sessionStore: 'redis' }, (error) =>
error.includes('Redis session store requires SQLPAD_REDIS_URI to be set')
);
configHasError({ queryResultStore: 'redis' }, (error) =>
error.includes(
'Redis query result store requires SQLPAD_REDIS_URI to be set'
)
);
});
it('Errors for old cli flag', function () {
const config = new Config({ debug: true }, {});
const validations = config.getValidations();
assert(validations.errors);
const found = validations.errors.find(
(error) => error.includes('debug') && error.includes('NOT RECOGNIZED')
);
assert(found, 'has error about old key');
});
it('Errors for old env var', function () {
const config = new Config({}, { SQLPAD_DEBUG: 'true' });
const validations = config.getValidations();
assert(validations.errors);
const found = validations.errors.find(
(error) =>
error.includes('SQLPAD_DEBUG') && error.includes('NOT RECOGNIZED')
);
assert(found, 'has error about old key');
});
it('Warns for unknown env var', function () {
const config = new Config({}, { SQLPAD_UNKNOWN_VAR: 'true' });
const validations = config.getValidations();
assert(validations.warnings);
const found = validations.warnings.find(
(warning) =>
warning.includes('SQLPAD_UNKNOWN_VAR') &&
warning.includes('NOT RECOGNIZED')
);
assert(found, 'has warning about unknown key');
});
it('Errors env connection missing name', function () {
const config = new Config(
{},
{ SQLPAD_CONNECTIONS__test__driver: 'postgres' }
);
const validations = config.getValidations();
assert(validations.errors);
const found = validations.errors.find((error) =>
error.includes('SQLPAD_CONNECTIONS__test__name missing')
);
assert(found, 'has error');
});
it('Errors env connection missing driver', function () {
const config = new Config(
{},
{ SQLPAD_CONNECTIONS__test__name: 'My test' }
);
const validations = config.getValidations();
assert(validations.errors);
const found = validations.errors.find((error) =>
error.includes('SQLPAD_CONNECTIONS__test__driver missing')
);
assert(found, 'has error');
});
it('Errors env connection invalid driver', function () {
const config = new Config(
{},
{
SQLPAD_CONNECTIONS__test__name: 'My test',
SQLPAD_CONNECTIONS__test__driver: 'foo',
}
);
const validations = config.getValidations();
assert(validations.errors);
const found = validations.errors.find((error) =>
error.includes(
'Environment config SQLPAD_CONNECTIONS__test__driver invalid. "foo" not a supported driver.'
)
);
assert(found, 'has error');
});
it('Errors env connection invalid driver field', function () {
const config = new Config(
{},
{
SQLPAD_CONNECTIONS__test__name: 'My test',
SQLPAD_CONNECTIONS__test__driver: 'postgres',
SQLPAD_CONNECTIONS__test__wrongField: 'localhost',
}
);
const validations = config.getValidations();
assert(validations.errors);
const found = validations.errors.find((error) =>
error.includes(
'Environment config SQLPAD_CONNECTIONS__test__wrongField invalid. "wrongField" not a known field for postgres.'
)
);
assert(found, 'has error');
});
it('Warns for deprecated config', function () {
const config = new Config({ deprecatedTestConfig: 'just a test' }, {});
const validations = config.getValidations();
assert(validations.warnings);
const found = validations.warnings.find(
(warning) =>
warning.includes('deprecatedTestConfig') &&
warning.includes('DEPRECATED')
);
assert(found, 'has deprecated key warning');
});
it('.get() should get a value provided by default', function () {
const config = new Config({}, {});
assert.equal(config.get('ip'), '0.0.0.0');
});
it('.get() should only accept key in config items', function () {
const config = new Config({}, {});
assert.throws(() => config.get('non-existent-key'), Error);
});
it('cli args overrides env', function () {
const config = new Config(
{
appLogLevel: 'warn',
},
{
SQLPAD_APP_LOG_LEVEL: 'silent',
}
);
assert.equal(config.get('appLogLevel'), 'warn');
});
it('env provides value expected', function () {
const config = new Config(
{
appLogLevel: 'warn',
},
{
SQLPAD_APP_LOG_LEVEL: 'silent',
SQLPAD_WEB_LOG_LEVEL: 'silent',
}
);
assert.equal(config.get('webLogLevel'), 'silent');
});
it('gets validation for missing dbPath', function () {
const config = new Config({}, {});
const { errors } = config.getValidations();
assert(errors[0].includes('dbPath'));
});
});