diff --git a/tests/test_cluster.py b/tests/test_cluster.py index 4cc2f5f103..bed3c7386b 100644 --- a/tests/test_cluster.py +++ b/tests/test_cluster.py @@ -6,7 +6,7 @@ import pytest -from redis import Redis +from redis import Redis, exceptions from redis.backoff import ExponentialBackoff, NoBackoff, default_backoff from redis.cluster import ( PRIMARY, @@ -43,6 +43,7 @@ skip_unless_arch_bits, wait_for_command, ) +from .test_credentials import init_acl_user, init_required_pass default_host = "127.0.0.1" default_port = 7000 @@ -813,8 +814,6 @@ def raise_connection_error(): nodes = r.cluster_nodes() assert "myself" not in nodes.get(curr_default_node.name).get("flags") assert r.get_default_node() != curr_default_node - # Rollback to the old default node - r.replace_default_node(curr_default_node) @pytest.mark.onlycluster @@ -2138,6 +2137,39 @@ def teardown(): assert "client-info" in r.acl_log(count=1, target_nodes=node)[0] assert r.acl_log_reset(target_nodes=node) + @skip_if_redis_enterprise() + def test_auth_requirepass(self, request): + r = _get_client(RedisCluster, request, flushdb=False) + # Test for default user (`username` is supposed to be optional) + default_username = "default" + temp_pass = "temp_pass" + init_required_pass(r, request, temp_pass) + r2 = _get_client( + RedisCluster, + request, + flushdb=False, + username=default_username, + password=temp_pass, + ) + assert r2.auth(temp_pass, default_username) is True + assert r2.auth(temp_pass) is True + + @skip_if_redis_enterprise() + def test_auth_acl(self, request): + r = _get_client(RedisCluster, request, flushdb=False) + # test for ACL users + username = "redis-py-auth" + password = "strong_password" + + init_acl_user(r, request, username, password) + r2 = _get_client( + RedisCluster, request, flushdb=False, username=username, password=password + ) + assert r2.auth(username=username, password="strong_password") is True + + with pytest.raises(exceptions.AuthenticationError): + r2.auth(username=username, password="wrong_password") + @pytest.mark.onlycluster class TestNodesManager: diff --git a/tests/test_commands.py b/tests/test_commands.py index aa6745b34b..b871040d24 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -66,6 +66,7 @@ def test_case_insensitive_command_names(self, r): class TestRedisCommands: + @pytest.mark.onlynoncluster @skip_if_redis_enterprise() def test_auth(self, r, request): # sending an AUTH command before setting a user/password on the @@ -88,19 +89,13 @@ def test_auth(self, r, request): username = "redis-py-auth" def teardown(): - try: - # this is needed because after an AuthenticationError the connection - # is closed, and if we send an AUTH command a new connection is - # created, but in this case we'd get an "Authentication required" - # error when switching to the db 9 because we're not authenticated yet - # setting the password on the connection itself triggers the - # authentication in the connection's `on_connect` method - r.connection.password = temp_pass - except AttributeError: - # connection field is not set in Redis Cluster, but that's ok - # because the problem discussed above does not apply to Redis Cluster - pass - + # this is needed because after an AuthenticationError the connection + # is closed, and if we send an AUTH command a new connection is + # created, but in this case we'd get an "Authentication required" + # error when switching to the db 9 because we're not authenticated yet + # setting the password on the connection itself triggers the + # authentication in the connection's `on_connect` method + r.connection.password = temp_pass r.auth(temp_pass) r.config_set("requirepass", "") r.acl_deluser(username) diff --git a/tests/test_credentials.py b/tests/test_credentials.py index 9aeb1ef1d5..d4ab0f165f 100644 --- a/tests/test_credentials.py +++ b/tests/test_credentials.py @@ -54,6 +54,7 @@ def init_acl_user(r, request, username, password): "+select", "+flushdb", "+cluster", + "+acl", ], ) is True