@@ -731,61 +731,64 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
731
731
{ }
732
732
) ;
733
733
734
- let db = con . db ( 'admin' ) ; // just to ensure it is actually the "admin" database AND to have the "Db" data
735
-
736
- // Create the root user
737
- this . debug ( `createAuth: Creating Root user, name: "${ this . auth . customRootName } "` ) ;
738
- await db . command ( {
739
- createUser : this . auth . customRootName ,
740
- pwd : this . auth . customRootPwd ,
741
- mechanisms : [ 'SCRAM-SHA-256' ] ,
742
- customData : {
743
- createdBy : 'mongodb-memory-server' ,
744
- as : 'ROOTUSER' ,
745
- } ,
746
- roles : [ 'root' ] ,
747
- // "writeConcern" is needced, otherwise replset servers might fail with "auth failed: such user does not exist"
748
- writeConcern : {
749
- w : 'majority' ,
750
- } ,
751
- } as CreateUserMongoDB ) ;
752
-
753
- if ( this . auth . extraUsers . length > 0 ) {
754
- this . debug ( `createAuth: Creating "${ this . auth . extraUsers . length } " Custom Users` ) ;
755
- this . auth . extraUsers . sort ( ( a , b ) => {
756
- if ( a . database === 'admin' ) {
757
- return - 1 ; // try to make all "admin" at the start of the array
758
- }
734
+ try {
735
+ let db = con . db ( 'admin' ) ; // just to ensure it is actually the "admin" database AND to have the "Db" data
736
+
737
+ // Create the root user
738
+ this . debug ( `createAuth: Creating Root user, name: "${ this . auth . customRootName } "` ) ;
739
+ await db . command ( {
740
+ createUser : this . auth . customRootName ,
741
+ pwd : this . auth . customRootPwd ,
742
+ mechanisms : [ 'SCRAM-SHA-256' ] ,
743
+ customData : {
744
+ createdBy : 'mongodb-memory-server' ,
745
+ as : 'ROOTUSER' ,
746
+ } ,
747
+ roles : [ 'root' ] ,
748
+ // "writeConcern" is needced, otherwise replset servers might fail with "auth failed: such user does not exist"
749
+ writeConcern : {
750
+ w : 'majority' ,
751
+ } ,
752
+ } as CreateUserMongoDB ) ;
759
753
760
- return a . database === b . database ? 0 : 1 ; // "0" to sort all databases that are the same after each other, and "1" to for pushing it back
761
- } ) ;
754
+ if ( this . auth . extraUsers . length > 0 ) {
755
+ this . debug ( `createAuth: Creating "${ this . auth . extraUsers . length } " Custom Users` ) ;
756
+ this . auth . extraUsers . sort ( ( a , b ) => {
757
+ if ( a . database === 'admin' ) {
758
+ return - 1 ; // try to make all "admin" at the start of the array
759
+ }
762
760
763
- for ( const user of this . auth . extraUsers ) {
764
- user . database = isNullOrUndefined ( user . database ) ? 'admin' : user . database ;
761
+ return a . database === b . database ? 0 : 1 ; // "0" to sort all databases that are the same after each other, and "1" to for pushing it back
762
+ } ) ;
765
763
766
- // just to have not to call "con.db" everytime in the loop if its the same
767
- if ( user . database !== db . databaseName ) {
768
- db = con . db ( user . database ) ;
764
+ for ( const user of this . auth . extraUsers ) {
765
+ user . database = isNullOrUndefined ( user . database ) ? 'admin' : user . database ;
766
+
767
+ // just to have not to call "con.db" everytime in the loop if its the same
768
+ if ( user . database !== db . databaseName ) {
769
+ db = con . db ( user . database ) ;
770
+ }
771
+
772
+ this . debug ( 'createAuth: Creating User: ' , user ) ;
773
+ await db . command ( {
774
+ createUser : user . createUser ,
775
+ pwd : user . pwd ,
776
+ customData : {
777
+ ...user . customData ,
778
+ createdBy : 'mongodb-memory-server' ,
779
+ as : 'EXTRAUSER' ,
780
+ } ,
781
+ roles : user . roles ,
782
+ authenticationRestrictions : user . authenticationRestrictions ?? [ ] ,
783
+ mechanisms : user . mechanisms ?? [ 'SCRAM-SHA-256' ] ,
784
+ digestPassword : user . digestPassword ?? true ,
785
+ } as CreateUserMongoDB ) ;
769
786
}
770
-
771
- this . debug ( 'createAuth: Creating User: ' , user ) ;
772
- await db . command ( {
773
- createUser : user . createUser ,
774
- pwd : user . pwd ,
775
- customData : {
776
- ...user . customData ,
777
- createdBy : 'mongodb-memory-server' ,
778
- as : 'EXTRAUSER' ,
779
- } ,
780
- roles : user . roles ,
781
- authenticationRestrictions : user . authenticationRestrictions ?? [ ] ,
782
- mechanisms : user . mechanisms ?? [ 'SCRAM-SHA-256' ] ,
783
- digestPassword : user . digestPassword ?? true ,
784
- } as CreateUserMongoDB ) ;
785
787
}
788
+ } finally {
789
+ // close connection in any case (even if throwing a error or being successfull)
790
+ await con . close ( ) ;
786
791
}
787
-
788
- await con . close ( ) ;
789
792
}
790
793
}
791
794
0 commit comments