-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathuri_spec_runner.ts
375 lines (341 loc) · 12.7 KB
/
uri_spec_runner.ts
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
366
367
368
369
370
371
372
373
374
375
import { expect } from 'chai';
import { MongoAPIError, MongoClient, MongoParseError, MongoRuntimeError } from '../mongodb';
type HostObject = {
type: 'ipv4' | 'ip_literal' | 'hostname' | 'unix';
host: string;
port: number;
};
type UriTestBase = {
description: string;
uri: string;
valid: boolean;
};
interface UriTest extends UriTestBase {
warning: boolean;
hosts: HostObject[];
auth: {
username: string;
password: string;
db: string;
};
options: Record<string, any>;
}
interface AuthTest extends UriTestBase {
credential: {
username: string;
password: string;
source: string;
mechanism: string;
mechanism_properties: Record<string, any>;
};
}
function isAuthTest(test: AuthTest | UriTest): test is AuthTest {
return !('options' in test);
}
function isUriTest(test: AuthTest | UriTest): test is UriTest {
return 'options' in test;
}
export function executeUriValidationTest(
test: UriTest | AuthTest,
shouldNotThrowOnWarn = false
): void {
const knownTestKeys = [
'callback',
'description',
'uri',
'valid',
'warning',
'hosts',
'auth',
'options',
'credential'
];
expect(knownTestKeys).to.include.members(Object.keys(test));
const errorMessage = `"${test.uri}"`;
const valid = test.valid && (!(test as UriTest).warning || shouldNotThrowOnWarn);
if (!valid) {
try {
new MongoClient(test.uri);
expect.fail(`Expected "${test.uri}" to be invalid${test.valid ? ' because of warning' : ''}`);
} catch (err) {
if (err instanceof MongoRuntimeError) {
expect(err).to.have.nested.property('cause.code').equal('ERR_INVALID_URL');
} else if (
// most of our validation is MongoParseError, which does not extend from MongoAPIError
!(err instanceof MongoParseError) &&
// the rest of our validation extends from MongoAPIError
!(err instanceof MongoAPIError) &&
// mongodb-connection-string-url does not export its MongoParseError so we can't check for it directly
err.name !== 'MongoParseError'
) {
throw err;
}
}
return;
}
// If a callback is specified in the spec test, we need to pass in references to those callbacks
// in the actual options provided to the MongoClient. This is because OIDC does not allow
// functions for callbacks in the URI itself but needs to validate they are passed.
const CALLBACKS = {
oidcRequest: async () => {
return { accessToken: '<test>' };
},
oidcRefresh: async () => {
return { accessToken: '<test>' };
}
};
const CALLBACK_MAPPINGS = {
oidcRequest: 'REQUEST_TOKEN_CALLBACK',
oidcRefresh: 'REFRESH_TOKEN_CALLBACK'
};
const mongoClientOptions = {};
if (test.callback) {
const authMechanismProperties = Object.create(null);
for (const callback of test.callback) {
authMechanismProperties[CALLBACK_MAPPINGS[callback]] = CALLBACKS[callback];
}
mongoClientOptions.authMechanismProperties = authMechanismProperties;
}
const client = new MongoClient(test.uri, mongoClientOptions);
const options = client.options;
expect(options, errorMessage).to.be.an('object').that.is.not.empty;
// non-auth tests can have an expected value for hosts
if (isUriTest(test) && test.hosts != null) {
for (const [index, { host, port }] of test.hosts.entries()) {
const actualHost = options.hosts[index];
if (actualHost.host == null && actualHost.socketPath == null) {
expect.fail('Expected host to define "host" or "socketPath" properties');
}
if (actualHost.host != null) {
expect(actualHost, errorMessage).property('host').to.equal(host);
} else {
expect(actualHost, errorMessage).property('socketPath').to.equal(host);
}
if (port != null) expect(actualHost).property('port').to.equal(port);
}
}
// depending on whether this is a UriTest or an AuthTest,
// expected credential option values are defined in test.auth or test.credential, respectively,
// and additional expected property values are defined in test.options or mixed into test.credential, respectively
let credentialsToTest: { source?: string; password?: string; username?: string } = {};
let optionsToTest: Record<string, any> = isUriTest(test) ? test.options || {} : {};
if (isAuthTest(test) && test.credential != null) {
// handle AuthTest credential testing
// Note: unlike the other URI tests, we cannot test dbName
// because the auth tests do not provide an expected value for it
const credentialOptions = [
'username',
'password',
'source',
'mechanism',
'mechanism_properties'
];
expect(test).property('credential').to.have.all.keys(credentialOptions);
optionsToTest = (({ mechanism, mechanism_properties }) => ({
mechanism,
mechanism_properties
}))(test.credential);
credentialsToTest = (({ username, password, source }) => ({
username,
password,
source
}))(test.credential);
} else if (isUriTest(test) && test.auth !== null) {
// handle UriTest credential and dbName testing
const credentialOptions = ['username', 'password', 'db'];
expect(test).property('auth').to.have.all.keys(credentialOptions);
credentialsToTest = (({ username, password, db }) => ({
username,
password,
source: db
}))(test.auth);
if (test.auth.db !== null) {
expect(options, `${errorMessage} dbName`).to.have.property('dbName').equal(test.auth.db);
} else {
expect(options, `${errorMessage} default dbName`).to.have.property('dbName').equal('test');
}
}
if (credentialsToTest.username != null) {
expect(options, errorMessage).to.have.property('credentials');
for (const [prop, value] of Object.entries(credentialsToTest)) {
if (value != null) {
expect(options, `${errorMessage} ${prop}`)
.to.have.nested.property(`credentials.${prop}`)
.equal(value);
}
}
}
for (const [optionKey, optionValue] of Object.entries(optionsToTest)) {
let expectedProp;
switch (optionKey) {
//** AUTH OPTIONS **/
case 'authSource':
expectedProp = 'credentials.source';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.nested.property(expectedProp)
.equal(optionValue);
break;
case 'authmechanism':
case 'authMechanism':
case 'mechanism':
expectedProp = 'credentials.mechanism';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.nested.property(expectedProp)
.equal(optionValue ?? 'DEFAULT');
break;
case 'authmechanismproperties':
case 'authMechanismProperties':
case 'mechanism_properties':
for (const [expectedMechProp, expectedMechValue] of Object.entries(optionValue || {})) {
if (
expectedMechProp === 'SERVICE_NAME' &&
options.credentials.mechanismProperties &&
!options.credentials.mechanismProperties.SERVICE_NAME
) {
// TODO(NODE-3925): Ensure default SERVICE_NAME is set on the parsed mechanism properties
continue;
}
if (
expectedMechProp === 'REQUEST_TOKEN_CALLBACK' ||
expectedMechProp === 'REFRESH_TOKEN_CALLBACK'
) {
expect(
options,
`${errorMessage} credentials.mechanismProperties.${expectedMechProp}`
).to.have.nested.property(`credentials.mechanismProperties.${expectedMechProp}`);
} else {
expect(options, `${errorMessage} credentials.mechanismProperties.${expectedMechProp}`)
.to.have.nested.property(`credentials.mechanismProperties.${expectedMechProp}`)
.equal(expectedMechValue);
}
}
break;
//** READ CONCERN OPTIONS **/
case 'readConcernLevel':
expectedProp = 'readConcern.level';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.nested.property(expectedProp)
.equal(optionValue);
break;
case 'readPreference':
expectedProp = 'readPreference.mode';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.nested.property(expectedProp)
.deep.equal(optionValue);
break;
case 'readPreferenceTags':
expectedProp = 'readPreference.tags';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.nested.property(expectedProp)
.deep.equal(optionValue);
break;
case 'maxStalenessSeconds':
expectedProp = 'readPreference.maxStalenessSeconds';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.nested.property(expectedProp)
.deep.equal(optionValue);
break;
//** WRITE CONCERN OPTIONS **/
case 'w':
expectedProp = 'writeConcern.w';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.nested.property(expectedProp)
.equal(optionValue);
break;
case 'wTimeoutMS':
case 'wtimeoutms':
expectedProp = 'writeConcern.wtimeout';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.nested.property(expectedProp)
.equal(optionValue);
break;
case 'journal':
expectedProp = 'writeConcern.j';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.nested.property(expectedProp)
.equal(optionValue);
break;
//** TLS OPTIONS **/
case 'tlsAllowInvalidCertificates':
expectedProp = 'rejectUnauthorized';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.property(expectedProp)
.equal(!optionValue);
break;
case 'tlsAllowInvalidHostnames':
expectedProp = 'checkServerIdentity';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.property(expectedProp)
.that.is.a(optionValue ? 'function' : 'undefined');
break;
case 'tlsInsecure':
expect(options, `${errorMessage} tlsInsecure -> rejectUnauthorized`)
.to.have.property('rejectUnauthorized')
.equal(!optionValue);
expect(options, `${errorMessage} tlsInsecure -> checkServerIdentity`)
.to.have.property('checkServerIdentity')
.that.is.a(optionValue ? 'function' : 'undefined');
break;
case 'tlsCertificateKeyFilePassword':
expectedProp = 'passphrase';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.property(expectedProp)
.equal(optionValue);
break;
case 'tlsCertificateKeyFile':
expectedProp = 'tlsCertificateKeyFile';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.property(expectedProp)
.equal(optionValue);
break;
case 'tlsCAFile':
expectedProp = 'tlsCAFile';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.property(expectedProp)
.equal(optionValue);
break;
//** MISC SPECIAL PARSE RULE OPTIONS **/
case 'appname':
expectedProp = 'metadata.application.name';
expect(options, `${errorMessage} ${optionKey} -> ${expectedProp}`)
.to.have.nested.property(expectedProp)
.equal(optionValue);
break;
case 'compressors':
expect(options, `${errorMessage} ${optionKey}`)
.to.have.property(optionKey)
.deep.equal(optionValue);
break;
case 'replicaset': // replicaset appears with both casings in the test expectations
expect(options, `${errorMessage} replicaSet`)
.to.have.property('replicaSet')
.equal(optionValue);
break;
//** DIRECTLY MAPPED OPTIONS **/
case 'zlibCompressionLevel':
case 'maxConnecting':
case 'maxPoolSize':
case 'minPoolSize':
case 'connectTimeoutMS':
case 'heartbeatFrequencyMS':
case 'localThresholdMS':
case 'maxIdleTimeMS':
case 'serverSelectionTimeoutMS':
case 'socketTimeoutMS':
case 'retryWrites':
case 'directConnection':
case 'loadBalanced':
case 'replicaSet':
case 'srvServiceName':
case 'srvMaxHosts':
case 'tls':
expect(options, `${errorMessage} ${optionKey}`)
.to.have.property(optionKey)
.equal(optionValue);
break;
//** UNKNOWN OPTIONS **/
default:
throw Error(`This option is not covered by the spec test runner: ${optionKey}`);
}
}
}