From 485af111299d30094480a0dba1fbc7d385c0e3a7 Mon Sep 17 00:00:00 2001 From: dogukanteber Date: Fri, 4 Feb 2022 15:12:36 +0300 Subject: [PATCH 1/4] Add support for AUTH --- redis/commands/core.py | 11 +++++++++++ tests/test_commands.py | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/redis/commands/core.py b/redis/commands/core.py index 8c259792cd..4f2dda3e6b 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -315,6 +315,17 @@ class ManagementCommands: Redis management commands """ + def auth(self, password, username=None, **kwargs): + """ + Authenticates the user. If you do not pass username, Redis will try to + authenticate for the "default" user. If you do pass username, it will + authenticate for the given user. + For more information check https://redis.io/commands/auth + """ + if username: + return self.execute_command("AUTH", username, password, **kwargs) + return self.execute_command + def bgrewriteaof(self, **kwargs): """Tell the Redis server to rewrite the AOF file from data in memory. diff --git a/tests/test_commands.py b/tests/test_commands.py index c8f7ddc3f4..0159a6ea2e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -65,6 +65,26 @@ def test_case_insensitive_command_names(self, r): class TestRedisCommands: + def test_auth(self, r, request): + username = "redis-py-auth" + + def teardown(): + r.acl_deluser(username) + + request.addfinalizer(teardown) + + assert r.acl_setuser( + username, + enabled=True, + passwords=["+strong_password"], + commands=["+acl"], + ) + + assert r.auth(username=username, password="strong_password") == True + + with pytest.raises(exceptions.ResponseError): + r.auth(username=username, password="wrong_password") + def test_command_on_invalid_key_type(self, r): r.lpush("a", "1") with pytest.raises(redis.ResponseError): From bd96bf83527a0fd0a3e4be2d9d70641de9d48def Mon Sep 17 00:00:00 2001 From: dogukanteber Date: Fri, 4 Feb 2022 15:26:32 +0300 Subject: [PATCH 2/4] Fix linter error --- tests/test_commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 0159a6ea2e..06b155eb43 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -80,7 +80,7 @@ def teardown(): commands=["+acl"], ) - assert r.auth(username=username, password="strong_password") == True + assert r.auth(username=username, password="strong_password") is True with pytest.raises(exceptions.ResponseError): r.auth(username=username, password="wrong_password") From 92c4e8b66cc2fe51ca1fa5c6d91f4a206f22b305 Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" Date: Sun, 13 Feb 2022 12:51:29 +0200 Subject: [PATCH 3/4] test fix --- tests/test_commands.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 498b1023b0..6353c37561 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -91,10 +91,6 @@ def test_command_on_invalid_key_type(self, r): r["a"] # SERVER INFORMATION - def test_auth_not_implemented(self, r): - with pytest.raises(NotImplementedError): - r.auth() - @skip_if_server_version_lt("6.0.0") def test_acl_cat_no_category(self, r): categories = r.acl_cat() From 8d3c7c31f98467c5817d70ab6cf295dc16d285ef Mon Sep 17 00:00:00 2001 From: dvora-h Date: Wed, 2 Mar 2022 12:07:01 +0200 Subject: [PATCH 4/4] fix test in cluster --- redis/cluster.py | 1 + 1 file changed, 1 insertion(+) diff --git a/redis/cluster.py b/redis/cluster.py index b8d6b1997f..4742f6e186 100644 --- a/redis/cluster.py +++ b/redis/cluster.py @@ -227,6 +227,7 @@ class RedisCluster(RedisClusterCommands): "ACL SETUSER", "ACL USERS", "ACL WHOAMI", + "AUTH", "CLIENT LIST", "CLIENT SETNAME", "CLIENT GETNAME",