From 4e4a41d8d9975f8739ac0e92cdce040917e4d9cd Mon Sep 17 00:00:00 2001 From: hasezoey Date: Thu, 14 Oct 2021 21:24:46 +0200 Subject: [PATCH] fix(MongoMemoryServer): change that "instance.auth" is not required to be set for enabling auth --- .../src/MongoMemoryServer.ts | 2 +- .../src/__tests__/MongoMemoryServer.test.ts | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts b/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts index f19d8a13b..7ef31f77c 100644 --- a/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts +++ b/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts @@ -380,7 +380,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced { } const createAuth: boolean = - !!instOpts.auth && // check if auth is even meant to be enabled + (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.auth.force || isNew) && // check that either "isNew" or "this.auth.force" is "true" diff --git a/packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts b/packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts index 78b36a157..34f40f2c3 100644 --- a/packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts +++ b/packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts @@ -110,6 +110,43 @@ describe('MongoMemoryServer', () => { await mongoServer.stop(); }); + it('should make use of "AutomaticAuth" even when "instance.auth" is not set (wiredTiger)', async () => { + jest.spyOn(MongoInstance.prototype, 'start'); + const mongoServer = await MongoMemoryServer.create({ + auth: {}, + instance: { + storageEngine: 'wiredTiger', + }, + }); + + utils.assertion(!utils.isNullOrUndefined(mongoServer.instanceInfo)); + utils.assertion(!utils.isNullOrUndefined(mongoServer.auth)); + + const con: MongoClient = await MongoClient.connect( + utils.uriTemplate(mongoServer.instanceInfo.ip, mongoServer.instanceInfo.port, 'admin'), + { + useNewUrlParser: true, + useUnifiedTopology: true, + authSource: 'admin', + authMechanism: 'SCRAM-SHA-256', + auth: { + user: mongoServer.auth.customRootName, + password: mongoServer.auth.customRootPwd, + }, + } + ); + const db = con.db('admin'); + const users: { users: { user: string }[] } = await db.command({ + usersInfo: mongoServer.auth.customRootName, + }); + expect(users.users).toHaveLength(1); + expect(users.users[0].user).toEqual(mongoServer.auth.customRootName); + expect(MongoInstance.prototype.start).toHaveBeenCalledTimes(2); + + await con.close(); + await mongoServer.stop(); + }); + it('should make use of "AutomaticAuth" (wiredTiger)', async () => { jest.spyOn(MongoInstance.prototype, 'start'); const mongoServer = await MongoMemoryServer.create({ @@ -260,6 +297,40 @@ describe('MongoMemoryServer', () => { } expect(MongoInstance.prototype.start).toHaveBeenCalledTimes(1); expect(MongoMemoryServer.prototype.createAuth).not.toHaveBeenCalled(); + expect(mongoServer.instanceInfo.instance.prepareCommandArgs().includes('--noauth')); + + await con.close(); + await mongoServer.stop(); + }); + + it('"createAuth" should not be called if "instance.auth" is false', async () => { + jest.spyOn(MongoInstance.prototype, 'start'); + jest.spyOn(MongoMemoryServer.prototype, 'createAuth'); + const mongoServer = await MongoMemoryServer.create({ + auth: {}, + instance: { + auth: false, + storageEngine: 'ephemeralForTest', + }, + }); + + utils.assertion(!utils.isNullOrUndefined(mongoServer.instanceInfo)); + utils.assertion(!utils.isNullOrUndefined(mongoServer.auth)); + + const con: MongoClient = await MongoClient.connect( + utils.uriTemplate(mongoServer.instanceInfo.ip, mongoServer.instanceInfo.port, 'admin'), + { + useNewUrlParser: true, + useUnifiedTopology: true, + } + ); + const db = con.db('admin'); + await db.command({ + usersInfo: 1, + }); + expect(MongoInstance.prototype.start).toHaveBeenCalledTimes(1); + expect(MongoMemoryServer.prototype.createAuth).not.toHaveBeenCalled(); + expect(mongoServer.instanceInfo.instance.prepareCommandArgs().includes('--noauth')); await con.close(); await mongoServer.stop();