From 0a28387d0bf273d06b61a7619313ec7ce19ec9e1 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Wed, 30 Oct 2024 13:17:51 -0600 Subject: [PATCH 1/4] asdf --- .../mongodb-handshake.test.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/integration/mongodb-handshake/mongodb-handshake.test.ts b/test/integration/mongodb-handshake/mongodb-handshake.test.ts index 85698af946b..3b53ae6204c 100644 --- a/test/integration/mongodb-handshake/mongodb-handshake.test.ts +++ b/test/integration/mongodb-handshake/mongodb-handshake.test.ts @@ -4,6 +4,7 @@ import type Sinon from 'sinon'; import * as sinon from 'sinon'; import { + connect, Connection, LEGACY_HELLO_COMMAND, MongoServerError, @@ -130,4 +131,38 @@ describe('MongoDB Handshake', () => { } }); }); + + context( + `when the handshake response includes 'saslSupportedMechs' and the array includes an unknown mechanism`, + function () { + beforeEach(() => { + sinon.stub(Connection.prototype, 'command').callsFake(async function (ns, cmd, options) { + // @ts-expect-error: sinon will place wrappedMethod there + const command = Connection.prototype.command.wrappedMethod.bind(this); + if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) { + return stub(); + } + return command(ns, cmd, options); + + async function stub() { + const response = await command(ns, cmd, options); + // console.error({ response }); + return { + ...response, + saslSupportedMechs: [...(response.saslSupportedMechs ?? []), 'random string'] + }; + } + }); + }); + + afterEach(() => sinon.restore()); + + it('no error is thrown', { requires: { auth: 'enabled' } }, async function () { + client = this.configuration.newClient(); + await client.connect(); + await client.db('foo').collection('bar').insertOne({ name: 'john doe' }); + await client.close(); + }); + } + ); }); From 56178143a8eac479cc2f0269264fbfe8804f600b Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Fri, 1 Nov 2024 14:14:13 -0600 Subject: [PATCH 2/4] move test to prose test file --- .../mongodb-handshake.prose.test.ts | 38 ++++++++++++++++++- .../mongodb-handshake.test.ts | 34 ----------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts index 9c6e4e3e798..d4dc0ffc782 100644 --- a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts +++ b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts @@ -1,7 +1,7 @@ import { expect } from 'chai'; +import * as sinon from 'sinon'; -import { getFAASEnv, type MongoClient } from '../../mongodb'; - +import { Connection, getFAASEnv, LEGACY_HELLO_COMMAND, type MongoClient } from '../../mongodb'; describe('Handshake Prose Tests', function () { let client: MongoClient; @@ -109,4 +109,38 @@ describe('Handshake Prose Tests', function () { }); }); } + + context(`Test 2: Test that the driver accepts an arbitrary auth mechanism`, function () { + beforeEach(() => { + // Mock the server response in a way that saslSupportedMechs array in the hello command response contains an arbitrary string. + sinon.stub(Connection.prototype, 'command').callsFake(async function (ns, cmd, options) { + // @ts-expect-error: sinon will place wrappedMethod there + const command = Connection.prototype.command.wrappedMethod.bind(this); + if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) { + return stub(); + } + return command(ns, cmd, options); + + async function stub() { + const response = await command(ns, cmd, options); + // console.error({ response }); + return { + ...response, + saslSupportedMechs: [...(response.saslSupportedMechs ?? []), 'random string'] + }; + } + }); + }); + + afterEach(() => sinon.restore()); + + it('no error is thrown', { requires: { auth: 'enabled' } }, async function () { + // Create and connect a Connection object that connects to the server that returns the mocked response. + // Assert that no error is raised. + client = this.configuration.newClient(); + await client.connect(); + await client.db('foo').collection('bar').insertOne({ name: 'john doe' }); + await client.close(); + }); + }); }); diff --git a/test/integration/mongodb-handshake/mongodb-handshake.test.ts b/test/integration/mongodb-handshake/mongodb-handshake.test.ts index 3b53ae6204c..e60612d51ee 100644 --- a/test/integration/mongodb-handshake/mongodb-handshake.test.ts +++ b/test/integration/mongodb-handshake/mongodb-handshake.test.ts @@ -131,38 +131,4 @@ describe('MongoDB Handshake', () => { } }); }); - - context( - `when the handshake response includes 'saslSupportedMechs' and the array includes an unknown mechanism`, - function () { - beforeEach(() => { - sinon.stub(Connection.prototype, 'command').callsFake(async function (ns, cmd, options) { - // @ts-expect-error: sinon will place wrappedMethod there - const command = Connection.prototype.command.wrappedMethod.bind(this); - if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) { - return stub(); - } - return command(ns, cmd, options); - - async function stub() { - const response = await command(ns, cmd, options); - // console.error({ response }); - return { - ...response, - saslSupportedMechs: [...(response.saslSupportedMechs ?? []), 'random string'] - }; - } - }); - }); - - afterEach(() => sinon.restore()); - - it('no error is thrown', { requires: { auth: 'enabled' } }, async function () { - client = this.configuration.newClient(); - await client.connect(); - await client.db('foo').collection('bar').insertOne({ name: 'john doe' }); - await client.close(); - }); - } - ); }); From effcbd41d0fd77f7adb2067caac933149754f715 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Tue, 5 Nov 2024 10:24:53 -0700 Subject: [PATCH 3/4] comments --- .../mongodb-handshake/mongodb-handshake.prose.test.ts | 1 - test/integration/mongodb-handshake/mongodb-handshake.test.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts index d4dc0ffc782..423122efa8e 100644 --- a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts +++ b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts @@ -123,7 +123,6 @@ describe('Handshake Prose Tests', function () { async function stub() { const response = await command(ns, cmd, options); - // console.error({ response }); return { ...response, saslSupportedMechs: [...(response.saslSupportedMechs ?? []), 'random string'] diff --git a/test/integration/mongodb-handshake/mongodb-handshake.test.ts b/test/integration/mongodb-handshake/mongodb-handshake.test.ts index e60612d51ee..85698af946b 100644 --- a/test/integration/mongodb-handshake/mongodb-handshake.test.ts +++ b/test/integration/mongodb-handshake/mongodb-handshake.test.ts @@ -4,7 +4,6 @@ import type Sinon from 'sinon'; import * as sinon from 'sinon'; import { - connect, Connection, LEGACY_HELLO_COMMAND, MongoServerError, From 05b3797c06f39e64ecd051b0f94da50aae1ca89d Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Wed, 6 Nov 2024 12:37:39 -0700 Subject: [PATCH 4/4] add check taht stub is called --- .../mongodb-handshake/mongodb-handshake.prose.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts index 423122efa8e..915ea30524e 100644 --- a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts +++ b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts @@ -111,6 +111,7 @@ describe('Handshake Prose Tests', function () { } context(`Test 2: Test that the driver accepts an arbitrary auth mechanism`, function () { + let stubCalled = false; beforeEach(() => { // Mock the server response in a way that saslSupportedMechs array in the hello command response contains an arbitrary string. sinon.stub(Connection.prototype, 'command').callsFake(async function (ns, cmd, options) { @@ -122,6 +123,7 @@ describe('Handshake Prose Tests', function () { return command(ns, cmd, options); async function stub() { + stubCalled = true; const response = await command(ns, cmd, options); return { ...response, @@ -139,6 +141,8 @@ describe('Handshake Prose Tests', function () { client = this.configuration.newClient(); await client.connect(); await client.db('foo').collection('bar').insertOne({ name: 'john doe' }); + + expect(stubCalled).to.be.true; await client.close(); }); });