@@ -4,7 +4,7 @@ const sinon = require('sinon');
4
4
const { expect } = require ( 'chai' ) ;
5
5
const { Connection } = require ( '../../../src/cmap/connection' ) ;
6
6
const { ScramSHA256 } = require ( '../../../src/cmap/auth/scram' ) ;
7
- const { setupDatabase, withClient } = require ( '../shared' ) ;
7
+ const { setupDatabase } = require ( '../shared' ) ;
8
8
const { LEGACY_HELLO_COMMAND } = require ( '../../../src/constants' ) ;
9
9
10
10
describe ( 'auth prose tests' , ( ) => {
@@ -49,30 +49,30 @@ describe('auth prose tests', () => {
49
49
* Step 1
50
50
* Create three test users, one with only SHA-1, one with only SHA-256 and one with both.
51
51
*/
52
- before ( function ( ) {
53
- return withClient ( this . configuration . newClient ( ) , client => {
54
- test . oldDbName = this . configuration . db ;
55
- this . configuration . db = 'admin' ;
56
- const db = client . db ( this . configuration . db ) ;
57
-
58
- const createUserCommands = users . map ( user => ( {
59
- createUser : user . username ,
60
- pwd : user . password ,
61
- roles : [ 'root' ] ,
62
- mechanisms : user . mechanisms
63
- } ) ) ;
64
-
65
- return Promise . all ( createUserCommands . map ( cmd => db . command ( cmd ) ) ) ;
66
- } ) ;
52
+ before ( async function ( ) {
53
+ const client = this . configuration . newClient ( ) ;
54
+ test . oldDbName = this . configuration . db ;
55
+ this . configuration . db = 'admin' ;
56
+ const db = client . db ( this . configuration . db ) ;
57
+
58
+ const createUserCommands = users . map ( user => ( {
59
+ createUser : user . username ,
60
+ pwd : user . password ,
61
+ roles : [ 'root' ] ,
62
+ mechanisms : user . mechanisms
63
+ } ) ) ;
64
+
65
+ await Promise . all ( createUserCommands . map ( cmd => db . command ( cmd ) ) ) ;
66
+ await client . close ( ) ;
67
67
} ) ;
68
68
69
- after ( function ( ) {
70
- return withClient ( this . configuration . newClient ( ) , client => {
71
- const db = client . db ( this . configuration . db ) ;
72
- this . configuration . db = test . oldDbName ;
69
+ after ( async function ( ) {
70
+ const client = this . configuration . newClient ( ) ;
71
+ const db = client . db ( this . configuration . db ) ;
72
+ this . configuration . db = test . oldDbName ;
73
73
74
- return Promise . all ( users . map ( user => db . removeUser ( user . username ) ) ) ;
75
- } ) ;
74
+ await Promise . all ( users . map ( user => db . removeUser ( user . username ) ) ) ;
75
+ await client . close ( ) ;
76
76
} ) ;
77
77
78
78
/**
@@ -86,7 +86,7 @@ describe('auth prose tests', () => {
86
86
user . mechanisms . forEach ( mechanism => {
87
87
it ( `should auth ${ user . description } when explicitly specifying ${ mechanism } ` , {
88
88
metadata : { requires : { mongodb : '>=3.7.3' } } ,
89
- test : function ( ) {
89
+ test : async function ( ) {
90
90
const options = {
91
91
auth : {
92
92
username : user . username ,
@@ -96,15 +96,15 @@ describe('auth prose tests', () => {
96
96
authSource : this . configuration . db
97
97
} ;
98
98
99
- return withClient ( this . configuration . newClient ( { } , options ) , client => {
100
- return client . db ( this . configuration . db ) . stats ( ) ;
101
- } ) ;
99
+ const client = this . configuration . newClient ( { } , options ) ;
100
+ await client . db ( this . configuration . db ) . stats ( ) ;
101
+ await client . close ( ) ;
102
102
}
103
103
} ) ;
104
104
105
105
it ( `should auth ${ user . description } when explicitly specifying ${ mechanism } in url` , {
106
106
metadata : { requires : { mongodb : '>=3.7.3' } } ,
107
- test : function ( ) {
107
+ test : async function ( ) {
108
108
const username = encodeURIComponent ( user . username ) ;
109
109
const password = encodeURIComponent ( user . password ) ;
110
110
@@ -116,16 +116,15 @@ describe('auth prose tests', () => {
116
116
117
117
const client = this . configuration . newClient ( url ) ;
118
118
119
- return withClient ( client , client => {
120
- return client . db ( this . configuration . db ) . stats ( ) ;
121
- } ) ;
119
+ await client . db ( this . configuration . db ) . stats ( ) ;
120
+ await client . close ( ) ;
122
121
}
123
122
} ) ;
124
123
} ) ;
125
124
126
125
it ( `should auth ${ user . description } using mechanism negotiaton` , {
127
126
metadata : { requires : { mongodb : '>=3.7.3' } } ,
128
- test : function ( ) {
127
+ test : async function ( ) {
129
128
const options = {
130
129
auth : {
131
130
username : user . username ,
@@ -134,24 +133,23 @@ describe('auth prose tests', () => {
134
133
authSource : this . configuration . db
135
134
} ;
136
135
137
- return withClient ( this . configuration . newClient ( { } , options ) , client => {
138
- return client . db ( this . configuration . db ) . stats ( ) ;
139
- } ) ;
136
+ const client = this . configuration . newClient ( { } , options ) ;
137
+ await client . db ( this . configuration . db ) . stats ( ) ;
138
+ await client . close ( ) ;
140
139
}
141
140
} ) ;
142
141
143
142
it ( `should auth ${ user . description } using mechanism negotiaton and url` , {
144
143
metadata : { requires : { mongodb : '>=3.7.3' } } ,
145
- test : function ( ) {
144
+ test : async function ( ) {
146
145
const username = encodeURIComponent ( user . username ) ;
147
146
const password = encodeURIComponent ( user . password ) ;
148
147
const url = makeConnectionString ( this . configuration , username , password ) ;
149
148
150
149
const client = this . configuration . newClient ( url ) ;
151
150
152
- return withClient ( client , client => {
153
- return client . db ( this . configuration . db ) . stats ( ) ;
154
- } ) ;
151
+ await client . db ( this . configuration . db ) . stats ( ) ;
152
+ await client . close ( ) ;
155
153
}
156
154
} ) ;
157
155
} ) ;
@@ -163,7 +161,7 @@ describe('auth prose tests', () => {
163
161
*/
164
162
it ( 'should select SCRAM-SHA-256 for a user that supports both auth mechanisms' , {
165
163
metadata : { requires : { mongodb : '>=3.7.3' } } ,
166
- test : function ( ) {
164
+ test : async function ( ) {
167
165
const options = {
168
166
auth : {
169
167
username : userMap . both . username ,
@@ -174,16 +172,17 @@ describe('auth prose tests', () => {
174
172
175
173
test . sandbox . spy ( ScramSHA256 . prototype , 'auth' ) ;
176
174
177
- return withClient ( this . configuration . newClient ( { } , options ) , ( ) => {
178
- expect ( ScramSHA256 . prototype . auth . called ) . to . equal ( true ) ;
179
- } ) ;
175
+ const client = this . configuration . newClient ( { } , options ) ;
176
+ await client . db ( ) . command ( { ping : 1 } ) ;
177
+ expect ( ScramSHA256 . prototype . auth . called ) . to . equal ( true ) ;
178
+ await client . close ( ) ;
180
179
}
181
180
} ) ;
182
181
183
182
// TODO: not spec
184
183
it ( 'should shorten SCRAM conversations if the server supports it' , {
185
184
metadata : { requires : { mongodb : '>=4.4' , topology : [ 'single' ] } } ,
186
- test : function ( ) {
185
+ test : async function ( ) {
187
186
const options = {
188
187
auth : {
189
188
username : userMap . both . username ,
@@ -207,9 +206,10 @@ describe('auth prose tests', () => {
207
206
auth . apply ( this , [ authContext , _callback ] ) ;
208
207
} ) ;
209
208
210
- return withClient ( this . configuration . newClient ( { } , options ) , ( ) => {
211
- expect ( runCommandSpy . callCount ) . to . equal ( 1 ) ;
212
- } ) ;
209
+ const client = this . configuration . newClient ( { } , options ) ;
210
+ await client . db ( ) . command ( { ping : 1 } ) ;
211
+ expect ( runCommandSpy . callCount ) . to . equal ( 1 ) ;
212
+ await client . close ( ) ;
213
213
}
214
214
} ) ;
215
215
@@ -219,7 +219,7 @@ describe('auth prose tests', () => {
219
219
*/
220
220
it ( 'should fail to connect if incorrect auth mechanism is explicitly specified' , {
221
221
metadata : { requires : { mongodb : '>=3.7.3' } } ,
222
- test : function ( ) {
222
+ test : async function ( ) {
223
223
const options = {
224
224
auth : {
225
225
username : userMap . sha256 . username ,
@@ -229,11 +229,13 @@ describe('auth prose tests', () => {
229
229
authMechanism : 'SCRAM-SHA-1'
230
230
} ;
231
231
232
- return withClient (
233
- this . configuration . newClient ( { } , options ) ,
234
- ( ) => Promise . reject ( new Error ( 'This request should have failed to authenticate' ) ) ,
235
- err => expect ( err ) . to . match ( / A u t h e n t i c a t i o n f a i l e d / )
236
- ) ;
232
+ const client = this . configuration . newClient ( { } , options ) ;
233
+ const error = await client
234
+ . db ( )
235
+ . command ( { ping : 1 } )
236
+ . catch ( error => error ) ;
237
+ expect ( error . message ) . to . match ( / A u t h e n t i c a t i o n f a i l e d / ) ;
238
+ await client . close ( ) ;
237
239
}
238
240
} ) ;
239
241
@@ -248,7 +250,7 @@ describe('auth prose tests', () => {
248
250
*/
249
251
it ( 'should fail for a nonexistent username with same error type as bad password' , {
250
252
metadata : { requires : { mongodb : '>=3.7.3' } } ,
251
- test : function ( ) {
253
+ test : async function ( ) {
252
254
const noUsernameOptions = {
253
255
auth : {
254
256
username : 'roth' ,
@@ -265,20 +267,27 @@ describe('auth prose tests', () => {
265
267
authSource : 'admin'
266
268
} ;
267
269
268
- const getErrorMsg = options =>
269
- withClient (
270
- this . configuration . newClient ( { } , options ) ,
271
- ( ) => Promise . reject ( new Error ( 'This request should have failed to authenticate' ) ) ,
272
- err => expect ( err ) . to . match ( / A u t h e n t i c a t i o n f a i l e d / )
273
- ) ;
274
-
275
- return Promise . all ( [ getErrorMsg ( noUsernameOptions ) , getErrorMsg ( badPasswordOptions ) ] ) ;
270
+ const noUsernameClient = this . configuration . newClient ( { } , noUsernameOptions ) ;
271
+ const errorNoUser = await noUsernameClient
272
+ . db ( )
273
+ . command ( { ping : 1 } )
274
+ . catch ( error => error ) ;
275
+ expect ( errorNoUser . message ) . to . match ( / A u t h e n t i c a t i o n f a i l e d / ) ;
276
+ await noUsernameClient . close ( ) ;
277
+
278
+ const badPasswordClient = this . configuration . newClient ( { } , badPasswordOptions ) ;
279
+ const errorNoPass = await badPasswordClient
280
+ . db ( )
281
+ . command ( { ping : 1 } )
282
+ . catch ( error => error ) ;
283
+ expect ( errorNoPass . message ) . to . match ( / A u t h e n t i c a t i o n f a i l e d / ) ;
284
+ await badPasswordClient . close ( ) ;
276
285
}
277
286
} ) ;
278
287
279
288
it ( 'should send speculativeAuthenticate on initial handshake on MongoDB 4.4+' , {
280
289
metadata : { requires : { mongodb : '>=4.4' , topology : [ 'single' ] } } ,
281
- test : function ( ) {
290
+ test : async function ( ) {
282
291
const options = {
283
292
auth : {
284
293
username : userMap . both . username ,
@@ -288,16 +297,17 @@ describe('auth prose tests', () => {
288
297
} ;
289
298
290
299
const commandSpy = test . sandbox . spy ( Connection . prototype , 'command' ) ;
291
- return withClient ( this . configuration . newClient ( { } , options ) , ( ) => {
292
- const calls = commandSpy
293
- . getCalls ( )
294
- . filter ( c => c . thisValue . id !== '<monitor>' ) // ignore all monitor connections
295
- . filter ( c => c . args [ 1 ] [ LEGACY_HELLO_COMMAND ] ) ; // only consider handshakes
296
-
297
- expect ( calls ) . to . have . length ( 1 ) ;
298
- const handshakeDoc = calls [ 0 ] . args [ 1 ] ;
299
- expect ( handshakeDoc ) . to . have . property ( 'speculativeAuthenticate' ) ;
300
- } ) ;
300
+ const client = this . configuration . newClient ( { } , options ) ;
301
+ await client . db ( ) . command ( { ping : 1 } ) ;
302
+ const calls = commandSpy
303
+ . getCalls ( )
304
+ . filter ( c => c . thisValue . id !== '<monitor>' ) // ignore all monitor connections
305
+ . filter ( c => c . args [ 1 ] [ LEGACY_HELLO_COMMAND ] ) ; // only consider handshakes
306
+
307
+ expect ( calls ) . to . have . length ( 1 ) ;
308
+ const handshakeDoc = calls [ 0 ] . args [ 1 ] ;
309
+ expect ( handshakeDoc ) . to . have . property ( 'speculativeAuthenticate' ) ;
310
+ await client . close ( ) ;
301
311
}
302
312
} ) ;
303
313
} ) ;
@@ -337,27 +347,27 @@ describe('auth prose tests', () => {
337
347
return setupDatabase ( this . configuration ) ;
338
348
} ) ;
339
349
340
- before ( function ( ) {
341
- return withClient ( this . configuration . newClient ( ) , client => {
342
- const db = client . db ( 'admin' ) ;
350
+ before ( async function ( ) {
351
+ const client = this . configuration . newClient ( ) ;
352
+ const db = client . db ( 'admin' ) ;
343
353
344
- const createUserCommands = users . map ( user => ( {
345
- createUser : user . username ,
346
- pwd : user . password ,
347
- roles : [ 'root' ] ,
348
- mechanisms : user . mechanisms
349
- } ) ) ;
354
+ const createUserCommands = users . map ( user => ( {
355
+ createUser : user . username ,
356
+ pwd : user . password ,
357
+ roles : [ 'root' ] ,
358
+ mechanisms : user . mechanisms
359
+ } ) ) ;
350
360
351
- return Promise . all ( createUserCommands . map ( cmd => db . command ( cmd ) ) ) ;
352
- } ) ;
353
- } ) ;
361
+ await Promise . all ( createUserCommands . map ( cmd => db . command ( cmd ) ) ) ;
354
362
355
- after ( function ( ) {
356
- return withClient ( this . configuration . newClient ( ) , client => {
357
- const db = client . db ( 'admin' ) ;
363
+ await client . close ( ) ;
364
+ } ) ;
358
365
359
- return Promise . all ( users . map ( user => db . removeUser ( user . username ) ) ) ;
360
- } ) ;
366
+ after ( async function ( ) {
367
+ const client = this . configuration . newClient ( ) ;
368
+ const db = client . db ( 'admin' ) ;
369
+ await Promise . all ( users . map ( user => db . removeUser ( user . username ) ) ) ;
370
+ await client . close ( ) ;
361
371
} ) ;
362
372
363
373
[
@@ -374,16 +384,16 @@ describe('auth prose tests', () => {
374
384
mongodb : '>=3.7.3'
375
385
}
376
386
} ,
377
- test : function ( ) {
387
+ test : async function ( ) {
378
388
const options = {
379
389
auth : { username, password } ,
380
390
authSource : 'admin' ,
381
391
authMechanism : 'SCRAM-SHA-256'
382
392
} ;
383
393
384
- return withClient ( this . configuration . newClient ( options ) , client => {
385
- return client . db ( 'admin' ) . stats ( ) ;
386
- } ) ;
394
+ const client = this . configuration . newClient ( { } , options ) ;
395
+ await client . db ( 'admin' ) . stats ( ) ;
396
+ await client . close ( ) ;
387
397
}
388
398
} ) ;
389
399
} ) ;
0 commit comments