Skip to content

Commit 1cc3d1c

Browse files
authored
fix(NODE-7290): use valueof for error code check (#4791)
1 parent 26eb0e6 commit 1cc3d1c

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/sdam/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
356356
} catch (operationError) {
357357
if (
358358
operationError instanceof MongoError &&
359-
operationError.code === MONGODB_ERROR_CODES.Reauthenticate
359+
operationError.code?.valueOf() === MONGODB_ERROR_CODES.Reauthenticate
360360
) {
361361
reauthPromise = this.pool.reauthenticate(conn);
362362
reauthPromise.then(undefined, error => {

test/integration/auth/mongodb_oidc.prose.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,70 @@ describe('OIDC Auth Spec Tests', function () {
417417
});
418418
});
419419

420+
describe('4.1 Reauthentication Succeeds (promoteValues: false)', function () {
421+
let utilClient: MongoClient;
422+
const callbackSpy = sinon.spy(createCallback());
423+
// Create an OIDC configured client.
424+
// Set a fail point for find commands of the form:
425+
// {
426+
// configureFailPoint: "failCommand",
427+
// mode: {
428+
// times: 1
429+
// },
430+
// data: {
431+
// failCommands: [
432+
// "find"
433+
// ],
434+
// errorCode: 391 // ReauthenticationRequired
435+
// }
436+
// }
437+
// Perform a find operation that succeeds.
438+
// Assert that the callback was called 2 times (once during the connection handshake, and again during reauthentication).
439+
// Close the client.
440+
beforeEach(async function () {
441+
client = new MongoClient(uriSingle, {
442+
authMechanismProperties: {
443+
OIDC_CALLBACK: callbackSpy
444+
},
445+
retryReads: false,
446+
promoteValues: false
447+
});
448+
utilClient = new MongoClient(uriSingle, {
449+
authMechanismProperties: {
450+
OIDC_CALLBACK: createCallback()
451+
},
452+
retryReads: false
453+
});
454+
collection = client.db('test').collection('test');
455+
await utilClient
456+
.db()
457+
.admin()
458+
.command({
459+
configureFailPoint: 'failCommand',
460+
mode: {
461+
times: 1
462+
},
463+
data: {
464+
failCommands: ['find'],
465+
errorCode: 391
466+
}
467+
});
468+
});
469+
470+
afterEach(async function () {
471+
await utilClient.db().admin().command({
472+
configureFailPoint: 'failCommand',
473+
mode: 'off'
474+
});
475+
await utilClient.close();
476+
});
477+
478+
it('successfully authenticates', async function () {
479+
await collection.findOne();
480+
expect(callbackSpy).to.have.been.calledTwice;
481+
});
482+
});
483+
420484
describe('4.2 Read Commands Fail If Reauthentication Fails', function () {
421485
let utilClient: MongoClient;
422486
const callbackSpy = sinon.spy(createBadCallback());

0 commit comments

Comments
 (0)