2015-09-11 01:06:27 +02: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-09-11 01:06:27 +02: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/].
|
|
|
|
"""This module contains a object that represents Tests for Telegram Bot"""
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
import io
|
2016-05-29 23:46:42 +02:00
|
|
|
import re
|
2015-09-11 01:06:27 +02:00
|
|
|
from datetime import datetime
|
|
|
|
import sys
|
2016-04-27 00:28:21 +02:00
|
|
|
|
2016-02-21 12:52:47 +01:00
|
|
|
from flaky import flaky
|
2015-12-31 20:33:37 +01:00
|
|
|
|
|
|
|
if sys.version_info[0:2] == (2, 6):
|
|
|
|
import unittest2 as unittest
|
|
|
|
else:
|
|
|
|
import unittest
|
|
|
|
|
2015-09-11 01:06:27 +02:00
|
|
|
sys.path.append('.')
|
|
|
|
|
|
|
|
import telegram
|
2016-02-22 00:38:26 +01:00
|
|
|
from tests.base import BaseTest, timeout
|
2015-09-11 01:06:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BotTest(BaseTest, unittest.TestCase):
|
|
|
|
"""This object represents Tests for Telegram Bot."""
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testGetMe(self):
|
|
|
|
bot = self._bot.getMe()
|
|
|
|
|
|
|
|
self.assertTrue(self.is_json(bot.to_json()))
|
2016-05-25 09:41:48 +02:00
|
|
|
self._testUserEqualsBot(bot)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testSendMessage(self):
|
2016-05-15 02:52:35 +02:00
|
|
|
message = self._bot.sendMessage(chat_id=self._chat_id,
|
|
|
|
text='Моё судно на воздушной подушке полно угрей')
|
2015-09-11 01:06:27 +02:00
|
|
|
|
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2016-05-15 02:52:35 +02:00
|
|
|
self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
|
2015-09-11 01:06:27 +02:00
|
|
|
self.assertTrue(isinstance(message.date, datetime))
|
|
|
|
|
2016-03-11 07:37:43 +01:00
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def testSilentSendMessage(self):
|
2016-05-15 02:52:35 +02:00
|
|
|
message = self._bot.sendMessage(chat_id=self._chat_id,
|
|
|
|
text='Моё судно на воздушной подушке полно угрей',
|
|
|
|
disable_notification=True)
|
2016-03-11 07:37:43 +01:00
|
|
|
|
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2016-05-15 02:52:35 +02:00
|
|
|
self.assertEqual(message.text, u'Моё судно на воздушной подушке полно угрей')
|
2016-03-11 07:37:43 +01:00
|
|
|
self.assertTrue(isinstance(message.date, datetime))
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testGetUpdates(self):
|
2016-05-24 02:09:07 +02:00
|
|
|
updates = self._bot.getUpdates(timeout=1)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
|
|
|
if updates:
|
|
|
|
self.assertTrue(self.is_json(updates[0].to_json()))
|
|
|
|
self.assertTrue(isinstance(updates[0], telegram.Update))
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testForwardMessage(self):
|
|
|
|
message = self._bot.forwardMessage(chat_id=self._chat_id,
|
|
|
|
from_chat_id=self._chat_id,
|
|
|
|
message_id=2398)
|
|
|
|
|
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
|
|
|
self.assertEqual(message.text, 'teste')
|
|
|
|
self.assertEqual(message.forward_from.username, 'leandrotoledo')
|
|
|
|
self.assertTrue(isinstance(message.forward_date, datetime))
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testSendPhoto(self):
|
2016-05-15 02:52:35 +02:00
|
|
|
message = self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'),
|
|
|
|
caption='testSendPhoto',
|
|
|
|
chat_id=self._chat_id)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
|
|
|
self.assertEqual(message.photo[0].file_size, 1451)
|
|
|
|
self.assertEqual(message.caption, 'testSendPhoto')
|
|
|
|
|
2016-03-11 07:37:43 +01:00
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def testSilentSendPhoto(self):
|
2016-05-15 02:52:35 +02:00
|
|
|
message = self._bot.sendPhoto(photo=open('tests/data/telegram.png', 'rb'),
|
|
|
|
caption='testSendPhoto',
|
|
|
|
chat_id=self._chat_id,
|
|
|
|
disable_notification=True)
|
2016-03-11 07:37:43 +01:00
|
|
|
|
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
|
|
|
self.assertEqual(message.photo[0].file_size, 1451)
|
|
|
|
self.assertEqual(message.caption, 'testSendPhoto')
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testResendPhoto(self):
|
2016-05-15 02:39:11 +02:00
|
|
|
message = self._bot.sendPhoto(
|
2016-05-22 17:55:48 +02:00
|
|
|
photo='AgADAQAD1y0yGx8j9Qf8f_m3CKeS6Iy95y8ABI1ggfVJ4-UvwJcAAgI',
|
2016-05-15 02:39:11 +02:00
|
|
|
chat_id=self._chat_id)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2016-05-15 02:52:35 +02:00
|
|
|
self.assertEqual(message.photo[0].file_id,
|
2016-05-22 17:55:48 +02:00
|
|
|
'AgADAQAD1y0yGx8j9Qf8f_m3CKeS6Iy95y8ABI1ggfVJ4-UvwJcAAgI')
|
2015-09-11 01:06:27 +02:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testSendJPGURLPhoto(self):
|
2016-05-15 02:39:11 +02:00
|
|
|
message = self._bot.sendPhoto(
|
|
|
|
photo='http://dummyimage.com/600x400/000/fff.jpg&text=telegram',
|
|
|
|
chat_id=self._chat_id)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
|
|
|
self.assertEqual(message.photo[0].file_size, 822)
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testSendPNGURLPhoto(self):
|
2016-05-15 02:39:11 +02:00
|
|
|
message = self._bot.sendPhoto(
|
|
|
|
photo='http://dummyimage.com/600x400/000/fff.png&text=telegram',
|
|
|
|
chat_id=self._chat_id)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2016-05-17 10:18:34 +02:00
|
|
|
self.assertEqual(message.photo[0].file_size, 685)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testSendGIFURLPhoto(self):
|
2016-05-15 02:39:11 +02:00
|
|
|
message = self._bot.sendPhoto(
|
|
|
|
photo='http://dummyimage.com/600x400/000/fff.gif&text=telegram',
|
|
|
|
chat_id=self._chat_id)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
2016-05-17 10:18:34 +02:00
|
|
|
self.assertEqual(message.photo[0].file_size, 685)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
2016-04-23 02:51:00 +02:00
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def testSendBufferedReaderPhoto(self):
|
|
|
|
photo = open('tests/data/telegram.png', 'rb')
|
2016-04-23 14:23:03 +02:00
|
|
|
br_photo = io.BufferedReader(io.BytesIO(photo.read()))
|
2016-05-15 02:39:11 +02:00
|
|
|
message = self._bot.sendPhoto(photo=br_photo, chat_id=self._chat_id)
|
2016-04-23 02:51:00 +02:00
|
|
|
|
|
|
|
self.assertTrue(self.is_json(message.to_json()))
|
|
|
|
self.assertEqual(message.photo[0].file_size, 1451)
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testSendChatAction(self):
|
2016-05-15 02:52:35 +02:00
|
|
|
self._bot.sendChatAction(action=telegram.ChatAction.TYPING, chat_id=self._chat_id)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-11 01:06:27 +02:00
|
|
|
def testGetUserProfilePhotos(self):
|
|
|
|
upf = self._bot.getUserProfilePhotos(user_id=self._chat_id)
|
|
|
|
|
|
|
|
self.assertTrue(self.is_json(upf.to_json()))
|
2015-11-13 13:53:31 +01:00
|
|
|
self.assertEqual(upf.photos[0][0].file_size, 12421)
|
2015-09-11 01:06:27 +02:00
|
|
|
|
2015-12-26 18:32:36 +01:00
|
|
|
def _test_invalid_token(self, token):
|
2016-05-15 02:52:35 +02:00
|
|
|
self.assertRaisesRegexp(telegram.error.InvalidToken, 'Invalid token', telegram.Bot, token)
|
2015-12-26 18:32:36 +01:00
|
|
|
|
|
|
|
def testInvalidToken1(self):
|
|
|
|
self._test_invalid_token('123')
|
|
|
|
|
|
|
|
def testInvalidToken2(self):
|
|
|
|
self._test_invalid_token('12a:')
|
|
|
|
|
|
|
|
def testInvalidToken3(self):
|
|
|
|
self._test_invalid_token('12:')
|
|
|
|
|
2016-05-26 21:09:14 +02:00
|
|
|
def testInvalidToken4(self):
|
|
|
|
# white spaces are invalid
|
|
|
|
self._test_invalid_token('1234:abcd1234\n')
|
|
|
|
self._test_invalid_token(' 1234:abcd1234')
|
|
|
|
self._test_invalid_token(' 1234:abcd1234\r')
|
|
|
|
self._test_invalid_token('1234:abcd 1234')
|
2016-05-22 23:21:51 +02:00
|
|
|
|
2015-12-31 21:23:23 +01:00
|
|
|
def testUnauthToken(self):
|
2016-05-15 02:52:35 +02:00
|
|
|
with self.assertRaisesRegexp(telegram.error.Unauthorized, 'Unauthorized'):
|
2015-12-31 21:23:23 +01:00
|
|
|
bot = telegram.Bot('1234:abcd1234')
|
|
|
|
bot.getMe()
|
|
|
|
|
|
|
|
def testInvalidSrvResp(self):
|
2016-05-15 02:52:35 +02:00
|
|
|
with self.assertRaisesRegexp(telegram.TelegramError, 'Invalid server response'):
|
2015-12-31 21:23:23 +01:00
|
|
|
# bypass the valid token check
|
2016-04-21 14:21:12 +02:00
|
|
|
bot = telegram.Bot.__new__(telegram.Bot)
|
2016-04-21 17:15:37 +02:00
|
|
|
bot.base_url = 'https://api.telegram.org/bot{0}'.format('12')
|
2016-04-21 14:21:12 +02:00
|
|
|
|
2015-12-31 21:23:23 +01:00
|
|
|
bot.getMe()
|
|
|
|
|
2016-05-26 02:15:17 +02:00
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def testLeaveChat(self):
|
2016-05-29 23:46:42 +02:00
|
|
|
regex = re.compile('chat not found', re.IGNORECASE)
|
|
|
|
with self.assertRaisesRegexp(telegram.error.BadRequest, regex):
|
2016-05-26 02:15:17 +02:00
|
|
|
chat = self._bot.leaveChat(-123456)
|
|
|
|
|
2016-05-29 23:46:42 +02:00
|
|
|
with self.assertRaisesRegexp(telegram.error.NetworkError, regex):
|
2016-05-26 03:09:18 +02:00
|
|
|
chat = self._bot.leaveChat(-123456)
|
|
|
|
|
2016-05-25 01:31:10 +02:00
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def testGetChat(self):
|
|
|
|
chat = self._bot.getChat(self._group_id)
|
|
|
|
|
|
|
|
self.assertTrue(self.is_json(chat.to_json()))
|
|
|
|
self.assertEqual(chat.type, "group")
|
|
|
|
self.assertEqual(chat.title, ">>> telegram.Bot() - Developers")
|
|
|
|
self.assertEqual(chat.id, int(self._group_id))
|
|
|
|
|
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def testGetChatAdministrators(self):
|
|
|
|
admins = self._bot.getChatAdministrators(self._channel_id)
|
|
|
|
self.assertTrue(isinstance(admins, list))
|
|
|
|
self.assertTrue(self.is_json(admins[0].to_json()))
|
|
|
|
|
|
|
|
for a in admins:
|
|
|
|
self.assertTrue(a.status in ("administrator", "creator"))
|
|
|
|
|
|
|
|
bot = [a.user for a in admins if a.user.id == 133505823][0]
|
2016-05-25 09:41:48 +02:00
|
|
|
self._testUserEqualsBot(bot)
|
2016-05-25 01:31:10 +02:00
|
|
|
|
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def testGetChatMembersCount(self):
|
|
|
|
count = self._bot.getChatMembersCount(self._channel_id)
|
|
|
|
self.assertTrue(isinstance(count, int))
|
|
|
|
self.assertTrue(count > 3)
|
|
|
|
|
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def testGetChatMember(self):
|
|
|
|
chat_member = self._bot.getChatMember(self._channel_id, 133505823)
|
|
|
|
bot = chat_member.user
|
|
|
|
|
|
|
|
self.assertTrue(self.is_json(chat_member.to_json()))
|
|
|
|
self.assertEqual(chat_member.status, "administrator")
|
2016-05-25 09:41:48 +02:00
|
|
|
self._testUserEqualsBot(bot)
|
|
|
|
|
|
|
|
def _testUserEqualsBot(self, user):
|
|
|
|
"""Tests if user is our trusty @PythonTelegramBot."""
|
|
|
|
self.assertEqual(user.id, 133505823)
|
|
|
|
self.assertEqual(user.first_name, 'PythonTelegramBot')
|
|
|
|
self.assertEqual(user.last_name, '')
|
|
|
|
self.assertEqual(user.username, 'PythonTelegramBot')
|
|
|
|
self.assertEqual(user.name, '@PythonTelegramBot')
|
|
|
|
|
2015-12-26 18:32:36 +01:00
|
|
|
|
2015-09-11 01:06:27 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|