From b6b7dcd61c28722f87f34bffefd84c0982b995ce Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 30 Aug 2019 06:59:19 +0200 Subject: [PATCH 1/4] Test NotImplemented is cought correctly --- freqtrade/tests/rpc/test_rpc_manager.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/freqtrade/tests/rpc/test_rpc_manager.py b/freqtrade/tests/rpc/test_rpc_manager.py index 468e3e8e5..d34d76524 100644 --- a/freqtrade/tests/rpc/test_rpc_manager.py +++ b/freqtrade/tests/rpc/test_rpc_manager.py @@ -115,6 +115,22 @@ def test_init_webhook_enabled(mocker, default_conf, caplog) -> None: assert 'webhook' in [mod.name for mod in rpc_manager.registered_modules] +def test_send_msg_webhook_CustomMessagetype(mocker, default_conf, caplog) -> None: + caplog.set_level(logging.DEBUG) + default_conf['telegram']['enabled'] = False + default_conf['webhook'] = {'enabled': True, 'url': "https://DEADBEEF.com"} + mocker.patch('freqtrade.rpc.webhook.Webhook.send_msg', + MagicMock(side_effect=NotImplementedError)) + rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf)) + + assert 'webhook' in [mod.name for mod in rpc_manager.registered_modules] + rpc_manager.send_msg({'type': RPCMessageType.CUSTOM_NOTIFICATION, + 'status': 'TestMessage'}) + assert log_has( + "Message type RPCMessageType.CUSTOM_NOTIFICATION not implemented by handler webhook.", + caplog) + + def test_startupmessages_telegram_enabled(mocker, default_conf, caplog) -> None: telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) From d977695d48594f7f7c10610ad9798398b2e9eae0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 30 Aug 2019 07:02:26 +0200 Subject: [PATCH 2/4] Catch NotImplementedError when sending messages (RPC should not crash your bot!) --- freqtrade/rpc/rpc_manager.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/freqtrade/rpc/rpc_manager.py b/freqtrade/rpc/rpc_manager.py index fad532aa0..d6e7b174d 100644 --- a/freqtrade/rpc/rpc_manager.py +++ b/freqtrade/rpc/rpc_manager.py @@ -56,7 +56,10 @@ class RPCManager(object): logger.info('Sending rpc message: %s', msg) for mod in self.registered_modules: logger.debug('Forwarding message to rpc.%s', mod.name) - mod.send_msg(msg) + try: + mod.send_msg(msg) + except NotImplementedError: + logger.error(f"Message type {msg['type']} not implemented by handler {mod.name}.") def startup_messages(self, config, pairlist) -> None: if config.get('dry_run', False): From 75dc174c76992ae7d762e2183acd798f111435be Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 30 Aug 2019 07:02:57 +0200 Subject: [PATCH 3/4] support all messagetypes in webhook --- freqtrade/rpc/webhook.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/freqtrade/rpc/webhook.py b/freqtrade/rpc/webhook.py index bfc82b8d6..37ca466de 100644 --- a/freqtrade/rpc/webhook.py +++ b/freqtrade/rpc/webhook.py @@ -43,7 +43,9 @@ class Webhook(RPC): valuedict = self._config['webhook'].get('webhookbuy', None) elif msg['type'] == RPCMessageType.SELL_NOTIFICATION: valuedict = self._config['webhook'].get('webhooksell', None) - elif msg['type'] == RPCMessageType.STATUS_NOTIFICATION: + elif msg['type'] in(RPCMessageType.STATUS_NOTIFICATION, + RPCMessageType.CUSTOM_NOTIFICATION, + RPCMessageType.WARNING_NOTIFICATION): valuedict = self._config['webhook'].get('webhookstatus', None) else: raise NotImplementedError('Unknown message type: {}'.format(msg['type'])) From d060d277457b26e1e7229e04ccb3d6a95ef6e90d Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 30 Aug 2019 07:05:22 +0200 Subject: [PATCH 4/4] Add test for all messagetypes --- freqtrade/tests/rpc/test_rpc_webhook.py | 33 ++++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/freqtrade/tests/rpc/test_rpc_webhook.py b/freqtrade/tests/rpc/test_rpc_webhook.py index cc491d4dd..1c6c07e16 100644 --- a/freqtrade/tests/rpc/test_rpc_webhook.py +++ b/freqtrade/tests/rpc/test_rpc_webhook.py @@ -91,21 +91,24 @@ def test_send_msg(default_conf, mocker): assert (msg_mock.call_args[0][0]["value3"] == default_conf["webhook"]["webhooksell"]["value3"].format(**msg)) - # Test notification - msg = { - 'type': RPCMessageType.STATUS_NOTIFICATION, - 'status': 'Unfilled sell order for BTC cancelled due to timeout' - } - msg_mock = MagicMock() - mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) - webhook.send_msg(msg) - assert msg_mock.call_count == 1 - assert (msg_mock.call_args[0][0]["value1"] == - default_conf["webhook"]["webhookstatus"]["value1"].format(**msg)) - assert (msg_mock.call_args[0][0]["value2"] == - default_conf["webhook"]["webhookstatus"]["value2"].format(**msg)) - assert (msg_mock.call_args[0][0]["value3"] == - default_conf["webhook"]["webhookstatus"]["value3"].format(**msg)) + for msgtype in [RPCMessageType.STATUS_NOTIFICATION, + RPCMessageType.WARNING_NOTIFICATION, + RPCMessageType.CUSTOM_NOTIFICATION]: + # Test notification + msg = { + 'type': msgtype, + 'status': 'Unfilled sell order for BTC cancelled due to timeout' + } + msg_mock = MagicMock() + mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) + webhook.send_msg(msg) + assert msg_mock.call_count == 1 + assert (msg_mock.call_args[0][0]["value1"] == + default_conf["webhook"]["webhookstatus"]["value1"].format(**msg)) + assert (msg_mock.call_args[0][0]["value2"] == + default_conf["webhook"]["webhookstatus"]["value2"].format(**msg)) + assert (msg_mock.call_args[0][0]["value3"] == + default_conf["webhook"]["webhookstatus"]["value3"].format(**msg)) def test_exception_send_msg(default_conf, mocker, caplog):