stable/tests/rpc/test_rpc_webhook.py

409 lines
15 KiB
Python
Raw Normal View History

2018-07-29 14:09:44 +00:00
# pragma pylint: disable=missing-docstring, C0103, protected-access
2018-07-05 19:33:01 +00:00
from unittest.mock import MagicMock
2018-07-04 19:42:17 +00:00
2018-07-05 19:33:01 +00:00
import pytest
from requests import RequestException
2021-06-09 17:51:44 +00:00
from freqtrade.enums import RPCMessageType, SellType
from freqtrade.rpc import RPC
2018-07-05 19:33:01 +00:00
from freqtrade.rpc.webhook import Webhook
2019-09-08 07:54:15 +00:00
from tests.conftest import get_patched_freqtradebot, log_has
2018-07-05 19:33:01 +00:00
def get_webhook_dict() -> dict:
return {
2020-02-08 20:02:52 +00:00
"enabled": True,
"url": "https://maker.ifttt.com/trigger/freqtrade_test/with/key/c764udvJ5jfSlswVRukZZ2/",
"webhookbuy": {
"value1": "Buying {pair}",
"value2": "limit {limit:8f}",
2021-12-29 13:24:12 +00:00
"value3": "{stake_amount:8f} {stake_currency}",
"value4": "leverage {leverage:.1f}",
"value5": "direction {direction}"
2020-02-08 20:02:52 +00:00
},
"webhookbuycancel": {
2020-02-11 14:58:40 +00:00
"value1": "Cancelling Open Buy Order for {pair}",
2020-02-08 20:02:52 +00:00
"value2": "limit {limit:8f}",
2021-12-29 13:24:12 +00:00
"value3": "{stake_amount:8f} {stake_currency}",
"value4": "leverage {leverage:.1f}",
"value5": "direction {direction}"
2020-02-08 20:02:52 +00:00
},
2021-04-19 17:53:16 +00:00
"webhookbuyfill": {
"value1": "Buy Order for {pair} filled",
"value2": "at {open_rate:8f}",
2021-12-29 13:24:12 +00:00
"value3": "{stake_amount:8f} {stake_currency}",
"value4": "leverage {leverage:.1f}",
"value5": "direction {direction}"
2021-04-19 17:53:16 +00:00
},
2020-02-08 20:02:52 +00:00
"webhooksell": {
"value1": "Selling {pair}",
"value2": "limit {limit:8f}",
2020-03-05 14:44:21 +00:00
"value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})"
2020-02-08 20:02:52 +00:00
},
"webhooksellcancel": {
2020-02-11 14:58:40 +00:00
"value1": "Cancelling Open Sell Order for {pair}",
2020-02-08 20:02:52 +00:00
"value2": "limit {limit:8f}",
2020-03-05 14:44:21 +00:00
"value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})"
2020-02-08 20:02:52 +00:00
},
2021-04-19 17:53:16 +00:00
"webhooksellfill": {
"value1": "Sell Order for {pair} filled",
"value2": "at {close_rate:8f}",
"value3": ""
2021-04-19 17:53:16 +00:00
},
2020-02-08 20:02:52 +00:00
"webhookstatus": {
"value1": "Status: {status}",
"value2": "",
"value3": ""
}
}
2018-07-04 19:42:17 +00:00
2018-07-05 19:33:01 +00:00
def test__init__(mocker, default_conf):
2018-07-14 11:29:50 +00:00
default_conf['webhook'] = {'enabled': True, 'url': "https://DEADBEEF.com"}
webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf)
2018-07-05 19:33:01 +00:00
assert webhook._config == default_conf
2018-07-04 19:42:17 +00:00
2018-07-05 19:33:01 +00:00
def test_send_msg_webhook(default_conf, mocker):
2018-07-05 19:33:01 +00:00
default_conf["webhook"] = get_webhook_dict()
msg_mock = MagicMock()
mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock)
webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf)
# Test buy
msg_mock = MagicMock()
mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock)
2018-07-05 19:33:01 +00:00
msg = {
2021-04-20 04:41:58 +00:00
'type': RPCMessageType.BUY,
2021-04-20 10:54:22 +00:00
'exchange': 'Binance',
2018-07-05 19:33:01 +00:00
'pair': 'ETH/BTC',
2021-12-29 13:24:12 +00:00
'leverage': 1.0,
'direction': 'Long',
2018-07-05 19:33:01 +00:00
'limit': 0.005,
'stake_amount': 0.8,
'stake_amount_fiat': 500,
'stake_currency': 'BTC',
'fiat_currency': 'EUR'
}
webhook.send_msg(msg=msg)
assert msg_mock.call_count == 1
assert (msg_mock.call_args[0][0]["value1"] ==
default_conf["webhook"]["webhookbuy"]["value1"].format(**msg))
assert (msg_mock.call_args[0][0]["value2"] ==
default_conf["webhook"]["webhookbuy"]["value2"].format(**msg))
assert (msg_mock.call_args[0][0]["value3"] ==
default_conf["webhook"]["webhookbuy"]["value3"].format(**msg))
2021-12-29 13:24:12 +00:00
assert (msg_mock.call_args[0][0]["value4"] ==
default_conf["webhook"]["webhookbuy"]["value4"].format(**msg))
assert (msg_mock.call_args[0][0]["value5"] ==
default_conf["webhook"]["webhookbuy"]["value5"].format(**msg))
# Test short
msg_mock.reset_mock()
msg = {
'type': RPCMessageType.SHORT,
'exchange': 'Binance',
'pair': 'ETH/BTC',
'leverage': 2.0,
'direction': 'Short',
'limit': 0.005,
'stake_amount': 0.8,
'stake_amount_fiat': 500,
'stake_currency': 'BTC',
'fiat_currency': 'EUR'
}
webhook.send_msg(msg=msg)
assert msg_mock.call_count == 1
assert (msg_mock.call_args[0][0]["value1"] ==
default_conf["webhook"]["webhookbuy"]["value1"].format(**msg))
assert (msg_mock.call_args[0][0]["value2"] ==
default_conf["webhook"]["webhookbuy"]["value2"].format(**msg))
assert (msg_mock.call_args[0][0]["value3"] ==
default_conf["webhook"]["webhookbuy"]["value3"].format(**msg))
assert (msg_mock.call_args[0][0]["value4"] ==
default_conf["webhook"]["webhookbuy"]["value4"].format(**msg))
assert (msg_mock.call_args[0][0]["value5"] ==
default_conf["webhook"]["webhookbuy"]["value5"].format(**msg))
# Test buy cancel
2021-04-19 17:53:16 +00:00
msg_mock.reset_mock()
msg = {
2021-04-20 04:41:58 +00:00
'type': RPCMessageType.BUY_CANCEL,
2021-04-20 10:54:22 +00:00
'exchange': 'Binance',
'pair': 'ETH/BTC',
2021-12-29 13:24:12 +00:00
'leverage': 1.0,
'direction': 'Long',
'limit': 0.005,
'stake_amount': 0.8,
'stake_amount_fiat': 500,
'stake_currency': 'BTC',
'fiat_currency': 'EUR'
}
webhook.send_msg(msg=msg)
assert msg_mock.call_count == 1
assert (msg_mock.call_args[0][0]["value1"] ==
default_conf["webhook"]["webhookbuycancel"]["value1"].format(**msg))
assert (msg_mock.call_args[0][0]["value2"] ==
default_conf["webhook"]["webhookbuycancel"]["value2"].format(**msg))
assert (msg_mock.call_args[0][0]["value3"] ==
default_conf["webhook"]["webhookbuycancel"]["value3"].format(**msg))
# Test short cancel
msg_mock.reset_mock()
msg = {
'type': RPCMessageType.SHORT_CANCEL,
'exchange': 'Binance',
'pair': 'ETH/BTC',
'leverage': 2.0,
'direction': 'Short',
'limit': 0.005,
'stake_amount': 0.8,
'stake_amount_fiat': 500,
'stake_currency': 'BTC',
'fiat_currency': 'EUR'
}
webhook.send_msg(msg=msg)
assert msg_mock.call_count == 1
assert (msg_mock.call_args[0][0]["value1"] ==
default_conf["webhook"]["webhookbuycancel"]["value1"].format(**msg))
assert (msg_mock.call_args[0][0]["value2"] ==
default_conf["webhook"]["webhookbuycancel"]["value2"].format(**msg))
assert (msg_mock.call_args[0][0]["value3"] ==
default_conf["webhook"]["webhookbuycancel"]["value3"].format(**msg))
2021-12-29 13:24:12 +00:00
assert (msg_mock.call_args[0][0]["value4"] ==
default_conf["webhook"]["webhookbuycancel"]["value4"].format(**msg))
assert (msg_mock.call_args[0][0]["value5"] ==
default_conf["webhook"]["webhookbuycancel"]["value5"].format(**msg))
# Test buy fill
msg_mock.reset_mock()
msg = {
2021-04-20 04:41:58 +00:00
'type': RPCMessageType.BUY_FILL,
'exchange': 'Binance',
'pair': 'ETH/BTC',
2021-12-29 13:24:12 +00:00
'leverage': 1.0,
'direction': 'Long',
'open_rate': 0.005,
'stake_amount': 0.8,
'stake_amount_fiat': 500,
'stake_currency': 'BTC',
'fiat_currency': 'EUR'
}
webhook.send_msg(msg=msg)
assert msg_mock.call_count == 1
assert (msg_mock.call_args[0][0]["value1"] ==
default_conf["webhook"]["webhookbuyfill"]["value1"].format(**msg))
assert (msg_mock.call_args[0][0]["value2"] ==
default_conf["webhook"]["webhookbuyfill"]["value2"].format(**msg))
assert (msg_mock.call_args[0][0]["value3"] ==
default_conf["webhook"]["webhookbuyfill"]["value3"].format(**msg))
2021-12-29 13:24:12 +00:00
assert (msg_mock.call_args[0][0]["value4"] ==
default_conf["webhook"]["webhookbuycancel"]["value4"].format(**msg))
assert (msg_mock.call_args[0][0]["value5"] ==
default_conf["webhook"]["webhookbuycancel"]["value5"].format(**msg))
# Test short fill
msg_mock.reset_mock()
msg = {
'type': RPCMessageType.SHORT_FILL,
'exchange': 'Binance',
'pair': 'ETH/BTC',
'leverage': 2.0,
'direction': 'Short',
'open_rate': 0.005,
'stake_amount': 0.8,
'stake_amount_fiat': 500,
'stake_currency': 'BTC',
'fiat_currency': 'EUR'
}
webhook.send_msg(msg=msg)
assert msg_mock.call_count == 1
assert (msg_mock.call_args[0][0]["value1"] ==
default_conf["webhook"]["webhookbuyfill"]["value1"].format(**msg))
assert (msg_mock.call_args[0][0]["value2"] ==
default_conf["webhook"]["webhookbuyfill"]["value2"].format(**msg))
assert (msg_mock.call_args[0][0]["value3"] ==
default_conf["webhook"]["webhookbuyfill"]["value3"].format(**msg))
assert (msg_mock.call_args[0][0]["value4"] ==
default_conf["webhook"]["webhookbuycancel"]["value4"].format(**msg))
assert (msg_mock.call_args[0][0]["value5"] ==
default_conf["webhook"]["webhookbuycancel"]["value5"].format(**msg))
2018-07-05 19:33:01 +00:00
# Test sell
2021-04-19 17:53:16 +00:00
msg_mock.reset_mock()
2021-12-29 13:24:12 +00:00
2018-07-05 19:33:01 +00:00
msg = {
2021-04-20 04:41:58 +00:00
'type': RPCMessageType.SELL,
2021-04-20 10:54:22 +00:00
'exchange': 'Binance',
2018-07-05 19:33:01 +00:00
'pair': 'ETH/BTC',
'gain': "profit",
'limit': 0.005,
'amount': 0.8,
2019-06-17 04:55:54 +00:00
'order_type': 'limit',
2018-07-05 19:33:01 +00:00
'open_rate': 0.004,
'current_rate': 0.005,
'profit_amount': 0.001,
2020-03-05 14:44:21 +00:00
'profit_ratio': 0.20,
2018-07-05 19:33:01 +00:00
'stake_currency': 'BTC',
'sell_reason': SellType.STOP_LOSS.value
2018-07-05 19:33:01 +00:00
}
webhook.send_msg(msg=msg)
assert msg_mock.call_count == 1
assert (msg_mock.call_args[0][0]["value1"] ==
default_conf["webhook"]["webhooksell"]["value1"].format(**msg))
assert (msg_mock.call_args[0][0]["value2"] ==
default_conf["webhook"]["webhooksell"]["value2"].format(**msg))
assert (msg_mock.call_args[0][0]["value3"] ==
default_conf["webhook"]["webhooksell"]["value3"].format(**msg))
# Test sell cancel
2021-04-19 17:53:16 +00:00
msg_mock.reset_mock()
msg = {
2021-04-20 04:41:58 +00:00
'type': RPCMessageType.SELL_CANCEL,
2021-04-20 10:54:22 +00:00
'exchange': 'Binance',
'pair': 'ETH/BTC',
'gain': "profit",
'limit': 0.005,
'amount': 0.8,
'order_type': 'limit',
'open_rate': 0.004,
'current_rate': 0.005,
'profit_amount': 0.001,
2020-03-05 14:44:21 +00:00
'profit_ratio': 0.20,
'stake_currency': 'BTC',
'sell_reason': SellType.STOP_LOSS.value
}
webhook.send_msg(msg=msg)
assert msg_mock.call_count == 1
assert (msg_mock.call_args[0][0]["value1"] ==
default_conf["webhook"]["webhooksellcancel"]["value1"].format(**msg))
assert (msg_mock.call_args[0][0]["value2"] ==
default_conf["webhook"]["webhooksellcancel"]["value2"].format(**msg))
assert (msg_mock.call_args[0][0]["value3"] ==
default_conf["webhook"]["webhooksellcancel"]["value3"].format(**msg))
# Test Sell fill
msg_mock.reset_mock()
msg = {
2021-04-20 04:41:58 +00:00
'type': RPCMessageType.SELL_FILL,
'exchange': 'Binance',
'pair': 'ETH/BTC',
'gain': "profit",
'close_rate': 0.005,
'amount': 0.8,
'order_type': 'limit',
'open_rate': 0.004,
'current_rate': 0.005,
'profit_amount': 0.001,
'profit_ratio': 0.20,
'stake_currency': 'BTC',
'sell_reason': SellType.STOP_LOSS.value
}
webhook.send_msg(msg=msg)
assert msg_mock.call_count == 1
assert (msg_mock.call_args[0][0]["value1"] ==
default_conf["webhook"]["webhooksellfill"]["value1"].format(**msg))
assert (msg_mock.call_args[0][0]["value2"] ==
default_conf["webhook"]["webhooksellfill"]["value2"].format(**msg))
assert (msg_mock.call_args[0][0]["value3"] ==
default_conf["webhook"]["webhooksellfill"]["value3"].format(**msg))
2021-04-20 04:41:58 +00:00
for msgtype in [RPCMessageType.STATUS,
RPCMessageType.WARNING,
RPCMessageType.STARTUP]:
2019-08-30 05:05:22 +00:00
# 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))
2018-07-05 19:33:01 +00:00
def test_exception_send_msg(default_conf, mocker, caplog):
default_conf["webhook"] = get_webhook_dict()
2019-11-23 14:20:53 +00:00
del default_conf["webhook"]["webhookbuy"]
2018-07-05 19:33:01 +00:00
webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf)
2021-04-20 04:41:58 +00:00
webhook.send_msg({'type': RPCMessageType.BUY})
assert log_has(f"Message type '{RPCMessageType.BUY}' not configured for webhooks",
2019-08-11 18:17:22 +00:00
caplog)
2018-07-05 19:33:01 +00:00
default_conf["webhook"] = get_webhook_dict()
default_conf["webhook"]["webhookbuy"]["value1"] = "{DEADBEEF:8f}"
msg_mock = MagicMock()
mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock)
webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf)
2018-07-05 19:33:01 +00:00
msg = {
2021-04-20 04:41:58 +00:00
'type': RPCMessageType.BUY,
2021-04-20 10:54:22 +00:00
'exchange': 'Binance',
2018-07-05 19:33:01 +00:00
'pair': 'ETH/BTC',
'limit': 0.005,
2019-06-17 04:55:30 +00:00
'order_type': 'limit',
2018-07-05 19:33:01 +00:00
'stake_amount': 0.8,
'stake_amount_fiat': 500,
'stake_currency': 'BTC',
'fiat_currency': 'EUR'
}
webhook.send_msg(msg)
assert log_has("Problem calling Webhook. Please check your webhook configuration. "
2019-08-11 18:17:22 +00:00
"Exception: 'DEADBEEF'", caplog)
2018-07-05 19:33:01 +00:00
msg_mock = MagicMock()
mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock)
msg = {
'type': 'DEADBEEF',
'status': 'whatever'
}
with pytest.raises(NotImplementedError):
webhook.send_msg(msg)
def test__send_msg(default_conf, mocker, caplog):
default_conf["webhook"] = get_webhook_dict()
webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf)
2018-07-05 19:33:01 +00:00
msg = {'value1': 'DEADBEEF',
'value2': 'ALIVEBEEF',
'value3': 'FREQTRADE'}
post = MagicMock()
mocker.patch("freqtrade.rpc.webhook.post", post)
webhook._send_msg(msg)
assert post.call_count == 1
assert post.call_args[1] == {'data': msg}
assert post.call_args[0] == (default_conf['webhook']['url'], )
post = MagicMock(side_effect=RequestException)
mocker.patch("freqtrade.rpc.webhook.post", post)
webhook._send_msg(msg)
2019-08-11 18:17:22 +00:00
assert log_has('Could not call webhook url. Exception: ', caplog)
2021-02-26 15:12:10 +00:00
2021-02-26 15:12:10 +00:00
def test__send_msg_with_json_format(default_conf, mocker, caplog):
default_conf["webhook"] = get_webhook_dict()
default_conf["webhook"]["format"] = "json"
webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf)
msg = {'text': 'Hello'}
post = MagicMock()
mocker.patch("freqtrade.rpc.webhook.post", post)
webhook._send_msg(msg)
assert post.call_args[1] == {'json': msg}
2021-11-29 18:54:54 +00:00
def test__send_msg_with_raw_format(default_conf, mocker, caplog):
default_conf["webhook"] = get_webhook_dict()
default_conf["webhook"]["format"] = "raw"
webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf)
msg = {'data': 'Hello'}
post = MagicMock()
mocker.patch("freqtrade.rpc.webhook.post", post)
webhook._send_msg(msg)
assert post.call_args[1] == {'data': msg['data'], 'headers': {'Content-Type': 'text/plain'}}