Skip to content

Commit

Permalink
Stabilize CI (#3119)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibo-Joshi committed Jun 27, 2022
1 parent 7559451 commit 01d6439
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
9 changes: 6 additions & 3 deletions tests/conftest.py
Expand Up @@ -250,13 +250,16 @@ def class_thumb_file():
f.close()


def make_bot(bot_info, **kwargs):
def make_bot(bot_info=None, **kwargs):
"""
Tests are executed on tg.ext.ExtBot, as that class only extends the functionality of tg.bot
"""
token = kwargs.pop("token", (bot_info or {}).get("token"))
private_key = kwargs.pop("private_key", PRIVATE_KEY)
kwargs.pop("token", None)
_bot = DictExtBot(
bot_info["token"],
private_key=PRIVATE_KEY,
token=token,
private_key=private_key,
request=TestHttpxRequest(8),
get_updates_request=TestHttpxRequest(1),
**kwargs,
Expand Down
9 changes: 5 additions & 4 deletions tests/test_bot.py
Expand Up @@ -75,6 +75,7 @@
check_defaults_handling,
data_file,
expect_bad_request,
make_bot,
)


Expand Down Expand Up @@ -282,7 +283,7 @@ async def test_callback_data_maxsize(self, bot, acd_in, maxsize, acd):
async def test_invalid_token_server_response(self, monkeypatch):
monkeypatch.setattr("telegram.Bot._validate_token", lambda x, y: "")
with pytest.raises(InvalidToken):
async with Bot("12") as bot:
async with make_bot(token="12") as bot:
await bot.get_me()

async def test_unknown_kwargs(self, bot, monkeypatch):
Expand Down Expand Up @@ -335,9 +336,9 @@ async def test_get_me_and_properties_not_initialized(self, bot: Bot, attribute):
await bot.shutdown()

async def test_equality(self):
async with Bot(FALLBACKS[0]["token"]) as a, Bot(FALLBACKS[0]["token"]) as b, Bot(
FALLBACKS[1]["token"]
) as c:
async with make_bot(token=FALLBACKS[0]["token"]) as a, make_bot(
token=FALLBACKS[0]["token"]
) as b, make_bot(token=FALLBACKS[1]["token"]) as c:
d = Update(123456789)

assert a == b
Expand Down
7 changes: 4 additions & 3 deletions tests/test_passport.py
Expand Up @@ -35,6 +35,7 @@
# here, although they are implicitly tested. Testing for those classes was too much work and not
# worth it.
from telegram.request import RequestData
from tests.conftest import make_bot

RAW_PASSPORT_DATA = {
"credentials": {
Expand Down Expand Up @@ -427,7 +428,7 @@ def test_bot_init_invalid_key(self, bot):
Bot(bot.token, private_key=b"Invalid key!")

async def test_passport_data_okay_with_non_crypto_bot(self, bot):
async with Bot(bot.token) as b:
async with make_bot(token=bot.token) as b:
assert PassportData.de_json(RAW_PASSPORT_DATA, bot=b)

def test_wrong_hash(self, bot):
Expand All @@ -439,13 +440,13 @@ def test_wrong_hash(self, bot):

async def test_wrong_key(self, bot):
short_key = b"-----BEGIN RSA PRIVATE KEY-----\r\nMIIBOQIBAAJBAKU+OZ2jJm7sCA/ec4gngNZhXYPu+DZ/TAwSMl0W7vAPXAsLplBk\r\nO8l6IBHx8N0ZC4Bc65mO3b2G8YAzqndyqH8CAwEAAQJAWOx3jQFzeVXDsOaBPdAk\r\nYTncXVeIc6tlfUl9mOLyinSbRNCy1XicOiOZFgH1rRKOGIC1235QmqxFvdecySoY\r\nwQIhAOFeGgeX9CrEPuSsd9+kqUcA2avCwqdQgSdy2qggRFyJAiEAu7QHT8JQSkHU\r\nDELfzrzc24AhjyG0z1DpGZArM8COascCIDK42SboXj3Z2UXiQ0CEcMzYNiVgOisq\r\nBUd5pBi+2mPxAiAM5Z7G/Sv1HjbKrOGh29o0/sXPhtpckEuj5QMC6E0gywIgFY6S\r\nNjwrAA+cMmsgY0O2fAzEKkDc5YiFsiXaGaSS4eA=\r\n-----END RSA PRIVATE KEY-----"
async with Bot(bot.token, private_key=short_key) as b:
async with make_bot(token=bot.token, private_key=short_key) as b:
passport_data = PassportData.de_json(RAW_PASSPORT_DATA, bot=b)
with pytest.raises(PassportDecryptionError):
assert passport_data.decrypted_data

wrong_key = b"-----BEGIN RSA PRIVATE KEY-----\r\nMIIEogIBAAKCAQB4qCFltuvHakZze86TUweU7E/SB3VLGEHAe7GJlBmrou9SSWsL\r\nH7E++157X6UqWFl54LOE9MeHZnoW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KBz7PtCU+38roI/vXF0MBVmGky+HwxREQLpcdl1TVCERpIT\r\nUFXThI9OLUwOGI1IcTZf9tby+1LtKvM++8n4wGdjp9qAv6ylQV9u09pAzZItMwCd\r\nUx5SL6wlaQ2y60tIKk0lfQKBgBJS+56YmA6JGzY11qz+I5FUhfcnpauDNGOTdGLT\r\n9IqRPR2fu7RCdgpva4+KkZHLOTLReoRNUojRPb4WubGfEk93AJju5pWXR7c6k3Bt\r\novS2mrJk8GQLvXVksQxjDxBH44sLDkKMEM3j7uYJqDaZNKbyoCWT7TCwikAau5qx\r\naRevAoGAAKZV705dvrpJuyoHFZ66luANlrAwG/vNf6Q4mBEXB7guqMkokCsSkjqR\r\nhsD79E6q06zA0QzkLCavbCn5kMmDS/AbA80+B7El92iIN6d3jRdiNZiewkhlWhEG\r\nm4N0gQRfIu+rUjsS/4xk8UuQUT/Ossjn/hExi7ejpKdCc7N++bc=\r\n-----END RSA PRIVATE KEY-----"
async with Bot(bot.token, private_key=wrong_key) as b:
async with make_bot(token=bot.token, private_key=short_key) as b:
passport_data = PassportData.de_json(RAW_PASSPORT_DATA, bot=b)
with pytest.raises(PassportDecryptionError):
assert passport_data.decrypted_data
Expand Down
5 changes: 3 additions & 2 deletions tests/test_picklepersistence.py
Expand Up @@ -24,9 +24,10 @@

import pytest

from telegram import Bot, Chat, Message, TelegramObject, Update, User
from telegram import Chat, Message, TelegramObject, Update, User
from telegram.ext import ContextTypes, PersistenceInput, PicklePersistence
from telegram.warnings import PTBUserWarning
from tests.conftest import make_bot


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -875,7 +876,7 @@ async def test_custom_pickler_unpickler_simple(
# Now test that pickling of unknown bots in TelegramObjects will be replaced by None-
assert not len(recwarn)
data_with_bot = {}
async with Bot(bot.token) as other_bot:
async with make_bot(token=bot.token) as other_bot:
data_with_bot["unknown_bot_in_user"] = User(1, "Dev", False, bot=other_bot)
await pickle_persistence.update_chat_data(12345, data_with_bot)
assert len(recwarn) == 1
Expand Down
5 changes: 3 additions & 2 deletions tests/test_updater.py
Expand Up @@ -34,6 +34,7 @@
from tests.conftest import (
DictBot,
data_file,
make_bot,
make_message,
make_message_update,
send_webhook_message,
Expand Down Expand Up @@ -83,7 +84,7 @@ async def test_initialize(self, bot, monkeypatch):
async def initialize_bot(*args, **kwargs):
self.test_flag = True

async with Bot(bot.token) as test_bot:
async with make_bot(token=bot.token) as test_bot:
monkeypatch.setattr(test_bot, "initialize", initialize_bot)

updater = Updater(bot=test_bot, update_queue=asyncio.Queue())
Expand All @@ -95,7 +96,7 @@ async def test_shutdown(self, bot, monkeypatch):
async def shutdown_bot(*args, **kwargs):
self.test_flag = True

async with Bot(bot.token) as test_bot:
async with make_bot(token=bot.token) as test_bot:
monkeypatch.setattr(test_bot, "shutdown", shutdown_bot)

updater = Updater(bot=test_bot, update_queue=asyncio.Queue())
Expand Down

0 comments on commit 01d6439

Please sign in to comment.