diff --git a/tests/gateway/conftest.py b/tests/gateway/conftest.py index 7a21465dd3d7..13a94dee76df 100644 --- a/tests/gateway/conftest.py +++ b/tests/gateway/conftest.py @@ -94,6 +94,23 @@ def _ensure_telegram_mock() -> None: if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"): return # Real library is installed — nothing to mock + # Prefer the REAL library whenever it is importable. The mock below is + # process-wide and permanent, and it poisons every later-collected test + # that needs genuine PTB classes (tests/test_telegram_polling_progress_ptb + # subclasses the real BaseRequest) whenever a gateway file collects + # first. PTB is a production dependency, so in a normal venv this branch + # always wins; the mock survives only for genuinely PTB-less environments. + try: + import telegram # noqa: F401 + import telegram.constants # noqa: F401 + import telegram.error # noqa: F401 + import telegram.ext # noqa: F401 + import telegram.request # noqa: F401 + if hasattr(sys.modules.get("telegram"), "__file__"): + return + except ImportError: + pass + mod = MagicMock() mod.ext.ContextTypes.DEFAULT_TYPE = type(None) # One shared PTB-faithful enum namespace per constant, attached to BOTH diff --git a/tests/gateway/test_telegram_approval_buttons.py b/tests/gateway/test_telegram_approval_buttons.py index 0f6ba60db35a..875c74c87b5f 100644 --- a/tests/gateway/test_telegram_approval_buttons.py +++ b/tests/gateway/test_telegram_approval_buttons.py @@ -24,6 +24,18 @@ def _ensure_telegram_mock(): if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"): return + # Prefer the REAL library whenever it is importable — same rationale as + # the gateway conftest: the fakes below are process-wide and permanent. + try: + import telegram # noqa: F401 + import telegram.constants # noqa: F401 + import telegram.error # noqa: F401 + import telegram.ext # noqa: F401 + import telegram.request # noqa: F401 + return + except ImportError: + pass + mod = MagicMock() mod.ext.ContextTypes.DEFAULT_TYPE = type(None) mod.constants.ParseMode.MARKDOWN = "Markdown" diff --git a/tests/gateway/test_telegram_rich_messages.py b/tests/gateway/test_telegram_rich_messages.py index 5e46cbc35dd3..e330a5c3e703 100644 --- a/tests/gateway/test_telegram_rich_messages.py +++ b/tests/gateway/test_telegram_rich_messages.py @@ -312,9 +312,12 @@ async def test_legacy_send_error_redacts_bot_token_without_traceback(monkeypatch assert result.success is False assert result.error is not None assert token not in result.error - assert "bot123456789:***/sendMessage" in result.error + # Real PTB TelegramError capitalize()s its message (the mock did not) — + # assert the token redaction case-insensitively; the property under + # test is the redaction, not the URL casing. + assert "bot123456789:***/sendmessage" in result.error.lower() assert token not in caplog.text - assert "bot123456789:***/sendMessage" in caplog.text + assert "bot123456789:***/sendmessage" in caplog.text.lower() adapter._bot.do_api_request.assert_not_called() @@ -454,9 +457,9 @@ async def test_legacy_edit_error_logs_redacted_bot_token_without_traceback(monke assert result.success is False assert result.error is not None assert token not in result.error - assert "bot123456789:***/editMessageText" in result.error + assert "bot123456789:***/editmessagetext" in result.error.lower() assert token not in caplog.text - assert "bot123456789:***/editMessageText" in caplog.text + assert "bot123456789:***/editmessagetext" in caplog.text.lower() # --------------------------------------------------------------------------