From 72aad53d99482abe747f5e678a723a1081b96f73 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Mon, 2 Jun 2025 14:25:42 -0400 Subject: [PATCH 1/2] chode: bring back token missing fix --- .../automated_callback_workflow.ts | 3 ++ .../automated_callback_workflow.test.ts | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts diff --git a/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts b/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts index 3ecb7c3f7d0..1f4947d89a9 100644 --- a/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts +++ b/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts @@ -34,6 +34,9 @@ export class AutomatedCallbackWorkflow extends CallbackWorkflow { // If the server fails for any other reason, do not clear the cache. if (this.cache.hasAccessToken) { const token = this.cache.getAccessToken(); + if (!connection.accessToken) { + connection.accessToken = token; + } try { return await this.finishAuthentication(connection, credentials, token); } catch (error) { diff --git a/test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts b/test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts new file mode 100644 index 00000000000..b5ec6c8042a --- /dev/null +++ b/test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts @@ -0,0 +1,37 @@ +import { expect } from 'chai'; +import * as sinon from 'sinon'; + +// eslint-disable-next-line @typescript-eslint/no-restricted-imports +import { AutomatedCallbackWorkflow } from '../../../../../src/cmap/auth/mongodb_oidc/automated_callback_workflow'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports +import { callback } from '../../../../../src/cmap/auth/mongodb_oidc/gcp_machine_workflow'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports +import { TokenCache } from '../../../../../src/cmap/auth/mongodb_oidc/token_cache'; +import { Connection, MongoCredentials } from '../../../../mongodb'; + +describe('AutomatedCallbackWorkflow', function () { + describe('#execute', function () { + context('when the cache has a token', function () { + context('when the connection has no token', function () { + const cache = new TokenCache(); + const connection = sinon.createStubInstance(Connection); + const credentials = sinon.createStubInstance(MongoCredentials); + const workflow = new AutomatedCallbackWorkflow(cache, callback); + sinon.stub(workflow, 'finishAuthentication').returns(Promise.resolve()); + + beforeEach(function () { + cache.put({ accessToken: 'test', expiresInSeconds: 7200 }); + workflow.execute(connection, credentials); + }); + + afterEach(function () { + sinon.restore(); + }); + + it('sets the token on the connection', async function () { + expect(connection.accessToken).to.equal('test'); + }); + }); + }); + }); +}); From 289f4fcd14c5171dca6ce93a7ba52d4a184c7c7c Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Mon, 2 Jun 2025 15:22:03 -0400 Subject: [PATCH 2/2] fix: test --- test/mongodb.ts | 1 + .../automated_callback_workflow.test.ts | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/test/mongodb.ts b/test/mongodb.ts index d9ffc4c0a11..45e6a6679c7 100644 --- a/test/mongodb.ts +++ b/test/mongodb.ts @@ -119,6 +119,7 @@ export * from '../src/cmap/auth/gssapi'; export * from '../src/cmap/auth/mongo_credentials'; export * from '../src/cmap/auth/mongodb_aws'; export * from '../src/cmap/auth/mongodb_oidc'; +export * from '../src/cmap/auth/mongodb_oidc/automated_callback_workflow'; export * from '../src/cmap/auth/mongodb_oidc/azure_machine_workflow'; export * from '../src/cmap/auth/mongodb_oidc/callback_workflow'; export * from '../src/cmap/auth/plain'; diff --git a/test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts b/test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts index b5ec6c8042a..33d37e593d7 100644 --- a/test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts +++ b/test/unit/cmap/auth/mongodb_oidc/automated_callback_workflow.test.ts @@ -1,23 +1,30 @@ import { expect } from 'chai'; import * as sinon from 'sinon'; -// eslint-disable-next-line @typescript-eslint/no-restricted-imports -import { AutomatedCallbackWorkflow } from '../../../../../src/cmap/auth/mongodb_oidc/automated_callback_workflow'; // eslint-disable-next-line @typescript-eslint/no-restricted-imports import { callback } from '../../../../../src/cmap/auth/mongodb_oidc/gcp_machine_workflow'; // eslint-disable-next-line @typescript-eslint/no-restricted-imports import { TokenCache } from '../../../../../src/cmap/auth/mongodb_oidc/token_cache'; -import { Connection, MongoCredentials } from '../../../../mongodb'; +import { + AutomatedCallbackWorkflow, + CallbackWorkflow, + Connection, + MongoCredentials +} from '../../../../mongodb'; describe('AutomatedCallbackWorkflow', function () { describe('#execute', function () { context('when the cache has a token', function () { + const sandbox = sinon.createSandbox(); + + // See NODE-6801 and corresponding PR: https://github.com/mongodb/node-mongodb-native/pull/4438 + // This is a test to ensure that we do not regress on the above issue. Do NOT remove this test. context('when the connection has no token', function () { const cache = new TokenCache(); - const connection = sinon.createStubInstance(Connection); - const credentials = sinon.createStubInstance(MongoCredentials); + const connection = sandbox.createStubInstance(Connection); + const credentials = sandbox.createStubInstance(MongoCredentials); + sandbox.stub(CallbackWorkflow.prototype, 'finishAuthentication').resolves(); const workflow = new AutomatedCallbackWorkflow(cache, callback); - sinon.stub(workflow, 'finishAuthentication').returns(Promise.resolve()); beforeEach(function () { cache.put({ accessToken: 'test', expiresInSeconds: 7200 }); @@ -25,7 +32,7 @@ describe('AutomatedCallbackWorkflow', function () { }); afterEach(function () { - sinon.restore(); + sandbox.restore(); }); it('sets the token on the connection', async function () {