From 5d3c764e279916e123bc836c89c550a3d4add8d5 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 16 Mar 2025 14:00:17 +0100 Subject: [PATCH] test for authorized_users telegram functionality closes #11503 --- tests/rpc/test_rpc_telegram.py | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index 92d152c83..6c222dc7a 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -258,6 +258,42 @@ async def test_authorized_only_unauthorized(default_conf, mocker, caplog) -> Non assert not log_has("Exception occurred within Telegram module", caplog) +async def test_authorized_users(default_conf, mocker, caplog, update) -> None: + patch_exchange(mocker) + caplog.set_level(logging.DEBUG) + default_conf["telegram"]["enabled"] = False + default_conf["telegram"]["authorized_users"] = ["5432"] + bot = FreqtradeBot(default_conf) + rpc = RPC(bot) + dummy = DummyCls(rpc, default_conf) + + await dummy.dummy_handler(update=update, context=MagicMock()) + assert dummy.state["called"] is True + assert log_has("Executing handler: dummy_handler for chat_id: 1235", caplog) + caplog.clear() + # Test empty case + default_conf["telegram"]["authorized_users"] = [] + dummy1 = DummyCls(rpc, default_conf) + await dummy1.dummy_handler(update=update, context=MagicMock()) + assert dummy1.state["called"] is False + assert log_has_re(r"Unauthorized user tried to .*5432", caplog) + caplog.clear() + # Test wrong user + default_conf["telegram"]["authorized_users"] = ["1234"] + dummy1 = DummyCls(rpc, default_conf) + await dummy1.dummy_handler(update=update, context=MagicMock()) + assert dummy1.state["called"] is False + assert log_has_re(r"Unauthorized user tried to .*5432", caplog) + caplog.clear() + + # Test reverse case again + default_conf["telegram"]["authorized_users"] = ["5432"] + dummy1 = DummyCls(rpc, default_conf) + await dummy1.dummy_handler(update=update, context=MagicMock()) + assert dummy1.state["called"] is True + assert not log_has_re(r"Unauthorized user tried to .*5432", caplog) + + async def test_authorized_only_exception(default_conf, mocker, caplog, update) -> None: patch_exchange(mocker)