Skip to content

Commit f7027f0

Browse files
committed
fix(NODE-4192): remove connect from tests part 1
1 parent aa652ae commit f7027f0

File tree

51 files changed

+5469
-6898
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+5469
-6898
lines changed

global.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
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?: {
127
topology?: TopologyTypeRequirement;
138
mongodb?: string;
149
os?: NodeJS.Platform | `!${NodeJS.Platform}`;
15-
apiVersion?: '1';
10+
apiVersion?: '1' | boolean;
1611
clientSideEncryption?: boolean;
1712
serverless?: 'forbid' | 'allow' | 'require';
1813
auth?: 'enabled' | 'disabled';
@@ -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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ 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

10-
describe('auth prose tests', () => {
10+
// TODO(NODE-XXX): withClient usage prevented these tests from running
11+
// the import has been removed since the function is being deleted, this is here to keep modifications minimal
12+
// so that the implementer of the fix for these tests can try to reference the original intent
13+
const withClient = () => null;
14+
15+
describe.skip('auth prose tests', () => {
1116
describe('SCRAM-SHA-256 prose test', () => {
1217
describe('SCRAM-SHA-256 prose test Steps 1-3', function () {
1318
const test = {};

test/integration/auth/mongodb_aws.test.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,28 @@ describe('MONGODB-AWS', function () {
1515
}
1616
});
1717

18-
it('should not authorize when not authenticated', function (done) {
19-
const config = this.configuration;
20-
const url = removeAuthFromConnectionString(config.url());
21-
const client = config.newClient(url); // strip provided URI of credentials
22-
client.connect(err => {
23-
expect(err).to.not.exist;
24-
this.defer(() => client.close());
18+
it('should not authorize when not authenticated', async function () {
19+
const url = removeAuthFromConnectionString(this.configuration.url());
20+
const client = this.configuration.newClient(url); // strip provided URI of credentials
2521

26-
client.db('aws').command({ count: 'test' }, (err, count) => {
27-
expect(err).to.exist;
28-
expect(count).to.not.exist;
22+
const error = await client
23+
.db('aws')
24+
.command({ ping: 1 })
25+
.catch(error => error);
2926

30-
done();
31-
});
32-
});
27+
expect(error).to.be.instanceOf(MongoAWSError);
3328
});
3429

35-
it('should authorize when successfully authenticated', function (done) {
36-
const config = this.configuration;
37-
const client = config.newClient(process.env.MONGODB_URI); // use the URI built by the test environment
38-
client.connect(err => {
39-
expect(err).to.not.exist;
40-
this.defer(() => client.close());
30+
it('should authorize when successfully authenticated', async function () {
31+
const client = this.configuration.newClient(process.env.MONGODB_URI); // use the URI built by the test environment
4132

42-
client.db('aws').command({ count: 'test' }, (err, count) => {
43-
expect(err).to.not.exist;
44-
expect(count).to.exist;
33+
const result = await client
34+
.db('aws')
35+
.command({ ping: 1 })
36+
.catch(error => error);
4537

46-
done();
47-
});
48-
});
38+
expect(result).to.not.be.instanceOf(MongoAWSError);
39+
expect(result).to.have.property('ok', 1);
4940
});
5041

5142
it('should allow empty string in authMechanismProperties.AWS_SESSION_TOKEN to override AWS_SESSION_TOKEN environment variable', function () {
@@ -84,10 +75,10 @@ describe('MONGODB-AWS', function () {
8475
client = config.newClient(process.env.MONGODB_URI, { authMechanism: 'MONGODB-AWS' }); // use the URI built by the test environment
8576
const startTime = performance.now();
8677

87-
let caughtError = null;
88-
await client.connect().catch(err => {
89-
caughtError = err;
90-
});
78+
const caughtError = await client
79+
.db()
80+
.command({ ping: 1 })
81+
.catch(error => error);
9182

9283
const endTime = performance.now();
9384
const timeTaken = endTime - startTime;

test/integration/auth/scram_sha_1.test.js

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { expect } from 'chai';
2+
3+
import type { MongoClient } from '../../../src';
4+
5+
describe('SCRAM-SHA-1', function () {
6+
let client: MongoClient;
7+
8+
beforeEach(async function () {
9+
const onlyScram1AuthMech =
10+
this.configuration.parameters?.authenticationMechanisms.length === 1 &&
11+
this.configuration.parameters?.authenticationMechanisms[0] === 'SCRAM-SHA-1';
12+
13+
if (!onlyScram1AuthMech) {
14+
this.currentTest.skipReason = `MongoDB auth mechanism must only be SCRAM-SHA-1, got ${this.configuration.parameters?.authenticationMechanisms}`;
15+
return this.skip();
16+
}
17+
18+
client = this.configuration.newClient();
19+
});
20+
21+
afterEach(async () => {
22+
await client?.close();
23+
});
24+
25+
it('successfuly authenticates', async () => {
26+
const result = await client.db().admin().command({ ping: 1 });
27+
expect(result).to.have.property('ok', 1);
28+
});
29+
});

test/integration/auth/scram_sha_256.test.js

Lines changed: 39 additions & 37 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('SCRAM_SHA_256', function () {
@@ -29,35 +29,35 @@ describe('SCRAM_SHA_256', function () {
2929
return setupDatabase(this.configuration);
3030
});
3131

32-
before(function () {
33-
return withClient(this.configuration.newClient(), client => {
34-
test.oldDbName = this.configuration.db;
35-
this.configuration.db = 'admin';
36-
const db = client.db(this.configuration.db);
37-
38-
const createUserCommands = users.map(user => ({
39-
createUser: user.username,
40-
pwd: user.password,
41-
roles: ['root'],
42-
mechanisms: user.mechanisms
43-
}));
44-
45-
return Promise.all(createUserCommands.map(cmd => db.command(cmd)));
46-
});
32+
before(async function () {
33+
const client = this.configuration.newClient();
34+
test.oldDbName = this.configuration.db;
35+
this.configuration.db = 'admin';
36+
const db = client.db(this.configuration.db);
37+
38+
const createUserCommands = users.map(user => ({
39+
createUser: user.username,
40+
pwd: user.password,
41+
roles: ['root'],
42+
mechanisms: user.mechanisms
43+
}));
44+
45+
await Promise.all(createUserCommands.map(cmd => db.command(cmd)));
46+
await client.close();
4747
});
4848

49-
after(function () {
50-
return withClient(this.configuration.newClient(), client => {
51-
const db = client.db(this.configuration.db);
52-
this.configuration.db = test.oldDbName;
49+
after(async function () {
50+
const client = this.configuration.newClient();
51+
const db = client.db(this.configuration.db);
52+
this.configuration.db = test.oldDbName;
5353

54-
return Promise.all(users.map(user => db.removeUser(user.username)));
55-
});
54+
await Promise.all(users.map(user => db.removeUser(user.username)));
55+
await client.close();
5656
});
5757

5858
it('should shorten SCRAM conversations if the server supports it', {
5959
metadata: { requires: { mongodb: '>=4.4', topology: ['single'] } },
60-
test: function () {
60+
test: async function () {
6161
const options = {
6262
auth: {
6363
username: userMap.both.username,
@@ -79,15 +79,16 @@ describe('SCRAM_SHA_256', function () {
7979
auth.apply(this, [authContext, _callback]);
8080
});
8181

82-
return withClient(this.configuration.newClient({}, options), () => {
83-
expect(runCommandSpy.callCount).to.equal(1);
84-
});
82+
const client = this.configuration.newClient({}, options);
83+
await client.db().command({ ping: 1 });
84+
expect(runCommandSpy.callCount).to.equal(1);
85+
await client.close();
8586
}
8687
});
8788

8889
it('should send speculativeAuthenticate on initial handshake on MongoDB 4.4+', {
8990
metadata: { requires: { mongodb: '>=4.4', topology: ['single'] } },
90-
test: function () {
91+
test: async function () {
9192
const options = {
9293
auth: {
9394
username: userMap.both.username,
@@ -97,16 +98,17 @@ describe('SCRAM_SHA_256', function () {
9798
};
9899

99100
const commandSpy = test.sandbox.spy(Connection.prototype, 'command');
100-
return withClient(this.configuration.newClient({}, options), () => {
101-
const calls = commandSpy
102-
.getCalls()
103-
.filter(c => c.thisValue.id !== '<monitor>') // ignore all monitor connections
104-
.filter(c => c.args[1][LEGACY_HELLO_COMMAND]); // only consider handshakes
105-
106-
expect(calls).to.have.length(1);
107-
const handshakeDoc = calls[0].args[1];
108-
expect(handshakeDoc).to.have.property('speculativeAuthenticate');
109-
});
101+
const client = this.configuration.newClient({}, options);
102+
await client.db().command({ ping: 1 });
103+
const calls = commandSpy
104+
.getCalls()
105+
.filter(c => c.thisValue.id !== '<monitor>') // ignore all monitor connections
106+
.filter(c => c.args[1][LEGACY_HELLO_COMMAND]); // only consider handshakes
107+
108+
expect(calls).to.have.length(1);
109+
const handshakeDoc = calls[0].args[1];
110+
expect(handshakeDoc).to.have.property('speculativeAuthenticate');
111+
await client.close();
110112
}
111113
});
112114
});

0 commit comments

Comments
 (0)