2015-11-06 00:23:34 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2016-01-05 14:12:03 +01:00
|
|
|
# Copyright (C) 2015-2016
|
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-11-06 00:23:34 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2015-11-24 21:06:55 +01:00
|
|
|
"""
|
2016-10-17 00:22:40 +02:00
|
|
|
This module contains an object that represents Tests for Updater, Dispatcher,
|
2015-11-24 21:06:55 +01:00
|
|
|
WebhookServer and WebhookHandler
|
|
|
|
"""
|
2015-11-21 21:22:58 +01:00
|
|
|
import logging
|
2015-11-24 14:57:54 +01:00
|
|
|
import signal
|
2016-04-24 04:11:25 +02:00
|
|
|
import sys
|
2016-04-27 00:28:21 +02:00
|
|
|
import os
|
|
|
|
import re
|
2016-08-20 22:01:07 +02:00
|
|
|
import unittest
|
2015-11-21 23:09:44 +01:00
|
|
|
from datetime import datetime
|
2016-04-24 04:11:25 +02:00
|
|
|
from time import sleep
|
2016-09-06 15:38:07 +02:00
|
|
|
from queue import Queue
|
2016-04-27 00:28:21 +02:00
|
|
|
from random import randrange
|
2016-04-24 04:11:25 +02:00
|
|
|
|
2016-02-19 16:37:38 +01:00
|
|
|
from future.builtins import bytes
|
2015-11-06 00:23:34 +01:00
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
from telegram.utils.request import Request as Requester
|
|
|
|
|
2015-11-21 23:09:44 +01:00
|
|
|
try:
|
2016-02-19 16:37:38 +01:00
|
|
|
# python2
|
|
|
|
from urllib2 import urlopen, Request, HTTPError
|
2015-11-21 23:09:44 +01:00
|
|
|
except ImportError:
|
2016-02-19 16:37:38 +01:00
|
|
|
# python3
|
2015-11-21 23:09:44 +01:00
|
|
|
from urllib.request import Request, urlopen
|
2016-02-19 16:37:38 +01:00
|
|
|
from urllib.error import HTTPError
|
2015-11-06 00:23:34 +01:00
|
|
|
|
|
|
|
sys.path.append('.')
|
|
|
|
|
2016-08-06 14:47:45 +02:00
|
|
|
from telegram import Update, Message, TelegramError, User, Chat, Bot, InlineQuery, CallbackQuery
|
2016-04-18 18:50:49 +02:00
|
|
|
from telegram.ext import *
|
2016-03-14 14:50:12 +01:00
|
|
|
from telegram.ext.dispatcher import run_async
|
2016-03-14 00:36:01 +01:00
|
|
|
from telegram.error import Unauthorized, InvalidToken
|
2015-11-21 15:48:45 +01:00
|
|
|
from tests.base import BaseTest
|
2016-09-06 15:38:07 +02:00
|
|
|
from threading import Lock, Thread, current_thread, Semaphore
|
2015-11-06 00:23:34 +01:00
|
|
|
|
2015-11-21 21:22:58 +01:00
|
|
|
# Enable logging
|
|
|
|
root = logging.getLogger()
|
|
|
|
root.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
|
|
|
ch.setLevel(logging.WARN)
|
2016-05-15 02:52:35 +02:00
|
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s ' '- %(message)s')
|
2015-11-21 21:22:58 +01:00
|
|
|
ch.setFormatter(formatter)
|
|
|
|
root.addHandler(ch)
|
|
|
|
|
2015-11-06 00:23:34 +01:00
|
|
|
|
2015-11-24 21:06:55 +01:00
|
|
|
class UpdaterTest(BaseTest, unittest.TestCase):
|
|
|
|
"""
|
|
|
|
This object represents Tests for Updater, Dispatcher, WebhookServer and
|
|
|
|
WebhookHandler
|
|
|
|
"""
|
2015-11-06 00:23:34 +01:00
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
_updater = None
|
2016-05-26 14:39:11 +02:00
|
|
|
received_message = None
|
|
|
|
|
2015-11-06 00:23:34 +01:00
|
|
|
def setUp(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self.updater = None
|
2015-11-06 00:23:34 +01:00
|
|
|
self.received_message = None
|
|
|
|
self.message_count = 0
|
2015-11-21 19:35:24 +01:00
|
|
|
self.lock = Lock()
|
2015-11-06 00:23:34 +01:00
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
@property
|
|
|
|
def updater(self):
|
|
|
|
return self._updater
|
|
|
|
|
|
|
|
@updater.setter
|
|
|
|
def updater(self, val):
|
|
|
|
if self._updater:
|
|
|
|
self._updater.stop()
|
|
|
|
self._updater.dispatcher._reset_singleton()
|
|
|
|
del self._updater.dispatcher
|
|
|
|
|
|
|
|
self._updater = val
|
|
|
|
|
2015-12-26 18:32:36 +01:00
|
|
|
def _setup_updater(self, *args, **kwargs):
|
|
|
|
bot = MockBot(*args, **kwargs)
|
|
|
|
self.updater = Updater(workers=2, bot=bot)
|
|
|
|
|
2015-11-21 15:45:45 +01:00
|
|
|
def tearDown(self):
|
2016-09-06 15:38:07 +02:00
|
|
|
self.updater = None
|
2015-11-21 15:45:45 +01:00
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
def reset(self):
|
|
|
|
self.message_count = 0
|
|
|
|
self.received_message = None
|
|
|
|
|
2015-11-06 00:23:34 +01:00
|
|
|
def telegramHandlerTest(self, bot, update):
|
|
|
|
self.received_message = update.message.text
|
|
|
|
self.message_count += 1
|
|
|
|
|
2016-05-27 11:06:29 +02:00
|
|
|
def telegramHandlerEditedTest(self, bot, update):
|
|
|
|
self.received_message = update.edited_message.text
|
|
|
|
self.message_count += 1
|
|
|
|
|
2016-01-29 23:54:07 +01:00
|
|
|
def telegramInlineHandlerTest(self, bot, update):
|
2016-05-15 02:52:35 +02:00
|
|
|
self.received_message = (update.inline_query, update.chosen_inline_result)
|
2016-01-29 23:54:07 +01:00
|
|
|
self.message_count += 1
|
|
|
|
|
2016-04-21 13:07:44 +02:00
|
|
|
def telegramCallbackHandlerTest(self, bot, update):
|
|
|
|
self.received_message = update.callback_query
|
|
|
|
self.message_count += 1
|
|
|
|
|
2015-11-21 19:35:24 +01:00
|
|
|
@run_async
|
2016-04-18 18:50:49 +02:00
|
|
|
def asyncHandlerTest(self, bot, update):
|
2015-11-21 19:35:24 +01:00
|
|
|
sleep(1)
|
|
|
|
with self.lock:
|
|
|
|
self.received_message = update.message.text
|
|
|
|
self.message_count += 1
|
|
|
|
|
2015-11-06 00:23:34 +01:00
|
|
|
def stringHandlerTest(self, bot, update):
|
|
|
|
self.received_message = update
|
|
|
|
self.message_count += 1
|
|
|
|
|
2016-04-18 18:50:49 +02:00
|
|
|
def regexGroupHandlerTest(self, bot, update, groups, groupdict):
|
2015-12-21 20:25:31 +01:00
|
|
|
self.received_message = (groups, groupdict)
|
|
|
|
self.message_count += 1
|
|
|
|
|
2016-05-26 14:39:11 +02:00
|
|
|
def additionalArgsTest(self, bot, update, update_queue, job_queue, args):
|
|
|
|
job_queue.put(Job(lambda bot, job: job.schedule_removal(), 0.1))
|
|
|
|
|
2015-11-22 14:08:33 +01:00
|
|
|
self.received_message = update
|
|
|
|
self.message_count += 1
|
2016-05-26 14:39:11 +02:00
|
|
|
|
2015-11-22 14:08:33 +01:00
|
|
|
if args[0] == 'resend':
|
|
|
|
update_queue.put('/test5 noresend')
|
|
|
|
elif args[0] == 'noresend':
|
|
|
|
pass
|
|
|
|
|
2016-10-25 19:28:34 +02:00
|
|
|
def userAndChatDataTest(self, bot, update, user_data, chat_data):
|
|
|
|
user_data['text'] = update.message.text
|
|
|
|
chat_data['text'] = update.message.text
|
|
|
|
self.received_message = update.message.text
|
|
|
|
self.message_count += 1
|
|
|
|
|
2015-12-21 19:36:17 +01:00
|
|
|
@run_async
|
2016-04-18 18:50:49 +02:00
|
|
|
def asyncAdditionalHandlerTest(self, bot, update, update_queue=None):
|
2015-12-21 19:36:17 +01:00
|
|
|
sleep(1)
|
|
|
|
with self.lock:
|
|
|
|
if update_queue is not None:
|
|
|
|
self.received_message = update.message.text
|
|
|
|
self.message_count += 1
|
|
|
|
|
2015-11-24 19:35:02 +01:00
|
|
|
def errorRaisingHandlerTest(self, bot, update):
|
|
|
|
raise TelegramError(update)
|
|
|
|
|
2015-11-21 15:45:45 +01:00
|
|
|
def errorHandlerTest(self, bot, update, error):
|
2015-11-24 19:35:02 +01:00
|
|
|
self.received_message = error.message
|
2015-11-21 15:45:45 +01:00
|
|
|
self.message_count += 1
|
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
def test_addRemoveTelegramMessageHandler(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('Test')
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-04-25 13:26:52 +02:00
|
|
|
from telegram.ext import Filters
|
|
|
|
handler = MessageHandler([Filters.text], self.telegramHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2015-11-24 19:35:02 +01:00
|
|
|
self.updater.start_polling(0.01)
|
2015-11-06 00:23:34 +01:00
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, 'Test')
|
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
# Remove handler
|
2016-05-26 14:39:11 +02:00
|
|
|
d.remove_handler(handler)
|
2015-11-24 20:34:38 +01:00
|
|
|
self.reset()
|
|
|
|
|
2015-12-26 18:32:36 +01:00
|
|
|
self.updater.bot.send_messages = 1
|
2015-11-24 20:34:38 +01:00
|
|
|
sleep(.1)
|
2015-11-24 20:40:40 +01:00
|
|
|
self.assertTrue(None is self.received_message)
|
2015-11-24 20:34:38 +01:00
|
|
|
|
2016-05-27 11:06:29 +02:00
|
|
|
def test_editedMessageHandler(self):
|
|
|
|
self._setup_updater('Test', edited=True)
|
|
|
|
d = self.updater.dispatcher
|
|
|
|
from telegram.ext import Filters
|
2016-10-15 22:59:41 +02:00
|
|
|
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, allow_edited=True)
|
2017-04-25 10:39:02 +02:00
|
|
|
d.add_handler(handler)
|
2016-05-27 11:06:29 +02:00
|
|
|
self.updater.start_polling(0.01)
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, 'Test')
|
|
|
|
|
|
|
|
# Remove handler
|
2017-04-25 10:39:02 +02:00
|
|
|
d.remove_handler(handler)
|
2016-10-15 22:59:41 +02:00
|
|
|
handler = MessageHandler(Filters.text, self.telegramHandlerEditedTest, allow_edited=False)
|
2017-04-25 10:39:02 +02:00
|
|
|
d.add_handler(handler)
|
2016-05-27 11:06:29 +02:00
|
|
|
self.reset()
|
|
|
|
|
|
|
|
self.updater.bot.send_messages = 1
|
|
|
|
sleep(.1)
|
|
|
|
self.assertTrue(None is self.received_message)
|
|
|
|
|
2015-11-06 00:23:34 +01:00
|
|
|
def test_addTelegramMessageHandlerMultipleMessages(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('Multiple', 100)
|
2016-10-15 22:59:41 +02:00
|
|
|
self.updater.dispatcher.add_handler(MessageHandler(Filters.all, self.telegramHandlerTest))
|
2015-11-22 19:16:49 +01:00
|
|
|
self.updater.start_polling(0.0)
|
2015-12-01 21:58:39 +01:00
|
|
|
sleep(2)
|
2015-11-06 00:23:34 +01:00
|
|
|
self.assertEqual(self.received_message, 'Multiple')
|
2015-11-21 16:18:19 +01:00
|
|
|
self.assertEqual(self.message_count, 100)
|
2015-11-06 00:23:34 +01:00
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
def test_addRemoveTelegramRegexHandler(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('Test2')
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
|
|
|
regobj = re.compile('Te.*')
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = RegexHandler(regobj, self.telegramHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
self.updater.dispatcher.add_handler(handler)
|
2015-11-24 19:35:02 +01:00
|
|
|
self.updater.start_polling(0.01)
|
2015-11-06 00:23:34 +01:00
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, 'Test2')
|
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
# Remove handler
|
2016-05-26 14:39:11 +02:00
|
|
|
d.remove_handler(handler)
|
2015-11-24 20:34:38 +01:00
|
|
|
self.reset()
|
|
|
|
|
2015-12-26 18:32:36 +01:00
|
|
|
self.updater.bot.send_messages = 1
|
2015-11-24 20:34:38 +01:00
|
|
|
sleep(.1)
|
2015-11-24 20:40:40 +01:00
|
|
|
self.assertTrue(None is self.received_message)
|
2015-11-24 20:34:38 +01:00
|
|
|
|
|
|
|
def test_addRemoveTelegramCommandHandler(self):
|
2017-02-27 14:26:53 +01:00
|
|
|
self._setup_updater('', messages=0)
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = CommandHandler('test', self.telegramHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
self.updater.dispatcher.add_handler(handler)
|
2017-02-27 14:26:53 +01:00
|
|
|
user = User(first_name="singelton", id=404)
|
|
|
|
bot = self.updater.bot
|
|
|
|
queue = self.updater.start_polling(0.01)
|
|
|
|
|
|
|
|
# regular use
|
|
|
|
message = Message(0, user, None, None, text="/test", bot=bot)
|
|
|
|
queue.put(Update(update_id=0, message=message))
|
2015-11-06 00:23:34 +01:00
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, '/test')
|
|
|
|
|
2017-02-27 14:26:53 +01:00
|
|
|
# assigned use
|
|
|
|
message = Message(0, user, None, None, text="/test@MockBot", bot=bot)
|
|
|
|
queue.put(Update(update_id=0, message=message))
|
2015-11-24 20:34:38 +01:00
|
|
|
sleep(.1)
|
2017-02-27 14:26:53 +01:00
|
|
|
self.assertEqual(self.received_message, '/test@MockBot')
|
2017-04-25 10:37:06 +02:00
|
|
|
message.text = "/test@mockbot"
|
|
|
|
queue.put(Update(update_id=0, message=message))
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, '/test@mockbot')
|
2015-11-24 20:34:38 +01:00
|
|
|
|
2017-02-27 14:26:53 +01:00
|
|
|
# directed at other bot
|
|
|
|
self.reset()
|
|
|
|
message = Message(0, user, None, None, text="/test@OtherBot", bot=bot)
|
|
|
|
queue.put(Update(update_id=0, message=message))
|
2016-05-27 11:06:29 +02:00
|
|
|
sleep(.1)
|
2017-02-27 14:26:53 +01:00
|
|
|
self.assertTrue(None is self.received_message)
|
2016-05-27 11:06:29 +02:00
|
|
|
|
|
|
|
# Remove handler
|
2017-04-25 10:39:02 +02:00
|
|
|
d.remove_handler(handler)
|
2016-05-27 11:06:29 +02:00
|
|
|
handler = CommandHandler('test', self.telegramHandlerEditedTest, allow_edited=False)
|
2017-04-25 10:39:02 +02:00
|
|
|
d.add_handler(handler)
|
2016-05-27 11:06:29 +02:00
|
|
|
self.reset()
|
|
|
|
|
|
|
|
self.updater.bot.send_messages = 1
|
|
|
|
sleep(.1)
|
|
|
|
self.assertTrue(None is self.received_message)
|
|
|
|
|
2017-02-28 15:44:55 +01:00
|
|
|
def test_filterPassTelegramCommandHandler(self):
|
|
|
|
self._setup_updater('', messages=0)
|
|
|
|
d = self.updater.dispatcher
|
|
|
|
handler = CommandHandler('test', self.telegramHandlerTest, lambda msg: True)
|
|
|
|
self.updater.dispatcher.add_handler(handler)
|
|
|
|
user = User(first_name="singelton", id=404)
|
|
|
|
bot = self.updater.bot
|
|
|
|
queue = self.updater.start_polling(0.01)
|
|
|
|
|
|
|
|
message = Message(0, user, None, None, text="/test", bot=bot)
|
|
|
|
queue.put(Update(update_id=0, message=message))
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, '/test')
|
|
|
|
|
|
|
|
def test_filterNotPassTelegramCommandHandler(self):
|
|
|
|
self._setup_updater('', messages=0)
|
|
|
|
d = self.updater.dispatcher
|
|
|
|
handler = CommandHandler('test', self.telegramHandlerTest, lambda msg: False)
|
|
|
|
self.updater.dispatcher.add_handler(handler)
|
|
|
|
user = User(first_name="singelton", id=404)
|
|
|
|
bot = self.updater.bot
|
|
|
|
queue = self.updater.start_polling(0.01)
|
|
|
|
|
|
|
|
message = Message(0, user, None, None, text="/test", bot=bot)
|
|
|
|
queue.put(Update(update_id=0, message=message))
|
|
|
|
sleep(.1)
|
|
|
|
self.assertTrue(None is self.received_message)
|
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
def test_addRemoveStringRegexHandler(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('', messages=0)
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = StringRegexHandler('Te.*', self.stringHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2015-11-24 19:35:02 +01:00
|
|
|
queue = self.updater.start_polling(0.01)
|
2015-11-06 00:23:34 +01:00
|
|
|
queue.put('Test3')
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, 'Test3')
|
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
# Remove handler
|
2016-05-26 14:39:11 +02:00
|
|
|
d.remove_handler(handler)
|
2015-11-24 20:34:38 +01:00
|
|
|
self.reset()
|
|
|
|
|
|
|
|
queue.put('Test3')
|
|
|
|
sleep(.1)
|
2015-11-24 20:40:40 +01:00
|
|
|
self.assertTrue(None is self.received_message)
|
2015-11-24 20:34:38 +01:00
|
|
|
|
|
|
|
def test_addRemoveStringCommandHandler(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('', messages=0)
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = StringCommandHandler('test3', self.stringHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2015-11-06 00:23:34 +01:00
|
|
|
|
2015-11-24 19:35:02 +01:00
|
|
|
queue = self.updater.start_polling(0.01)
|
2015-11-06 00:23:34 +01:00
|
|
|
queue.put('/test3')
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, '/test3')
|
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
# Remove handler
|
2016-05-26 14:39:11 +02:00
|
|
|
d.remove_handler(handler)
|
2015-11-24 20:34:38 +01:00
|
|
|
self.reset()
|
|
|
|
|
|
|
|
queue.put('/test3')
|
|
|
|
sleep(.1)
|
2015-11-24 20:40:40 +01:00
|
|
|
self.assertTrue(None is self.received_message)
|
2015-11-24 20:34:38 +01:00
|
|
|
|
|
|
|
def test_addRemoveErrorHandler(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('', messages=0)
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_error_handler(self.errorHandlerTest)
|
2015-11-24 19:35:02 +01:00
|
|
|
queue = self.updater.start_polling(0.01)
|
2015-11-06 00:23:34 +01:00
|
|
|
error = TelegramError("Unauthorized.")
|
|
|
|
queue.put(error)
|
|
|
|
sleep(.1)
|
2015-11-24 19:35:02 +01:00
|
|
|
self.assertEqual(self.received_message, "Unauthorized.")
|
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
# Remove handler
|
2016-05-26 14:39:11 +02:00
|
|
|
d.remove_error_handler(self.errorHandlerTest)
|
2015-11-24 20:34:38 +01:00
|
|
|
self.reset()
|
|
|
|
|
|
|
|
queue.put(error)
|
|
|
|
sleep(.1)
|
2015-11-24 20:40:40 +01:00
|
|
|
self.assertTrue(None is self.received_message)
|
2015-11-24 20:34:38 +01:00
|
|
|
|
2015-11-24 19:35:02 +01:00
|
|
|
def test_errorInHandler(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('', messages=0)
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = StringRegexHandler('.*', self.errorRaisingHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
|
|
|
self.updater.dispatcher.add_error_handler(self.errorHandlerTest)
|
2015-11-24 19:35:02 +01:00
|
|
|
queue = self.updater.start_polling(0.01)
|
|
|
|
|
|
|
|
queue.put('Test Error 1')
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, 'Test Error 1')
|
2015-11-06 00:23:34 +01:00
|
|
|
|
2016-03-01 20:40:04 +01:00
|
|
|
def test_cleanBeforeStart(self):
|
|
|
|
self._setup_updater('')
|
|
|
|
d = self.updater.dispatcher
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = MessageHandler([], self.telegramHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2016-03-01 20:40:04 +01:00
|
|
|
self.updater.start_polling(0.01, clean=True)
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.message_count, 0)
|
|
|
|
self.assertIsNone(self.received_message)
|
|
|
|
|
2015-11-21 21:22:58 +01:00
|
|
|
def test_errorOnGetUpdates(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('', raise_error=True)
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_error_handler(self.errorHandlerTest)
|
2015-11-24 19:35:02 +01:00
|
|
|
self.updater.start_polling(0.01)
|
2015-11-21 21:22:58 +01:00
|
|
|
sleep(.1)
|
2015-11-24 19:35:02 +01:00
|
|
|
self.assertEqual(self.received_message, "Test Error 2")
|
2015-11-21 21:22:58 +01:00
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
def test_addRemoveTypeHandler(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('', messages=0)
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = TypeHandler(dict, self.stringHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2015-11-24 19:35:02 +01:00
|
|
|
queue = self.updater.start_polling(0.01)
|
2015-11-06 00:23:34 +01:00
|
|
|
payload = {"Test": 42}
|
|
|
|
queue.put(payload)
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, payload)
|
|
|
|
|
2015-11-24 20:34:38 +01:00
|
|
|
# Remove handler
|
2016-05-26 14:39:11 +02:00
|
|
|
d.remove_handler(handler)
|
2015-11-24 20:34:38 +01:00
|
|
|
self.reset()
|
|
|
|
|
|
|
|
queue.put(payload)
|
|
|
|
sleep(.1)
|
2015-11-24 20:40:40 +01:00
|
|
|
self.assertTrue(None is self.received_message)
|
2015-11-24 20:34:38 +01:00
|
|
|
|
2016-04-18 18:50:49 +02:00
|
|
|
def test_addRemoveInlineQueryHandler(self):
|
2016-01-29 23:54:07 +01:00
|
|
|
self._setup_updater('', messages=0)
|
|
|
|
d = self.updater.dispatcher
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = InlineQueryHandler(self.telegramInlineHandlerTest)
|
|
|
|
handler2 = ChosenInlineResultHandler(self.telegramInlineHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
|
|
|
d.add_handler(handler2)
|
2016-01-29 23:54:07 +01:00
|
|
|
queue = self.updater.start_polling(0.01)
|
|
|
|
update = Update(update_id=0, inline_query="testquery")
|
|
|
|
update2 = Update(update_id=0, chosen_inline_result="testresult")
|
|
|
|
queue.put(update)
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message[0], "testquery")
|
|
|
|
|
|
|
|
queue.put(update2)
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message[1], "testresult")
|
|
|
|
|
|
|
|
# Remove handler
|
2016-05-26 14:39:11 +02:00
|
|
|
d.remove_handler(handler)
|
|
|
|
d.remove_handler(handler2)
|
2016-01-29 23:54:07 +01:00
|
|
|
self.reset()
|
|
|
|
|
|
|
|
queue.put(update)
|
|
|
|
sleep(.1)
|
|
|
|
self.assertTrue(None is self.received_message)
|
|
|
|
|
2016-04-21 13:07:44 +02:00
|
|
|
def test_addRemoveCallbackQueryHandler(self):
|
|
|
|
self._setup_updater('', messages=0)
|
|
|
|
d = self.updater.dispatcher
|
|
|
|
handler = CallbackQueryHandler(self.telegramCallbackHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2016-04-21 13:07:44 +02:00
|
|
|
queue = self.updater.start_polling(0.01)
|
|
|
|
update = Update(update_id=0, callback_query="testcallback")
|
|
|
|
queue.put(update)
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, "testcallback")
|
|
|
|
|
|
|
|
# Remove handler
|
2016-05-26 14:39:11 +02:00
|
|
|
d.remove_handler(handler)
|
2016-04-21 13:07:44 +02:00
|
|
|
self.reset()
|
|
|
|
|
|
|
|
queue.put(update)
|
|
|
|
sleep(.1)
|
|
|
|
self.assertTrue(None is self.received_message)
|
|
|
|
|
2015-11-21 19:35:24 +01:00
|
|
|
def test_runAsync(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('Test5', messages=2)
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = MessageHandler([], self.asyncHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2015-11-22 19:16:49 +01:00
|
|
|
self.updater.start_polling(0.01)
|
2015-11-21 19:35:24 +01:00
|
|
|
sleep(1.2)
|
2015-11-24 19:35:02 +01:00
|
|
|
self.assertEqual(self.received_message, 'Test5')
|
2015-11-21 19:35:24 +01:00
|
|
|
self.assertEqual(self.message_count, 2)
|
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
def test_multiple_dispatchers(self):
|
|
|
|
|
|
|
|
def get_dispatcher_name(q):
|
|
|
|
q.put(current_thread().name)
|
|
|
|
sleep(1.2)
|
|
|
|
|
2016-09-27 09:21:35 +02:00
|
|
|
d1 = Dispatcher(MockBot('disp1'), Queue())
|
|
|
|
d2 = Dispatcher(MockBot('disp2'), Queue())
|
2016-09-06 15:38:07 +02:00
|
|
|
q1 = Queue()
|
|
|
|
q2 = Queue()
|
2016-09-27 09:21:35 +02:00
|
|
|
d1._init_async_threads('test_1', workers=1)
|
|
|
|
d2._init_async_threads('test_2', workers=1)
|
2016-09-06 15:38:07 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
d1.run_async(get_dispatcher_name, q1)
|
|
|
|
d2.run_async(get_dispatcher_name, q2)
|
|
|
|
|
|
|
|
name1 = q1.get()
|
|
|
|
name2 = q2.get()
|
|
|
|
|
|
|
|
self.assertNotEqual(name1, name2)
|
|
|
|
finally:
|
|
|
|
d1.stop()
|
|
|
|
d2.stop()
|
|
|
|
# following three lines are for pypy unitests
|
|
|
|
d1._reset_singleton()
|
|
|
|
del d1
|
|
|
|
del d2
|
|
|
|
|
|
|
|
def test_multiple_dispatcers_no_decorator(self):
|
|
|
|
|
|
|
|
@run_async
|
|
|
|
def must_raise_runtime_error():
|
|
|
|
pass
|
|
|
|
|
|
|
|
d1 = Dispatcher(MockBot('disp1'), Queue(), workers=1)
|
|
|
|
d2 = Dispatcher(MockBot('disp2'), Queue(), workers=1)
|
|
|
|
|
|
|
|
self.assertRaises(RuntimeError, must_raise_runtime_error)
|
|
|
|
|
|
|
|
d1.stop()
|
|
|
|
d2.stop()
|
|
|
|
# following three lines are for pypy unitests
|
|
|
|
d1._reset_singleton()
|
|
|
|
del d1
|
|
|
|
del d2
|
|
|
|
|
2015-11-22 14:08:33 +01:00
|
|
|
def test_additionalArgs(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('', messages=0)
|
2016-08-26 11:17:05 +02:00
|
|
|
handler = StringCommandHandler(
|
|
|
|
'test5',
|
|
|
|
self.additionalArgsTest,
|
|
|
|
pass_update_queue=True,
|
|
|
|
pass_job_queue=True,
|
|
|
|
pass_args=True)
|
2016-05-26 14:39:11 +02:00
|
|
|
self.updater.dispatcher.add_handler(handler)
|
2015-11-22 14:08:33 +01:00
|
|
|
|
2015-11-24 19:35:02 +01:00
|
|
|
queue = self.updater.start_polling(0.01)
|
2015-11-22 14:08:33 +01:00
|
|
|
queue.put('/test5 resend')
|
2015-11-24 19:35:02 +01:00
|
|
|
sleep(.1)
|
2015-11-22 14:08:33 +01:00
|
|
|
self.assertEqual(self.received_message, '/test5 noresend')
|
|
|
|
self.assertEqual(self.message_count, 2)
|
|
|
|
|
2016-10-25 19:28:34 +02:00
|
|
|
def test_user_and_chat_data(self):
|
|
|
|
self._setup_updater('/test_data', messages=1)
|
|
|
|
handler = CommandHandler(
|
|
|
|
'test_data', self.userAndChatDataTest, pass_user_data=True, pass_chat_data=True)
|
|
|
|
self.updater.dispatcher.add_handler(handler)
|
|
|
|
|
|
|
|
self.updater.start_polling(0.01)
|
|
|
|
sleep(.1)
|
|
|
|
self.assertEqual(self.received_message, '/test_data')
|
|
|
|
self.assertEqual(self.message_count, 1)
|
|
|
|
self.assertDictEqual(dict(self.updater.dispatcher.user_data), {0: {'text': '/test_data'}})
|
|
|
|
self.assertDictEqual(dict(self.updater.dispatcher.chat_data), {0: {'text': '/test_data'}})
|
|
|
|
|
2015-12-21 20:25:31 +01:00
|
|
|
def test_regexGroupHandler(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('', messages=0)
|
2015-12-21 20:25:31 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-08-26 11:17:05 +02:00
|
|
|
handler = StringRegexHandler(
|
|
|
|
'^(This).*?(?P<testgroup>regex group).*',
|
|
|
|
self.regexGroupHandlerTest,
|
|
|
|
pass_groupdict=True,
|
|
|
|
pass_groups=True)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2015-12-21 20:25:31 +01:00
|
|
|
queue = self.updater.start_polling(0.01)
|
|
|
|
queue.put('This is a test message for regex group matching.')
|
|
|
|
sleep(.1)
|
2016-10-12 22:56:57 +02:00
|
|
|
self.assertEqual(self.received_message, (('This', 'regex group'), {
|
|
|
|
'testgroup': 'regex group'
|
|
|
|
}))
|
2015-12-21 20:25:31 +01:00
|
|
|
|
2016-08-06 14:47:45 +02:00
|
|
|
def test_regexGroupHandlerInlineQuery(self):
|
|
|
|
self._setup_updater('', messages=0)
|
|
|
|
d = self.updater.dispatcher
|
2016-08-26 11:17:05 +02:00
|
|
|
handler = InlineQueryHandler(
|
|
|
|
self.regexGroupHandlerTest,
|
|
|
|
pattern='^(This).*?(?P<testgroup>regex group).*',
|
|
|
|
pass_groupdict=True,
|
|
|
|
pass_groups=True)
|
2016-08-06 14:47:45 +02:00
|
|
|
d.add_handler(handler)
|
|
|
|
queue = self.updater.start_polling(0.01)
|
2016-08-26 11:17:05 +02:00
|
|
|
queue.put(
|
|
|
|
Update(
|
|
|
|
update_id=0,
|
|
|
|
inline_query=InlineQuery(
|
|
|
|
0, None, 'This is a test message for regex group matching.', None)))
|
2016-08-06 14:47:45 +02:00
|
|
|
|
|
|
|
sleep(.1)
|
2016-10-12 22:56:57 +02:00
|
|
|
self.assertEqual(self.received_message, (('This', 'regex group'), {
|
|
|
|
'testgroup': 'regex group'
|
|
|
|
}))
|
2016-08-06 14:47:45 +02:00
|
|
|
|
|
|
|
def test_regexGroupHandlerCallbackQuery(self):
|
|
|
|
self._setup_updater('', messages=0)
|
|
|
|
d = self.updater.dispatcher
|
2016-08-26 11:17:05 +02:00
|
|
|
handler = CallbackQueryHandler(
|
|
|
|
self.regexGroupHandlerTest,
|
|
|
|
pattern='^(This).*?(?P<testgroup>regex group).*',
|
|
|
|
pass_groupdict=True,
|
|
|
|
pass_groups=True)
|
2016-08-06 14:47:45 +02:00
|
|
|
d.add_handler(handler)
|
|
|
|
queue = self.updater.start_polling(0.01)
|
2016-08-26 11:17:05 +02:00
|
|
|
queue.put(
|
|
|
|
Update(
|
|
|
|
update_id=0,
|
2016-10-03 21:29:30 +02:00
|
|
|
callback_query=CallbackQuery(
|
2016-10-07 22:32:54 +02:00
|
|
|
0, None, None, data='This is a test message for regex group matching.')))
|
2016-08-06 14:47:45 +02:00
|
|
|
|
|
|
|
sleep(.1)
|
2016-10-12 22:56:57 +02:00
|
|
|
self.assertEqual(self.received_message, (('This', 'regex group'), {
|
|
|
|
'testgroup': 'regex group'
|
|
|
|
}))
|
2016-08-06 14:47:45 +02:00
|
|
|
|
2015-12-21 19:36:17 +01:00
|
|
|
def test_runAsyncWithAdditionalArgs(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('Test6', messages=2)
|
2015-12-21 19:36:17 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-05-15 02:52:35 +02:00
|
|
|
handler = MessageHandler([], self.asyncAdditionalHandlerTest, pass_update_queue=True)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2015-12-21 19:36:17 +01:00
|
|
|
self.updater.start_polling(0.01)
|
|
|
|
sleep(1.2)
|
|
|
|
self.assertEqual(self.received_message, 'Test6')
|
|
|
|
self.assertEqual(self.message_count, 2)
|
|
|
|
|
2015-11-21 21:22:58 +01:00
|
|
|
def test_webhook(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('', messages=0)
|
2015-11-24 20:34:38 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = MessageHandler([], self.telegramHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2015-11-21 23:22:41 +01:00
|
|
|
|
2016-02-19 16:37:38 +01:00
|
|
|
ip = '127.0.0.1'
|
|
|
|
port = randrange(1024, 49152) # Select random port for travis
|
2016-08-26 11:17:05 +02:00
|
|
|
self.updater.start_webhook(
|
|
|
|
ip,
|
|
|
|
port,
|
|
|
|
url_path='TOKEN',
|
|
|
|
cert='./tests/test_updater.py',
|
|
|
|
key='./tests/test_updater.py',
|
|
|
|
webhook_url=None)
|
2015-11-21 23:09:44 +01:00
|
|
|
sleep(0.5)
|
|
|
|
# SSL-Wrapping will fail, so we start the server without SSL
|
2015-11-22 19:16:49 +01:00
|
|
|
Thread(target=self.updater.httpd.serve_forever).start()
|
2015-11-21 23:09:44 +01:00
|
|
|
|
|
|
|
# Now, we send an update to the server via urlopen
|
2016-08-26 11:17:05 +02:00
|
|
|
message = Message(
|
|
|
|
1, User(1, "Tester"), datetime.now(), Chat(
|
|
|
|
1, "group", title="Test Group"))
|
2015-11-21 21:22:58 +01:00
|
|
|
|
2015-11-21 23:09:44 +01:00
|
|
|
message.text = "Webhook Test"
|
|
|
|
update = Update(1)
|
|
|
|
update.message = message
|
|
|
|
|
2016-02-19 16:37:38 +01:00
|
|
|
self._send_webhook_msg(ip, port, update.to_json(), 'TOKEN')
|
2015-11-21 23:09:44 +01:00
|
|
|
|
|
|
|
sleep(1)
|
|
|
|
self.assertEqual(self.received_message, 'Webhook Test')
|
2015-11-21 21:22:58 +01:00
|
|
|
|
2015-11-30 23:00:56 +01:00
|
|
|
print("Test other webhook server functionalities...")
|
2016-02-19 16:37:38 +01:00
|
|
|
response = self._send_webhook_msg(ip, port, None, 'webookhandler.py')
|
2015-11-24 19:35:02 +01:00
|
|
|
self.assertEqual(b'', response.read())
|
|
|
|
self.assertEqual(200, response.code)
|
|
|
|
|
2016-04-26 23:23:15 +02:00
|
|
|
response = self._send_webhook_msg(
|
2016-08-26 11:17:05 +02:00
|
|
|
ip, port, None, 'webookhandler.py', get_method=lambda: 'HEAD')
|
2015-11-24 19:35:02 +01:00
|
|
|
|
|
|
|
self.assertEqual(b'', response.read())
|
|
|
|
self.assertEqual(200, response.code)
|
|
|
|
|
|
|
|
# Test multiple shutdown() calls
|
|
|
|
self.updater.httpd.shutdown()
|
|
|
|
self.updater.httpd.shutdown()
|
|
|
|
self.assertTrue(True)
|
|
|
|
|
2015-11-30 23:00:56 +01:00
|
|
|
def test_webhook_no_ssl(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('', messages=0)
|
2015-11-30 23:00:56 +01:00
|
|
|
d = self.updater.dispatcher
|
2016-04-18 18:50:49 +02:00
|
|
|
handler = MessageHandler([], self.telegramHandlerTest)
|
2016-05-26 14:39:11 +02:00
|
|
|
d.add_handler(handler)
|
2015-11-30 23:00:56 +01:00
|
|
|
|
2016-02-19 16:37:38 +01:00
|
|
|
ip = '127.0.0.1'
|
|
|
|
port = randrange(1024, 49152) # Select random port for travis
|
2016-04-21 13:07:44 +02:00
|
|
|
self.updater.start_webhook(ip, port, webhook_url=None)
|
2015-11-30 23:00:56 +01:00
|
|
|
sleep(0.5)
|
|
|
|
|
|
|
|
# Now, we send an update to the server via urlopen
|
2016-08-26 11:17:05 +02:00
|
|
|
message = Message(
|
|
|
|
1, User(1, "Tester 2"), datetime.now(), Chat(
|
|
|
|
1, 'group', title="Test Group 2"))
|
2015-11-30 23:00:56 +01:00
|
|
|
|
|
|
|
message.text = "Webhook Test 2"
|
|
|
|
update = Update(1)
|
|
|
|
update.message = message
|
|
|
|
|
2016-02-19 16:37:38 +01:00
|
|
|
self._send_webhook_msg(ip, port, update.to_json())
|
|
|
|
sleep(1)
|
|
|
|
self.assertEqual(self.received_message, 'Webhook Test 2')
|
|
|
|
|
2016-04-21 13:07:44 +02:00
|
|
|
def test_start_dispatcher_twice(self):
|
|
|
|
self._setup_updater('', messages=0)
|
|
|
|
self.updater.start_polling(0.1)
|
2016-09-27 09:21:35 +02:00
|
|
|
sleep(0.5)
|
|
|
|
self.updater.dispatcher.start()
|
2016-04-21 13:07:44 +02:00
|
|
|
|
2016-03-14 00:36:01 +01:00
|
|
|
def test_bootstrap_retries_success(self):
|
|
|
|
retries = 3
|
|
|
|
self._setup_updater('', messages=0, bootstrap_retries=retries)
|
|
|
|
|
2016-05-10 23:58:55 +02:00
|
|
|
self.updater._bootstrap(retries, False, 'path', None)
|
2016-03-14 00:36:01 +01:00
|
|
|
self.assertEqual(self.updater.bot.bootstrap_attempts, retries)
|
|
|
|
|
|
|
|
def test_bootstrap_retries_unauth(self):
|
|
|
|
retries = 3
|
2016-05-15 02:52:35 +02:00
|
|
|
self._setup_updater(
|
2016-08-26 11:17:05 +02:00
|
|
|
'', messages=0, bootstrap_retries=retries, bootstrap_err=Unauthorized())
|
2016-03-14 00:36:01 +01:00
|
|
|
|
2016-05-15 02:52:35 +02:00
|
|
|
self.assertRaises(Unauthorized, self.updater._bootstrap, retries, False, 'path', None)
|
2016-03-14 00:36:01 +01:00
|
|
|
self.assertEqual(self.updater.bot.bootstrap_attempts, 1)
|
|
|
|
|
|
|
|
def test_bootstrap_retries_invalid_token(self):
|
|
|
|
retries = 3
|
2016-05-15 02:52:35 +02:00
|
|
|
self._setup_updater(
|
2016-08-26 11:17:05 +02:00
|
|
|
'', messages=0, bootstrap_retries=retries, bootstrap_err=InvalidToken())
|
2016-03-14 00:36:01 +01:00
|
|
|
|
2016-05-15 02:52:35 +02:00
|
|
|
self.assertRaises(InvalidToken, self.updater._bootstrap, retries, False, 'path', None)
|
2016-03-14 00:36:01 +01:00
|
|
|
self.assertEqual(self.updater.bot.bootstrap_attempts, 1)
|
|
|
|
|
|
|
|
def test_bootstrap_retries_fail(self):
|
|
|
|
retries = 1
|
|
|
|
self._setup_updater('', messages=0, bootstrap_retries=retries)
|
|
|
|
|
2016-05-15 02:52:35 +02:00
|
|
|
self.assertRaisesRegexp(TelegramError, 'test', self.updater._bootstrap, retries - 1, False,
|
|
|
|
'path', None)
|
2016-03-14 00:36:01 +01:00
|
|
|
self.assertEqual(self.updater.bot.bootstrap_attempts, 1)
|
|
|
|
|
2016-02-19 16:37:38 +01:00
|
|
|
def test_webhook_invalid_posts(self):
|
|
|
|
self._setup_updater('', messages=0)
|
|
|
|
|
|
|
|
ip = '127.0.0.1'
|
|
|
|
port = randrange(1024, 49152) # select random port for travis
|
2016-08-26 11:17:05 +02:00
|
|
|
thr = Thread(
|
2017-03-25 12:46:37 +01:00
|
|
|
target=self.updater._start_webhook,
|
|
|
|
args=(ip, port, '', None, None, 0, False, None, None))
|
2016-02-19 16:37:38 +01:00
|
|
|
thr.start()
|
2015-11-30 23:00:56 +01:00
|
|
|
|
2016-02-19 16:37:38 +01:00
|
|
|
sleep(0.5)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with self.assertRaises(HTTPError) as ctx:
|
2016-08-26 11:17:05 +02:00
|
|
|
self._send_webhook_msg(
|
|
|
|
ip, port, '<root><bla>data</bla></root>', content_type='application/xml')
|
2016-02-19 16:37:38 +01:00
|
|
|
self.assertEqual(ctx.exception.code, 403)
|
|
|
|
|
|
|
|
with self.assertRaises(HTTPError) as ctx:
|
2016-05-15 02:52:35 +02:00
|
|
|
self._send_webhook_msg(ip, port, 'dummy-payload', content_len=-2)
|
2016-02-19 16:37:38 +01:00
|
|
|
self.assertEqual(ctx.exception.code, 403)
|
|
|
|
|
|
|
|
# TODO: prevent urllib or the underlying from adding content-length
|
|
|
|
# with self.assertRaises(HTTPError) as ctx:
|
|
|
|
# self._send_webhook_msg(ip, port, 'dummy-payload',
|
|
|
|
# content_len=None)
|
|
|
|
# self.assertEqual(ctx.exception.code, 411)
|
|
|
|
|
|
|
|
with self.assertRaises(HTTPError) as ctx:
|
2016-05-15 02:52:35 +02:00
|
|
|
self._send_webhook_msg(ip, port, 'dummy-payload', content_len='not-a-number')
|
2016-02-19 16:37:38 +01:00
|
|
|
self.assertEqual(ctx.exception.code, 403)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
self.updater._stop_httpd()
|
|
|
|
thr.join()
|
|
|
|
|
2016-04-26 23:23:15 +02:00
|
|
|
def _send_webhook_msg(self,
|
|
|
|
ip,
|
|
|
|
port,
|
|
|
|
payload_str,
|
|
|
|
url_path='',
|
|
|
|
content_len=-1,
|
|
|
|
content_type='application/json',
|
2016-02-19 16:37:38 +01:00
|
|
|
get_method=None):
|
2016-05-15 02:52:35 +02:00
|
|
|
headers = {'content-type': content_type,}
|
2015-11-30 23:00:56 +01:00
|
|
|
|
2016-02-19 16:37:38 +01:00
|
|
|
if not payload_str:
|
|
|
|
content_len = None
|
|
|
|
payload = None
|
|
|
|
else:
|
|
|
|
payload = bytes(payload_str, encoding='utf-8')
|
2015-11-30 23:00:56 +01:00
|
|
|
|
2016-02-19 16:37:38 +01:00
|
|
|
if content_len == -1:
|
|
|
|
content_len = len(payload)
|
|
|
|
|
|
|
|
if content_len is not None:
|
|
|
|
headers['content-length'] = str(content_len)
|
|
|
|
|
2016-05-15 02:52:35 +02:00
|
|
|
url = 'http://{ip}:{port}/{path}'.format(ip=ip, port=port, path=url_path)
|
2016-02-19 16:37:38 +01:00
|
|
|
|
|
|
|
req = Request(url, data=payload, headers=headers)
|
|
|
|
|
|
|
|
if get_method is not None:
|
|
|
|
req.get_method = get_method
|
|
|
|
|
|
|
|
return urlopen(req)
|
2015-11-30 23:00:56 +01:00
|
|
|
|
2015-11-24 14:57:54 +01:00
|
|
|
def signalsender(self):
|
|
|
|
sleep(0.5)
|
|
|
|
os.kill(os.getpid(), signal.SIGTERM)
|
|
|
|
|
|
|
|
def test_idle(self):
|
2015-12-26 18:32:36 +01:00
|
|
|
self._setup_updater('Test6', messages=0)
|
2015-11-24 14:57:54 +01:00
|
|
|
self.updater.start_polling(poll_interval=0.01)
|
|
|
|
Thread(target=self.signalsender).start()
|
|
|
|
self.updater.idle()
|
|
|
|
# If we get this far, idle() ran through
|
|
|
|
sleep(1)
|
2016-01-29 23:54:07 +01:00
|
|
|
self.assertFalse(self.updater.running)
|
2015-11-24 14:57:54 +01:00
|
|
|
|
2017-03-26 14:36:13 +02:00
|
|
|
def test_userSignal(self):
|
|
|
|
self._setup_updater('Test7', messages=0)
|
|
|
|
|
|
|
|
tempVar = {'a': 0}
|
|
|
|
|
|
|
|
def userSignalInc(signum, frame):
|
|
|
|
tempVar['a'] = 1
|
|
|
|
|
|
|
|
self.updater.user_sig_handler = userSignalInc
|
|
|
|
self.updater.start_polling(poll_interval=0.01)
|
|
|
|
Thread(target=self.signalsender).start()
|
|
|
|
self.updater.idle()
|
|
|
|
# If we get this far, idle() ran through
|
|
|
|
sleep(1)
|
|
|
|
self.assertFalse(self.updater.running)
|
|
|
|
self.assertTrue(tempVar['a'] != 0)
|
|
|
|
|
2015-12-31 21:23:23 +01:00
|
|
|
def test_createBot(self):
|
2016-06-17 23:01:36 +02:00
|
|
|
self.updater = Updater('123:abcd')
|
|
|
|
self.assertIsNotNone(self.updater.bot)
|
2015-11-24 14:57:54 +01:00
|
|
|
|
2015-12-26 18:32:36 +01:00
|
|
|
def test_mutualExclusiveTokenBot(self):
|
|
|
|
bot = Bot('123:zyxw')
|
|
|
|
self.assertRaises(ValueError, Updater, token='123:abcd', bot=bot)
|
|
|
|
|
|
|
|
def test_noTokenOrBot(self):
|
|
|
|
self.assertRaises(ValueError, Updater)
|
|
|
|
|
2015-12-31 21:23:23 +01:00
|
|
|
|
2016-04-15 16:20:37 +02:00
|
|
|
class MockBot(object):
|
2016-05-15 02:52:35 +02:00
|
|
|
|
2016-04-26 23:23:15 +02:00
|
|
|
def __init__(self,
|
|
|
|
text,
|
|
|
|
messages=1,
|
|
|
|
raise_error=False,
|
|
|
|
bootstrap_retries=None,
|
2016-05-27 11:06:29 +02:00
|
|
|
bootstrap_err=TelegramError('test'),
|
|
|
|
edited=False):
|
2015-11-06 00:23:34 +01:00
|
|
|
self.text = text
|
|
|
|
self.send_messages = messages
|
2015-11-21 21:22:58 +01:00
|
|
|
self.raise_error = raise_error
|
|
|
|
self.token = "TOKEN"
|
2016-03-14 00:36:01 +01:00
|
|
|
self.bootstrap_retries = bootstrap_retries
|
|
|
|
self.bootstrap_attempts = 0
|
|
|
|
self.bootstrap_err = bootstrap_err
|
2016-05-27 11:06:29 +02:00
|
|
|
self.edited = edited
|
2017-02-27 14:26:53 +01:00
|
|
|
self.username = "MockBot"
|
2015-11-06 00:23:34 +01:00
|
|
|
|
2016-05-27 11:06:29 +02:00
|
|
|
def mockUpdate(self, text):
|
2017-02-27 14:26:53 +01:00
|
|
|
message = Message(0, User(0, 'Testuser'), None, Chat(0, Chat.GROUP), bot=self)
|
2015-11-06 00:23:34 +01:00
|
|
|
message.text = text
|
|
|
|
update = Update(0)
|
2016-05-27 11:06:29 +02:00
|
|
|
|
|
|
|
if self.edited:
|
|
|
|
update.edited_message = message
|
|
|
|
else:
|
|
|
|
update.message = message
|
|
|
|
|
2015-11-06 00:23:34 +01:00
|
|
|
return update
|
|
|
|
|
2017-03-25 12:46:37 +01:00
|
|
|
def setWebhook(self, url=None, certificate=None, allowed_updates=None):
|
2016-03-14 00:36:01 +01:00
|
|
|
if self.bootstrap_retries is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.bootstrap_attempts < self.bootstrap_retries:
|
|
|
|
self.bootstrap_attempts += 1
|
|
|
|
raise self.bootstrap_err
|
2015-11-21 15:48:45 +01:00
|
|
|
|
2017-01-16 00:28:47 +01:00
|
|
|
def deleteWebhook(self):
|
|
|
|
if self.bootstrap_retries is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.bootstrap_attempts < self.bootstrap_retries:
|
|
|
|
self.bootstrap_attempts += 1
|
|
|
|
raise self.bootstrap_err
|
|
|
|
|
|
|
|
def getUpdates(self,
|
|
|
|
offset=None,
|
|
|
|
limit=100,
|
|
|
|
timeout=0,
|
|
|
|
network_delay=None,
|
|
|
|
read_latency=2.,
|
|
|
|
allowed_updates=None):
|
2015-11-06 00:23:34 +01:00
|
|
|
|
2015-11-21 21:22:58 +01:00
|
|
|
if self.raise_error:
|
2015-11-24 19:35:02 +01:00
|
|
|
raise TelegramError('Test Error 2')
|
2015-11-21 21:22:58 +01:00
|
|
|
elif self.send_messages >= 2:
|
2015-11-06 00:23:34 +01:00
|
|
|
self.send_messages -= 2
|
|
|
|
return self.mockUpdate(self.text), self.mockUpdate(self.text)
|
|
|
|
elif self.send_messages == 1:
|
|
|
|
self.send_messages -= 1
|
|
|
|
return self.mockUpdate(self.text),
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2016-09-20 06:36:55 +02:00
|
|
|
def create_references(self, d):
|
|
|
|
pass
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
|
2015-11-06 00:23:34 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|