File: test_requestdata.py

package info (click to toggle)
python-telegram-bot 22.3-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 11,060 kB
  • sloc: python: 90,298; makefile: 176; sh: 4
file content (228 lines) | stat: -rw-r--r-- 8,328 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2025
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser 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 Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program.  If not, see [http://www.gnu.org/licenses/].
import json
from typing import Any
from urllib.parse import quote

import pytest

from telegram import InputFile, InputMediaPhoto, InputMediaVideo, MessageEntity
from telegram.request import RequestData
from telegram.request._requestparameter import RequestParameter
from tests.auxil.files import data_file
from tests.auxil.slots import mro_slots


@pytest.fixture(scope="module")
def inputfiles() -> dict[bool, InputFile]:
    return {True: InputFile(obj="data", attach=True), False: InputFile(obj="data", attach=False)}


@pytest.fixture(scope="module")
def input_media_video() -> InputMediaVideo:
    return InputMediaVideo(
        media=data_file("telegram.mp4").read_bytes(),
        thumbnail=data_file("telegram.jpg").read_bytes(),
        parse_mode=None,
    )


@pytest.fixture(scope="module")
def input_media_photo() -> InputMediaPhoto:
    return InputMediaPhoto(
        media=data_file("telegram.jpg").read_bytes(),
        parse_mode=None,
    )


@pytest.fixture(scope="module")
def simple_params() -> dict[str, Any]:
    return {
        "string": "string",
        "integer": 1,
        "tg_object": MessageEntity("type", 1, 1),
        "list": [1, "string", MessageEntity("type", 1, 1)],
    }


@pytest.fixture(scope="module")
def simple_jsons() -> dict[str, Any]:
    return {
        "string": "string",
        "integer": json.dumps(1),
        "tg_object": MessageEntity("type", 1, 1).to_json(),
        "list": json.dumps([1, "string", MessageEntity("type", 1, 1).to_dict()]),
    }


@pytest.fixture(scope="module")
def simple_rqs(simple_params) -> RequestData:
    return RequestData(
        [RequestParameter.from_input(key, value) for key, value in simple_params.items()]
    )


@pytest.fixture(scope="module")
def file_params(inputfiles, input_media_video, input_media_photo) -> dict[str, Any]:
    return {
        "inputfile_attach": inputfiles[True],
        "inputfile_no_attach": inputfiles[False],
        "inputmedia": input_media_video,
        "inputmedia_list": [input_media_video, input_media_photo],
    }


@pytest.fixture(scope="module")
def file_jsons(inputfiles, input_media_video, input_media_photo) -> dict[str, Any]:
    input_media_video_dict = input_media_video.to_dict()
    input_media_video_dict["media"] = input_media_video.media.attach_uri
    input_media_video_dict["thumbnail"] = input_media_video.thumbnail.attach_uri
    input_media_photo_dict = input_media_photo.to_dict()
    input_media_photo_dict["media"] = input_media_photo.media.attach_uri
    return {
        "inputfile_attach": inputfiles[True].attach_uri,
        "inputmedia": json.dumps(input_media_video_dict),
        "inputmedia_list": json.dumps([input_media_video_dict, input_media_photo_dict]),
    }


@pytest.fixture(scope="module")
def file_rqs(file_params) -> RequestData:
    return RequestData(
        [RequestParameter.from_input(key, value) for key, value in file_params.items()]
    )


@pytest.fixture(scope="module")
def mixed_params(file_params, simple_params) -> dict[str, Any]:
    both = file_params.copy()
    both.update(simple_params)
    return both


@pytest.fixture(scope="module")
def mixed_jsons(file_jsons, simple_jsons) -> dict[str, Any]:
    both = file_jsons.copy()
    both.update(simple_jsons)
    return both


@pytest.fixture(scope="module")
def mixed_rqs(mixed_params) -> RequestData:
    return RequestData(
        [RequestParameter.from_input(key, value) for key, value in mixed_params.items()]
    )


class TestRequestDataWithoutRequest:
    def test_slot_behaviour(self, simple_rqs):
        for attr in simple_rqs.__slots__:
            assert getattr(simple_rqs, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(simple_rqs)) == len(set(mro_slots(simple_rqs))), "duplicate slot"

    def test_contains_files(self, simple_rqs, file_rqs, mixed_rqs):
        assert not simple_rqs.contains_files
        assert file_rqs.contains_files
        assert mixed_rqs.contains_files

    def test_parameters(
        self, simple_rqs, file_rqs, mixed_rqs, inputfiles, input_media_video, input_media_photo
    ):
        simple_params_expected = {
            "string": "string",
            "integer": 1,
            "tg_object": MessageEntity("type", 1, 1).to_dict(),
            "list": [1, "string", MessageEntity("type", 1, 1).to_dict()],
        }
        video_value = {
            "media": input_media_video.media.attach_uri,
            "thumbnail": input_media_video.thumbnail.attach_uri,
            "type": input_media_video.type,
        }
        photo_value = {"media": input_media_photo.media.attach_uri, "type": input_media_photo.type}
        file_params_expected = {
            "inputfile_attach": inputfiles[True].attach_uri,
            "inputmedia": video_value,
            "inputmedia_list": [video_value, photo_value],
        }
        mixed_params_expected = simple_params_expected.copy()
        mixed_params_expected.update(file_params_expected)

        assert simple_rqs.parameters == simple_params_expected
        assert file_rqs.parameters == file_params_expected
        assert mixed_rqs.parameters == mixed_params_expected

    def test_json_parameters(
        self, simple_rqs, file_rqs, mixed_rqs, simple_jsons, file_jsons, mixed_jsons
    ):
        assert simple_rqs.json_parameters == simple_jsons
        assert file_rqs.json_parameters == file_jsons
        assert mixed_rqs.json_parameters == mixed_jsons

    def test_json_payload(
        self, simple_rqs, file_rqs, mixed_rqs, simple_jsons, file_jsons, mixed_jsons
    ):
        assert simple_rqs.json_payload == json.dumps(simple_jsons).encode()
        assert file_rqs.json_payload == json.dumps(file_jsons).encode()
        assert mixed_rqs.json_payload == json.dumps(mixed_jsons).encode()

    def test_multipart_data(
        self,
        simple_rqs,
        file_rqs,
        mixed_rqs,
        inputfiles,
        input_media_video,
        input_media_photo,
    ):
        expected = {
            inputfiles[True].attach_name: inputfiles[True].field_tuple,
            "inputfile_no_attach": inputfiles[False].field_tuple,
            input_media_photo.media.attach_name: input_media_photo.media.field_tuple,
            input_media_video.media.attach_name: input_media_video.media.field_tuple,
            input_media_video.thumbnail.attach_name: input_media_video.thumbnail.field_tuple,
        }
        assert simple_rqs.multipart_data == {}
        assert file_rqs.multipart_data == expected
        assert mixed_rqs.multipart_data == expected

    def test_url_encoding(self):
        data = RequestData(
            [
                RequestParameter.from_input("chat_id", 123),
                RequestParameter.from_input("text", "Hello there/!"),
            ]
        )
        expected_params = "chat_id=123&text=Hello+there%2F%21"
        expected_url = "https://te.st/method?" + expected_params
        assert data.url_encoded_parameters() == expected_params
        assert data.parametrized_url("https://te.st/method") == expected_url

        expected_params = "chat_id=123&text=Hello%20there/!"
        expected_url = "https://te.st/method?" + expected_params
        assert (
            data.url_encoded_parameters(encode_kwargs={"quote_via": quote, "safe": "/!"})
            == expected_params
        )
        assert (
            data.parametrized_url(
                "https://te.st/method", encode_kwargs={"quote_via": quote, "safe": "/!"}
            )
            == expected_url
        )