|
1 | 1 | import { expect } from 'chai';
|
| 2 | +import * as sinon from 'sinon'; |
2 | 3 |
|
3 |
| -import { getFAASEnv, type MongoClient } from '../../mongodb'; |
4 |
| - |
| 4 | +import { Connection, getFAASEnv, LEGACY_HELLO_COMMAND, type MongoClient } from '../../mongodb'; |
5 | 5 | describe('Handshake Prose Tests', function () {
|
6 | 6 | let client: MongoClient;
|
7 | 7 |
|
@@ -109,4 +109,41 @@ describe('Handshake Prose Tests', function () {
|
109 | 109 | });
|
110 | 110 | });
|
111 | 111 | }
|
| 112 | + |
| 113 | + context(`Test 2: Test that the driver accepts an arbitrary auth mechanism`, function () { |
| 114 | + let stubCalled = false; |
| 115 | + beforeEach(() => { |
| 116 | + // Mock the server response in a way that saslSupportedMechs array in the hello command response contains an arbitrary string. |
| 117 | + sinon.stub(Connection.prototype, 'command').callsFake(async function (ns, cmd, options) { |
| 118 | + // @ts-expect-error: sinon will place wrappedMethod there |
| 119 | + const command = Connection.prototype.command.wrappedMethod.bind(this); |
| 120 | + if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) { |
| 121 | + return stub(); |
| 122 | + } |
| 123 | + return command(ns, cmd, options); |
| 124 | + |
| 125 | + async function stub() { |
| 126 | + stubCalled = true; |
| 127 | + const response = await command(ns, cmd, options); |
| 128 | + return { |
| 129 | + ...response, |
| 130 | + saslSupportedMechs: [...(response.saslSupportedMechs ?? []), 'random string'] |
| 131 | + }; |
| 132 | + } |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + afterEach(() => sinon.restore()); |
| 137 | + |
| 138 | + it('no error is thrown', { requires: { auth: 'enabled' } }, async function () { |
| 139 | + // Create and connect a Connection object that connects to the server that returns the mocked response. |
| 140 | + // Assert that no error is raised. |
| 141 | + client = this.configuration.newClient(); |
| 142 | + await client.connect(); |
| 143 | + await client.db('foo').collection('bar').insertOne({ name: 'john doe' }); |
| 144 | + |
| 145 | + expect(stubCalled).to.be.true; |
| 146 | + await client.close(); |
| 147 | + }); |
| 148 | + }); |
112 | 149 | });
|
0 commit comments