@@ -234,6 +234,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
234234 super ( ) ;
235235 this . opts = { ...opts } ;
236236
237+ // TODO: consider changing this to not be set if "instance.auth" is false in 9.0
237238 if ( ! isNullOrUndefined ( this . opts . auth ) ) {
238239 // assign defaults
239240 this . auth = authDefault ( this . opts . auth ) ;
@@ -399,8 +400,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
399400
400401 const enableAuth : boolean =
401402 ( typeof instOpts . auth === 'boolean' ? instOpts . auth : true ) && // check if auth is even meant to be enabled
402- ! isNullOrUndefined ( this . auth ) && // check if "this.auth" is defined
403- ! this . auth . disable ; // check that "this.auth.disable" is falsey
403+ this . authObjectEnable ( ) ;
404404
405405 const createAuth : boolean =
406406 enableAuth && // re-use all the checks from "enableAuth"
@@ -458,7 +458,11 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
458458 } ;
459459
460460 // always set the "extraConnectionOptions" when "auth" is enabled, regardless of if "createAuth" gets run
461- if ( ! isNullOrUndefined ( this . auth ) && ! isNullOrUndefined ( mongodOptions . instance ?. auth ) ) {
461+ if (
462+ this . authObjectEnable ( ) &&
463+ mongodOptions . instance ?. auth === true &&
464+ ! isNullOrUndefined ( this . auth ) // extra check again for typescript, because it cant reuse checks from "enableAuth" yet
465+ ) {
462466 instance . extraConnectionOptions = {
463467 authSource : 'admin' ,
464468 authMechanism : 'SCRAM-SHA-256' ,
@@ -813,6 +817,21 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
813817 await con . close ( ) ;
814818 }
815819 }
820+
821+ /**
822+ * Helper function to determine if the "auth" object is set and not to be disabled
823+ * This function expectes to be run after the auth object has been transformed to a object
824+ * @returns "true" when "auth" should be enabled
825+ */
826+ protected authObjectEnable ( ) : boolean {
827+ if ( isNullOrUndefined ( this . auth ) ) {
828+ return false ;
829+ }
830+
831+ return typeof this . auth . disable === 'boolean' // if "this._replSetOpts.auth.disable" is defined, use that
832+ ? ! this . auth . disable // invert the disable boolean, because "auth" should only be disabled if "disabled = true"
833+ : true ; // if "this._replSetOpts.auth.disable" is not defined, default to true because "this._replSetOpts.auth" is defined
834+ }
816835}
817836
818837export default MongoMemoryServer ;
0 commit comments