Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-3767): don't delete dbName if authSource is provided #3055

Merged
merged 5 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
});
});
});
});