-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtest_a_telegram_asyncio.py
More file actions
334 lines (282 loc) · 11.8 KB
/
test_a_telegram_asyncio.py
File metadata and controls
334 lines (282 loc) · 11.8 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from datetime import datetime, timedelta
from fastapi import status
import pytest
from app.telegram.handlers import MessageHandler, reply_unknown_user
from app.telegram.keyboards import DATE_FORMAT
from app.telegram.models import Bot, Chat
from tests.asyncio_fixture import today_date
from tests.client_fixture import get_test_placeholder_user
def gen_message(text):
return {
'update_id': 10000000,
'message': {
'message_id': 2434,
'from': {
'id': 666666,
'is_bot': False,
'first_name': 'Moshe',
'username': 'banana',
'language_code': 'en'
},
'chat': {
'id': 666666,
'first_name': 'Moshe',
'username': 'banana',
'type': 'private'
},
'date': 1611240725,
'text': f'{text}'
}
}
def gen_callback(text):
return {
'update_id': 568265,
'callback_query': {
'id': '546565356486',
'from': {
'id': 666666,
'is_bot': False,
'first_name': 'Moshe',
'username': 'banana',
'language_code': 'en'
}, 'message': {
'message_id': 838,
'from': {
'id': 2566252,
'is_bot': True,
'first_name': 'PyLandar',
'username': 'pylander_bot'
}, 'chat': {
'id': 666666,
'first_name': 'Moshe',
'username': 'banana',
'type': 'private'
},
'date': 161156,
'text': 'Choose events day.',
'reply_markup': {
'inline_keyboard': [
[
{
'text': 'Today',
'callback_data': 'Today'
},
{
'text': 'This week',
'callback_data': 'This week'
}
]
]
}
},
'chat_instance': '-154494',
'data': f'{text}'}}
class TestChatModel:
@staticmethod
def test_private_message():
chat = Chat(gen_message('Cool message'))
assert chat.message == 'Cool message'
assert chat.user_id == 666666
assert chat.first_name == 'Moshe'
@staticmethod
def test_callback_message():
chat = Chat(gen_callback('Callback Message'))
assert chat.message == 'Callback Message'
assert chat.user_id == 666666
assert chat.first_name == 'Moshe'
@pytest.mark.asyncio
async def test_bot_model():
bot = Bot("fake bot id", "https://google.com")
assert bot.base == 'https://api.telegram.org/botfake bot id/'
assert bot.webhook_setter_url == 'https://api.telegram.org/botfake \
bot id/setWebhook?url=https://google.com/telegram/'
assert bot.base == bot._set_base_url("fake bot id")
assert bot.webhook_setter_url == bot._set_webhook_setter_url(
"https://google.com")
set_request = await bot.set_webhook()
assert set_request.json() == {
'ok': False,
'error_code': 404,
'description': 'Not Found'
}
drop_request = await bot.drop_webhook()
assert drop_request.json() == {
'ok': False,
'error_code': 404,
'description': 'Not Found'
}
send_request = await bot.send_message("654654645", "hello")
assert send_request.status_code == status.HTTP_404_NOT_FOUND
assert send_request.json() == {
'ok': False,
'error_code': 404,
'description': 'Not Found'
}
class TestBotClient:
@staticmethod
@pytest.mark.asyncio
async def test_user_not_registered(telegram_client):
response = await telegram_client.post(
'/telegram/', json=gen_message('/start'))
assert response.status_code == status.HTTP_200_OK
assert b'Hello, Moshe!' in response.content
assert b'To use PyLendar Bot you have to register' \
in response.content
@staticmethod
@pytest.mark.asyncio
async def test_user_registered(telegram_client, session):
session.add(get_test_placeholder_user())
session.commit()
response = await telegram_client.post(
'/telegram/', json=gen_message('/start'))
assert response.status_code == status.HTTP_200_OK
assert b'Welcome to PyLendar telegram client!' in response.content
class TestHandlers:
TEST_USER = get_test_placeholder_user()
@pytest.mark.asyncio
async def test_start_handlers(self):
chat = Chat(gen_message('/start'))
message = MessageHandler(chat, self.TEST_USER)
assert '/start' in message.handlers
assert await message.process_callback() == '''Hello, Moshe!
Welcome to PyLendar telegram client!'''
@pytest.mark.asyncio
async def test_default_handlers(self):
wrong_start = MessageHandler(
Chat(gen_message('start')), self.TEST_USER)
wrong_show_events = MessageHandler(
Chat(gen_message('show_events')), self.TEST_USER)
message = MessageHandler(
Chat(gen_message('hello')), self.TEST_USER)
assert await wrong_start.process_callback() == "Unknown command."
assert await wrong_show_events.process_callback() == "Unknown command."
assert await message.process_callback() == "Unknown command."
@pytest.mark.asyncio
async def test_show_events_handler(self):
chat = Chat(gen_message('/show_events'))
message = MessageHandler(chat, self.TEST_USER)
assert await message.process_callback() == 'Choose events day.'
@pytest.mark.asyncio
async def test_no_today_events_handler(self):
chat = Chat(gen_callback('Today'))
message = MessageHandler(chat, self.TEST_USER)
assert await message.process_callback() == "There're no events today."
@pytest.mark.asyncio
async def test_today_handler(self, fake_user_events):
chat = Chat(gen_callback('Today'))
message = MessageHandler(chat, fake_user_events)
answer = f"{today_date.strftime('%A, %B %d')}:\n"
assert await message.process_callback() == answer
@pytest.mark.asyncio
async def test_this_week_handler(self):
chat = Chat(gen_callback('This week'))
message = MessageHandler(chat, self.TEST_USER)
assert await message.process_callback() == 'Choose a day.'
@pytest.mark.asyncio
async def test_no_chosen_day_handler(self):
chat = Chat(gen_callback('10 Feb 2021'))
message = MessageHandler(chat, self.TEST_USER)
message.handlers['10 Feb 2021'] = message.chosen_day_handler
answer = "There're no events on February 10."
assert await message.process_callback() == answer
@pytest.mark.asyncio
async def test_chosen_day_handler(self, fake_user_events):
chosen_date = today_date + timedelta(days=2)
button = str(chosen_date.strftime(DATE_FORMAT))
chat = Chat(gen_callback(button))
message = MessageHandler(chat, fake_user_events)
message.handlers[button] = message.chosen_day_handler
answer = f"{chosen_date.strftime('%A, %B %d')}:\n"
assert await message.chosen_day_handler() == answer
@pytest.mark.asyncio
async def test_new_event_handler(self):
chat = Chat(gen_message('/new_event'))
message = MessageHandler(chat, self.TEST_USER)
answer = 'Please, give your event a title.'
assert await message.process_callback() == answer
@pytest.mark.asyncio
async def test_process_new_event(self):
chat = Chat(gen_message('New Title'))
message = MessageHandler(chat, self.TEST_USER)
answer = 'Title:\nNew Title\n\n'
answer += 'Add a description of the event.'
assert await message.process_callback() == answer
chat = Chat(gen_message('New Content'))
message = MessageHandler(chat, self.TEST_USER)
answer = 'Content:\nNew Content\n\n'
answer += 'Where the event will be held?'
assert await message.process_callback() == answer
chat = Chat(gen_message('Universe'))
message = MessageHandler(chat, self.TEST_USER)
answer = 'Location:\nUniverse\n\n'
answer += 'is the event public? (yes\\no)'
assert await message.process_callback() == answer
chat = Chat(gen_message('yes'))
message = MessageHandler(chat, self.TEST_USER)
answer = 'public\\private event:\npublic event\n\n'
answer += 'When does it start?'
assert await message.process_callback() == answer
chat = Chat(gen_message('Not valid start datetime input'))
message = MessageHandler(chat, self.TEST_USER)
answer = '❗️ Please, enter a valid date/time.'
assert await message.process_callback() == answer
chat = Chat(gen_message('today'))
message = MessageHandler(chat, self.TEST_USER)
today = datetime.today()
answer = f'Starts on:\n{today.strftime("%d %b %Y %H:%M")}\n\n'
answer += 'And when does it end?'
assert await message.process_callback() == answer
chat = Chat(gen_message('Not valid end datetime input'))
message = MessageHandler(chat, self.TEST_USER)
answer = '❗️ Please, enter a valid date/time.'
assert await message.process_callback() == answer
chat = Chat(gen_message('tomorrow'))
message = MessageHandler(chat, self.TEST_USER)
tomorrow = today + timedelta(days=1)
answer = 'Title:\nNew Title\n\n'
answer += 'Content:\nNew Content\n\n'
answer += 'Location:\nUniverse\n\n'
answer += f'Starts on:\n{today.strftime("%d %b %Y %H:%M")}\n\n'
answer += f'Ends on:\n{tomorrow.strftime("%d %b %Y %H:%M")}'
assert await message.process_callback() == answer
chat = Chat(gen_message('create'))
message = MessageHandler(chat, self.TEST_USER)
answer = 'New event was successfully created 🎉'
assert await message.process_callback() == answer
@pytest.mark.asyncio
async def test_process_new_event_cancel(self):
chat = Chat(gen_message('/new_event'))
message = MessageHandler(chat, self.TEST_USER)
answer = 'Please, give your event a title.'
assert await message.process_callback() == answer
chat = Chat(gen_message('cancel'))
message = MessageHandler(chat, self.TEST_USER)
answer = '🚫 The process was canceled.'
assert await message.process_callback() == answer
@pytest.mark.asyncio
async def test_process_new_event_restart(self):
chat = Chat(gen_message('/new_event'))
message = MessageHandler(chat, self.TEST_USER)
answer = 'Please, give your event a title.'
assert await message.process_callback() == answer
chat = Chat(gen_message('New Title'))
message = MessageHandler(chat, self.TEST_USER)
answer = 'Title:\nNew Title\n\n'
answer += 'Add a description of the event.'
assert await message.process_callback() == answer
chat = Chat(gen_message('restart'))
message = MessageHandler(chat, self.TEST_USER)
answer = 'Please, give your event a title.'
assert await message.process_callback() == answer
@pytest.mark.asyncio
async def test_reply_unknown_user():
chat = Chat(gen_message('/show_events'))
answer = await reply_unknown_user(chat)
assert answer == '''
Hello, Moshe!
To use PyLendar Bot you have to register
your Telegram Id in your profile page.
Your Id is 666666
Keep it secret!
https://calendar.pythonic.guru/profile/
'''