Skip to content

Commit ae3c3ef

Browse files
make users in a before/after hook
1 parent 34c287b commit ae3c3ef

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

test/integration/auth/auth.prose.test.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect } from 'chai';
22
import * as sinon from 'sinon';
33

44
import { Connection, LEGACY_HELLO_COMMAND, type MongoClient, ScramSHA256 } from '../../mongodb';
5+
import { type TestConfiguration } from '../../tools/runner/config';
56

67
function makeConnectionString(config, username, password) {
78
return `mongodb://${username}:${password}@${config.host}:${config.port}/admin?`;
@@ -321,7 +322,6 @@ describe('Authentication Spec Prose Tests', function () {
321322
* mongodb://%E2%85%A8:IV\@mongodb.example.com/admin
322323
* mongodb://%E2%85%A8:I%C2%ADV\@mongodb.example.com/admin
323324
*/
324-
let utilClient: MongoClient;
325325
let client: MongoClient;
326326
const users = [
327327
{
@@ -336,14 +336,19 @@ describe('Authentication Spec Prose Tests', function () {
336336
}
337337
];
338338

339-
beforeEach(async function () {
340-
utilClient = this.configuration.newClient(this.configuration.url());
339+
async function cleanUpUsers(configuration: TestConfiguration) {
340+
const utilClient = configuration.newClient();
341341
const db = utilClient.db('admin');
342342

343-
// We do not care if this fails - this is just cleanup for any
344-
// previous test iterations or previous test runs.
345343
await Promise.allSettled(users.map(user => db.removeUser(user.username)));
346344

345+
await utilClient.close();
346+
}
347+
348+
async function createUsers(configuration: TestConfiguration) {
349+
const utilClient = configuration.newClient();
350+
const db = utilClient.db('admin');
351+
347352
const createUserCommands = users.map(user => ({
348353
createUser: user.username,
349354
pwd: user.password,
@@ -355,15 +360,25 @@ describe('Authentication Spec Prose Tests', function () {
355360
createUserCommands.map(cmd => db.command(cmd))
356361
).then(resolutions => resolutions.filter(resolution => resolution.status === 'rejected'));
357362

363+
await utilClient.close();
364+
358365
if (failures.length) {
359366
throw new Error(
360367
'Error(s) creating users: ' + failures.map(failure => failure.reason).join(' | ')
361368
);
362369
}
370+
}
371+
372+
before(async function () {
373+
await cleanUpUsers(this.configuration);
374+
await createUsers(this.configuration);
375+
});
376+
377+
after(function () {
378+
return cleanUpUsers(this.configuration);
363379
});
364380

365381
afterEach(async function () {
366-
await utilClient?.close();
367382
await client?.close();
368383
});
369384

0 commit comments

Comments
 (0)