From 840584b36a035a571096581487c81f94d332fe2f Mon Sep 17 00:00:00 2001 From: hasezoey Date: Fri, 9 Jun 2023 17:11:51 +0200 Subject: [PATCH] feat(MongoMemoryServer): change "instance.auth" to not be manually set-able --- .../interfaces/mongo-memory-server-opts.md | 6 ++-- .../src/MongoMemoryServer.ts | 22 +++++++++------ .../src/__tests__/MongoMemoryServer.test.ts | 28 +++---------------- 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/docs/api/interfaces/mongo-memory-server-opts.md b/docs/api/interfaces/mongo-memory-server-opts.md index c9f82cd3a..28cd8de5e 100644 --- a/docs/api/interfaces/mongo-memory-server-opts.md +++ b/docs/api/interfaces/mongo-memory-server-opts.md @@ -9,9 +9,11 @@ API Documentation of `MongoMemoryServerOpts`-Interface ### instance -Typings: `instance?: MongoMemoryInstanceOpts` +Typings: `instance?: MemoryServerInstanceOpts` -Set custom options for the instance, uses [`MongoMemoryInstanceOpts`](./mongo-memory-instance-opts.md). +Set custom options based on [`MongoMemoryInstanceOpts`](./mongo-memory-instance-opts.md), but ignores some properties: + +- `auth` is ignored because it is set via [auth](#auth) property. ### binary diff --git a/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts b/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts index 4060069c3..8c0de1625 100644 --- a/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts +++ b/packages/mongodb-memory-server-core/src/MongoMemoryServer.ts @@ -23,11 +23,17 @@ import * as os from 'os'; const log = debug('MongoMS:MongoMemoryServer'); +/** + * Type with automatic options removed + * "auth" is automatically handled and set via {@link AutomaticAuth} + */ +export type MemoryServerInstanceOpts = Omit; + /** * MongoMemoryServer Stored Options */ export interface MongoMemoryServerOpts { - instance?: MongoMemoryInstanceOpts; + instance?: MemoryServerInstanceOpts; binary?: MongoBinaryOpts; spawn?: SpawnOptions; /** @@ -234,11 +240,13 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced { super(); this.opts = { ...opts }; - if (!isNullOrUndefined(this.opts.auth)) { - if (this.opts.instance?.auth === false) { - log('opts.instance.auth is false, but opts.auth was defined!'); - } + // instance option "auth" will be automatically set and handled via AutomaticAuth + if ('auth' in (this.opts.instance ?? {})) { + log('opts.instance.auth was defined, but will be set automatically, ignoring'); + delete (this.opts.instance as MongoMemoryInstanceOpts | undefined)?.auth; + } + if (!isNullOrUndefined(this.opts.auth)) { // assign defaults this.auth = authDefault(this.opts.auth); } @@ -399,9 +407,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced { isNew = false; } - const enableAuth: boolean = - (typeof instOpts.auth === 'boolean' ? instOpts.auth : true) && // check if auth is even meant to be enabled - this.authObjectEnable(); + const enableAuth: boolean = this.authObjectEnable(); const createAuth: boolean = enableAuth && // re-use all the checks from "enableAuth" 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 d221025ad..233fb6b91 100644 --- a/packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts +++ b/packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts @@ -73,7 +73,6 @@ describe('MongoMemoryServer', () => { const mongoServer = await MongoMemoryServer.create({ auth: {}, instance: { - auth: true, storageEngine: 'ephemeralForTest', }, }); @@ -206,7 +205,6 @@ describe('MongoMemoryServer', () => { const mongoServer = await MongoMemoryServer.create({ auth: {}, instance: { - auth: true, storageEngine: 'wiredTiger', }, }); @@ -300,7 +298,6 @@ describe('MongoMemoryServer', () => { ], }, instance: { - auth: true, storageEngine: 'ephemeralForTest', }, }); @@ -413,7 +410,6 @@ describe('MongoMemoryServer', () => { disable: true, }, instance: { - auth: true, storageEngine: 'ephemeralForTest', }, }); @@ -439,35 +435,19 @@ describe('MongoMemoryServer', () => { await mongoServer.stop(); }); - it('"createAuth" should not be called if "instance.auth" is false', async () => { + it('"createAuth" should ignore "instance.auth"', async () => { jest.spyOn(MongoInstance.prototype, 'start'); jest.spyOn(MongoMemoryServer.prototype, 'createAuth'); - const mongoServer = await MongoMemoryServer.create({ + const mongoServer = new MongoMemoryServer({ auth: {}, instance: { + // @ts-expect-error "auth" is removed from the type 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') - ); - 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') - ).toStrictEqual(true); - - await con.close(); - await mongoServer.stop(); + expect(mongoServer.opts.instance).not.toHaveProperty('auth'); }); it('should throw an error if state is not "new" or "stopped"', async () => {