Skip to content

Commit

Permalink
fix(MongoMemoryServer): change that "instance.auth" is not required t…
Browse files Browse the repository at this point in the history
…o be set for enabling auth
  • Loading branch information
hasezoey committed Oct 14, 2021
1 parent 9c3fea0 commit 4e4a41d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
Expand Up @@ -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"
Expand Down
Expand Up @@ -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({
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4e4a41d

Please sign in to comment.