From 8d99371906d1c399abaadec6db38fc3036cd75dd Mon Sep 17 00:00:00 2001 From: dogukanteber Date: Tue, 15 Feb 2022 23:18:36 +0300 Subject: [PATCH 1/4] Add support for ACL DRYRUN --- redis/commands/core.py | 8 ++++++++ tests/test_commands.py | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/redis/commands/core.py b/redis/commands/core.py index 5074e8064d..1e221c9402 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -28,6 +28,14 @@ def acl_cat(self, category=None, **kwargs): pieces = [category] if category else [] return self.execute_command("ACL CAT", *pieces, **kwargs) + def acl_dryrun(self, username, *args, **kwargs): + """ + Simulate the execution of a given command by a given ``username``. + + For more information check https://redis.io/commands/acl-dryrun + """ + return self.execute_command("ACL DRYRUN", username, *args, **kwargs) + def acl_deluser(self, *username, **kwargs): """ Delete the ACL for the specified ``username``s diff --git a/tests/test_commands.py b/tests/test_commands.py index 0e27836b05..d82573bda5 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -87,6 +87,17 @@ def test_acl_cat_with_category(self, r): assert isinstance(commands, list) assert "get" in commands + @skip_if_server_version_lt("7.0.0") + def test_acl_dryrun(self, r): + username = "redis-py-user" + r.acl_setuser( + username, + keys=["*"], + commands=["+set"], + ) + assert r.acl_dryrun(username, "set", "key", "value") == b"OK" + assert r.acl_dryrun(username, "get", "key").startswith(b"This user has no permissions to run the") + @skip_if_server_version_lt("6.0.0") @skip_if_redis_enterprise() def test_acl_deluser(self, r, request): From aa1cf04ea7e93abedfd3a33b818f8f64b92147c4 Mon Sep 17 00:00:00 2001 From: dogukanteber Date: Tue, 15 Feb 2022 23:35:43 +0300 Subject: [PATCH 2/4] Fix linter error --- tests/test_commands.py | 46 +++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index d82573bda5..73c0a180b3 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -96,7 +96,9 @@ def test_acl_dryrun(self, r): commands=["+set"], ) assert r.acl_dryrun(username, "set", "key", "value") == b"OK" - assert r.acl_dryrun(username, "get", "key").startswith(b"This user has no permissions to run the") + assert r.acl_dryrun(username, "get", "key").startswith( + b"This user has no permissions to run the" + ) @skip_if_server_version_lt("6.0.0") @skip_if_redis_enterprise() @@ -3068,18 +3070,15 @@ def test_geosearch_with(self, r): (2.19093829393386841, 41.43379028184083523), ] ] - assert ( - r.geosearch( - "barcelona", - longitude=2.191, - latitude=41.433, - radius=1, - unit="km", - withdist=True, - withcoord=True, - ) - == [[b"place1", 0.0881, (2.19093829393386841, 41.43379028184083523)]] - ) + assert r.geosearch( + "barcelona", + longitude=2.191, + latitude=41.433, + radius=1, + unit="km", + withdist=True, + withcoord=True, + ) == [[b"place1", 0.0881, (2.19093829393386841, 41.43379028184083523)]] assert r.geosearch( "barcelona", longitude=2.191, @@ -3409,7 +3408,7 @@ def test_xack(self, r): def test_xadd(self, r): stream = "stream" message_id = r.xadd(stream, {"foo": "bar"}) - assert re.match(br"[0-9]+\-[0-9]+", message_id) + assert re.match(rb"[0-9]+\-[0-9]+", message_id) # explicit message id message_id = b"9999999999999999999-0" @@ -3548,17 +3547,14 @@ def test_xclaim(self, r): # reclaim the message as consumer1, but use the justid argument # which only returns message ids - assert ( - r.xclaim( - stream, - group, - consumer1, - min_idle_time=0, - message_ids=(message_id,), - justid=True, - ) - == [message_id] - ) + assert r.xclaim( + stream, + group, + consumer1, + min_idle_time=0, + message_ids=(message_id,), + justid=True, + ) == [message_id] @skip_if_server_version_lt("5.0.0") def test_xclaim_trimmed(self, r): From 24d141d99ba3ba49792b9b0743d258514bde42e0 Mon Sep 17 00:00:00 2001 From: dogukanteber Date: Tue, 15 Feb 2022 23:38:07 +0300 Subject: [PATCH 3/4] Revert "Fix linter error" This reverts commit aa1cf04ea7e93abedfd3a33b818f8f64b92147c4. --- tests/test_commands.py | 46 +++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 73c0a180b3..d82573bda5 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -96,9 +96,7 @@ def test_acl_dryrun(self, r): commands=["+set"], ) assert r.acl_dryrun(username, "set", "key", "value") == b"OK" - assert r.acl_dryrun(username, "get", "key").startswith( - b"This user has no permissions to run the" - ) + assert r.acl_dryrun(username, "get", "key").startswith(b"This user has no permissions to run the") @skip_if_server_version_lt("6.0.0") @skip_if_redis_enterprise() @@ -3070,15 +3068,18 @@ def test_geosearch_with(self, r): (2.19093829393386841, 41.43379028184083523), ] ] - assert r.geosearch( - "barcelona", - longitude=2.191, - latitude=41.433, - radius=1, - unit="km", - withdist=True, - withcoord=True, - ) == [[b"place1", 0.0881, (2.19093829393386841, 41.43379028184083523)]] + assert ( + r.geosearch( + "barcelona", + longitude=2.191, + latitude=41.433, + radius=1, + unit="km", + withdist=True, + withcoord=True, + ) + == [[b"place1", 0.0881, (2.19093829393386841, 41.43379028184083523)]] + ) assert r.geosearch( "barcelona", longitude=2.191, @@ -3408,7 +3409,7 @@ def test_xack(self, r): def test_xadd(self, r): stream = "stream" message_id = r.xadd(stream, {"foo": "bar"}) - assert re.match(rb"[0-9]+\-[0-9]+", message_id) + assert re.match(br"[0-9]+\-[0-9]+", message_id) # explicit message id message_id = b"9999999999999999999-0" @@ -3547,14 +3548,17 @@ def test_xclaim(self, r): # reclaim the message as consumer1, but use the justid argument # which only returns message ids - assert r.xclaim( - stream, - group, - consumer1, - min_idle_time=0, - message_ids=(message_id,), - justid=True, - ) == [message_id] + assert ( + r.xclaim( + stream, + group, + consumer1, + min_idle_time=0, + message_ids=(message_id,), + justid=True, + ) + == [message_id] + ) @skip_if_server_version_lt("5.0.0") def test_xclaim_trimmed(self, r): From fe385e545bda2d4c00770bce9c39e271edba6919 Mon Sep 17 00:00:00 2001 From: dogukanteber Date: Tue, 15 Feb 2022 23:38:47 +0300 Subject: [PATCH 4/4] Fix linter error --- tests/test_commands.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index d82573bda5..ad9ee2c5fd 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -96,7 +96,9 @@ def test_acl_dryrun(self, r): commands=["+set"], ) assert r.acl_dryrun(username, "set", "key", "value") == b"OK" - assert r.acl_dryrun(username, "get", "key").startswith(b"This user has no permissions to run the") + assert r.acl_dryrun(username, "get", "key").startswith( + b"This user has no permissions to run the" + ) @skip_if_server_version_lt("6.0.0") @skip_if_redis_enterprise()