2017-08-11 23:58:41 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2020-02-02 22:08:54 +01:00
|
|
|
# Copyright (C) 2015-2020
|
2017-08-11 23:58:41 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2019-10-12 15:11:09 +02:00
|
|
|
import datetime
|
2017-08-11 23:58:41 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2019-10-12 15:11:09 +02:00
|
|
|
import re
|
2017-08-11 23:58:41 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
from queue import Queue
|
|
|
|
from threading import Thread, Event
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2019-11-15 21:51:22 +01:00
|
|
|
from telegram import (Bot, Message, User, Chat, MessageEntity, Update,
|
|
|
|
InlineQuery, CallbackQuery, ShippingQuery, PreCheckoutQuery,
|
|
|
|
ChosenInlineResult)
|
2020-02-06 11:22:56 +01:00
|
|
|
from telegram.ext import Dispatcher, JobQueue, Updater, BaseFilter, Defaults
|
2020-05-01 13:27:46 +02:00
|
|
|
from telegram.error import BadRequest
|
2017-08-11 23:58:41 +02:00
|
|
|
from tests.bots import get_bot
|
|
|
|
|
2019-10-27 14:28:33 +01:00
|
|
|
GITHUB_ACTION = os.getenv('GITHUB_ACTION', False)
|
|
|
|
|
|
|
|
if GITHUB_ACTION:
|
|
|
|
pytest_plugins = ['tests.plugin_github_group']
|
|
|
|
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
# THIS KEY IS OBVIOUSLY COMPROMISED
|
|
|
|
# DO NOT USE IN PRODUCTION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45AFhqCKhlG4l51oW7TUH50qE3GM+4ivaH5YZB3biwQ9Wqw+QyNLAh/Q\r\nnS4/Wwb8qC9QuyEgcCju5lsCaPEXZiZqtPVxZd0CgYEAshBG31yZjO0zG1TZUwfy\r\nfN3euc8mRgZpSdXIHiS5NSyg7Zr8ZcUSID8jAkJiQ3n3OiAsuq1MGQ6kNa582kLT\r\nFPQdI9Ea8ahyDbkNR0gAY9xbM2kg/Gnro1PorH9PTKE0ekSodKk1UUyNrg4DBAwn\r\nqE6E3ebHXt/2WmqIbUD653ECgYBQCC8EAQNX3AFegPd1GGxU33Lz4tchJ4kMCNU0\r\nN2NZh9VCr3nTYjdTbxsXU8YP44CCKFG2/zAO4kymyiaFAWEOn5P7irGF/JExrjt4\r\nibGy5lFLEq/HiPtBjhgsl1O0nXlwUFzd7OLghXc+8CPUJaz5w42unqT3PBJa40c3\r\nQcIPdQKBgBnSb7BcDAAQ/Qx9juo/RKpvhyeqlnp0GzPSQjvtWi9dQRIu9Pe7luHc\r\nm1Img1EO1OyE3dis/rLaDsAa2AKu1Yx6h85EmNjavBqP9wqmFa0NIQQH8fvzKY3/\r\nP8IHY6009aoamLqYaexvrkHVq7fFKiI6k8myMJ6qblVNFv14+KXU\r\n-----END RSA PRIVATE KEY-----" # noqa: E501
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def bot_info():
|
|
|
|
return get_bot()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def bot(bot_info):
|
2019-10-12 15:11:09 +02:00
|
|
|
return make_bot(bot_info)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
|
2020-02-06 11:22:56 +01:00
|
|
|
DEFAULT_BOTS = {}
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def default_bot(request, bot_info):
|
|
|
|
param = request.param if hasattr(request, 'param') else {}
|
|
|
|
|
|
|
|
defaults = Defaults(**param)
|
|
|
|
default_bot = DEFAULT_BOTS.get(defaults)
|
|
|
|
if default_bot:
|
|
|
|
return default_bot
|
|
|
|
else:
|
|
|
|
default_bot = make_bot(bot_info, **{'defaults': defaults})
|
|
|
|
DEFAULT_BOTS[defaults] = default_bot
|
|
|
|
return default_bot
|
|
|
|
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def chat_id(bot_info):
|
|
|
|
return bot_info['chat_id']
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
2019-08-23 21:20:41 +02:00
|
|
|
def super_group_id(bot_info):
|
|
|
|
return bot_info['super_group_id']
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def channel_id(bot_info):
|
|
|
|
return bot_info['channel_id']
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def provider_token(bot_info):
|
|
|
|
return bot_info['payment_provider_token']
|
|
|
|
|
|
|
|
|
|
|
|
def create_dp(bot):
|
|
|
|
# Dispatcher is heavy to init (due to many threads and such) so we have a single session
|
|
|
|
# scoped one here, but before each test, reset it (dp fixture below)
|
2018-09-21 08:57:01 +02:00
|
|
|
dispatcher = Dispatcher(bot, Queue(), job_queue=JobQueue(), workers=2, use_context=False)
|
|
|
|
dispatcher.job_queue.set_dispatcher(dispatcher)
|
2017-08-11 23:58:41 +02:00
|
|
|
thr = Thread(target=dispatcher.start)
|
|
|
|
thr.start()
|
|
|
|
sleep(2)
|
|
|
|
yield dispatcher
|
|
|
|
sleep(1)
|
|
|
|
if dispatcher.running:
|
|
|
|
dispatcher.stop()
|
|
|
|
thr.join()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def _dp(bot):
|
|
|
|
for dp in create_dp(bot):
|
|
|
|
yield dp
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def dp(_dp):
|
|
|
|
# Reset the dispatcher first
|
|
|
|
while not _dp.update_queue.empty():
|
|
|
|
_dp.update_queue.get(False)
|
|
|
|
_dp.chat_data = defaultdict(dict)
|
|
|
|
_dp.user_data = defaultdict(dict)
|
2020-02-02 22:20:31 +01:00
|
|
|
_dp.bot_data = {}
|
2018-11-09 11:44:20 +01:00
|
|
|
_dp.persistence = None
|
2017-08-11 23:58:41 +02:00
|
|
|
_dp.handlers = {}
|
|
|
|
_dp.groups = []
|
|
|
|
_dp.error_handlers = []
|
|
|
|
_dp.__stop_event = Event()
|
|
|
|
_dp.__exception_event = Event()
|
|
|
|
_dp.__async_queue = Queue()
|
|
|
|
_dp.__async_threads = set()
|
2018-10-04 08:58:40 +02:00
|
|
|
_dp.persistence = None
|
|
|
|
_dp.use_context = False
|
2017-08-11 23:58:41 +02:00
|
|
|
if _dp._Dispatcher__singleton_semaphore.acquire(blocking=0):
|
|
|
|
Dispatcher._set_singleton(_dp)
|
|
|
|
yield _dp
|
|
|
|
Dispatcher._Dispatcher__singleton_semaphore.release()
|
|
|
|
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def cdp(dp):
|
|
|
|
dp.use_context = True
|
|
|
|
yield dp
|
|
|
|
dp.use_context = False
|
|
|
|
|
|
|
|
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def updater(bot):
|
|
|
|
up = Updater(bot=bot, workers=2)
|
|
|
|
yield up
|
|
|
|
if up.running:
|
|
|
|
up.stop()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def thumb_file():
|
|
|
|
f = open(u'tests/data/thumb.jpg', 'rb')
|
|
|
|
yield f
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='class')
|
|
|
|
def class_thumb_file():
|
|
|
|
f = open(u'tests/data/thumb.jpg', 'rb')
|
|
|
|
yield f
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
def pytest_configure(config):
|
|
|
|
if sys.version_info >= (3,):
|
|
|
|
config.addinivalue_line('filterwarnings', 'ignore::ResourceWarning')
|
|
|
|
# TODO: Write so good code that we don't need to ignore ResourceWarnings anymore
|
2019-10-12 15:11:09 +02:00
|
|
|
|
|
|
|
|
2020-02-06 11:22:56 +01:00
|
|
|
def make_bot(bot_info, **kwargs):
|
|
|
|
return Bot(bot_info['token'], private_key=PRIVATE_KEY, **kwargs)
|
2019-10-12 15:11:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
CMD_PATTERN = re.compile(r'/[\da-z_]{1,32}(?:@\w{1,32})?')
|
|
|
|
DATE = datetime.datetime.now()
|
|
|
|
|
|
|
|
|
|
|
|
def make_message(text, **kwargs):
|
|
|
|
"""
|
|
|
|
Testing utility factory to create a fake ``telegram.Message`` with
|
|
|
|
reasonable defaults for mimicking a real message.
|
|
|
|
:param text: (str) message text
|
|
|
|
:return: a (fake) ``telegram.Message``
|
|
|
|
"""
|
|
|
|
return Message(message_id=1,
|
|
|
|
from_user=kwargs.pop('user', User(id=1, first_name='', is_bot=False)),
|
|
|
|
date=kwargs.pop('date', DATE),
|
|
|
|
chat=kwargs.pop('chat', Chat(id=1, type='')),
|
|
|
|
text=text,
|
|
|
|
bot=kwargs.pop('bot', make_bot(get_bot())),
|
|
|
|
**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def make_command_message(text, **kwargs):
|
|
|
|
"""
|
|
|
|
Testing utility factory to create a message containing a single telegram
|
|
|
|
command.
|
|
|
|
Mimics the Telegram API in that it identifies commands within the message
|
|
|
|
and tags the returned ``Message`` object with the appropriate ``MessageEntity``
|
|
|
|
tag (but it does this only for commands).
|
|
|
|
|
|
|
|
:param text: (str) message text containing (or not) the command
|
|
|
|
:return: a (fake) ``telegram.Message`` containing only the command
|
|
|
|
"""
|
|
|
|
|
|
|
|
match = re.search(CMD_PATTERN, text)
|
|
|
|
entities = [MessageEntity(type=MessageEntity.BOT_COMMAND,
|
|
|
|
offset=match.start(0),
|
|
|
|
length=len(match.group(0)))] if match else []
|
|
|
|
|
|
|
|
return make_message(text, entities=entities, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def make_message_update(message, message_factory=make_message, edited=False, **kwargs):
|
|
|
|
"""
|
|
|
|
Testing utility factory to create an update from a message, as either a
|
|
|
|
``telegram.Message`` or a string. In the latter case ``message_factory``
|
|
|
|
is used to convert ``message`` to a ``telegram.Message``.
|
|
|
|
:param message: either a ``telegram.Message`` or a string with the message text
|
|
|
|
:param message_factory: function to convert the message text into a ``telegram.Message``
|
|
|
|
:param edited: whether the message should be stored as ``edited_message`` (vs. ``message``)
|
|
|
|
:return: ``telegram.Update`` with the given message
|
|
|
|
"""
|
|
|
|
if not isinstance(message, Message):
|
|
|
|
message = message_factory(message, **kwargs)
|
|
|
|
update_kwargs = {'message' if not edited else 'edited_message': message}
|
|
|
|
return Update(0, **update_kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def make_command_update(message, edited=False, **kwargs):
|
|
|
|
"""
|
|
|
|
Testing utility factory to create an update from a message that potentially
|
|
|
|
contains a command. See ``make_command_message`` for more details.
|
|
|
|
:param message: message potentially containing a command
|
|
|
|
:param edited: whether the message should be stored as ``edited_message`` (vs. ``message``)
|
|
|
|
:return: ``telegram.Update`` with the given message
|
|
|
|
"""
|
|
|
|
return make_message_update(message, make_command_message, edited, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def mock_filter():
|
|
|
|
class MockFilter(BaseFilter):
|
|
|
|
def __init__(self):
|
|
|
|
self.tested = False
|
|
|
|
|
|
|
|
def filter(self, message):
|
|
|
|
self.tested = True
|
|
|
|
|
|
|
|
return MockFilter()
|
|
|
|
|
|
|
|
|
|
|
|
def get_false_update_fixture_decorator_params():
|
|
|
|
message = Message(1, User(1, '', False), DATE, Chat(1, ''), text='test')
|
|
|
|
params = [
|
|
|
|
{'callback_query': CallbackQuery(1, User(1, '', False), 'chat', message=message)},
|
|
|
|
{'channel_post': message},
|
|
|
|
{'edited_channel_post': message},
|
|
|
|
{'inline_query': InlineQuery(1, User(1, '', False), '', '')},
|
|
|
|
{'chosen_inline_result': ChosenInlineResult('id', User(1, '', False), '')},
|
|
|
|
{'shipping_query': ShippingQuery('id', User(1, '', False), '', None)},
|
|
|
|
{'pre_checkout_query': PreCheckoutQuery('id', User(1, '', False), '', 0, '')},
|
|
|
|
{'callback_query': CallbackQuery(1, User(1, '', False), 'chat')}
|
|
|
|
]
|
|
|
|
ids = tuple(key for kwargs in params for key in kwargs)
|
|
|
|
return {'params': params, 'ids': ids}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function', **get_false_update_fixture_decorator_params())
|
|
|
|
def false_update(request):
|
|
|
|
return Update(update_id=1, **request.param)
|
2019-11-15 21:51:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2], ids=lambda h: 'UTC +{hour:0>2}:00'.format(hour=h))
|
|
|
|
def utc_offset(request):
|
|
|
|
return datetime.timedelta(hours=request.param)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def timezone(utc_offset):
|
2020-05-01 22:55:13 +02:00
|
|
|
return datetime.timezone(utc_offset)
|
2020-05-01 13:27:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
def expect_bad_request(func, message, reason):
|
|
|
|
"""
|
|
|
|
Wrapper for testing bot functions expected to result in an :class:`telegram.error.BadRequest`.
|
|
|
|
Makes it XFAIL, if the specified error message is present.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
func: The callable to be executed.
|
|
|
|
message: The expected message of the bad request error. If another message is present,
|
|
|
|
the error will be reraised.
|
|
|
|
reason: Explanation for the XFAIL.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
On success, returns the return value of :attr:`func`
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return func()
|
|
|
|
except BadRequest as e:
|
|
|
|
if message in str(e):
|
|
|
|
pytest.xfail('{}. {}'.format(reason, e))
|
|
|
|
else:
|
|
|
|
raise e
|