Skip to content

Commit 9d30f73

Browse files
committed
Adding support for GENPASS bits
Part of redis#1546 commands.
1 parent efdba1a commit 9d30f73

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

redis/commands.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,22 @@ def acl_deluser(self, username):
5252
"Delete the ACL for the specified ``username``"
5353
return self.execute_command('ACL DELUSER', username)
5454

55-
def acl_genpass(self):
56-
"Generate a random password value"
57-
return self.execute_command('ACL GENPASS')
55+
def acl_genpass(self, bits=None):
56+
"""Generate a random password value.
57+
If ``bits`` is supplied then use this number of bits, rounded to
58+
the next multiple of 4.
59+
See: https://redis.io/commands/acl-genpass
60+
"""
61+
pieces = []
62+
if bits is not None:
63+
try:
64+
b = int(bits)
65+
if b < 0 or b > 4096:
66+
raise ValueError
67+
except ValueError:
68+
raise DataError('genpass optionally accepts a bits argument, '
69+
'between 0 and 4096.')
70+
return self.execute_command('ACL GENPASS', *pieces)
5871

5972
def acl_getuser(self, username):
6073
"""

tests/test_commands.py

+8
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ def test_acl_genpass(self, r):
9898
password = r.acl_genpass()
9999
assert isinstance(password, str)
100100

101+
with pytest.raises(exceptions.DataError):
102+
r.acl_genpass('value')
103+
r.acl_genpass(-5)
104+
r.acl_genpass(5555)
105+
106+
r.acl_genpass(555)
107+
assert isinstance(password, str)
108+
101109
@skip_if_server_version_lt(REDIS_6_VERSION)
102110
def test_acl_getuser_setuser(self, r, request):
103111
username = 'redis-py-user'

0 commit comments

Comments
 (0)