Skip to content

Commit

Permalink
fix(MongoMemoryServer): dont set "extraConnectionOptions" when auth-o…
Browse files Browse the repository at this point in the history
…bject is disabled
  • Loading branch information
hasezoey committed Sep 25, 2022
1 parent 5020be8 commit 80d6fe1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Expand Up @@ -234,6 +234,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
super();
this.opts = { ...opts };

// TODO: consider changing this to not be set if "instance.auth" is false in 9.0
if (!isNullOrUndefined(this.opts.auth)) {
// assign defaults
this.auth = authDefault(this.opts.auth);
Expand Down Expand Up @@ -399,8 +400,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {

const enableAuth: boolean =
(typeof instOpts.auth === 'boolean' ? instOpts.auth : true) && // check if auth is even meant to be enabled
!isNullOrUndefined(this.auth) && // check if "this.auth" is defined
!this.auth.disable; // check that "this.auth.disable" is falsey
this.authObjectEnable();

const createAuth: boolean =
enableAuth && // re-use all the checks from "enableAuth"
Expand Down Expand Up @@ -458,7 +458,11 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
};

// always set the "extraConnectionOptions" when "auth" is enabled, regardless of if "createAuth" gets run
if (!isNullOrUndefined(this.auth) && !isNullOrUndefined(mongodOptions.instance?.auth)) {
if (
this.authObjectEnable() &&
mongodOptions.instance?.auth === true &&
!isNullOrUndefined(this.auth) // extra check again for typescript, because it cant reuse checks from "enableAuth" yet
) {
instance.extraConnectionOptions = {
authSource: 'admin',
authMechanism: 'SCRAM-SHA-256',
Expand Down Expand Up @@ -813,6 +817,21 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
await con.close();
}
}

/**
* Helper function to determine if the "auth" object is set and not to be disabled
* This function expectes to be run after the auth object has been transformed to a object
* @returns "true" when "auth" should be enabled
*/
protected authObjectEnable(): boolean {
if (isNullOrUndefined(this.auth)) {
return false;
}

return typeof this.auth.disable === 'boolean' // if "this._replSetOpts.auth.disable" is defined, use that
? !this.auth.disable // invert the disable boolean, because "auth" should only be disabled if "disabled = true"
: true; // if "this._replSetOpts.auth.disable" is not defined, default to true because "this._replSetOpts.auth" is defined
}
}

export default MongoMemoryServer;
Expand Down
Expand Up @@ -1074,4 +1074,36 @@ describe('MongoMemoryServer', () => {
expect(mongoServer.instanceInfo).toBeFalsy();
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
});

describe('authObjectEnable()', () => {
it('should with defaults return "false"', () => {
const mongoServer = new MongoMemoryServer();

expect(mongoServer.auth).toBeFalsy();

expect(
// @ts-expect-error "authObjectEnable" is protected
mongoServer.authObjectEnable()
).toStrictEqual(false);
});

it('should with defaults return "true" if empty object OR "disable: false"', () => {
{
const mongoServer = new MongoMemoryServer({ auth: {} });

expect(
// @ts-expect-error "authObjectEnable" is protected
mongoServer.authObjectEnable()
).toStrictEqual(true);
}
{
const mongoServer = new MongoMemoryServer({ auth: { disable: false } });

expect(
// @ts-expect-error "authObjectEnable" is protected
mongoServer.authObjectEnable()
).toStrictEqual(true);
}
});
});
});

0 comments on commit 80d6fe1

Please sign in to comment.