Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AUTH #1929

Merged
merged 6 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class RedisCluster(RedisClusterCommands):
"ACL SETUSER",
"ACL USERS",
"ACL WHOAMI",
"AUTH",
"CLIENT LIST",
"CLIENT SETNAME",
"CLIENT GETNAME",
Expand Down
14 changes: 8 additions & 6 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,16 @@ class ManagementCommands(CommandsProtocol):
Redis management commands
"""

def auth(self):
def auth(self, password, username=None, **kwargs):
"""
This function throws a NotImplementedError since it is intentionally
not supported.
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
"""
raise NotImplementedError(
"AUTH is intentionally not implemented in the client."
)
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.
Expand Down
24 changes: 20 additions & 4 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,32 @@ 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") is 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):
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()
Expand Down