auth.tcl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. set testmodule [file normalize tests/modules/auth.so]
  2. start_server {tags {"modules"}} {
  3. r module load $testmodule
  4. test {Modules can create a user that can be authenticated} {
  5. # Make sure we start authenticated with default user
  6. r auth default ""
  7. assert_equal [r acl whoami] "default"
  8. r auth.createmoduleuser
  9. set id [r auth.authmoduleuser]
  10. assert_equal [r client id] $id
  11. # Verify returned id is the same as our current id and
  12. # we are authenticated with the specified user
  13. assert_equal [r acl whoami] "global"
  14. }
  15. test {De-authenticating clients is tracked and kills clients} {
  16. assert_equal [r auth.changecount] 0
  17. r auth.createmoduleuser
  18. # Catch the I/O exception that was thrown when Redis
  19. # disconnected with us.
  20. catch { [r ping] } e
  21. assert_match {*I/O*} $e
  22. # Check that a user change was registered
  23. assert_equal [r auth.changecount] 1
  24. }
  25. test {Modules cant authenticate with ACLs users that dont exist} {
  26. catch { [r auth.authrealuser auth-module-test-fake] } e
  27. assert_match {*Invalid user*} $e
  28. }
  29. test {Modules can authenticate with ACL users} {
  30. assert_equal [r acl whoami] "default"
  31. # Create user to auth into
  32. r acl setuser auth-module-test on allkeys allcommands
  33. set id [r auth.authrealuser auth-module-test]
  34. # Verify returned id is the same as our current id and
  35. # we are authenticated with the specified user
  36. assert_equal [r client id] $id
  37. assert_equal [r acl whoami] "auth-module-test"
  38. }
  39. test {Client callback is called on user switch} {
  40. assert_equal [r auth.changecount] 0
  41. # Auth again and validate change count
  42. r auth.authrealuser auth-module-test
  43. assert_equal [r auth.changecount] 1
  44. # Re-auth with the default user
  45. r auth default ""
  46. assert_equal [r auth.changecount] 1
  47. assert_equal [r acl whoami] "default"
  48. # Re-auth with the default user again, to
  49. # verify the callback isn't fired again
  50. r auth default ""
  51. assert_equal [r auth.changecount] 0
  52. assert_equal [r acl whoami] "default"
  53. }
  54. }