Skip to content

Commit

Permalink
test: Test password-only AUTH for Redis versions below 6.0.0 without …
Browse files Browse the repository at this point in the history
…HELLO command
  • Loading branch information
zeze1004 committed Jun 6, 2024
1 parent 4f382a0 commit e365726
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e365726

Please sign in to comment.