| 1 | # pragma pylint: disable=missing-docstring, C0103, protected-access |
| 2 | |
| 3 | from unittest.mock import MagicMock |
| 4 | |
| 5 | import pytest |
| 6 | from requests import RequestException |
| 7 | |
| 8 | from freqtrade.rpc import RPC, RPCMessageType |
| 9 | from freqtrade.rpc.webhook import Webhook |
| 10 | from freqtrade.strategy.interface import SellType |
| 11 | from tests.conftest import get_patched_freqtradebot, log_has |
| 12 | |
| 13 | |
| 14 | def get_webhook_dict() -> dict: |
| 15 | return { |
| 16 | "enabled": True, |
| 17 | "url": "https://maker.ifttt.com/trigger/freqtrade_test/with/key/c764udvJ5jfSlswVRukZZ2/", |
| 18 | "webhookbuy": { |
| 19 | "value1": "Buying {pair}", |
| 20 | "value2": "limit {limit:8f}", |
| 21 | "value3": "{stake_amount:8f} {stake_currency}" |
| 22 | }, |
| 23 | "webhookbuycancel": { |
| 24 | "value1": "Cancelling Open Buy Order for {pair}", |
| 25 | "value2": "limit {limit:8f}", |
| 26 | "value3": "{stake_amount:8f} {stake_currency}" |
| 27 | }, |
| 28 | "webhookbuyfill": { |
| 29 | "value1": "Buy Order for {pair} filled", |
| 30 | "value2": "at {open_rate:8f}", |
| 31 | "value3": "{stake_amount:8f} {stake_currency}" |
| 32 | }, |
| 33 | "webhooksell": { |
| 34 | "value1": "Selling {pair}", |
| 35 | "value2": "limit {limit:8f}", |
| 36 | "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})" |
| 37 | }, |
| 38 | "webhooksellcancel": { |
| 39 | "value1": "Cancelling Open Sell Order for {pair}", |
| 40 | "value2": "limit {limit:8f}", |
| 41 | "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})" |
| 42 | }, |
| 43 | "webhooksellfill": { |
| 44 | "value1": "Sell Order for {pair} filled", |
| 45 | "value2": "at {close_rate:8f}", |
| 46 | "value3": "" |
| 47 | }, |
| 48 | "webhookstatus": { |
| 49 | "value1": "Status: {status}", |
| 50 | "value2": "", |
| 51 | "value3": "" |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | def test__init__(mocker, default_conf): |
| 57 | default_conf['webhook'] = {'enabled': True, 'url': "https://DEADBEEF.com"} |
| 58 | webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf) |
| 59 | assert webhook._config == default_conf |
| 60 | |
| 61 | |
| 62 | def test_send_msg_webhook(default_conf, mocker): |
| 63 | default_conf["webhook"] = get_webhook_dict() |
| 64 | msg_mock = MagicMock() |
| 65 | mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) |
| 66 | webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf) |
| 67 | # Test buy |
| 68 | msg_mock = MagicMock() |
| 69 | mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) |
| 70 | msg = { |
| 71 | 'type': RPCMessageType.BUY, |
| 72 | 'exchange': 'Binance', |
| 73 | 'pair': 'ETH/BTC', |
| 74 | 'limit': 0.005, |
| 75 | 'stake_amount': 0.8, |
| 76 | 'stake_amount_fiat': 500, |
| 77 | 'stake_currency': 'BTC', |
| 78 | 'fiat_currency': 'EUR' |
| 79 | } |
| 80 | webhook.send_msg(msg=msg) |
| 81 | assert msg_mock.call_count == 1 |
| 82 | assert (msg_mock.call_args[0][0]["value1"] == |
| 83 | default_conf["webhook"]["webhookbuy"]["value1"].format(**msg)) |
| 84 | assert (msg_mock.call_args[0][0]["value2"] == |
| 85 | default_conf["webhook"]["webhookbuy"]["value2"].format(**msg)) |
| 86 | assert (msg_mock.call_args[0][0]["value3"] == |
| 87 | default_conf["webhook"]["webhookbuy"]["value3"].format(**msg)) |
| 88 | # Test buy cancel |
| 89 | msg_mock.reset_mock() |
| 90 | |
| 91 | msg = { |
| 92 | 'type': RPCMessageType.BUY_CANCEL, |
| 93 | 'exchange': 'Binance', |
| 94 | 'pair': 'ETH/BTC', |
| 95 | 'limit': 0.005, |
| 96 | 'stake_amount': 0.8, |
| 97 | 'stake_amount_fiat': 500, |
| 98 | 'stake_currency': 'BTC', |
| 99 | 'fiat_currency': 'EUR' |
| 100 | } |
| 101 | webhook.send_msg(msg=msg) |
| 102 | assert msg_mock.call_count == 1 |
| 103 | assert (msg_mock.call_args[0][0]["value1"] == |
| 104 | default_conf["webhook"]["webhookbuycancel"]["value1"].format(**msg)) |
| 105 | assert (msg_mock.call_args[0][0]["value2"] == |
| 106 | default_conf["webhook"]["webhookbuycancel"]["value2"].format(**msg)) |
| 107 | assert (msg_mock.call_args[0][0]["value3"] == |
| 108 | default_conf["webhook"]["webhookbuycancel"]["value3"].format(**msg)) |
| 109 | # Test buy fill |
| 110 | msg_mock.reset_mock() |
| 111 | |
| 112 | msg = { |
| 113 | 'type': RPCMessageType.BUY_FILL, |
| 114 | 'exchange': 'Binance', |
| 115 | 'pair': 'ETH/BTC', |
| 116 | 'open_rate': 0.005, |
| 117 | 'stake_amount': 0.8, |
| 118 | 'stake_amount_fiat': 500, |
| 119 | 'stake_currency': 'BTC', |
| 120 | 'fiat_currency': 'EUR' |
| 121 | } |
| 122 | webhook.send_msg(msg=msg) |
| 123 | assert msg_mock.call_count == 1 |
| 124 | assert (msg_mock.call_args[0][0]["value1"] == |
| 125 | default_conf["webhook"]["webhookbuyfill"]["value1"].format(**msg)) |
| 126 | assert (msg_mock.call_args[0][0]["value2"] == |
| 127 | default_conf["webhook"]["webhookbuyfill"]["value2"].format(**msg)) |
| 128 | assert (msg_mock.call_args[0][0]["value3"] == |
| 129 | default_conf["webhook"]["webhookbuyfill"]["value3"].format(**msg)) |
| 130 | # Test sell |
| 131 | msg_mock.reset_mock() |
| 132 | msg = { |
| 133 | 'type': RPCMessageType.SELL, |
| 134 | 'exchange': 'Binance', |
| 135 | 'pair': 'ETH/BTC', |
| 136 | 'gain': "profit", |
| 137 | 'limit': 0.005, |
| 138 | 'amount': 0.8, |
| 139 | 'order_type': 'limit', |
| 140 | 'open_rate': 0.004, |
| 141 | 'current_rate': 0.005, |
| 142 | 'profit_amount': 0.001, |
| 143 | 'profit_ratio': 0.20, |
| 144 | 'stake_currency': 'BTC', |
| 145 | 'sell_reason': SellType.STOP_LOSS.value |
| 146 | } |
| 147 | webhook.send_msg(msg=msg) |
| 148 | assert msg_mock.call_count == 1 |
| 149 | assert (msg_mock.call_args[0][0]["value1"] == |
| 150 | default_conf["webhook"]["webhooksell"]["value1"].format(**msg)) |
| 151 | assert (msg_mock.call_args[0][0]["value2"] == |
| 152 | default_conf["webhook"]["webhooksell"]["value2"].format(**msg)) |
| 153 | assert (msg_mock.call_args[0][0]["value3"] == |
| 154 | default_conf["webhook"]["webhooksell"]["value3"].format(**msg)) |
| 155 | # Test sell cancel |
| 156 | msg_mock.reset_mock() |
| 157 | msg = { |
| 158 | 'type': RPCMessageType.SELL_CANCEL, |
| 159 | 'exchange': 'Binance', |
| 160 | 'pair': 'ETH/BTC', |
| 161 | 'gain': "profit", |
| 162 | 'limit': 0.005, |
| 163 | 'amount': 0.8, |
| 164 | 'order_type': 'limit', |
| 165 | 'open_rate': 0.004, |
| 166 | 'current_rate': 0.005, |
| 167 | 'profit_amount': 0.001, |
| 168 | 'profit_ratio': 0.20, |
| 169 | 'stake_currency': 'BTC', |
| 170 | 'sell_reason': SellType.STOP_LOSS.value |
| 171 | } |
| 172 | webhook.send_msg(msg=msg) |
| 173 | assert msg_mock.call_count == 1 |
| 174 | assert (msg_mock.call_args[0][0]["value1"] == |
| 175 | default_conf["webhook"]["webhooksellcancel"]["value1"].format(**msg)) |
| 176 | assert (msg_mock.call_args[0][0]["value2"] == |
| 177 | default_conf["webhook"]["webhooksellcancel"]["value2"].format(**msg)) |
| 178 | assert (msg_mock.call_args[0][0]["value3"] == |
| 179 | default_conf["webhook"]["webhooksellcancel"]["value3"].format(**msg)) |
| 180 | # Test Sell fill |
| 181 | msg_mock.reset_mock() |
| 182 | msg = { |
| 183 | 'type': RPCMessageType.SELL_FILL, |
| 184 | 'exchange': 'Binance', |
| 185 | 'pair': 'ETH/BTC', |
| 186 | 'gain': "profit", |
| 187 | 'close_rate': 0.005, |
| 188 | 'amount': 0.8, |
| 189 | 'order_type': 'limit', |
| 190 | 'open_rate': 0.004, |
| 191 | 'current_rate': 0.005, |
| 192 | 'profit_amount': 0.001, |
| 193 | 'profit_ratio': 0.20, |
| 194 | 'stake_currency': 'BTC', |
| 195 | 'sell_reason': SellType.STOP_LOSS.value |
| 196 | } |
| 197 | webhook.send_msg(msg=msg) |
| 198 | assert msg_mock.call_count == 1 |
| 199 | assert (msg_mock.call_args[0][0]["value1"] == |
| 200 | default_conf["webhook"]["webhooksellfill"]["value1"].format(**msg)) |
| 201 | assert (msg_mock.call_args[0][0]["value2"] == |
| 202 | default_conf["webhook"]["webhooksellfill"]["value2"].format(**msg)) |
| 203 | assert (msg_mock.call_args[0][0]["value3"] == |
| 204 | default_conf["webhook"]["webhooksellfill"]["value3"].format(**msg)) |
| 205 | |
| 206 | for msgtype in [RPCMessageType.STATUS, |
| 207 | RPCMessageType.WARNING, |
| 208 | RPCMessageType.STARTUP]: |
| 209 | # Test notification |
| 210 | msg = { |
| 211 | 'type': msgtype, |
| 212 | 'status': 'Unfilled sell order for BTC cancelled due to timeout' |
| 213 | } |
| 214 | msg_mock = MagicMock() |
| 215 | mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) |
| 216 | webhook.send_msg(msg) |
| 217 | assert msg_mock.call_count == 1 |
| 218 | assert (msg_mock.call_args[0][0]["value1"] == |
| 219 | default_conf["webhook"]["webhookstatus"]["value1"].format(**msg)) |
| 220 | assert (msg_mock.call_args[0][0]["value2"] == |
| 221 | default_conf["webhook"]["webhookstatus"]["value2"].format(**msg)) |
| 222 | assert (msg_mock.call_args[0][0]["value3"] == |
| 223 | default_conf["webhook"]["webhookstatus"]["value3"].format(**msg)) |
| 224 | |
| 225 | |
| 226 | def test_exception_send_msg(default_conf, mocker, caplog): |
| 227 | default_conf["webhook"] = get_webhook_dict() |
| 228 | del default_conf["webhook"]["webhookbuy"] |
| 229 | |
| 230 | webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf) |
| 231 | webhook.send_msg({'type': RPCMessageType.BUY}) |
| 232 | assert log_has(f"Message type '{RPCMessageType.BUY}' not configured for webhooks", |
| 233 | caplog) |
| 234 | |
| 235 | default_conf["webhook"] = get_webhook_dict() |
| 236 | default_conf["webhook"]["webhookbuy"]["value1"] = "{DEADBEEF:8f}" |
| 237 | msg_mock = MagicMock() |
| 238 | mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) |
| 239 | webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf) |
| 240 | msg = { |
| 241 | 'type': RPCMessageType.BUY, |
| 242 | 'exchange': 'Binance', |
| 243 | 'pair': 'ETH/BTC', |
| 244 | 'limit': 0.005, |
| 245 | 'order_type': 'limit', |
| 246 | 'stake_amount': 0.8, |
| 247 | 'stake_amount_fiat': 500, |
| 248 | 'stake_currency': 'BTC', |
| 249 | 'fiat_currency': 'EUR' |
| 250 | } |
| 251 | webhook.send_msg(msg) |
| 252 | assert log_has("Problem calling Webhook. Please check your webhook configuration. " |
| 253 | "Exception: 'DEADBEEF'", caplog) |
| 254 | |
| 255 | msg_mock = MagicMock() |
| 256 | mocker.patch("freqtrade.rpc.webhook.Webhook._send_msg", msg_mock) |
| 257 | msg = { |
| 258 | 'type': 'DEADBEEF', |
| 259 | 'status': 'whatever' |
| 260 | } |
| 261 | with pytest.raises(NotImplementedError): |
| 262 | webhook.send_msg(msg) |
| 263 | |
| 264 | |
| 265 | def test__send_msg(default_conf, mocker, caplog): |
| 266 | default_conf["webhook"] = get_webhook_dict() |
| 267 | webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf) |
| 268 | msg = {'value1': 'DEADBEEF', |
| 269 | 'value2': 'ALIVEBEEF', |
| 270 | 'value3': 'FREQTRADE'} |
| 271 | post = MagicMock() |
| 272 | mocker.patch("freqtrade.rpc.webhook.post", post) |
| 273 | webhook._send_msg(msg) |
| 274 | |
| 275 | assert post.call_count == 1 |
| 276 | assert post.call_args[1] == {'data': msg} |
| 277 | assert post.call_args[0] == (default_conf['webhook']['url'], ) |
| 278 | |
| 279 | post = MagicMock(side_effect=RequestException) |
| 280 | mocker.patch("freqtrade.rpc.webhook.post", post) |
| 281 | webhook._send_msg(msg) |
| 282 | assert log_has('Could not call webhook url. Exception: ', caplog) |
| 283 | |
| 284 | |
| 285 | def test__send_msg_with_json_format(default_conf, mocker, caplog): |
| 286 | default_conf["webhook"] = get_webhook_dict() |
| 287 | default_conf["webhook"]["format"] = "json" |
| 288 | webhook = Webhook(RPC(get_patched_freqtradebot(mocker, default_conf)), default_conf) |
| 289 | msg = {'text': 'Hello'} |
| 290 | post = MagicMock() |
| 291 | mocker.patch("freqtrade.rpc.webhook.post", post) |
| 292 | webhook._send_msg(msg) |
| 293 | |
| 294 | assert post.call_args[1] == {'json': msg} |