Skip to content

Commit

Permalink
fix(NODE-3767): don't delete dbName if authSource is provided (#3055)
Browse files Browse the repository at this point in the history
Co-authored-by: Neal Beeken <neal.beeken@mongodb.com>
  • Loading branch information
avlan and nbbeeken committed Nov 29, 2021
1 parent 1294122 commit 0a830e2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/connection_string.ts
Expand Up @@ -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)
);
Expand Down
11 changes: 11 additions & 0 deletions test/unit/connection_string.test.js
Expand Up @@ -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');
Expand Down
59 changes: 59 additions & 0 deletions test/unit/mongo_client.test.js
Expand Up @@ -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');
});
});
});
});

0 comments on commit 0a830e2

Please sign in to comment.