2018-07-29 14:09:44 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, C0103
|
2018-02-13 03:45:59 +00:00
|
|
|
import logging
|
2020-09-28 17:43:15 +00:00
|
|
|
import time
|
2022-07-27 04:32:23 +00:00
|
|
|
from collections import deque
|
2018-02-13 03:45:59 +00:00
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
2021-06-09 17:51:44 +00:00
|
|
|
from freqtrade.enums import RPCMessageType
|
|
|
|
from freqtrade.rpc import RPCManager
|
2021-07-06 18:29:04 +00:00
|
|
|
from freqtrade.rpc.api_server.webserver import ApiServer
|
2020-09-28 17:43:15 +00:00
|
|
|
from tests.conftest import get_patched_freqtradebot, log_has
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test__init__(mocker, default_conf) -> None:
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf))
|
2018-02-13 03:45:59 +00:00
|
|
|
assert rpc_manager.registered_modules == []
|
|
|
|
|
|
|
|
|
|
|
|
def test_init_telegram_disabled(mocker, default_conf, caplog) -> None:
|
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
|
|
|
rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf))
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2019-08-11 18:17:22 +00:00
|
|
|
assert not log_has('Enabling rpc.telegram ...', caplog)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert rpc_manager.registered_modules == []
|
|
|
|
|
|
|
|
|
|
|
|
def test_init_telegram_enabled(mocker, default_conf, caplog) -> None:
|
|
|
|
caplog.set_level(logging.DEBUG)
|
2023-04-15 15:39:23 +00:00
|
|
|
default_conf['telegram']['enabled'] = True
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
2018-06-08 01:49:09 +00:00
|
|
|
rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf))
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2019-08-11 18:17:22 +00:00
|
|
|
assert log_has('Enabling rpc.telegram ...', caplog)
|
2018-02-13 03:45:59 +00:00
|
|
|
len_modules = len(rpc_manager.registered_modules)
|
|
|
|
assert len_modules == 1
|
2018-06-08 01:49:09 +00:00
|
|
|
assert 'telegram' in [mod.name for mod in rpc_manager.registered_modules]
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cleanup_telegram_disabled(mocker, default_conf, caplog) -> None:
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.cleanup', MagicMock())
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc_manager = RPCManager(freqtradebot)
|
|
|
|
rpc_manager.cleanup()
|
|
|
|
|
2019-08-11 18:17:22 +00:00
|
|
|
assert not log_has('Cleaning up rpc.telegram ...', caplog)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert telegram_mock.call_count == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_cleanup_telegram_enabled(mocker, default_conf, caplog) -> None:
|
|
|
|
caplog.set_level(logging.DEBUG)
|
2023-04-15 15:39:23 +00:00
|
|
|
default_conf['telegram']['enabled'] = True
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
|
|
|
telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.cleanup', MagicMock())
|
|
|
|
|
2018-03-04 10:06:40 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc_manager = RPCManager(freqtradebot)
|
|
|
|
|
|
|
|
# Check we have Telegram as a registered modules
|
2018-06-08 01:49:09 +00:00
|
|
|
assert 'telegram' in [mod.name for mod in rpc_manager.registered_modules]
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
rpc_manager.cleanup()
|
2019-08-11 18:17:22 +00:00
|
|
|
assert log_has('Cleaning up rpc.telegram ...', caplog)
|
2018-06-08 01:49:09 +00:00
|
|
|
assert 'telegram' not in [mod.name for mod in rpc_manager.registered_modules]
|
2018-02-13 03:45:59 +00:00
|
|
|
assert telegram_mock.call_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_msg_telegram_disabled(mocker, default_conf, caplog) -> None:
|
|
|
|
telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc_manager = RPCManager(freqtradebot)
|
2018-07-03 18:26:48 +00:00
|
|
|
rpc_manager.send_msg({
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.STATUS,
|
2018-07-03 18:26:48 +00:00
|
|
|
'status': 'test'
|
|
|
|
})
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2019-08-11 18:17:22 +00:00
|
|
|
assert log_has("Sending rpc message: {'type': status, 'status': 'test'}", caplog)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert telegram_mock.call_count == 0
|
|
|
|
|
|
|
|
|
2022-09-20 16:09:14 +00:00
|
|
|
def test_send_msg_telegram_error(mocker, default_conf, caplog) -> None:
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', side_effect=ValueError())
|
2023-04-15 15:39:23 +00:00
|
|
|
default_conf['telegram']['enabled'] = True
|
2022-09-20 16:09:14 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
rpc_manager = RPCManager(freqtradebot)
|
|
|
|
rpc_manager.send_msg({
|
|
|
|
'type': RPCMessageType.STATUS,
|
|
|
|
'status': 'test'
|
|
|
|
})
|
|
|
|
|
|
|
|
assert log_has("Sending rpc message: {'type': status, 'status': 'test'}", caplog)
|
|
|
|
assert log_has("Exception occurred within RPC module telegram", caplog)
|
|
|
|
|
|
|
|
|
2022-07-27 04:32:23 +00:00
|
|
|
def test_process_msg_queue(mocker, default_conf, caplog) -> None:
|
|
|
|
telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg')
|
2023-04-15 15:39:23 +00:00
|
|
|
default_conf['telegram']['enabled'] = True
|
2022-10-07 18:44:47 +00:00
|
|
|
default_conf['telegram']['allow_custom_messages'] = True
|
2022-07-27 04:32:23 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init')
|
|
|
|
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
rpc_manager = RPCManager(freqtradebot)
|
|
|
|
queue = deque()
|
|
|
|
queue.append('Test message')
|
|
|
|
queue.append('Test message 2')
|
|
|
|
rpc_manager.process_msg_queue(queue)
|
|
|
|
|
2022-10-07 18:44:47 +00:00
|
|
|
assert log_has("Sending rpc strategy_msg: Test message", caplog)
|
|
|
|
assert log_has("Sending rpc strategy_msg: Test message 2", caplog)
|
2022-07-27 04:32:23 +00:00
|
|
|
assert telegram_mock.call_count == 2
|
|
|
|
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def test_send_msg_telegram_enabled(mocker, default_conf, caplog) -> None:
|
2023-04-15 15:39:23 +00:00
|
|
|
default_conf['telegram']['enabled'] = True
|
2022-07-27 04:32:23 +00:00
|
|
|
telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg')
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init')
|
2018-03-04 10:06:40 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc_manager = RPCManager(freqtradebot)
|
2018-07-03 18:26:48 +00:00
|
|
|
rpc_manager.send_msg({
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.STATUS,
|
2018-07-03 18:26:48 +00:00
|
|
|
'status': 'test'
|
|
|
|
})
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2019-08-11 18:17:22 +00:00
|
|
|
assert log_has("Sending rpc message: {'type': status, 'status': 'test'}", caplog)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert telegram_mock.call_count == 1
|
2018-07-12 18:17:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_init_webhook_disabled(mocker, default_conf, caplog) -> None:
|
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
|
|
|
default_conf['webhook'] = {'enabled': False}
|
|
|
|
rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf))
|
2018-07-12 18:17:45 +00:00
|
|
|
|
2019-08-11 18:17:22 +00:00
|
|
|
assert not log_has('Enabling rpc.webhook ...', caplog)
|
2018-07-12 18:17:45 +00:00
|
|
|
assert rpc_manager.registered_modules == []
|
|
|
|
|
|
|
|
|
|
|
|
def test_init_webhook_enabled(mocker, default_conf, caplog) -> None:
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
default_conf['telegram']['enabled'] = False
|
2018-07-14 11:29:50 +00:00
|
|
|
default_conf['webhook'] = {'enabled': True, 'url': "https://DEADBEEF.com"}
|
2018-07-12 18:17:45 +00:00
|
|
|
rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf))
|
|
|
|
|
2019-08-11 18:17:22 +00:00
|
|
|
assert log_has('Enabling rpc.webhook ...', caplog)
|
2018-07-29 14:09:44 +00:00
|
|
|
assert len(rpc_manager.registered_modules) == 1
|
2018-07-12 18:17:45 +00:00
|
|
|
assert 'webhook' in [mod.name for mod in rpc_manager.registered_modules]
|
2018-11-29 19:02:12 +00:00
|
|
|
|
|
|
|
|
2019-08-30 04:59:19 +00:00
|
|
|
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]
|
2021-04-20 04:41:58 +00:00
|
|
|
rpc_manager.send_msg({'type': RPCMessageType.STARTUP,
|
2019-08-30 04:59:19 +00:00
|
|
|
'status': 'TestMessage'})
|
|
|
|
assert log_has(
|
2020-09-19 18:04:12 +00:00
|
|
|
"Message type 'startup' not implemented by handler webhook.",
|
2019-08-30 04:59:19 +00:00
|
|
|
caplog)
|
|
|
|
|
|
|
|
|
2023-04-15 15:39:23 +00:00
|
|
|
def test_startupmessages_telegram_enabled(mocker, default_conf) -> None:
|
|
|
|
default_conf['telegram']['enabled'] = True
|
2018-11-29 19:02:12 +00:00
|
|
|
telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
|
|
|
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
rpc_manager = RPCManager(freqtradebot)
|
2020-12-07 09:54:37 +00:00
|
|
|
rpc_manager.startup_messages(default_conf, freqtradebot.pairlists, freqtradebot.protections)
|
2018-11-29 19:02:12 +00:00
|
|
|
|
|
|
|
assert telegram_mock.call_count == 3
|
2021-04-20 10:54:22 +00:00
|
|
|
assert "*Exchange:* `binance`" in telegram_mock.call_args_list[1][0][0]['status']
|
2018-11-29 19:02:12 +00:00
|
|
|
|
|
|
|
telegram_mock.reset_mock()
|
|
|
|
default_conf['dry_run'] = True
|
2018-12-03 19:31:25 +00:00
|
|
|
default_conf['whitelist'] = {'method': 'VolumePairList',
|
|
|
|
'config': {'number_assets': 20}
|
|
|
|
}
|
2020-12-07 09:54:37 +00:00
|
|
|
default_conf['protections'] = [{"method": "StoplossGuard",
|
|
|
|
"lookback_period": 60, "trade_limit": 2, "stop_duration": 60}]
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-11-29 19:02:12 +00:00
|
|
|
|
2020-12-07 09:54:37 +00:00
|
|
|
rpc_manager.startup_messages(default_conf, freqtradebot.pairlists, freqtradebot.protections)
|
|
|
|
assert telegram_mock.call_count == 4
|
2018-11-29 19:02:12 +00:00
|
|
|
assert "Dry run is enabled." in telegram_mock.call_args_list[0][0][0]['status']
|
2020-12-07 09:54:37 +00:00
|
|
|
assert 'StoplossGuard' in telegram_mock.call_args_list[-1][0][0]['status']
|
2019-05-11 11:40:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_init_apiserver_disabled(mocker, default_conf, caplog) -> None:
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
run_mock = MagicMock()
|
2020-12-31 10:01:50 +00:00
|
|
|
mocker.patch('freqtrade.rpc.api_server.ApiServer.start_api', run_mock)
|
2019-05-11 11:40:30 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
|
|
|
rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf))
|
|
|
|
|
2019-08-11 18:17:22 +00:00
|
|
|
assert not log_has('Enabling rpc.api_server', caplog)
|
2019-05-11 11:40:30 +00:00
|
|
|
assert rpc_manager.registered_modules == []
|
|
|
|
assert run_mock.call_count == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_init_apiserver_enabled(mocker, default_conf, caplog) -> None:
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
run_mock = MagicMock()
|
2020-12-31 10:01:50 +00:00
|
|
|
mocker.patch('freqtrade.rpc.api_server.ApiServer.start_api', run_mock)
|
2019-05-11 11:40:30 +00:00
|
|
|
|
|
|
|
default_conf["telegram"]["enabled"] = False
|
|
|
|
default_conf["api_server"] = {"enabled": True,
|
|
|
|
"listen_ip_address": "127.0.0.1",
|
2019-11-23 14:20:53 +00:00
|
|
|
"listen_port": 8080,
|
|
|
|
"username": "TestUser",
|
|
|
|
"password": "TestPass",
|
|
|
|
}
|
2019-05-11 11:40:30 +00:00
|
|
|
rpc_manager = RPCManager(get_patched_freqtradebot(mocker, default_conf))
|
|
|
|
|
2019-11-12 12:14:43 +00:00
|
|
|
# Sleep to allow the thread to start
|
|
|
|
time.sleep(0.5)
|
2019-08-11 18:17:22 +00:00
|
|
|
assert log_has('Enabling rpc.api_server', caplog)
|
2019-05-11 11:40:30 +00:00
|
|
|
assert len(rpc_manager.registered_modules) == 1
|
|
|
|
assert 'apiserver' in [mod.name for mod in rpc_manager.registered_modules]
|
|
|
|
assert run_mock.call_count == 1
|
2021-07-06 18:29:04 +00:00
|
|
|
ApiServer.shutdown()
|