Skip to content

Commit 0a830e2

Browse files
avlannbbeeken
andauthored
fix(NODE-3767): don't delete dbName if authSource is provided (#3055)
Co-authored-by: Neal Beeken <[email protected]>
1 parent 1294122 commit 0a830e2

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

src/connection_string.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,6 @@ export function parseOptions(
291291
}
292292
}
293293

294-
if (urlOptions.has('authSource')) {
295-
// If authSource is an explicit key in the urlOptions we need to remove the dbName
296-
urlOptions.delete('dbName');
297-
}
298-
299294
const objectOptions = new CaseInsensitiveMap(
300295
Object.entries(options).filter(([, v]) => v != null)
301296
);

test/unit/connection_string.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ describe('Connection String', function () {
9999
expect(options.credentials.source).to.equal('0001');
100100
});
101101

102+
it('should not remove dbName from the options if authSource is provided', function () {
103+
const dbName = 'my-db-name';
104+
const authSource = 'admin';
105+
const options = parseOptions(
106+
`mongodb://myName:myPassword@localhost:27017/${dbName}?authSource=${authSource}`
107+
);
108+
109+
expect(options).has.property('dbName', dbName);
110+
expect(options.credentials).to.have.property('source', authSource);
111+
});
112+
102113
it('should parse a replicaSet with a leading number', function () {
103114
const options = parseOptions('mongodb://localhost/?replicaSet=123abc');
104115
expect(options).to.have.property('replicaSet');

test/unit/mongo_client.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,4 +787,63 @@ describe('MongoOptions', function () {
787787
// Nothing wrong with the name, just DNE
788788
expect(thrownError).to.have.property('code', 'ENOTFOUND');
789789
});
790+
791+
describe('dbName and authSource', () => {
792+
describe('in the URI', () => {
793+
it('should set the database name to the dbName in the uri', () => {
794+
const client = new MongoClient('mongodb://u:p@host/myDb');
795+
const db = client.db();
796+
expect(db).to.have.property('databaseName', 'myDb');
797+
expect(client).to.have.nested.property('options.credentials.source', 'myDb');
798+
});
799+
it('should set the database name to the uri pathname and respect the authSource option', () => {
800+
const client = new MongoClient('mongodb://u:p@host/myDb?authSource=myAuthDb');
801+
const db = client.db();
802+
expect(db).to.have.property('databaseName', 'myDb');
803+
expect(client).to.have.nested.property('options.credentials.source', 'myAuthDb');
804+
});
805+
it('should set the database name to the uri pathname and respect the authSource option in options object', () => {
806+
const client = new MongoClient('mongodb://u:p@host/myDb', { authSource: 'myAuthDb' });
807+
const db = client.db();
808+
expect(db).to.have.property('databaseName', 'myDb');
809+
expect(client).to.have.nested.property('options.credentials.source', 'myAuthDb');
810+
});
811+
});
812+
813+
describe('in the options object', () => {
814+
it('should set the database name to the dbName in the options object', () => {
815+
const client = new MongoClient('mongodb://u:p@host', { dbName: 'myDb' });
816+
const db = client.db();
817+
expect(db).to.have.property('databaseName', 'myDb');
818+
expect(client).to.have.nested.property('options.credentials.source', 'myDb');
819+
});
820+
it('should set the database name to dbName and respect the authSource option', () => {
821+
const client = new MongoClient('mongodb://u:p@host?authSource=myAuthDb', {
822+
dbName: 'myDb'
823+
});
824+
const db = client.db();
825+
expect(db).to.have.property('databaseName', 'myDb');
826+
expect(client).to.have.nested.property('options.credentials.source', 'myAuthDb');
827+
});
828+
it('should set the database name to dbName and respect the authSource option in options object', () => {
829+
const client = new MongoClient('mongodb://u:p@host', {
830+
dbName: 'myDb',
831+
authSource: 'myAuthDb'
832+
});
833+
const db = client.db();
834+
expect(db).to.have.property('databaseName', 'myDb');
835+
expect(client).to.have.nested.property('options.credentials.source', 'myAuthDb');
836+
});
837+
838+
it('should set the database name to dbName in options object and respect the authSource option in options object', () => {
839+
const client = new MongoClient('mongodb://u:p@host/myIgnoredDb', {
840+
dbName: 'myDb',
841+
authSource: 'myAuthDb'
842+
});
843+
const db = client.db();
844+
expect(db).to.have.property('databaseName', 'myDb');
845+
expect(client).to.have.nested.property('options.credentials.source', 'myAuthDb');
846+
});
847+
});
848+
});
790849
});

0 commit comments

Comments
 (0)