From 9d30f73028284ea32be60af400496e59f3e9afb7 Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" <c@kirshen.com> Date: Wed, 1 Sep 2021 12:01:38 +0300 Subject: [PATCH] Adding support for GENPASS bits Part of #1546 commands. --- redis/commands.py | 19 ++++++++++++++++--- tests/test_commands.py | 8 ++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/redis/commands.py b/redis/commands.py index 29877dbef9..19e9f2aaaa 100644 --- a/redis/commands.py +++ b/redis/commands.py @@ -52,9 +52,22 @@ def acl_deluser(self, username): "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 0c9ca1eb57..10cf78350d 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -98,6 +98,14 @@ def test_acl_genpass(self, r): 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'