2015-09-07 17:10:29 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2017-05-14 12:18:29 +02:00
|
|
|
# Copyright (C) 2015-2017
|
2016-01-05 14:12:03 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2015-09-07 17:10:29 +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/].
|
2016-10-17 00:22:40 +02:00
|
|
|
"""This module contains an object that represents Tests for Telegram Sticker"""
|
2015-09-07 17:10:29 +02:00
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
import os
|
2017-06-22 18:04:19 +02:00
|
|
|
import unittest
|
2016-04-27 00:28:21 +02:00
|
|
|
|
2016-02-21 12:52:47 +01:00
|
|
|
from flaky import flaky
|
2016-05-12 08:31:47 +02:00
|
|
|
from future.utils import PY2
|
2016-02-21 12:52:47 +01:00
|
|
|
|
2015-09-07 17:10:29 +02:00
|
|
|
import telegram
|
2016-02-22 00:38:26 +01:00
|
|
|
from tests.base import BaseTest, timeout
|
2017-06-22 18:04:19 +02:00
|
|
|
from tests.bots import get_bot
|
2015-09-07 17:10:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
class StickerTest(BaseTest, unittest.TestCase):
|
|
|
|
"""This object represents Tests for Telegram Sticker."""
|
|
|
|
|
2017-06-22 18:04:19 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.emoji = telegram.Emoji.FLEXED_BICEPS
|
2017-06-23 20:10:43 +02:00
|
|
|
# cls.sticker_file_url = "https://python-telegram-bot.org/static/testfiles/telegram.webp"
|
|
|
|
# Serving sticker from gh since our server sends wrong content_type
|
|
|
|
cls.sticker_file_url = "https://github.com/python-telegram-bot/python-telegram-bot/blob/master/tests/data/telegram.webp?raw=true" # noqa
|
2017-06-22 18:04:19 +02:00
|
|
|
|
|
|
|
bot_info = get_bot()
|
|
|
|
cls._chat_id = bot_info['chat_id']
|
|
|
|
cls._bot = telegram.Bot(bot_info['token'])
|
|
|
|
|
|
|
|
sticker_file = open('tests/data/telegram.webp', 'rb')
|
|
|
|
sticker = cls._bot.send_sticker(cls._chat_id, sticker=sticker_file, timeout=10).sticker
|
|
|
|
cls.sticker = sticker
|
|
|
|
cls.thumb = sticker.thumb
|
|
|
|
|
|
|
|
# Make sure file has been uploaded.
|
|
|
|
# Simple assertions PY2 Only
|
|
|
|
assert isinstance(cls.sticker, telegram.Sticker)
|
|
|
|
assert isinstance(cls.sticker.file_id, str)
|
|
|
|
assert cls.sticker.file_id is not ''
|
|
|
|
assert isinstance(cls.thumb, telegram.PhotoSize)
|
|
|
|
assert isinstance(cls.thumb.file_id, str)
|
|
|
|
assert cls.thumb.file_id is not ''
|
2015-09-07 17:10:29 +02:00
|
|
|
|
2017-06-22 18:04:19 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.sticker_file = open('tests/data/telegram.webp', 'rb')
|
2015-09-07 17:10:29 +02:00
|
|
|
self.json_dict = {
|
2017-06-22 18:04:19 +02:00
|
|
|
'file_id': self.sticker.file_id,
|
|
|
|
'width': self.sticker.width,
|
|
|
|
'height': self.sticker.height,
|
|
|
|
'thumb': self.thumb.to_dict(),
|
2016-05-11 23:16:36 +02:00
|
|
|
'emoji': self.emoji,
|
2017-06-22 18:04:19 +02:00
|
|
|
'file_size': self.sticker.file_size
|
2015-09-07 17:10:29 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 18:04:19 +02:00
|
|
|
def test_expected_values(self):
|
|
|
|
self.assertEqual(self.sticker.width, 510)
|
|
|
|
self.assertEqual(self.sticker.height, 512)
|
|
|
|
self.assertEqual(self.sticker.file_size, 39518)
|
|
|
|
self.assertEqual(self.thumb.width, 90)
|
|
|
|
self.assertEqual(self.thumb.height, 90)
|
|
|
|
self.assertEqual(self.thumb.file_size, 3672)
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2017-06-22 18:04:19 +02:00
|
|
|
def test_send_sticker_all_args(self):
|
|
|
|
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker.file_id, disable_notification=False)
|
|
|
|
sticker = message.sticker
|
|
|
|
|
|
|
|
self.assertEqual(sticker, self.sticker)
|
2015-09-07 17:10:29 +02:00
|
|
|
|
2017-06-22 18:29:21 +02:00
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def test_get_and_download_sticker(self):
|
|
|
|
new_file = self._bot.getFile(self.sticker.file_id)
|
|
|
|
|
|
|
|
self.assertEqual(new_file.file_size, self.sticker.file_size)
|
|
|
|
self.assertEqual(new_file.file_id, self.sticker.file_id)
|
|
|
|
self.assertTrue(new_file.file_path.startswith('https://'))
|
|
|
|
|
|
|
|
new_file.download('telegram.webp')
|
|
|
|
|
|
|
|
self.assertTrue(os.path.isfile('telegram.webp'))
|
|
|
|
|
2016-02-22 00:52:31 +01:00
|
|
|
@flaky(3, 1)
|
2016-02-22 00:38:26 +01:00
|
|
|
@timeout(10)
|
2015-09-07 17:10:29 +02:00
|
|
|
def test_send_sticker_resend(self):
|
2017-06-22 18:04:19 +02:00
|
|
|
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker.file_id)
|
2015-09-07 17:10:29 +02:00
|
|
|
|
|
|
|
sticker = message.sticker
|
|
|
|
|
2017-06-22 18:04:19 +02:00
|
|
|
self.assertEqual(sticker.file_id, self.sticker.file_id)
|
|
|
|
self.assertEqual(sticker.width, self.sticker.width)
|
|
|
|
self.assertEqual(sticker.height, self.sticker.height)
|
|
|
|
self.assertIsInstance(sticker.thumb, telegram.PhotoSize)
|
|
|
|
self.assertEqual(sticker.file_size, self.sticker.file_size)
|
|
|
|
|
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def test_sticker_on_server_emoji(self):
|
|
|
|
server_file_id = "CAADAQADHAADyIsGAAFZfq1bphjqlgI"
|
|
|
|
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=server_file_id)
|
|
|
|
sticker = message.sticker
|
2016-05-12 08:31:47 +02:00
|
|
|
if PY2:
|
|
|
|
self.assertEqual(sticker.emoji, self.emoji.decode('utf-8'))
|
|
|
|
else:
|
|
|
|
self.assertEqual(sticker.emoji, self.emoji)
|
2017-06-22 18:04:19 +02:00
|
|
|
|
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def test_send_sticker_from_url(self):
|
|
|
|
message = self._bot.sendSticker(chat_id=self._chat_id, sticker=self.sticker_file_url)
|
|
|
|
sticker = message.sticker
|
|
|
|
|
|
|
|
self.assertIsInstance(sticker, telegram.Sticker)
|
|
|
|
self.assertIsInstance(sticker.file_id, str)
|
|
|
|
self.assertNotEqual(sticker.file_id, '')
|
|
|
|
self.assertEqual(sticker.file_size, self.sticker.file_size)
|
|
|
|
self.assertEqual(sticker.height, self.sticker.height)
|
|
|
|
self.assertEqual(sticker.width, self.sticker.width)
|
|
|
|
thumb = sticker.thumb
|
|
|
|
self.assertIsInstance(thumb, telegram.PhotoSize)
|
|
|
|
self.assertIsInstance(thumb.file_id, str)
|
|
|
|
self.assertNotEqual(thumb.file_id, '')
|
|
|
|
self.assertEqual(thumb.file_size, self.thumb.file_size)
|
|
|
|
self.assertEqual(thumb.width, self.thumb.width)
|
|
|
|
self.assertEqual(thumb.height, self.thumb.height)
|
2015-09-07 17:10:29 +02:00
|
|
|
|
|
|
|
def test_sticker_de_json(self):
|
2016-09-20 06:36:55 +02:00
|
|
|
sticker = telegram.Sticker.de_json(self.json_dict, self._bot)
|
2015-09-07 17:10:29 +02:00
|
|
|
|
2017-06-22 18:04:19 +02:00
|
|
|
self.assertEqual(sticker.file_id, self.sticker.file_id)
|
|
|
|
self.assertEqual(sticker.width, self.sticker.width)
|
|
|
|
self.assertEqual(sticker.height, self.sticker.height)
|
|
|
|
self.assertIsInstance(sticker.thumb, telegram.PhotoSize)
|
2016-05-11 23:16:36 +02:00
|
|
|
self.assertEqual(sticker.emoji, self.emoji)
|
2017-06-22 18:04:19 +02:00
|
|
|
self.assertEqual(sticker.file_size, self.sticker.file_size)
|
2015-09-07 17:10:29 +02:00
|
|
|
|
|
|
|
def test_sticker_to_json(self):
|
2017-06-22 18:04:19 +02:00
|
|
|
self.assertTrue(self.is_json(self.sticker.to_json()))
|
2015-09-07 17:10:29 +02:00
|
|
|
|
|
|
|
def test_sticker_to_dict(self):
|
2017-06-22 18:04:19 +02:00
|
|
|
sticker = self.sticker.to_dict()
|
2015-09-07 17:10:29 +02:00
|
|
|
|
2017-06-22 18:04:19 +02:00
|
|
|
self.is_dict(sticker)
|
|
|
|
self.assertEqual(sticker['file_id'], self.sticker.file_id)
|
|
|
|
self.assertEqual(sticker['width'], self.sticker.width)
|
|
|
|
self.assertEqual(sticker['height'], self.sticker.height)
|
2017-06-22 18:29:21 +02:00
|
|
|
self.assertIsInstance(sticker['thumb'], dict)
|
2017-06-22 18:04:19 +02:00
|
|
|
self.assertEqual(sticker['file_size'], self.sticker.file_size)
|
2015-09-07 17:10:29 +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-07 17:10:29 +02:00
|
|
|
def test_error_send_sticker_empty_file(self):
|
|
|
|
json_dict = self.json_dict
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
del (json_dict['file_id'])
|
2015-09-07 17:10:29 +02:00
|
|
|
json_dict['sticker'] = open(os.devnull, 'rb')
|
|
|
|
|
2017-06-22 18:04:19 +02:00
|
|
|
with self.assertRaises(telegram.TelegramError):
|
|
|
|
self._bot.sendSticker(chat_id=self._chat_id, **json_dict)
|
2015-09-07 17:10:29 +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-07 17:10:29 +02:00
|
|
|
def test_error_send_sticker_empty_file_id(self):
|
|
|
|
json_dict = self.json_dict
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
del (json_dict['file_id'])
|
2015-09-07 17:10:29 +02:00
|
|
|
json_dict['sticker'] = ''
|
|
|
|
|
2017-06-22 18:04:19 +02:00
|
|
|
with self.assertRaises(telegram.TelegramError):
|
|
|
|
self._bot.sendSticker(chat_id=self._chat_id, **json_dict)
|
2015-09-07 17:10:29 +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-07 17:10:29 +02:00
|
|
|
def test_error_sticker_without_required_args(self):
|
|
|
|
json_dict = self.json_dict
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
del (json_dict['file_id'])
|
2015-09-07 17:10:29 +02:00
|
|
|
|
2017-06-22 18:04:19 +02:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
self._bot.sendSticker(chat_id=self._chat_id, **json_dict)
|
2016-04-24 04:11:25 +02:00
|
|
|
|
2016-09-20 06:36:55 +02:00
|
|
|
@flaky(3, 1)
|
|
|
|
@timeout(10)
|
|
|
|
def test_reply_sticker(self):
|
|
|
|
"""Test for Message.reply_sticker"""
|
|
|
|
message = self._bot.sendMessage(self._chat_id, '.')
|
2017-06-22 18:04:19 +02:00
|
|
|
message = message.reply_sticker(self.sticker.file_id)
|
2016-09-20 06:36:55 +02:00
|
|
|
|
|
|
|
self.assertNotEqual(message.sticker.file_id, '')
|
|
|
|
|
2017-05-14 23:29:31 +02:00
|
|
|
def test_equality(self):
|
2017-06-22 18:04:19 +02:00
|
|
|
a = telegram.Sticker(self.sticker.file_id, self.sticker.width, self.sticker.height)
|
|
|
|
b = telegram.Sticker(self.sticker.file_id, self.sticker.width, self.sticker.height)
|
|
|
|
c = telegram.Sticker(self.sticker.file_id, 0, 0)
|
|
|
|
d = telegram.Sticker("", self.sticker.width, self.sticker.height)
|
|
|
|
e = telegram.PhotoSize(self.sticker.file_id, self.sticker.width, self.sticker.height)
|
2017-05-14 23:29:31 +02:00
|
|
|
|
|
|
|
self.assertEqual(a, b)
|
|
|
|
self.assertEqual(hash(a), hash(b))
|
|
|
|
self.assertIsNot(a, b)
|
|
|
|
|
|
|
|
self.assertEqual(a, c)
|
|
|
|
self.assertEqual(hash(a), hash(c))
|
|
|
|
|
|
|
|
self.assertNotEqual(a, d)
|
|
|
|
self.assertNotEqual(hash(a), hash(d))
|
|
|
|
|
|
|
|
self.assertNotEqual(a, e)
|
|
|
|
self.assertNotEqual(hash(a), hash(e))
|
|
|
|
|
2015-09-07 17:10:29 +02:00
|
|
|
|
2017-07-22 13:34:51 +02:00
|
|
|
class TestStickerSet(BaseTest, unittest.TestCase):
|
|
|
|
# TODO: Implement bot tests for StickerSet
|
|
|
|
# It's hard to test creation when we can't delete sticker sets
|
|
|
|
def setUp(self):
|
|
|
|
self.name = 'test_by_{0}'.format(self._bot.username)
|
|
|
|
self.title = 'Test stickers'
|
|
|
|
self.contains_masks = False
|
|
|
|
self.stickers = [telegram.Sticker('file_id', 512, 512)]
|
|
|
|
|
|
|
|
self.json_dict = {
|
|
|
|
'name': self.name,
|
|
|
|
'title': self.title,
|
|
|
|
'contains_masks': self.contains_masks,
|
|
|
|
'stickers': [x.to_dict() for x in self.stickers]
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_sticker_set_de_json(self):
|
|
|
|
sticker_set = telegram.StickerSet.de_json(self.json_dict, self._bot)
|
|
|
|
|
|
|
|
self.assertEqual(sticker_set.name, self.name)
|
|
|
|
self.assertEqual(sticker_set.title, self.title)
|
|
|
|
self.assertEqual(sticker_set.contains_masks, self.contains_masks)
|
|
|
|
self.assertEqual(sticker_set.stickers, self.stickers)
|
|
|
|
|
|
|
|
def test_sticker_set_to_json(self):
|
|
|
|
sticker_set = telegram.StickerSet.de_json(self.json_dict, self._bot)
|
|
|
|
|
|
|
|
self.assertTrue(self.is_json(sticker_set.to_json()))
|
|
|
|
|
|
|
|
def test_sticker_set_to_dict(self):
|
|
|
|
sticker_set = telegram.StickerSet.de_json(self.json_dict, self._bot).to_dict()
|
|
|
|
|
|
|
|
self.assertTrue(self.is_dict(sticker_set))
|
|
|
|
self.assertDictEqual(self.json_dict, sticker_set)
|
|
|
|
|
|
|
|
def test_equality(self):
|
|
|
|
a = telegram.StickerSet(self.name, self.title, self.contains_masks, self.stickers)
|
|
|
|
b = telegram.StickerSet(self.name, self.title, self.contains_masks, self.stickers)
|
|
|
|
c = telegram.StickerSet(self.name, None, None, None)
|
|
|
|
d = telegram.StickerSet('blah', self.title, self.contains_masks, self.stickers)
|
|
|
|
e = telegram.Audio(self.name, 0, None, None)
|
|
|
|
|
|
|
|
self.assertEqual(a, b)
|
|
|
|
self.assertEqual(hash(a), hash(b))
|
|
|
|
self.assertIsNot(a, b)
|
|
|
|
|
|
|
|
self.assertEqual(a, c)
|
|
|
|
self.assertEqual(hash(a), hash(c))
|
|
|
|
|
|
|
|
self.assertNotEqual(a, d)
|
|
|
|
self.assertNotEqual(hash(a), hash(d))
|
|
|
|
|
|
|
|
self.assertNotEqual(a, e)
|
|
|
|
self.assertNotEqual(hash(a), hash(e))
|
|
|
|
|
|
|
|
|
|
|
|
class TestMaskPosition(BaseTest, unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.point = telegram.MaskPosition.EYES
|
|
|
|
self.x_shift = -1
|
|
|
|
self.y_shift = 1
|
|
|
|
self.zoom = 2
|
|
|
|
|
|
|
|
self.json_dict = {
|
|
|
|
'point': self.point,
|
|
|
|
'x_shift': self.x_shift,
|
|
|
|
'y_shift': self.y_shift,
|
|
|
|
'zoom': self.zoom
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_mask_position_de_json(self):
|
|
|
|
mask_position = telegram.MaskPosition.de_json(self.json_dict, self._bot)
|
|
|
|
|
|
|
|
self.assertEqual(mask_position.point, self.point)
|
|
|
|
self.assertEqual(mask_position.x_shift, self.x_shift)
|
|
|
|
self.assertEqual(mask_position.y_shift, self.y_shift)
|
|
|
|
self.assertEqual(mask_position.zoom, self.zoom)
|
|
|
|
|
|
|
|
def test_mask_positiont_to_json(self):
|
|
|
|
mask_position = telegram.MaskPosition.de_json(self.json_dict, self._bot)
|
|
|
|
|
|
|
|
self.assertTrue(self.is_json(mask_position.to_json()))
|
|
|
|
|
|
|
|
def test_mask_position_to_dict(self):
|
|
|
|
mask_position = telegram.MaskPosition.de_json(self.json_dict, self._bot).to_dict()
|
|
|
|
|
|
|
|
self.assertTrue(self.is_dict(mask_position))
|
|
|
|
self.assertDictEqual(self.json_dict, mask_position)
|
|
|
|
|
|
|
|
|
2015-09-07 17:10:29 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|