Skip to content

Commit

Permalink
Separated cluster and standalone AUTH tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barshaul committed Nov 20, 2022
1 parent f8060b7 commit 2ed7898
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
38 changes: 35 additions & 3 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
21 changes: 8 additions & 13 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def init_acl_user(r, request, username, password):
"+select",
"+flushdb",
"+cluster",
"+acl",
],
)
is True
Expand Down

0 comments on commit 2ed7898

Please sign in to comment.