Skip to content

Commit

Permalink
Add support for AUTH (redis#1929)
Browse files Browse the repository at this point in the history
* Add support for AUTH

* Fix linter error

* test fix

* fix test in cluster

Co-authored-by: Chayim <chayim@users.noreply.github.com>
Co-authored-by: Chayim I. Kirshen <c@kirshen.com>
Co-authored-by: dvora-h <dvora.heller@redis.com>
  • Loading branch information
4 people committed Mar 6, 2022
1 parent 4366719 commit a10e0c6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,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

0 comments on commit a10e0c6

Please sign in to comment.