|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# A library that provides a Python interface to the Telegram Bot API |
| 4 | +# Copyright (C) 2015-2020 |
| 5 | +# Leandro Toledo de Souza <devs@python-telegram-bot.org> |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU Lesser Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU Lesser Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser Public License |
| 18 | +# along with this program. If not, see [http://www.gnu.org/licenses/]. |
| 19 | + |
| 20 | +import os |
| 21 | +import pytest |
| 22 | +from flaky import flaky |
| 23 | + |
| 24 | +from telegram import ChatPhoto, Voice, TelegramError |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(scope='function') |
| 28 | +def chatphoto_file(): |
| 29 | + f = open('tests/data/telegram.jpg', 'rb') |
| 30 | + yield f |
| 31 | + f.close() |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope='function') |
| 35 | +def chat_photo(bot, super_group_id): |
| 36 | + return bot.get_chat(super_group_id, timeout=50).photo |
| 37 | + |
| 38 | + |
| 39 | +class TestChatPhoto(object): |
| 40 | + chatphoto_small_file_id = 'smallCgADAQADngIAAuyVeEez0xRovKi9VAI' |
| 41 | + chatphoto_big_file_id = 'bigCgADAQADngIAAuyVeEez0xRovKi9VAI' |
| 42 | + chatphoto_small_file_unique_id = 'smalladc3145fd2e84d95b64d68eaa22aa33e' |
| 43 | + chatphoto_big_file_unique_id = 'bigadc3145fd2e84d95b64d68eaa22aa33e' |
| 44 | + chatphoto_file_url = 'https://python-telegram-bot.org/static/testfiles/telegram.jpg' |
| 45 | + |
| 46 | + @flaky(3, 1) |
| 47 | + @pytest.mark.timeout(10) |
| 48 | + def test_send_all_args(self, bot, super_group_id, chatphoto_file, chat_photo, thumb_file): |
| 49 | + assert bot.set_chat_photo(super_group_id, chatphoto_file) |
| 50 | + |
| 51 | + @flaky(3, 1) |
| 52 | + @pytest.mark.timeout(10) |
| 53 | + def test_get_and_download(self, bot, chat_photo): |
| 54 | + new_file = bot.get_file(chat_photo.small_file_id) |
| 55 | + |
| 56 | + assert new_file.file_id == chat_photo.small_file_id |
| 57 | + assert new_file.file_path.startswith('https://') |
| 58 | + |
| 59 | + new_file.download('telegram.jpg') |
| 60 | + |
| 61 | + assert os.path.isfile('telegram.jpg') |
| 62 | + |
| 63 | + new_file = bot.get_file(chat_photo.big_file_id) |
| 64 | + |
| 65 | + assert new_file.file_id == chat_photo.big_file_id |
| 66 | + assert new_file.file_path.startswith('https://') |
| 67 | + |
| 68 | + new_file.download('telegram.jpg') |
| 69 | + |
| 70 | + assert os.path.isfile('telegram.jpg') |
| 71 | + |
| 72 | + def test_send_with_chat_photo(self, monkeypatch, bot, super_group_id, chat_photo): |
| 73 | + def test(_, url, data, **kwargs): |
| 74 | + return data['photo'] == chat_photo |
| 75 | + |
| 76 | + monkeypatch.setattr('telegram.utils.request.Request.post', test) |
| 77 | + message = bot.set_chat_photo(photo=chat_photo, chat_id=super_group_id) |
| 78 | + assert message |
| 79 | + |
| 80 | + def test_de_json(self, bot, chat_photo): |
| 81 | + json_dict = { |
| 82 | + 'small_file_id': self.chatphoto_small_file_id, |
| 83 | + 'big_file_id': self.chatphoto_big_file_id, |
| 84 | + 'small_file_unique_id': self.chatphoto_small_file_unique_id, |
| 85 | + 'big_file_unique_id': self.chatphoto_big_file_unique_id, |
| 86 | + } |
| 87 | + chat_photo = ChatPhoto.de_json(json_dict, bot) |
| 88 | + assert chat_photo.small_file_id == self.chatphoto_small_file_id |
| 89 | + assert chat_photo.big_file_id == self.chatphoto_big_file_id |
| 90 | + assert chat_photo.small_file_unique_id == self.chatphoto_small_file_unique_id |
| 91 | + assert chat_photo.big_file_unique_id == self.chatphoto_big_file_unique_id |
| 92 | + |
| 93 | + def test_to_dict(self, chat_photo): |
| 94 | + chat_photo_dict = chat_photo.to_dict() |
| 95 | + |
| 96 | + assert isinstance(chat_photo_dict, dict) |
| 97 | + assert chat_photo_dict['small_file_id'] == chat_photo.small_file_id |
| 98 | + assert chat_photo_dict['big_file_id'] == chat_photo.big_file_id |
| 99 | + assert chat_photo_dict['small_file_unique_id'] == chat_photo.small_file_unique_id |
| 100 | + assert chat_photo_dict['big_file_unique_id'] == chat_photo.big_file_unique_id |
| 101 | + |
| 102 | + @flaky(3, 1) |
| 103 | + @pytest.mark.timeout(10) |
| 104 | + def test_error_send_empty_file(self, bot, super_group_id): |
| 105 | + chatphoto_file = open(os.devnull, 'rb') |
| 106 | + |
| 107 | + with pytest.raises(TelegramError): |
| 108 | + bot.set_chat_photo(chat_id=super_group_id, photo=chatphoto_file) |
| 109 | + |
| 110 | + @flaky(3, 1) |
| 111 | + @pytest.mark.timeout(10) |
| 112 | + def test_error_send_empty_file_id(self, bot, super_group_id): |
| 113 | + with pytest.raises(TelegramError): |
| 114 | + bot.set_chat_photo(chat_id=super_group_id, photo='') |
| 115 | + |
| 116 | + def test_error_send_without_required_args(self, bot, super_group_id): |
| 117 | + with pytest.raises(TypeError): |
| 118 | + bot.set_chat_photo(chat_id=super_group_id) |
| 119 | + |
| 120 | + def test_get_small_file_instance_method(self, monkeypatch, chat_photo): |
| 121 | + def test(*args, **kwargs): |
| 122 | + return args[1] == chat_photo.small_file_id |
| 123 | + |
| 124 | + monkeypatch.setattr('telegram.Bot.get_file', test) |
| 125 | + assert chat_photo.get_small_file() |
| 126 | + |
| 127 | + def test_get_big_file_instance_method(self, monkeypatch, chat_photo): |
| 128 | + def test(*args, **kwargs): |
| 129 | + return args[1] == chat_photo.big_file_id |
| 130 | + |
| 131 | + monkeypatch.setattr('telegram.Bot.get_file', test) |
| 132 | + assert chat_photo.get_big_file() |
| 133 | + |
| 134 | + def test_equality(self): |
| 135 | + a = ChatPhoto(self.chatphoto_small_file_id, self.chatphoto_big_file_id, |
| 136 | + self.chatphoto_small_file_unique_id, self.chatphoto_big_file_unique_id) |
| 137 | + b = ChatPhoto(self.chatphoto_small_file_id, self.chatphoto_big_file_id, |
| 138 | + self.chatphoto_small_file_unique_id, self.chatphoto_big_file_unique_id) |
| 139 | + c = ChatPhoto('', '', self.chatphoto_small_file_unique_id, |
| 140 | + self.chatphoto_big_file_unique_id) |
| 141 | + d = ChatPhoto('', '', 0, 0) |
| 142 | + e = Voice(self.chatphoto_small_file_id, self.chatphoto_small_file_unique_id, 0) |
| 143 | + |
| 144 | + assert a == b |
| 145 | + assert hash(a) == hash(b) |
| 146 | + assert a is not b |
| 147 | + |
| 148 | + assert a != c |
| 149 | + assert hash(a) != hash(c) |
| 150 | + |
| 151 | + assert a != d |
| 152 | + assert hash(a) != hash(d) |
| 153 | + |
| 154 | + assert a != e |
| 155 | + assert hash(a) != hash(e) |
0 commit comments