@@ -328,7 +328,12 @@ def test_command_missing
328328 end
329329
330330 def test_authentication
331- @redis . call ( "ACL" , "SETUSER" , "AzureDiamond" , ">hunter2" , "on" , "+PING" )
331+ @redis . call ( "ACL" , "DELUSER" , "AzureDiamond" )
332+ @redis . call ( "ACL" , "SETUSER" , "AzureDiamond" , ">hunter2" , "on" , "+PING" , "+CLIENT" )
333+ @redis . call ( "ACL" , "DELUSER" , "backup_admin" )
334+ @redis . call ( "ACL" , "SETUSER" , "backup_admin" , ">hunter2" , "on" , "~*" , "&*" , "+@all" )
335+ backup = new_client ( username : "backup_admin" , password : "hunter2" )
336+ backup . call ( "ACL" , "SETUSER" , "default" , "off" )
332337
333338 client = new_client ( username : "AzureDiamond" , password : "hunter2" )
334339 assert_equal "PONG" , client . call ( "PING" )
@@ -337,10 +342,57 @@ def test_authentication
337342 client . call ( "GET" , "foo" )
338343 end
339344
345+ # Wrong password
340346 client = new_client ( username : "AzureDiamond" , password : "trolilol" )
341- assert_raises RedisClient ::AuthenticationError do
347+ error = assert_raises RedisClient ::AuthenticationError do
342348 client . call ( "PING" )
343349 end
350+ assert_match ( /WRONGPASS invalid username-password pair/ , error . message )
351+
352+ # The same error is raised, this shows that the client retried AUTH and didn't fall back to the default user
353+ error = assert_raises RedisClient ::AuthenticationError do
354+ client . call ( "PING" )
355+ end
356+ assert_match ( /WRONGPASS invalid username-password pair/ , error . message )
357+
358+ # Correct password, but user disabled
359+ backup . call ( "ACL" , "SETUSER" , "AzureDiamond" , "<hunter2" , ">trolilol" , "off" )
360+ error = assert_raises RedisClient ::AuthenticationError do
361+ client . call_once ( "PING" )
362+ end
363+ assert_match ( /WRONGPASS invalid username-password pair/ , error . message )
364+
365+ # Correct password, user enabled
366+ backup . call ( "ACL" , "SETUSER" , "AzureDiamond" , "on" )
367+ assert_equal "PONG" , client . call_once ( "PING" )
368+ assert_match ( /user=AzureDiamond/ , client . call ( "CLIENT" , "INFO" ) )
369+
370+ # Wrong username
371+ client = new_client ( username : "GreenOpal" , password : "trolilol" )
372+ error = assert_raises RedisClient ::AuthenticationError do
373+ client . call ( "PING" )
374+ end
375+ assert_match ( /WRONGPASS invalid username-password pair/ , error . message )
376+ ensure
377+ backup . call ( "ACL" , "SETUSER" , "default" , "on" )
378+ end
379+
380+ def test_noauth
381+ @redis . call ( "ACL" , "DELUSER" , "AzureDiamond" )
382+ @redis . call ( "ACL" , "SETUSER" , "AzureDiamond" , ">hunter2" , "on" , "~*" , "&*" , "+@all" )
383+ backup = new_client ( username : "AzureDiamond" , password : "hunter2" )
384+ backup . call ( "ACL" , "SETUSER" , "default" , "off" )
385+
386+ client = new_client ( protocol : 2 )
387+ error = assert_raises RedisClient ::CommandError do
388+ client . call ( "PING" )
389+ end
390+ assert_match ( /NOAUTH Authentication required/ , error . message )
391+
392+ backup . call ( "ACL" , "SETUSER" , "default" , "on" )
393+ client . call ( "PING" )
394+ ensure
395+ backup . call ( "ACL" , "SETUSER" , "default" , "on" )
344396 end
345397
346398 def test_transaction
0 commit comments