def print_and_validate_reply(reply_msg, expected_code): """Prints and validates `reply_msg` based on `expected_code`. Parameters ---------- reply_msg: str The message being validated. The first 3 characters of the message are a code, representing the server's response. expected_code: int The code expected to be found in reply message. Raises ------ UnexpectedResponseCode If `reply_msg` is a message with a code other than `expected_code`. Returns ------- None """ print(reply_msg) actual_code = int(reply_msg[:3]) if actual_code != expected_code: raise exc.UnexpectedResponseCode(actual_code, expected_code)
def test_send_message_wait_for_reply_exception(mock_mail_client, mock_client_socket): mock_client_socket.send = MagicMock(side_effect=None) mock_mail_client.receive_and_validate_reply = MagicMock( side_effect=exc.UnexpectedResponseCode(530, 250)) with pytest.raises(exc.UnexpectedResponseCode): mock_mail_client.send_message_wait_for_reply("HELO Matt", 1024, 250) mock_client_socket.send.assert_called_once_with(b'HELO Matt\r\n') mock_mail_client.receive_and_validate_reply.assert_called_once_with( 1024, 250)
def test_connect_and_validate_connection_failure(mock_mail_client, mock_client_socket): mock_mail_client.connect = MagicMock(side_effect=None) mock_mail_client.receive_and_validate_reply = MagicMock( side_effect=exc.UnexpectedResponseCode(530, 220)) mock_mail_client._is_connected = True mock_mail_client.connect_and_validate_connection() mock_mail_client.connect.assert_called_once() mock_mail_client.receive_and_validate_reply.assert_called_once_with( 1024, 220) assert not mock_mail_client.is_connected()
def test_authenticate_with_error(mock_mail_client): mock_mail_client.send_message_wait_for_reply = MagicMock( side_effect=exc.UnexpectedResponseCode(535, 235)) with patch(PASS_STR, return_value="Password2") as mock_pass, \ patch(BASE64_STR, return_value=b'x17hfdT11') as mock_base: with pytest.raises(exc.UnexpectedResponseCode): mock_mail_client.authenticate() mock_pass.assert_called_once() mock_base.assert_called_once_with( b'\[email protected]\x00Password2') mock_mail_client.send_message_wait_for_reply.assert_called_once_with( "AUTH PLAIN x17hfdT11", 1024, 235)
def test_unexpected_response_code(): with pytest.raises(exc.UnexpectedResponseCode) as resp_exc: raise exc.UnexpectedResponseCode(404, 200) assert (str(resp_exc.value) == "200 reply not received from the server. " "404 was returned instead.")
assert mock_mail_client.is_connected() mock_mail_client.disconnect() mock_client_socket.close.assert_called_once() assert not mock_mail_client.is_connected() def test_receive_and_validate_no_exception(mock_mail_client, mock_client_socket): mock_client_socket.recv = MagicMock(return_value=b'250 Hello Matt.') with patch(VALIDATE_STR, side_effect=None) as mock_validate: mock_mail_client.receive_and_validate_reply(1024, 250) mock_client_socket.recv.assert_called_once_with(1024) mock_validate.assert_called_once_with('250 Hello Matt.', 250) @patch(VALIDATE_STR, side_effect=exc.UnexpectedResponseCode(530, 250)) def test_receive_and_validate_with_exception(mock_validate, mock_mail_client, mock_client_socket): mock_client_socket.recv = MagicMock( return_value=b'530 Could not authenticate.') with pytest.raises(exc.UnexpectedResponseCode): mock_mail_client.receive_and_validate_reply(2048, 250) mock_client_socket.recv.assert_called_once_with(2048) mock_validate.assert_called_once_with('530 Could not authenticate.', 250) def test_connect_and_validate_connection_success(mock_mail_client, mock_client_socket): mock_mail_client.connect = MagicMock(side_effect=None) mock_mail_client.receive_and_validate_reply = MagicMock(side_effect=None) mock_mail_client._is_connected = True