@@ -6,8 +6,13 @@ import { promisify } from 'util';
66import { MongoCredentials } from '../../src/cmap/auth/mongo_credentials' ;
77import { AUTH_MECHS_AUTH_SRC_EXTERNAL , AuthMechanism } from '../../src/cmap/auth/providers' ;
88import { parseOptions , resolveSRVRecord } from '../../src/connection_string' ;
9- import { MongoDriverError , MongoInvalidArgumentError , MongoParseError } from '../../src/error' ;
10- import { MongoOptions } from '../../src/mongo_client' ;
9+ import {
10+ MongoDriverError ,
11+ MongoInvalidArgumentError ,
12+ MongoParseError ,
13+ MongoServerSelectionError
14+ } from '../../src/error' ;
15+ import { MongoClient , MongoOptions } from '../../src/mongo_client' ;
1116
1217describe ( 'Connection String' , function ( ) {
1318 it ( 'should not support auth passed with user' , function ( ) {
@@ -98,6 +103,24 @@ describe('Connection String', function () {
98103 expect ( options ) . to . have . nested . property ( 'credentials.source' , mockAuthSource ) ;
99104 } ) ;
100105
106+ it ( 'should omit credentials if the only auth related option is authSource' , async ( ) => {
107+ const client = new MongoClient ( 'mongodb://localhost:123/?authSource=someDb' , {
108+ serverSelectionTimeoutMS : 500
109+ } ) ;
110+
111+ let thrownError : Error ;
112+ try {
113+ // relies on us not running a mongod on port 123, fairly likely assumption
114+ await client . connect ( ) ;
115+ } catch ( error ) {
116+ thrownError = error ;
117+ }
118+
119+ // We should fail to connect, not fail to find an auth provider
120+ expect ( thrownError ) . to . be . instanceOf ( MongoServerSelectionError ) ;
121+ expect ( client . options ) . to . not . have . a . property ( 'credentials' ) ;
122+ } ) ;
123+
101124 it ( 'should parse a numeric authSource with variable width' , function ( ) {
102125 const options = parseOptions ( 'mongodb://test@localhost/?authSource=0001' ) ;
103126 expect ( options . credentials . source ) . to . equal ( '0001' ) ;
0 commit comments