Skip to content

Commit

Permalink
feat(MongoMemoryServer): change "instance.auth" to not be manually se…
Browse files Browse the repository at this point in the history
…t-able
  • Loading branch information
hasezoey committed Jun 9, 2023
1 parent eb97968 commit 840584b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 34 deletions.
6 changes: 4 additions & 2 deletions docs/api/interfaces/mongo-memory-server-opts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 14 additions & 8 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MongoMemoryInstanceOpts, 'auth'>;

/**
* MongoMemoryServer Stored Options
*/
export interface MongoMemoryServerOpts {
instance?: MongoMemoryInstanceOpts;
instance?: MemoryServerInstanceOpts;
binary?: MongoBinaryOpts;
spawn?: SpawnOptions;
/**
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ describe('MongoMemoryServer', () => {
const mongoServer = await MongoMemoryServer.create({
auth: {},
instance: {
auth: true,
storageEngine: 'ephemeralForTest',
},
});
Expand Down Expand Up @@ -206,7 +205,6 @@ describe('MongoMemoryServer', () => {
const mongoServer = await MongoMemoryServer.create({
auth: {},
instance: {
auth: true,
storageEngine: 'wiredTiger',
},
});
Expand Down Expand Up @@ -300,7 +298,6 @@ describe('MongoMemoryServer', () => {
],
},
instance: {
auth: true,
storageEngine: 'ephemeralForTest',
},
});
Expand Down Expand Up @@ -413,7 +410,6 @@ describe('MongoMemoryServer', () => {
disable: true,
},
instance: {
auth: true,
storageEngine: 'ephemeralForTest',
},
});
Expand All @@ -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 () => {
Expand Down

0 comments on commit 840584b

Please sign in to comment.