Skip to content

Commit 2ed7898

Browse files
committed
Separated cluster and standalone AUTH tests
1 parent f8060b7 commit 2ed7898

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

tests/test_cluster.py

+35-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from redis import Redis
9+
from redis import Redis, exceptions
1010
from redis.backoff import ExponentialBackoff, NoBackoff, default_backoff
1111
from redis.cluster import (
1212
PRIMARY,
@@ -43,6 +43,7 @@
4343
skip_unless_arch_bits,
4444
wait_for_command,
4545
)
46+
from .test_credentials import init_acl_user, init_required_pass
4647

4748
default_host = "127.0.0.1"
4849
default_port = 7000
@@ -813,8 +814,6 @@ def raise_connection_error():
813814
nodes = r.cluster_nodes()
814815
assert "myself" not in nodes.get(curr_default_node.name).get("flags")
815816
assert r.get_default_node() != curr_default_node
816-
# Rollback to the old default node
817-
r.replace_default_node(curr_default_node)
818817

819818

820819
@pytest.mark.onlycluster
@@ -2138,6 +2137,39 @@ def teardown():
21382137
assert "client-info" in r.acl_log(count=1, target_nodes=node)[0]
21392138
assert r.acl_log_reset(target_nodes=node)
21402139

2140+
@skip_if_redis_enterprise()
2141+
def test_auth_requirepass(self, request):
2142+
r = _get_client(RedisCluster, request, flushdb=False)
2143+
# Test for default user (`username` is supposed to be optional)
2144+
default_username = "default"
2145+
temp_pass = "temp_pass"
2146+
init_required_pass(r, request, temp_pass)
2147+
r2 = _get_client(
2148+
RedisCluster,
2149+
request,
2150+
flushdb=False,
2151+
username=default_username,
2152+
password=temp_pass,
2153+
)
2154+
assert r2.auth(temp_pass, default_username) is True
2155+
assert r2.auth(temp_pass) is True
2156+
2157+
@skip_if_redis_enterprise()
2158+
def test_auth_acl(self, request):
2159+
r = _get_client(RedisCluster, request, flushdb=False)
2160+
# test for ACL users
2161+
username = "redis-py-auth"
2162+
password = "strong_password"
2163+
2164+
init_acl_user(r, request, username, password)
2165+
r2 = _get_client(
2166+
RedisCluster, request, flushdb=False, username=username, password=password
2167+
)
2168+
assert r2.auth(username=username, password="strong_password") is True
2169+
2170+
with pytest.raises(exceptions.AuthenticationError):
2171+
r2.auth(username=username, password="wrong_password")
2172+
21412173

21422174
@pytest.mark.onlycluster
21432175
class TestNodesManager:

tests/test_commands.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def test_case_insensitive_command_names(self, r):
6666

6767

6868
class TestRedisCommands:
69+
@pytest.mark.onlynoncluster
6970
@skip_if_redis_enterprise()
7071
def test_auth(self, r, request):
7172
# sending an AUTH command before setting a user/password on the
@@ -88,19 +89,13 @@ def test_auth(self, r, request):
8889
username = "redis-py-auth"
8990

9091
def teardown():
91-
try:
92-
# this is needed because after an AuthenticationError the connection
93-
# is closed, and if we send an AUTH command a new connection is
94-
# created, but in this case we'd get an "Authentication required"
95-
# error when switching to the db 9 because we're not authenticated yet
96-
# setting the password on the connection itself triggers the
97-
# authentication in the connection's `on_connect` method
98-
r.connection.password = temp_pass
99-
except AttributeError:
100-
# connection field is not set in Redis Cluster, but that's ok
101-
# because the problem discussed above does not apply to Redis Cluster
102-
pass
103-
92+
# this is needed because after an AuthenticationError the connection
93+
# is closed, and if we send an AUTH command a new connection is
94+
# created, but in this case we'd get an "Authentication required"
95+
# error when switching to the db 9 because we're not authenticated yet
96+
# setting the password on the connection itself triggers the
97+
# authentication in the connection's `on_connect` method
98+
r.connection.password = temp_pass
10499
r.auth(temp_pass)
105100
r.config_set("requirepass", "")
106101
r.acl_deluser(username)

tests/test_credentials.py

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def init_acl_user(r, request, username, password):
5454
"+select",
5555
"+flushdb",
5656
"+cluster",
57+
"+acl",
5758
],
5859
)
5960
is True

0 commit comments

Comments
 (0)