From 7734c1d6a60951378028f027deceb8d67f6b18a6 Mon Sep 17 00:00:00 2001 From: Noam Meltzer Date: Sat, 26 Dec 2015 19:32:36 +0200 Subject: [PATCH] fix unitests to work with new token validator --- telegram/updater.py | 17 +++++++-- tests/test_bot.py | 14 ++++++++ tests/test_updater.py | 83 ++++++++++++++++++++----------------------- 3 files changed, 67 insertions(+), 47 deletions(-) diff --git a/telegram/updater.py b/telegram/updater.py index 0014e7764..932b8f377 100644 --- a/telegram/updater.py +++ b/telegram/updater.py @@ -64,15 +64,26 @@ class Updater: Attributes: Args: - token (str): The bots token given by the @BotFather + token (Optional[str]): The bot's token given by the @BotFather base_url (Optional[str]): workers (Optional[int]): Amount of threads in the thread pool for functions decorated with @run_async + bot (Optional[Bot]): + + Raises: + ValueError: If both `token` and `bot` are passed or none of them. """ - def __init__(self, token, base_url=None, workers=4): + def __init__(self, token=None, base_url=None, workers=4, bot=None): + if (token is None) and (bot is None): + raise ValueError('`token` or `bot` must be passed') + if (token is not None) and (bot is not None): + raise ValueError('`token` and `bot` are mutually exclusive') - self.bot = Bot(token, base_url) + if bot is not None: + self.bot = bot + else: + self.bot = Bot(token, base_url) self.update_queue = Queue() self.dispatcher = Dispatcher(self.bot, self.update_queue, workers=workers) diff --git a/tests/test_bot.py b/tests/test_bot.py index 6ffc0ee56..8c6d9949b 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -137,5 +137,19 @@ class BotTest(BaseTest, unittest.TestCase): self.assertTrue(self.is_json(upf.to_json())) self.assertEqual(upf.photos[0][0].file_size, 12421) + def _test_invalid_token(self, token): + print('Testing invalid token: {0}'.format(token)) + self.assertRaisesRegexp(telegram.TelegramError, 'Invalid token', telegram.Bot, token) + + def testInvalidToken1(self): + self._test_invalid_token('123') + + def testInvalidToken2(self): + self._test_invalid_token('12a:') + + def testInvalidToken3(self): + self._test_invalid_token('12:') + + if __name__ == '__main__': unittest.main() diff --git a/tests/test_updater.py b/tests/test_updater.py index 6adc7bd8b..e427511ee 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -38,7 +38,7 @@ except ImportError: sys.path.append('.') -from telegram import Update, Message, TelegramError, User, Chat, Updater +from telegram import Update, Message, TelegramError, User, Chat, Updater, Bot from telegram.dispatcher import run_async from tests.base import BaseTest from threading import Lock, Thread @@ -61,14 +61,18 @@ class UpdaterTest(BaseTest, unittest.TestCase): """ def setUp(self): - self.updater = Updater('', workers=2) - + self.updater = None self.received_message = None self.message_count = 0 self.lock = Lock() + def _setup_updater(self, *args, **kwargs): + bot = MockBot(*args, **kwargs) + self.updater = Updater(workers=2, bot=bot) + def tearDown(self): - self.updater.stop() + if self.updater is not None: + self.updater.stop() def reset(self): self.message_count = 0 @@ -119,8 +123,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_addRemoveTelegramMessageHandler(self): print('Testing add/removeTelegramMessageHandler') - bot = MockBot('Test') - self.updater.bot = bot + self._setup_updater('Test') d = self.updater.dispatcher d.addTelegramMessageHandler( self.telegramHandlerTest) @@ -132,13 +135,13 @@ class UpdaterTest(BaseTest, unittest.TestCase): d.removeTelegramMessageHandler(self.telegramHandlerTest) self.reset() - bot.send_messages = 1 + self.updater.bot.send_messages = 1 sleep(.1) self.assertTrue(None is self.received_message) def test_addTelegramMessageHandlerMultipleMessages(self): print('Testing addTelegramMessageHandler and send 100 messages...') - self.updater.bot = MockBot('Multiple', 100) + self._setup_updater('Multiple', 100) self.updater.dispatcher.addTelegramMessageHandler( self.telegramHandlerTest) self.updater.start_polling(0.0) @@ -148,8 +151,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_addRemoveTelegramRegexHandler(self): print('Testing add/removeStringRegexHandler') - bot = MockBot('Test2') - self.updater.bot = bot + self._setup_updater('Test2') d = self.updater.dispatcher regobj = re.compile('Te.*') self.updater.dispatcher.addTelegramRegexHandler(regobj, @@ -162,14 +164,13 @@ class UpdaterTest(BaseTest, unittest.TestCase): d.removeTelegramRegexHandler(regobj, self.telegramHandlerTest) self.reset() - bot.send_messages = 1 + self.updater.bot.send_messages = 1 sleep(.1) self.assertTrue(None is self.received_message) def test_addRemoveTelegramCommandHandler(self): print('Testing add/removeTelegramCommandHandler') - bot = MockBot('/test') - self.updater.bot = bot + self._setup_updater('/test') d = self.updater.dispatcher self.updater.dispatcher.addTelegramCommandHandler( 'test', self.telegramHandlerTest) @@ -181,14 +182,13 @@ class UpdaterTest(BaseTest, unittest.TestCase): d.removeTelegramCommandHandler('test', self.telegramHandlerTest) self.reset() - bot.send_messages = 1 + self.updater.bot.send_messages = 1 sleep(.1) self.assertTrue(None is self.received_message) def test_addRemoveUnknownTelegramCommandHandler(self): print('Testing add/removeUnknownTelegramCommandHandler') - bot = MockBot('/test2') - self.updater.bot = bot + self._setup_updater('/test2') d = self.updater.dispatcher self.updater.dispatcher.addUnknownTelegramCommandHandler( self.telegramHandlerTest) @@ -200,14 +200,13 @@ class UpdaterTest(BaseTest, unittest.TestCase): d.removeUnknownTelegramCommandHandler(self.telegramHandlerTest) self.reset() - bot.send_messages = 1 + self.updater.bot.send_messages = 1 sleep(.1) self.assertTrue(None is self.received_message) def test_addRemoveStringRegexHandler(self): print('Testing add/removeStringRegexHandler') - bot = MockBot('', messages=0) - self.updater.bot = bot + self._setup_updater('', messages=0) d = self.updater.dispatcher d.addStringRegexHandler('Te.*', self.stringHandlerTest) queue = self.updater.start_polling(0.01) @@ -225,8 +224,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_addRemoveStringCommandHandler(self): print('Testing add/removeStringCommandHandler') - bot = MockBot('', messages=0) - self.updater.bot = bot + self._setup_updater('', messages=0) d = self.updater.dispatcher d.addStringCommandHandler( 'test3', self.stringHandlerTest) @@ -246,8 +244,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_addRemoveUnknownStringCommandHandler(self): print('Testing add/removeUnknownStringCommandHandler') - bot = MockBot('/test') - self.updater.bot = bot + self._setup_updater('/test') d = self.updater.dispatcher d.addUnknownStringCommandHandler( self.stringHandlerTest) @@ -260,14 +257,13 @@ class UpdaterTest(BaseTest, unittest.TestCase): d.removeUnknownStringCommandHandler(self.stringHandlerTest) self.reset() - bot.send_messages = 1 + self.updater.bot.send_messages = 1 sleep(.1) self.assertTrue(None is self.received_message) def test_addRemoveErrorHandler(self): print('Testing add/removeErrorHandler') - bot = MockBot('', messages=0) - self.updater.bot = bot + self._setup_updater('', messages=0) d = self.updater.dispatcher d.addErrorHandler(self.errorHandlerTest) queue = self.updater.start_polling(0.01) @@ -286,8 +282,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_errorInHandler(self): print('Testing error in Handler') - bot = MockBot('', messages=0) - self.updater.bot = bot + self._setup_updater('', messages=0) d = self.updater.dispatcher d.addStringRegexHandler('.*', self.errorRaisingHandlerTest) @@ -300,8 +295,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_errorOnGetUpdates(self): print('Testing error on getUpdates') - bot = MockBot('', raise_error=True) - self.updater.bot = bot + self._setup_updater('', raise_error=True) d = self.updater.dispatcher d.addErrorHandler(self.errorHandlerTest) self.updater.start_polling(0.01) @@ -310,8 +304,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_addRemoveTypeHandler(self): print('Testing add/removeTypeHandler') - bot = MockBot('', messages=0) - self.updater.bot = bot + self._setup_updater('', messages=0) d = self.updater.dispatcher d.addTypeHandler(dict, self.stringHandlerTest) queue = self.updater.start_polling(0.01) @@ -330,8 +323,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_runAsync(self): print('Testing @run_async') - bot = MockBot('Test5', messages=2) - self.updater.bot = bot + self._setup_updater('Test5', messages=2) d = self.updater.dispatcher d.addTelegramMessageHandler( self.asyncHandlerTest) @@ -342,7 +334,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_additionalArgs(self): print('Testing additional arguments for handlers') - self.updater.bot = MockBot('', messages=0) + self._setup_updater('', messages=0) self.updater.dispatcher.addStringCommandHandler( 'test5', self.additionalArgsTest) @@ -354,8 +346,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_regexGroupHandler(self): print('Testing optional groups and groupdict parameters') - bot = MockBot('', messages=0) - self.updater.bot = bot + self._setup_updater('', messages=0) d = self.updater.dispatcher d.addStringRegexHandler('^(This).*?(?Pregex group).*', self.regexGroupHandlerTest) @@ -368,8 +359,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_runAsyncWithAdditionalArgs(self): print('Testing @run_async with additional parameters') - bot = MockBot('Test6', messages=2) - self.updater.bot = bot + self._setup_updater('Test6', messages=2) d = self.updater.dispatcher d.addTelegramMessageHandler( self.asyncAdditionalHandlerTest) @@ -380,8 +370,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_webhook(self): print('Testing Webhook') - bot = MockBot('', messages=0) - self.updater.bot = bot + self._setup_updater('', messages=0) d = self.updater.dispatcher d.addTelegramMessageHandler( self.telegramHandlerTest) @@ -442,8 +431,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_webhook_no_ssl(self): print('Testing Webhook without SSL') - bot = MockBot('', messages=0) - self.updater.bot = bot + self._setup_updater('', messages=0) d = self.updater.dispatcher d.addTelegramMessageHandler( self.telegramHandlerTest) @@ -485,7 +473,7 @@ class UpdaterTest(BaseTest, unittest.TestCase): def test_idle(self): print('Testing idle') - self.updater.bot = MockBot('Test6', messages=0) + self._setup_updater('Test6', messages=0) self.updater.start_polling(poll_interval=0.01) Thread(target=self.signalsender).start() self.updater.idle() @@ -494,6 +482,13 @@ class UpdaterTest(BaseTest, unittest.TestCase): self.updater.running = False + 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) + class MockBot: def __init__(self, text, messages=1, raise_error=False):