summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChayim <chayim@users.noreply.github.com>2021-09-01 13:09:03 +0300
committerGitHub <noreply@github.com>2021-09-01 13:09:03 +0300
commit3dc2bf906f634383d33952d36cd78156a6e36e0e (patch)
treeeec571dd575a15fa17e0215ba8cef7fe4ede3e14
parent879584b7a359cfd7eeb008b41ba9ca9be16e6633 (diff)
downloadredis-py-3dc2bf906f634383d33952d36cd78156a6e36e0e.tar.gz
Adding support for GENPASS bits (#1558)
Part of #1546 commands.
-rw-r--r--redis/commands.py19
-rw-r--r--tests/test_commands.py8
2 files changed, 24 insertions, 3 deletions
diff --git a/redis/commands.py b/redis/commands.py
index 4230779..895b00a 100644
--- a/redis/commands.py
+++ b/redis/commands.py
@@ -52,9 +52,22 @@ class Commands:
"Delete the ACL for the specified ``username``"
return self.execute_command('ACL DELUSER', username)
- def acl_genpass(self):
- "Generate a random password value"
- return self.execute_command('ACL GENPASS')
+ def acl_genpass(self, bits=None):
+ """Generate a random password value.
+ If ``bits`` is supplied then use this number of bits, rounded to
+ the next multiple of 4.
+ See: https://redis.io/commands/acl-genpass
+ """
+ pieces = []
+ if bits is not None:
+ try:
+ b = int(bits)
+ if b < 0 or b > 4096:
+ raise ValueError
+ except ValueError:
+ raise DataError('genpass optionally accepts a bits argument, '
+ 'between 0 and 4096.')
+ return self.execute_command('ACL GENPASS', *pieces)
def acl_getuser(self, username):
"""
diff --git a/tests/test_commands.py b/tests/test_commands.py
index d77a01c..6877265 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -98,6 +98,14 @@ class TestRedisCommands:
password = r.acl_genpass()
assert isinstance(password, str)
+ with pytest.raises(exceptions.DataError):
+ r.acl_genpass('value')
+ r.acl_genpass(-5)
+ r.acl_genpass(5555)
+
+ r.acl_genpass(555)
+ assert isinstance(password, str)
+
@skip_if_server_version_lt(REDIS_6_VERSION)
def test_acl_getuser_setuser(self, r, request):
username = 'redis-py-user'