1
1
var config = require ( '../../common.js' ) . config ;
2
+ var Buffer = require ( 'safe-buffer' ) . Buffer ;
2
3
3
4
var skipTest = false ;
4
5
if ( typeof Promise == 'undefined' ) {
@@ -23,6 +24,7 @@ var doneEventsConnect = false;
23
24
var doneCalledPool = false ;
24
25
var exceptionCaughtPool = false ;
25
26
var doneEventsPool = false ;
27
+ var doneChangeUser = false ;
26
28
27
29
function testBasic ( ) {
28
30
var connResolved ;
@@ -65,7 +67,6 @@ function testErrors() {
65
67
return connResolved . query ( 'select 2+2 as qqq' ) ;
66
68
} )
67
69
. catch ( function ( err ) {
68
- console . log ( err ) ;
69
70
exceptionCaught = true ;
70
71
if ( connResolved ) {
71
72
connResolved . end ( ) ;
@@ -247,6 +248,74 @@ function testEventsPool() {
247
248
pool . pool . emit ( 'release' ) ;
248
249
}
249
250
251
+ function testChangeUser ( ) {
252
+ var onlyUsername = function ( name ) {
253
+ return name . substring ( 0 , name . indexOf ( '@' ) ) ;
254
+ } ;
255
+ var connResolved ;
256
+ var connPromise = createConnection ( config )
257
+ . then ( function ( conn ) {
258
+ connResolved = conn ;
259
+ return connResolved . query (
260
+ "GRANT ALL ON *.* TO 'changeuser1'@'%' IDENTIFIED BY 'changeuser1pass'"
261
+ ) ;
262
+ } )
263
+ . then ( function ( ) {
264
+ return connResolved . query (
265
+ "GRANT ALL ON *.* TO 'changeuser2'@'%' IDENTIFIED BY 'changeuser2pass'"
266
+ ) ;
267
+ } )
268
+ . then ( function ( ) {
269
+ return connResolved . query ( 'FLUSH PRIVILEGES' ) ;
270
+ } )
271
+ . then ( function ( ) {
272
+ return connResolved . changeUser ( {
273
+ user : 'changeuser1' ,
274
+ password : 'changeuser1pass'
275
+ } ) ;
276
+ } )
277
+ . then ( function ( ) {
278
+ return connResolved . query ( 'select current_user()' ) ;
279
+ } )
280
+ . then ( function ( result ) {
281
+ const rows = result [ 0 ] ;
282
+ assert . deepEqual ( onlyUsername ( rows [ 0 ] [ 'current_user()' ] ) , 'changeuser1' ) ;
283
+ return connResolved . changeUser ( {
284
+ user : 'changeuser2' ,
285
+ password : 'changeuser2pass'
286
+ } ) ;
287
+ } )
288
+ . then ( function ( ) {
289
+ return connResolved . query ( 'select current_user()' ) ;
290
+ } )
291
+ . then ( function ( result ) {
292
+ const rows = result [ 0 ] ;
293
+ assert . deepEqual ( onlyUsername ( rows [ 0 ] [ 'current_user()' ] ) , 'changeuser2' ) ;
294
+ return connResolved . changeUser ( {
295
+ user : 'changeuser1' ,
296
+ passwordSha1 : Buffer . from (
297
+ 'f961d39c82138dcec42b8d0dcb3e40a14fb7e8cd' ,
298
+ 'hex'
299
+ ) // sha1(changeuser1pass)
300
+ } ) ;
301
+ } )
302
+ . then ( function ( ) {
303
+ return connResolved . query ( 'select current_user()' ) ;
304
+ } )
305
+ . then ( function ( result ) {
306
+ const rows = result [ 0 ] ;
307
+ assert . deepEqual ( onlyUsername ( rows [ 0 ] [ 'current_user()' ] ) , 'changeuser1' ) ;
308
+ doneChangeUser = true ;
309
+ return connResolved . end ( ) ;
310
+ } )
311
+ . catch ( function ( err ) {
312
+ if ( connResolved ) {
313
+ connResolved . end ( ) ;
314
+ }
315
+ throw err ;
316
+ } ) ;
317
+ }
318
+
250
319
testBasic ( ) ;
251
320
testErrors ( ) ;
252
321
testObjParams ( ) ;
@@ -256,6 +325,7 @@ testBasicPool();
256
325
testErrorsPool ( ) ;
257
326
testObjParamsPool ( ) ;
258
327
testEventsPool ( ) ;
328
+ testChangeUser ( ) ;
259
329
260
330
process . on ( 'exit' , function ( ) {
261
331
if ( skipTest ) {
@@ -267,6 +337,7 @@ process.on('exit', function() {
267
337
assert . equal ( doneCalledPool , true ) ;
268
338
assert . equal ( exceptionCaughtPool , true ) ;
269
339
assert . equal ( doneEventsPool , true ) ;
340
+ assert . equal ( doneChangeUser , true ) ;
270
341
} ) ;
271
342
272
343
process . on ( 'unhandledRejection' , function ( err ) {
0 commit comments