From 0a830e240adcd8564b6f11d8af0da50a411db58f Mon Sep 17 00:00:00 2001 From: Carlos Avilan Date: Mon, 29 Nov 2021 21:05:42 +0100 Subject: [PATCH] fix(NODE-3767): don't delete dbName if authSource is provided (#3055) Co-authored-by: Neal Beeken --- src/connection_string.ts | 5 --- test/unit/connection_string.test.js | 11 ++++++ test/unit/mongo_client.test.js | 59 +++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/connection_string.ts b/src/connection_string.ts index ed897000dc..4fc5d817e0 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -291,11 +291,6 @@ export function parseOptions( } } - if (urlOptions.has('authSource')) { - // If authSource is an explicit key in the urlOptions we need to remove the dbName - urlOptions.delete('dbName'); - } - const objectOptions = new CaseInsensitiveMap( Object.entries(options).filter(([, v]) => v != null) ); diff --git a/test/unit/connection_string.test.js b/test/unit/connection_string.test.js index e17276e9c2..f96089a409 100644 --- a/test/unit/connection_string.test.js +++ b/test/unit/connection_string.test.js @@ -99,6 +99,17 @@ describe('Connection String', function () { expect(options.credentials.source).to.equal('0001'); }); + it('should not remove dbName from the options if authSource is provided', function () { + const dbName = 'my-db-name'; + const authSource = 'admin'; + const options = parseOptions( + `mongodb://myName:myPassword@localhost:27017/${dbName}?authSource=${authSource}` + ); + + expect(options).has.property('dbName', dbName); + expect(options.credentials).to.have.property('source', authSource); + }); + it('should parse a replicaSet with a leading number', function () { const options = parseOptions('mongodb://localhost/?replicaSet=123abc'); expect(options).to.have.property('replicaSet'); diff --git a/test/unit/mongo_client.test.js b/test/unit/mongo_client.test.js index e5ffce996b..b96f133f7b 100644 --- a/test/unit/mongo_client.test.js +++ b/test/unit/mongo_client.test.js @@ -787,4 +787,63 @@ describe('MongoOptions', function () { // Nothing wrong with the name, just DNE expect(thrownError).to.have.property('code', 'ENOTFOUND'); }); + + describe('dbName and authSource', () => { + describe('in the URI', () => { + it('should set the database name to the dbName in the uri', () => { + const client = new MongoClient('mongodb://u:p@host/myDb'); + const db = client.db(); + expect(db).to.have.property('databaseName', 'myDb'); + expect(client).to.have.nested.property('options.credentials.source', 'myDb'); + }); + it('should set the database name to the uri pathname and respect the authSource option', () => { + const client = new MongoClient('mongodb://u:p@host/myDb?authSource=myAuthDb'); + const db = client.db(); + expect(db).to.have.property('databaseName', 'myDb'); + expect(client).to.have.nested.property('options.credentials.source', 'myAuthDb'); + }); + it('should set the database name to the uri pathname and respect the authSource option in options object', () => { + const client = new MongoClient('mongodb://u:p@host/myDb', { authSource: 'myAuthDb' }); + const db = client.db(); + expect(db).to.have.property('databaseName', 'myDb'); + expect(client).to.have.nested.property('options.credentials.source', 'myAuthDb'); + }); + }); + + describe('in the options object', () => { + it('should set the database name to the dbName in the options object', () => { + const client = new MongoClient('mongodb://u:p@host', { dbName: 'myDb' }); + const db = client.db(); + expect(db).to.have.property('databaseName', 'myDb'); + expect(client).to.have.nested.property('options.credentials.source', 'myDb'); + }); + it('should set the database name to dbName and respect the authSource option', () => { + const client = new MongoClient('mongodb://u:p@host?authSource=myAuthDb', { + dbName: 'myDb' + }); + const db = client.db(); + expect(db).to.have.property('databaseName', 'myDb'); + expect(client).to.have.nested.property('options.credentials.source', 'myAuthDb'); + }); + it('should set the database name to dbName and respect the authSource option in options object', () => { + const client = new MongoClient('mongodb://u:p@host', { + dbName: 'myDb', + authSource: 'myAuthDb' + }); + const db = client.db(); + expect(db).to.have.property('databaseName', 'myDb'); + expect(client).to.have.nested.property('options.credentials.source', 'myAuthDb'); + }); + + it('should set the database name to dbName in options object and respect the authSource option in options object', () => { + const client = new MongoClient('mongodb://u:p@host/myIgnoredDb', { + dbName: 'myDb', + authSource: 'myAuthDb' + }); + const db = client.db(); + expect(db).to.have.property('databaseName', 'myDb'); + expect(client).to.have.nested.property('options.credentials.source', 'myAuthDb'); + }); + }); + }); });