Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/gateway/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/gateway/test_telegram_approval_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 7 additions & 4 deletions tests/gateway/test_telegram_rich_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -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()


# --------------------------------------------------------------------------
Expand Down
Loading