Skip to content

Commit a10e0c6

Browse files
dogukanteberchayimdvora-h
committed
Add support for AUTH (redis#1929)
* 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>
1 parent 4366719 commit a10e0c6

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

redis/cluster.py

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ class RedisCluster(RedisClusterCommands):
228228
"ACL SETUSER",
229229
"ACL USERS",
230230
"ACL WHOAMI",
231+
"AUTH",
231232
"CLIENT LIST",
232233
"CLIENT SETNAME",
233234
"CLIENT GETNAME",

redis/commands/core.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,16 @@ class ManagementCommands(CommandsProtocol):
369369
Redis management commands
370370
"""
371371

372-
def auth(self):
372+
def auth(self, password, username=None, **kwargs):
373373
"""
374-
This function throws a NotImplementedError since it is intentionally
375-
not supported.
374+
Authenticates the user. If you do not pass username, Redis will try to
375+
authenticate for the "default" user. If you do pass username, it will
376+
authenticate for the given user.
377+
For more information check https://redis.io/commands/auth
376378
"""
377-
raise NotImplementedError(
378-
"AUTH is intentionally not implemented in the client."
379-
)
379+
if username:
380+
return self.execute_command("AUTH", username, password, **kwargs)
381+
return self.execute_command
380382

381383
def bgrewriteaof(self, **kwargs):
382384
"""Tell the Redis server to rewrite the AOF file from data in memory.

tests/test_commands.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,32 @@ def test_case_insensitive_command_names(self, r):
6565

6666

6767
class TestRedisCommands:
68+
def test_auth(self, r, request):
69+
username = "redis-py-auth"
70+
71+
def teardown():
72+
r.acl_deluser(username)
73+
74+
request.addfinalizer(teardown)
75+
76+
assert r.acl_setuser(
77+
username,
78+
enabled=True,
79+
passwords=["+strong_password"],
80+
commands=["+acl"],
81+
)
82+
83+
assert r.auth(username=username, password="strong_password") is True
84+
85+
with pytest.raises(exceptions.ResponseError):
86+
r.auth(username=username, password="wrong_password")
87+
6888
def test_command_on_invalid_key_type(self, r):
6989
r.lpush("a", "1")
7090
with pytest.raises(redis.ResponseError):
7191
r["a"]
7292

7393
# SERVER INFORMATION
74-
def test_auth_not_implemented(self, r):
75-
with pytest.raises(NotImplementedError):
76-
r.auth()
77-
7894
@skip_if_server_version_lt("6.0.0")
7995
def test_acl_cat_no_category(self, r):
8096
categories = r.acl_cat()

0 commit comments

Comments
 (0)