@@ -2,6 +2,7 @@ import { expect } from 'chai';
2
2
import * as sinon from 'sinon' ;
3
3
4
4
import { Connection , LEGACY_HELLO_COMMAND , type MongoClient , ScramSHA256 } from '../../mongodb' ;
5
+ import { type TestConfiguration } from '../../tools/runner/config' ;
5
6
6
7
function makeConnectionString ( config , username , password ) {
7
8
return `mongodb://${ username } :${ password } @${ config . host } :${ config . port } /admin?` ;
@@ -321,7 +322,6 @@ describe('Authentication Spec Prose Tests', function () {
321
322
* mongodb://%E2%85%A8:IV\@mongodb.example.com/admin
322
323
* mongodb://%E2%85%A8:I%C2%ADV\@mongodb.example.com/admin
323
324
*/
324
- let utilClient : MongoClient ;
325
325
let client : MongoClient ;
326
326
const users = [
327
327
{
@@ -336,14 +336,19 @@ describe('Authentication Spec Prose Tests', function () {
336
336
}
337
337
] ;
338
338
339
- beforeEach ( async function ( ) {
340
- utilClient = this . configuration . newClient ( this . configuration . url ( ) ) ;
339
+ async function cleanUpUsers ( configuration : TestConfiguration ) {
340
+ const utilClient = configuration . newClient ( ) ;
341
341
const db = utilClient . db ( 'admin' ) ;
342
342
343
- // We do not care if this fails - this is just cleanup for any
344
- // previous test iterations or previous test runs.
345
343
await Promise . allSettled ( users . map ( user => db . removeUser ( user . username ) ) ) ;
346
344
345
+ await utilClient . close ( ) ;
346
+ }
347
+
348
+ async function createUsers ( configuration : TestConfiguration ) {
349
+ const utilClient = configuration . newClient ( ) ;
350
+ const db = utilClient . db ( 'admin' ) ;
351
+
347
352
const createUserCommands = users . map ( user => ( {
348
353
createUser : user . username ,
349
354
pwd : user . password ,
@@ -355,15 +360,25 @@ describe('Authentication Spec Prose Tests', function () {
355
360
createUserCommands . map ( cmd => db . command ( cmd ) )
356
361
) . then ( resolutions => resolutions . filter ( resolution => resolution . status === 'rejected' ) ) ;
357
362
363
+ await utilClient . close ( ) ;
364
+
358
365
if ( failures . length ) {
359
366
throw new Error (
360
367
'Error(s) creating users: ' + failures . map ( failure => failure . reason ) . join ( ' | ' )
361
368
) ;
362
369
}
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 ) ;
363
379
} ) ;
364
380
365
381
afterEach ( async function ( ) {
366
- await utilClient ?. close ( ) ;
367
382
await client ?. close ( ) ;
368
383
} ) ;
369
384
0 commit comments