From e365726b98ae0293bc0769dd917f3f860c7ef0f6 Mon Sep 17 00:00:00 2001 From: "sojung.kim" Date: Fri, 7 Jun 2024 00:30:17 +0900 Subject: [PATCH] test: Test password-only AUTH for Redis versions below 6.0.0 without HELLO command --- tests/test_connection.py | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/test_connection.py b/tests/test_connection.py index 473d184930..a724c2e5b9 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -264,6 +264,58 @@ def test_on_connect_fail_auth(self, mock_read_response, mock_send_command): mock_send_command.assert_called() mock_read_response.assert_called() + @patch.object(Connection, 'send_command') + @patch.object(Connection, 'read_response') + def test_on_connect_auth_with_password_only( + self, mock_read_response, mock_send_command): + """Test on_connect handling of password-only AUTH for Redis versions below 6.0.0 without HELLO command""" + conn = Connection() + + conn._parser = MagicMock() + conn._parser.on_connect.return_value = None + conn.credential_provider = None + conn.username = None + conn.password = "password" + conn.protocol = 1 + conn.client_name = "test-client" + conn.lib_name = "test" + conn.lib_version = "1234" + conn.db = 1 + conn.client_cache = True + + # command response to simulate Redis < 6.0.0 behavior + mock_read_response.side_effect = itertools.cycle([ + Exception("ERR HELLO"), # HELLO (fails) + b'QUEUED', # MULTI + b'QUEUED', # AUTH + b'QUEUED', # CLIENT SETNAME + b'QUEUED', # CLIENT SETINFO LIB-NAME + b'QUEUED', # CLIENT SETINFO LIB-VER + b'QUEUED', # SELECT + b'QUEUED', # CLIENT TRACKING ON + [ + b'OK', # AUTH response + b'OK', # CLIENT SETNAME response + b'OK', # CLIENT SETINFO LIB-NAME response + b'OK', # CLIENT SETINFO LIB-VER response + b'OK', # SELECT response + b'OK' # CLIENT TRACKING ON response + ] + ]) + + conn.on_connect() + + mock_send_command.assert_any_call('HELLO', 1, 'AUTH', 'default', 'password'), + mock_send_command.assert_any_call('MULTI'), + mock_send_command.assert_any_call( + 'AUTH', 'default', 'password', check_health=False) + mock_send_command.assert_any_call('CLIENT', 'SETNAME', 'test-client') + mock_send_command.assert_any_call('CLIENT', 'SETINFO', 'LIB-NAME', 'test') + mock_send_command.assert_any_call('CLIENT', 'SETINFO', 'LIB-VER', '1234') + mock_send_command.assert_any_call('SELECT', 1) + mock_send_command.assert_any_call('CLIENT', 'TRACKING', 'ON') + mock_send_command.assert_any_call('EXEC') + mock_read_response.assert_called() @pytest.mark.onlynoncluster