Skip to content

Commit 0eac7c8

Browse files
committed
test: test for on_connect to handle AUTH failure
1 parent 560d82a commit 0eac7c8

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests/test_connection.py

+54
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,60 @@ def test_on_connect_fail_hello(self, mock_read_response, mock_send_command):
210210
mock_send_command.assert_called()
211211
mock_read_response.assert_called()
212212

213+
@patch.object(Connection, 'send_command')
214+
@patch.object(Connection, 'read_response')
215+
def test_on_connect_fail_auth(self, mock_read_response, mock_send_command):
216+
"""Test that on_connect handles connection failure AUTH command"""
217+
conn = Connection()
218+
219+
conn._parser = MagicMock()
220+
conn._parser.on_connect.return_value = None
221+
conn.credential_provider = None
222+
conn.username = "myuser"
223+
conn.password = "wrong-password"
224+
conn.protocol = 3
225+
conn.client_name = "test-client"
226+
conn.lib_name = "test"
227+
conn.lib_version = "1234"
228+
conn.db = 1
229+
conn.client_cache = True
230+
231+
# simulate a failure in the HELLO command response
232+
mock_read_response.side_effect = itertools.cycle([
233+
{"proto": 3, "version": "6"}, # HELLO
234+
b'QUEUED', # MULTI
235+
b'QUEUED', # AUTH
236+
b'QUEUED', # CLIENT SETNAME
237+
b'QUEUED', # CLIENT SETINFO LIB-NAME
238+
b'QUEUED', # CLIENT SETINFO LIB-VER
239+
b'QUEUED', # SELECT
240+
b'QUEUED', # CLIENT TRACKING ON
241+
[
242+
{"proto": 3, "version": "6"}, # HELLO response
243+
b'ERR invalid password', # AUTH response
244+
b'OK', # CLIENT SETNAME response
245+
b'OK', # CLIENT SETINFO LIB-NAME response
246+
b'OK', # CLIENT SETINFO LIB-VER response
247+
b'OK', # SELECT response
248+
b'OK' # CLIENT TRACKING ON response
249+
]
250+
])
251+
252+
with self.assertRaises(AuthenticationError):
253+
conn.on_connect()
254+
255+
mock_send_command.assert_any_call(
256+
'HELLO', 3, 'AUTH', 'myuser', 'wrong-password'),
257+
mock_send_command.assert_any_call('CLIENT', 'SETNAME', 'test-client'),
258+
mock_send_command.assert_any_call('CLIENT', 'SETINFO', 'LIB-NAME', 'test'),
259+
mock_send_command.assert_any_call('CLIENT', 'SETINFO', 'LIB-VER', '1234'),
260+
mock_send_command.assert_any_call('SELECT', 1),
261+
mock_send_command.assert_any_call('CLIENT', 'TRACKING', 'ON'),
262+
mock_send_command.assert_any_call('EXEC')
263+
264+
mock_send_command.assert_called()
265+
mock_read_response.assert_called()
266+
213267

214268

215269
@pytest.mark.onlynoncluster

0 commit comments

Comments
 (0)