@@ -6,8 +6,13 @@ import { promisify } from 'util';
6
6
import { MongoCredentials } from '../../src/cmap/auth/mongo_credentials' ;
7
7
import { AUTH_MECHS_AUTH_SRC_EXTERNAL , AuthMechanism } from '../../src/cmap/auth/providers' ;
8
8
import { 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' ;
11
16
12
17
describe ( 'Connection String' , function ( ) {
13
18
it ( 'should not support auth passed with user' , function ( ) {
@@ -98,6 +103,24 @@ describe('Connection String', function () {
98
103
expect ( options ) . to . have . nested . property ( 'credentials.source' , mockAuthSource ) ;
99
104
} ) ;
100
105
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
+
101
124
it ( 'should parse a numeric authSource with variable width' , function ( ) {
102
125
const options = parseOptions ( 'mongodb://test@localhost/?authSource=0001' ) ;
103
126
expect ( options . credentials . source ) . to . equal ( '0001' ) ;
0 commit comments