Skip to content

Commit 1f19ce6

Browse files
committed
remove withClient
1 parent fb3e43f commit 1f19ce6

20 files changed

+2082
-1248
lines changed

global.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { OneOrMore } from './src/mongo_types';
22
import type { TestConfiguration } from './test/tools/runner/config';
33

4-
type WithExclusion<T extends string> = `!${T}`;
5-
/** Defined in test/tools/runner/filters/mongodb_topology_filter.js (topologyTypeToString) */
6-
type TopologyTypes = 'single' | 'replicaset' | 'sharded' | 'load-balanced';
7-
type TopologyTypeRequirement = OneOrMore<TopologyTypes> | OneOrMore<WithExclusion<TopologyTypes>>;
8-
94
declare global {
105
interface MongoDBMetadataUI {
116
requires?: {
@@ -23,6 +18,11 @@ declare global {
2318
};
2419
}
2520

21+
type WithExclusion<T extends string> = `!${T}`;
22+
/** Defined in test/tools/runner/filters/mongodb_topology_filter.js (topologyTypeToString) */
23+
type TopologyTypes = 'single' | 'replicaset' | 'sharded' | 'load-balanced';
24+
type TopologyTypeRequirement = OneOrMore<TopologyTypes> | OneOrMore<WithExclusion<TopologyTypes>>;
25+
2626
interface MetadataAndTest<Fn> {
2727
metadata: MongoDBMetadataUI;
2828
test: Fn;

test/integration/auth/auth.prose.test.js

Lines changed: 103 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const sinon = require('sinon');
44
const { expect } = require('chai');
55
const { Connection } = require('../../../src/cmap/connection');
66
const { ScramSHA256 } = require('../../../src/cmap/auth/scram');
7-
const { setupDatabase, withClient } = require('../shared');
7+
const { setupDatabase } = require('../shared');
88
const { LEGACY_HELLO_COMMAND } = require('../../../src/constants');
99

1010
describe('auth prose tests', () => {
@@ -49,30 +49,30 @@ describe('auth prose tests', () => {
4949
* Step 1
5050
* Create three test users, one with only SHA-1, one with only SHA-256 and one with both.
5151
*/
52-
before(function () {
53-
return withClient(this.configuration.newClient(), client => {
54-
test.oldDbName = this.configuration.db;
55-
this.configuration.db = 'admin';
56-
const db = client.db(this.configuration.db);
57-
58-
const createUserCommands = users.map(user => ({
59-
createUser: user.username,
60-
pwd: user.password,
61-
roles: ['root'],
62-
mechanisms: user.mechanisms
63-
}));
64-
65-
return Promise.all(createUserCommands.map(cmd => db.command(cmd)));
66-
});
52+
before(async function () {
53+
const client = this.configuration.newClient();
54+
test.oldDbName = this.configuration.db;
55+
this.configuration.db = 'admin';
56+
const db = client.db(this.configuration.db);
57+
58+
const createUserCommands = users.map(user => ({
59+
createUser: user.username,
60+
pwd: user.password,
61+
roles: ['root'],
62+
mechanisms: user.mechanisms
63+
}));
64+
65+
await Promise.all(createUserCommands.map(cmd => db.command(cmd)));
66+
await client.close();
6767
});
6868

69-
after(function () {
70-
return withClient(this.configuration.newClient(), client => {
71-
const db = client.db(this.configuration.db);
72-
this.configuration.db = test.oldDbName;
69+
after(async function () {
70+
const client = this.configuration.newClient();
71+
const db = client.db(this.configuration.db);
72+
this.configuration.db = test.oldDbName;
7373

74-
return Promise.all(users.map(user => db.removeUser(user.username)));
75-
});
74+
await Promise.all(users.map(user => db.removeUser(user.username)));
75+
await client.close();
7676
});
7777

7878
/**
@@ -86,7 +86,7 @@ describe('auth prose tests', () => {
8686
user.mechanisms.forEach(mechanism => {
8787
it(`should auth ${user.description} when explicitly specifying ${mechanism}`, {
8888
metadata: { requires: { mongodb: '>=3.7.3' } },
89-
test: function () {
89+
test: async function () {
9090
const options = {
9191
auth: {
9292
username: user.username,
@@ -96,15 +96,15 @@ describe('auth prose tests', () => {
9696
authSource: this.configuration.db
9797
};
9898

99-
return withClient(this.configuration.newClient({}, options), client => {
100-
return client.db(this.configuration.db).stats();
101-
});
99+
const client = this.configuration.newClient({}, options);
100+
await client.db(this.configuration.db).stats();
101+
await client.close();
102102
}
103103
});
104104

105105
it(`should auth ${user.description} when explicitly specifying ${mechanism} in url`, {
106106
metadata: { requires: { mongodb: '>=3.7.3' } },
107-
test: function () {
107+
test: async function () {
108108
const username = encodeURIComponent(user.username);
109109
const password = encodeURIComponent(user.password);
110110

@@ -116,16 +116,15 @@ describe('auth prose tests', () => {
116116

117117
const client = this.configuration.newClient(url);
118118

119-
return withClient(client, client => {
120-
return client.db(this.configuration.db).stats();
121-
});
119+
await client.db(this.configuration.db).stats();
120+
await client.close();
122121
}
123122
});
124123
});
125124

126125
it(`should auth ${user.description} using mechanism negotiaton`, {
127126
metadata: { requires: { mongodb: '>=3.7.3' } },
128-
test: function () {
127+
test: async function () {
129128
const options = {
130129
auth: {
131130
username: user.username,
@@ -134,24 +133,23 @@ describe('auth prose tests', () => {
134133
authSource: this.configuration.db
135134
};
136135

137-
return withClient(this.configuration.newClient({}, options), client => {
138-
return client.db(this.configuration.db).stats();
139-
});
136+
const client = this.configuration.newClient({}, options);
137+
await client.db(this.configuration.db).stats();
138+
await client.close();
140139
}
141140
});
142141

143142
it(`should auth ${user.description} using mechanism negotiaton and url`, {
144143
metadata: { requires: { mongodb: '>=3.7.3' } },
145-
test: function () {
144+
test: async function () {
146145
const username = encodeURIComponent(user.username);
147146
const password = encodeURIComponent(user.password);
148147
const url = makeConnectionString(this.configuration, username, password);
149148

150149
const client = this.configuration.newClient(url);
151150

152-
return withClient(client, client => {
153-
return client.db(this.configuration.db).stats();
154-
});
151+
await client.db(this.configuration.db).stats();
152+
await client.close();
155153
}
156154
});
157155
});
@@ -163,7 +161,7 @@ describe('auth prose tests', () => {
163161
*/
164162
it('should select SCRAM-SHA-256 for a user that supports both auth mechanisms', {
165163
metadata: { requires: { mongodb: '>=3.7.3' } },
166-
test: function () {
164+
test: async function () {
167165
const options = {
168166
auth: {
169167
username: userMap.both.username,
@@ -174,16 +172,17 @@ describe('auth prose tests', () => {
174172

175173
test.sandbox.spy(ScramSHA256.prototype, 'auth');
176174

177-
return withClient(this.configuration.newClient({}, options), () => {
178-
expect(ScramSHA256.prototype.auth.called).to.equal(true);
179-
});
175+
const client = this.configuration.newClient({}, options);
176+
await client.db().command({ ping: 1 });
177+
expect(ScramSHA256.prototype.auth.called).to.equal(true);
178+
await client.close();
180179
}
181180
});
182181

183182
// TODO: not spec
184183
it('should shorten SCRAM conversations if the server supports it', {
185184
metadata: { requires: { mongodb: '>=4.4', topology: ['single'] } },
186-
test: function () {
185+
test: async function () {
187186
const options = {
188187
auth: {
189188
username: userMap.both.username,
@@ -207,9 +206,10 @@ describe('auth prose tests', () => {
207206
auth.apply(this, [authContext, _callback]);
208207
});
209208

210-
return withClient(this.configuration.newClient({}, options), () => {
211-
expect(runCommandSpy.callCount).to.equal(1);
212-
});
209+
const client = this.configuration.newClient({}, options);
210+
await client.db().command({ ping: 1 });
211+
expect(runCommandSpy.callCount).to.equal(1);
212+
await client.close();
213213
}
214214
});
215215

@@ -219,7 +219,7 @@ describe('auth prose tests', () => {
219219
*/
220220
it('should fail to connect if incorrect auth mechanism is explicitly specified', {
221221
metadata: { requires: { mongodb: '>=3.7.3' } },
222-
test: function () {
222+
test: async function () {
223223
const options = {
224224
auth: {
225225
username: userMap.sha256.username,
@@ -229,11 +229,13 @@ describe('auth prose tests', () => {
229229
authMechanism: 'SCRAM-SHA-1'
230230
};
231231

232-
return withClient(
233-
this.configuration.newClient({}, options),
234-
() => Promise.reject(new Error('This request should have failed to authenticate')),
235-
err => expect(err).to.match(/Authentication failed/)
236-
);
232+
const client = this.configuration.newClient({}, options);
233+
const error = await client
234+
.db()
235+
.command({ ping: 1 })
236+
.catch(error => error);
237+
expect(error.message).to.match(/Authentication failed/);
238+
await client.close();
237239
}
238240
});
239241

@@ -248,7 +250,7 @@ describe('auth prose tests', () => {
248250
*/
249251
it('should fail for a nonexistent username with same error type as bad password', {
250252
metadata: { requires: { mongodb: '>=3.7.3' } },
251-
test: function () {
253+
test: async function () {
252254
const noUsernameOptions = {
253255
auth: {
254256
username: 'roth',
@@ -265,20 +267,27 @@ describe('auth prose tests', () => {
265267
authSource: 'admin'
266268
};
267269

268-
const getErrorMsg = options =>
269-
withClient(
270-
this.configuration.newClient({}, options),
271-
() => Promise.reject(new Error('This request should have failed to authenticate')),
272-
err => expect(err).to.match(/Authentication failed/)
273-
);
274-
275-
return Promise.all([getErrorMsg(noUsernameOptions), getErrorMsg(badPasswordOptions)]);
270+
const noUsernameClient = this.configuration.newClient({}, noUsernameOptions);
271+
const errorNoUser = await noUsernameClient
272+
.db()
273+
.command({ ping: 1 })
274+
.catch(error => error);
275+
expect(errorNoUser.message).to.match(/Authentication failed/);
276+
await noUsernameClient.close();
277+
278+
const badPasswordClient = this.configuration.newClient({}, badPasswordOptions);
279+
const errorNoPass = await badPasswordClient
280+
.db()
281+
.command({ ping: 1 })
282+
.catch(error => error);
283+
expect(errorNoPass.message).to.match(/Authentication failed/);
284+
await badPasswordClient.close();
276285
}
277286
});
278287

279288
it('should send speculativeAuthenticate on initial handshake on MongoDB 4.4+', {
280289
metadata: { requires: { mongodb: '>=4.4', topology: ['single'] } },
281-
test: function () {
290+
test: async function () {
282291
const options = {
283292
auth: {
284293
username: userMap.both.username,
@@ -288,16 +297,17 @@ describe('auth prose tests', () => {
288297
};
289298

290299
const commandSpy = test.sandbox.spy(Connection.prototype, 'command');
291-
return withClient(this.configuration.newClient({}, options), () => {
292-
const calls = commandSpy
293-
.getCalls()
294-
.filter(c => c.thisValue.id !== '<monitor>') // ignore all monitor connections
295-
.filter(c => c.args[1][LEGACY_HELLO_COMMAND]); // only consider handshakes
296-
297-
expect(calls).to.have.length(1);
298-
const handshakeDoc = calls[0].args[1];
299-
expect(handshakeDoc).to.have.property('speculativeAuthenticate');
300-
});
300+
const client = this.configuration.newClient({}, options);
301+
await client.db().command({ ping: 1 });
302+
const calls = commandSpy
303+
.getCalls()
304+
.filter(c => c.thisValue.id !== '<monitor>') // ignore all monitor connections
305+
.filter(c => c.args[1][LEGACY_HELLO_COMMAND]); // only consider handshakes
306+
307+
expect(calls).to.have.length(1);
308+
const handshakeDoc = calls[0].args[1];
309+
expect(handshakeDoc).to.have.property('speculativeAuthenticate');
310+
await client.close();
301311
}
302312
});
303313
});
@@ -337,27 +347,27 @@ describe('auth prose tests', () => {
337347
return setupDatabase(this.configuration);
338348
});
339349

340-
before(function () {
341-
return withClient(this.configuration.newClient(), client => {
342-
const db = client.db('admin');
350+
before(async function () {
351+
const client = this.configuration.newClient();
352+
const db = client.db('admin');
343353

344-
const createUserCommands = users.map(user => ({
345-
createUser: user.username,
346-
pwd: user.password,
347-
roles: ['root'],
348-
mechanisms: user.mechanisms
349-
}));
354+
const createUserCommands = users.map(user => ({
355+
createUser: user.username,
356+
pwd: user.password,
357+
roles: ['root'],
358+
mechanisms: user.mechanisms
359+
}));
350360

351-
return Promise.all(createUserCommands.map(cmd => db.command(cmd)));
352-
});
353-
});
361+
await Promise.all(createUserCommands.map(cmd => db.command(cmd)));
354362

355-
after(function () {
356-
return withClient(this.configuration.newClient(), client => {
357-
const db = client.db('admin');
363+
await client.close();
364+
});
358365

359-
return Promise.all(users.map(user => db.removeUser(user.username)));
360-
});
366+
after(async function () {
367+
const client = this.configuration.newClient();
368+
const db = client.db('admin');
369+
await Promise.all(users.map(user => db.removeUser(user.username)));
370+
await client.close();
361371
});
362372

363373
[
@@ -374,16 +384,16 @@ describe('auth prose tests', () => {
374384
mongodb: '>=3.7.3'
375385
}
376386
},
377-
test: function () {
387+
test: async function () {
378388
const options = {
379389
auth: { username, password },
380390
authSource: 'admin',
381391
authMechanism: 'SCRAM-SHA-256'
382392
};
383393

384-
return withClient(this.configuration.newClient(options), client => {
385-
return client.db('admin').stats();
386-
});
394+
const client = this.configuration.newClient({}, options);
395+
await client.db('admin').stats();
396+
await client.close();
387397
}
388398
});
389399
});

0 commit comments

Comments
 (0)