Merge branch 'develop' into api-server-list-custom-data

This commit is contained in:
Axel-CH
2025-03-18 00:53:10 -04:00
16 changed files with 1192 additions and 203 deletions

View File

@@ -6,7 +6,7 @@ import asyncio
import logging
import re
import threading
from datetime import datetime, timedelta, timezone
from datetime import timedelta
from functools import reduce
from random import choice, randint
from string import ascii_uppercase
@@ -16,7 +16,7 @@ import pytest
import time_machine
from pandas import DataFrame
from sqlalchemy import select
from telegram import Chat, Message, ReplyKeyboardMarkup, Update
from telegram import Chat, Message, ReplyKeyboardMarkup, Update, User
from telegram.error import BadRequest, NetworkError, TelegramError
from freqtrade import __version__
@@ -67,7 +67,12 @@ def default_conf(default_conf) -> dict:
@pytest.fixture
def update():
message = Message(0, datetime.now(timezone.utc), Chat(1235, 0))
message = Message(
0,
dt_now(),
Chat(1235, 0),
from_user=User(5432, "test", is_bot=False),
)
_update = Update(0, message=message)
return _update
@@ -232,8 +237,12 @@ async def test_authorized_only(default_conf, mocker, caplog, update) -> None:
async def test_authorized_only_unauthorized(default_conf, mocker, caplog) -> None:
patch_exchange(mocker)
caplog.set_level(logging.DEBUG)
chat = Chat(0xDEADBEEF, 0)
message = Message(randint(1, 100), datetime.now(timezone.utc), chat)
message = Message(
randint(1, 100),
dt_now(),
Chat(0xDEADBEEF, 0),
from_user=User(5432, "test", is_bot=False),
)
update = Update(randint(1, 100), message=message)
default_conf["telegram"]["enabled"] = False
@@ -249,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)
@@ -638,7 +683,7 @@ async def test_daily_handle(default_conf_usdt, update, ticker, fee, mocker, time
assert msg_mock.call_count == 1
assert "Daily Profit over the last 2 days</b>:" in msg_mock.call_args_list[0][0][0]
assert "Day " in msg_mock.call_args_list[0][0][0]
assert str(datetime.now(timezone.utc).date()) in msg_mock.call_args_list[0][0][0]
assert str(dt_now().date()) in msg_mock.call_args_list[0][0][0]
assert " 6.83 USDT" in msg_mock.call_args_list[0][0][0]
assert " 7.51 USD" in msg_mock.call_args_list[0][0][0]
assert "(2)" in msg_mock.call_args_list[0][0][0]
@@ -651,11 +696,8 @@ async def test_daily_handle(default_conf_usdt, update, ticker, fee, mocker, time
await telegram._daily(update=update, context=context)
assert msg_mock.call_count == 1
assert "Daily Profit over the last 7 days</b>:" in msg_mock.call_args_list[0][0][0]
assert str(datetime.now(timezone.utc).date()) in msg_mock.call_args_list[0][0][0]
assert (
str((datetime.now(timezone.utc) - timedelta(days=5)).date())
in msg_mock.call_args_list[0][0][0]
)
assert str(dt_now().date()) in msg_mock.call_args_list[0][0][0]
assert str((dt_now() - timedelta(days=5)).date()) in msg_mock.call_args_list[0][0][0]
assert " 6.83 USDT" in msg_mock.call_args_list[0][0][0]
assert " 7.51 USD" in msg_mock.call_args_list[0][0][0]
assert "(2)" in msg_mock.call_args_list[0][0][0]
@@ -725,7 +767,7 @@ async def test_weekly_handle(default_conf_usdt, update, ticker, fee, mocker, tim
in msg_mock.call_args_list[0][0][0]
)
assert "Monday " in msg_mock.call_args_list[0][0][0]
today = datetime.now(timezone.utc).date()
today = dt_now().date()
first_iso_day_of_current_week = today - timedelta(days=today.weekday())
assert str(first_iso_day_of_current_week) in msg_mock.call_args_list[0][0][0]
assert " 2.74 USDT" in msg_mock.call_args_list[0][0][0]
@@ -793,7 +835,7 @@ async def test_monthly_handle(default_conf_usdt, update, ticker, fee, mocker, ti
assert msg_mock.call_count == 1
assert "Monthly Profit over the last 2 months</b>:" in msg_mock.call_args_list[0][0][0]
assert "Month " in msg_mock.call_args_list[0][0][0]
today = datetime.now(timezone.utc).date()
today = dt_now().date()
current_month = f"{today.year}-{today.month:02} "
assert current_month in msg_mock.call_args_list[0][0][0]
assert " 2.74 USDT" in msg_mock.call_args_list[0][0][0]
@@ -898,7 +940,7 @@ async def test_telegram_profit_handle(
trade.orders.append(oobj)
trade.update_trade(oobj)
trade.close_date = datetime.now(timezone.utc)
trade.close_date = dt_now()
trade.is_open = False
Trade.commit()