Skip to content

Commit 80d6fe1

Browse files
committed
fix(MongoMemoryServer): dont set "extraConnectionOptions" when auth-object is disabled
1 parent 5020be8 commit 80d6fe1

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

packages/mongodb-memory-server-core/src/MongoMemoryServer.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

818837
export default MongoMemoryServer;

packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,4 +1074,36 @@ describe('MongoMemoryServer', () => {
10741074
expect(mongoServer.instanceInfo).toBeFalsy();
10751075
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
10761076
});
1077+
1078+
describe('authObjectEnable()', () => {
1079+
it('should with defaults return "false"', () => {
1080+
const mongoServer = new MongoMemoryServer();
1081+
1082+
expect(mongoServer.auth).toBeFalsy();
1083+
1084+
expect(
1085+
// @ts-expect-error "authObjectEnable" is protected
1086+
mongoServer.authObjectEnable()
1087+
).toStrictEqual(false);
1088+
});
1089+
1090+
it('should with defaults return "true" if empty object OR "disable: false"', () => {
1091+
{
1092+
const mongoServer = new MongoMemoryServer({ auth: {} });
1093+
1094+
expect(
1095+
// @ts-expect-error "authObjectEnable" is protected
1096+
mongoServer.authObjectEnable()
1097+
).toStrictEqual(true);
1098+
}
1099+
{
1100+
const mongoServer = new MongoMemoryServer({ auth: { disable: false } });
1101+
1102+
expect(
1103+
// @ts-expect-error "authObjectEnable" is protected
1104+
mongoServer.authObjectEnable()
1105+
).toStrictEqual(true);
1106+
}
1107+
});
1108+
});
10771109
});

0 commit comments

Comments
 (0)