Skip to content

Commit a9df023

Browse files
committed
test(MongoMemoryReplSet): extend "AutomaticAuth" (wiredTiger) test to restart
to test "initAllServers -> _ranCreateAuth"
1 parent e116128 commit a9df023

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

packages/mongodb-memory-server-core/src/__tests__/MongoMemoryReplSet.test.ts

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -304,64 +304,68 @@ describe('single server replset', () => {
304304
await replSet.stop();
305305
});
306306

307-
it('should make use of "AutomaticAuth" (wiredTiger)', async () => {
307+
it.only('should make use of "AutomaticAuth" (wiredTiger)', async () => {
308308
// @ts-expect-error because "initAllServers" is protected
309309
jest.spyOn(MongoMemoryReplSet.prototype, 'initAllServers');
310310
jest.spyOn(console, 'warn').mockImplementationOnce(() => void 0);
311311
const replSet = await MongoMemoryReplSet.create({
312312
replSet: { auth: {}, count: 3, storageEngine: 'wiredTiger' },
313313
});
314314

315-
utils.assertion(!utils.isNullOrUndefined(replSet.replSetOpts.auth));
316-
utils.assertion(typeof replSet.replSetOpts.auth === 'object');
315+
async function testConnections() {
316+
utils.assertion(!utils.isNullOrUndefined(replSet.replSetOpts.auth));
317+
utils.assertion(typeof replSet.replSetOpts.auth === 'object');
317318

318-
utils.assertion(!utils.isNullOrUndefined(replSet.replSetOpts.auth.customRootName));
319-
utils.assertion(!utils.isNullOrUndefined(replSet.replSetOpts.auth.customRootPwd));
319+
utils.assertion(!utils.isNullOrUndefined(replSet.replSetOpts.auth.customRootName));
320+
utils.assertion(!utils.isNullOrUndefined(replSet.replSetOpts.auth.customRootPwd));
320321

321-
// test unpriviliged connection
322-
{
323-
const con = await MongoClient.connect(replSet.getUri());
322+
// test unpriviliged connection
323+
{
324+
const con = await MongoClient.connect(replSet.getUri());
324325

325-
const db = con.db('somedb');
326-
const col = db.collection('somecol');
326+
const db = con.db('somedb');
327+
const col = db.collection('somecol');
327328

328-
try {
329-
await col.insertOne({ test: 1 });
330-
fail('Expected insertion to fail');
331-
} catch (err) {
332-
expect(err).toBeInstanceOf(MongoServerError);
333-
expect((err as MongoServerError).codeName).toEqual('Unauthorized');
334-
} finally {
335-
await con.close();
329+
try {
330+
await col.insertOne({ test: 1 });
331+
fail('Expected insertion to fail');
332+
} catch (err) {
333+
expect(err).toBeInstanceOf(MongoServerError);
334+
expect((err as MongoServerError).codeName).toEqual('Unauthorized');
335+
} finally {
336+
await con.close();
337+
}
336338
}
337-
}
338339

339-
// test priviliged connection
340-
{
341-
const con: MongoClient = await MongoClient.connect(replSet.getUri(), {
342-
authSource: 'admin',
343-
authMechanism: 'SCRAM-SHA-256',
344-
auth: {
345-
username: replSet.replSetOpts.auth.customRootName,
346-
password: replSet.replSetOpts.auth.customRootPwd,
347-
},
348-
});
340+
// test priviliged connection
341+
{
342+
const con: MongoClient = await MongoClient.connect(replSet.getUri(), {
343+
authSource: 'admin',
344+
authMechanism: 'SCRAM-SHA-256',
345+
auth: {
346+
username: replSet.replSetOpts.auth.customRootName,
347+
password: replSet.replSetOpts.auth.customRootPwd,
348+
},
349+
});
349350

350-
const admindb = con.db('admin');
351-
const users: { users?: { user: string }[] } = await admindb.command({
352-
usersInfo: replSet.replSetOpts.auth.customRootName,
353-
});
354-
expect(users.users).toHaveLength(1);
355-
expect(users.users?.[0].user).toEqual(replSet.replSetOpts.auth.customRootName);
351+
const admindb = con.db('admin');
352+
const users: { users?: { user: string }[] } = await admindb.command({
353+
usersInfo: replSet.replSetOpts.auth.customRootName,
354+
});
355+
expect(users.users).toHaveLength(1);
356+
expect(users.users?.[0].user).toEqual(replSet.replSetOpts.auth.customRootName);
356357

357-
const db = con.db('somedb');
358-
const col = db.collection('somecol');
358+
const db = con.db('somedb');
359+
const col = db.collection('somecol');
359360

360-
expect(await col.insertOne({ test: 1 })).toHaveProperty('acknowledged', true);
361+
expect(await col.insertOne({ test: 1 })).toHaveProperty('acknowledged', true);
361362

362-
await con.close();
363+
await con.close();
364+
}
363365
}
364366

367+
await testConnections();
368+
365369
// @ts-expect-error because "initAllServers" is protected
366370
expect(MongoMemoryReplSet.prototype.initAllServers).toHaveBeenCalledTimes(1);
367371
expect(console.warn).toHaveBeenCalledTimes(0);
@@ -372,6 +376,13 @@ describe('single server replset', () => {
372376
expect(replSet.servers[2].instanceInfo?.instance.instanceOpts.auth).toStrictEqual(true);
373377
}
374378

379+
await replSet.stop({ doCleanup: false });
380+
381+
// test restart for "_ranCreateAuth" restart testing
382+
await replSet.start();
383+
384+
await testConnections();
385+
375386
await replSet.stop();
376387
});
377388
});

0 commit comments

Comments
 (0)