diff options
| author | shacharPash <shachar.pashchur@redis.com> | 2023-03-21 17:48:32 +0200 |
|---|---|---|
| committer | shacharPash <shachar.pashchur@redis.com> | 2023-03-21 17:48:32 +0200 |
| commit | a06b710eff650a17cabaff2b0abdb64859983a39 (patch) | |
| tree | 5a75e69230bb6fa5ed1ff174b19cf643c03e4a2d | |
| parent | 318b114f4da9846a2a7c150e1fb702e9bebd9fdf (diff) | |
| download | redis-py-a06b710eff650a17cabaff2b0abdb64859983a39.tar.gz | |
adding async tests - start with test_auth
| -rw-r--r-- | tests/test_asyncio/test_commands.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/test_asyncio/test_commands.py b/tests/test_asyncio/test_commands.py index 7c6fd45..f7070e2 100644 --- a/tests/test_asyncio/test_commands.py +++ b/tests/test_asyncio/test_commands.py @@ -13,6 +13,7 @@ import redis from redis import exceptions from redis.client import EMPTY_RESPONSE, NEVER_DECODE, parse_info from tests.conftest import ( + skip_if_redis_enterprise, skip_if_server_version_gte, skip_if_server_version_lt, skip_unless_arch_bits, @@ -82,6 +83,56 @@ class TestResponseCallbacks: class TestRedisCommands: + @skip_if_redis_enterprise() + async def test_auth(self, r, request): + # sending an AUTH command before setting a user/password on the + # server should return an AuthenticationError + with pytest.raises(exceptions.AuthenticationError): + await r.auth("some_password") + + with pytest.raises(exceptions.AuthenticationError): + await r.auth("some_password", "some_user") + + # first, test for default user (`username` is supposed to be optional) + default_username = "default" + temp_pass = "temp_pass" + await r.config_set("requirepass", temp_pass) + + assert await r.auth(temp_pass, default_username) is True + assert await r.auth(temp_pass) is True + + # test for other users + username = "redis-py-auth" + + async 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 + + await r.auth(temp_pass) + await r.config_set("requirepass", "") + await r.acl_deluser(username) + + request.addfinalizer(teardown) + + assert await r.acl_setuser( + username, enabled=True, passwords=["+strong_password"], commands=["+acl"] + ) + + assert await r.auth(username=username, password="strong_password") is True + + with pytest.raises(exceptions.AuthenticationError): + await r.auth(username=username, password="wrong_password") + async def test_command_on_invalid_key_type(self, r: redis.Redis): await r.lpush("a", "1") with pytest.raises(redis.ResponseError): |
