-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bot.py
519 lines (424 loc) · 21.7 KB
/
test_bot.py
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import pytest
from unittest.mock import AsyncMock, patch
from bot import replace_ids, Channel, User, Usergroup, get_team_of, process_command, clean_slack_text, send_message, route_message
from slack_sdk.errors import SlackApiError
@pytest.mark.asyncio
async def test_replace_ids_user_id():
app = AsyncMock()
text = "Hello <@U12345>!"
app.client.users_info.return_value = {"user": {"id": "U12345", "real_name": "John Doe"}}
with patch('bot.get_user_by_id', return_value=User(id="U12345", name="johndoe", real_name="John Doe", team="team1")):
result = await replace_ids(app, None, text)
assert result == "Hello John Doe!"
@pytest.mark.asyncio
async def test_replace_ids_channel_id():
app = AsyncMock()
text = "Check out <#C67890> channel."
app.client.conversations_info.return_value = {"channel": {"id": "C67890", "name": "random"}}
with patch('bot.get_channel_by_id', return_value=Channel(id="C67890", name="random", config={})):
result = await replace_ids(app, None, text)
assert result == "Check out #random channel."
@pytest.mark.asyncio
async def test_replace_ids_fallback():
app = AsyncMock()
text = "Hello <@U99999|unknown>!"
with patch('bot.get_user_by_id', return_value=User(id=None, name="unknown", real_name="", team="")):
result = await replace_ids(app, None, text)
assert result == "Hello unknown!"
@pytest.mark.asyncio
async def test_replace_ids_no_user():
app = AsyncMock()
text = "Hello <@U99999|>!"
with patch('bot.get_user_by_id', return_value=User(id=None, name="unknown", real_name="", team="")):
result = await replace_ids(app, None, text)
assert result == "Hello @U99999!"
@pytest.mark.asyncio
async def test_replace_ids_usergroup():
app = AsyncMock()
text = "Hello <!subteam^S07RS6NT467>!"
with patch('bot.get_usergroup_by_id', return_value=Usergroup(id="S07RS6NT467", handle="supergroup", name="The Super Group")):
result = await replace_ids(app, None, text)
assert result == "Hello @supergroup!"
@pytest.mark.asyncio
async def test_replace_ids_no_match():
app = AsyncMock()
text = "Hello world!"
result = await replace_ids(app, None, text)
assert result == "Hello world!"
@pytest.mark.asyncio
async def test_get_team_of_single_user():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
username = "@johndoe"
user = User(id="U12345", name="test", real_name="Test User", team="Testers")
thread_ts = "1234567890.123456"
with patch('bot.get_user_by_name', return_value=User(id="U12345", name="johndoe", real_name="John Doe", team="team1")):
with patch('bot.send_message') as mock_send_message:
await get_team_of(app, channel, username, user, thread_ts)
mock_send_message.assert_called_once_with(app, channel, user, "*John Doe* (<@U12345>): team1", thread_ts)
@pytest.mark.asyncio
async def test_get_team_of_multiple_users():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
username = "@johndoe @janedoe"
user = User(id="U12345", name="test", real_name="Test User", team="Testers")
thread_ts = "1234567890.123456"
with patch('bot.get_user_by_name', side_effect=[
User(id="U12345", name="johndoe", real_name="John Doe", team="team1"),
User(id="U67890", name="janedoe", real_name="Jane Doe", team="team2")
]):
with patch('bot.send_message') as mock_send_message:
await get_team_of(app, channel, username, user, thread_ts)
mock_send_message.assert_called_once_with(app, channel, user, "*John Doe* (<@U12345>): team1\n*Jane Doe* (<@U67890>): team2", thread_ts)
@pytest.mark.asyncio
async def test_get_team_of_unknown_user():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
username = "@unknownuser"
user = User(id="U12345", name="test", real_name="Test User", team="Testers")
thread_ts = "1234567890.123456"
with patch('bot.get_user_by_name', return_value=User(id=None, name="unknownuser", real_name="", team="")):
with patch('bot.send_message') as mock_send_message:
await get_team_of(app, channel, username, user, thread_ts)
mock_send_message.assert_called_once_with(app, channel, user, "Unknown user: `@unknownuser`.", thread_ts)
@pytest.mark.asyncio
async def test_get_team_of_no_mentions():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
username = "no mentions here"
user = User(id="U12345", name="test", real_name="Test User", team="Testers")
thread_ts = "1234567890.123456"
with patch('bot.send_message') as mock_send_message:
await get_team_of(app, channel, username, user, thread_ts)
mock_send_message.assert_called_once_with(app, channel, user, "Unknown user: `no mentions here`.", thread_ts)
@pytest.mark.asyncio
async def test_process_command_set_wait_time():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
for text in [ "set wait-time 10", "wait-time 10", "set wait_time 10", "set waittime 10", "waittime \"10\"", "waittime '10'" ]:
with patch('bot.set_wait_time') as mock_set_wait_time:
await process_command(app, text, channel, user, thread_ts)
mock_set_wait_time.assert_called_once_with(app, channel, 10, user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_set_reply_message():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
for text in [ "set message \"Hello, world!\"", "message \"Hello, world!\"", "message 'Hello, world!'", "message Hello, world!" ]:
with patch('bot.set_reply_message') as mock_set_reply_message:
await process_command(app, text, channel, user, thread_ts)
mock_set_reply_message.assert_called_once_with(app, channel, "Hello, world!", user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_enable_opsgenie():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
text = f"enable opsgenie"
for text in [ "enable opsgenie", "enable alerts", "enable alert" ]:
with patch('bot.set_opsgenie') as mock_set_opsgenie:
await process_command(app, text, channel, user, thread_ts)
mock_set_opsgenie.assert_called_once_with(app, channel, True, user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_disable_opsgenie():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
for text in [ "disable opsgenie", "disable alerts", "disable alert" ]:
with patch('bot.set_opsgenie') as mock_set_opsgenie:
await process_command(app, text, channel, user, thread_ts)
mock_set_opsgenie.assert_called_once_with(app, channel, False, user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_list_teams():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
for text in [ "list teams", "list team", "teams", "team" ]:
with patch('bot.list_teams') as mock_list_teams:
await process_command(app, text, channel, user, thread_ts)
mock_list_teams.assert_called_once_with(app, channel, user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_get_team_of():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
for text in [ "team of @johndoe", "team @johndoe", "team @johndoe", "team of @johndoe", "team of \"@johndoe\"", "team of '@johndoe'" ]:
with patch('bot.get_team_of') as mock_get_team_of:
await process_command(app, text, channel, user, thread_ts)
mock_get_team_of.assert_called_once_with(app, channel, "@johndoe", user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_add_excluded_team():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
text = f"add excluded-team team1"
for text in [ "add excluded-teams team1", "add exclude team1", "add excluded team1", "add excluded-team team1", "add exclude_team team1", "add exclude_team \"team1\"", "add exclude_team 'team1'" ]:
with patch('bot.add_excluded_team') as mock_add_excluded_team:
await process_command(app, text, channel, user, thread_ts)
mock_add_excluded_team.assert_called_once_with(app, channel, "team1", user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_clear_excluded_team():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
for text in [ "clear excluded-teams", "clear exclude", "clear excluded", "clear excluded-team", "clear exclude_team" ]:
with patch('bot.clear_excluded_team') as mock_clear_excluded_team:
await process_command(app, text, channel, user, thread_ts)
mock_clear_excluded_team.assert_called_once_with(app, channel, user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_add_included_team():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
for text in [ "add included-teams team1", "add include team1", "add included team1", "add included-team team1", "add include_team team1", "add include_team \"team1\"", "add include_team 'team1'" ]:
with patch('bot.add_included_team') as mock_add_included_team:
await process_command(app, text, channel, user, thread_ts)
mock_add_included_team.assert_called_once_with(app, channel, "team1", user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_clear_included_team():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
for text in [ "clear included-teams", "clear include", "clear included", "clear included-team", "clear include_team" ]:
with patch('bot.clear_included_team') as mock_clear_included_team:
await process_command(app, text, channel, user, thread_ts)
mock_clear_included_team.assert_called_once_with(app, channel, user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_show_config():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
for text in [ "show config", "config", "show configuration", "configuration" ]:
with patch('bot.show_config') as mock_show_config:
await process_command(app, text, channel, user, thread_ts)
mock_show_config.assert_called_once_with(app, channel, user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_help():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
text = f"help"
with patch('bot.send_help_message') as mock_send_help_message:
await process_command(app, text, channel, user, thread_ts)
mock_send_help_message.assert_called_once_with(app, channel, user, thread_ts)
@pytest.mark.asyncio
async def test_process_command_unknown():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User("U12345", "test", "Test User", "Testers")
thread_ts = "1234567890.123456"
text = f"unknown command"
with patch('bot.send_message') as mock_send_message:
await process_command(app, text, channel, user, thread_ts)
mock_send_message.assert_called_once_with(app, channel, user, "Huh? :thinking_face: Maybe type `/hutbot help` for a list of commands.", thread_ts)
@pytest.mark.asyncio
async def test_clean_slack_text_unescape_formatting():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
text = r"Hello \*world\*!"
result = await clean_slack_text(app, channel, text)
assert result == "Hello world!"
@pytest.mark.asyncio
async def test_clean_slack_text_replace_ids():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
text = "Hello <@U12345>!"
with patch('bot.replace_ids', return_value="Hello John Doe!"):
result = await clean_slack_text(app, channel, text)
assert result == "Hello John Doe!"
@pytest.mark.asyncio
async def test_clean_slack_text_replace_links():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
text = "Check this link: <http://example.com|example>"
result = await clean_slack_text(app, channel, text)
assert result == "Check this link: example"
@pytest.mark.asyncio
async def test_clean_slack_text_replace_links_no_text():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
text = "Check this link: <http://example.com>"
result = await clean_slack_text(app, channel, text)
assert result == "Check this link: [URL]"
@pytest.mark.asyncio
async def test_clean_slack_text_remove_formatting():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
text = """
*bold* _italic_ ~strikethrough~ `code`
```
code
block
```
"""
result = await clean_slack_text(app, channel, text)
assert result == "bold italic strikethrough code code block"
@pytest.mark.asyncio
async def test_clean_slack_text_remove_newlines():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
text = "Hello\nworld!"
result = await clean_slack_text(app, channel, text)
assert result == "Hello world!"
@pytest.mark.asyncio
async def test_send_message_success():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User(id="U12345", name="johndoe", real_name="John Doe", team="team1")
text = "Hello, world!"
thread_ts = "1234567890.123456"
with patch('bot.log_debug') as mock_log_debug:
await send_message(app, channel, user, text, thread_ts)
app.client.chat_postMessage.assert_called_once_with(
channel=channel.id,
thread_ts=thread_ts,
text=text,
mrkdwn=True
)
mock_log_debug.assert_called()
@pytest.mark.asyncio
async def test_send_message_ephemeral_success():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User(id="U12345", name="johndoe", real_name="John Doe", team="team1")
text = "Hello, world!"
with patch('bot.log_debug') as mock_log_debug:
await send_message(app, channel, user, text)
app.client.chat_postEphemeral.assert_called_once_with(
channel=channel.id,
user=user.id,
text=text,
mrkdwn=True
)
mock_log_debug.assert_called()
@pytest.mark.asyncio
async def test_send_message_retry():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User(id="U12345", name="johndoe", real_name="John Doe", team="team1")
text = "Hello, world!"
thread_ts = "1234567890.123456"
app.client.chat_postMessage.side_effect = [SlackApiError("error", "error"), None]
with patch('bot.log_warning') as mock_log_warning, patch('bot.log_debug') as mock_log_debug:
await send_message(app, channel, user, text, thread_ts)
assert app.client.chat_postMessage.call_count == 2
mock_log_warning.assert_called()
mock_log_debug.assert_called()
@pytest.mark.asyncio
async def test_send_message_fail():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User(id="U12345", name="johndoe", real_name="John Doe", team="team1")
text = "Hello, world!"
thread_ts = "1234567890.123456"
app.client.chat_postMessage.side_effect = SlackApiError("error", "error")
with patch('bot.log_error') as mock_log_error, patch('bot.log_warning') as mock_log_warning, patch('bot.log_debug') as mock_log_debug:
await send_message(app, channel, user, text, thread_ts)
assert app.client.chat_postMessage.call_count == 3
mock_log_warning.assert_called()
mock_log_error.assert_called()
mock_log_debug.assert_called()
@pytest.mark.asyncio
async def test_send_message_ephemeral_fail():
app = AsyncMock()
channel = Channel(id="C12345", name="general", config={})
user = User(id="U12345", name="johndoe", real_name="John Doe", team="team1")
text = "Hello, world!"
app.client.chat_postEphemeral.side_effect = SlackApiError("error", "error")
with patch('bot.log_error') as mock_log_error, patch('bot.log_warning') as mock_log_warning, patch('bot.log_debug') as mock_log_debug:
await send_message(app, channel, user, text)
assert app.client.chat_postEphemeral.call_count == 3
mock_log_warning.assert_called()
mock_log_error.assert_called()
mock_log_debug.assert_called()
@pytest.mark.asyncio
async def test_route_message_ignore_subtype():
app = AsyncMock()
opsgenie_token = "dummy_token"
event = {
"channel": "C12345",
"user": "U67890",
"subtype": "channel_join",
"text": "Hello, world!",
"ts": "1234567890.123456"
}
with patch('bot.get_channel_name', return_value="general"), patch('bot.log') as mock_log:
await route_message(app, opsgenie_token, event)
mock_log.assert_called_with("Ignoring message with subtype 'channel_join' for channel #general.")
@pytest.mark.asyncio
async def test_route_message_deleted_message():
app = AsyncMock()
opsgenie_token = "dummy_token"
event = {
"channel": "C12345",
"user": "U67890",
"subtype": "message_deleted",
"previous_message": {
"user": "U12345",
"ts": "1234567890.123456"
}
}
with patch('bot.get_channel_by_id', return_value=Channel(id="C12345", name="general", config={})), \
patch('bot.get_user_by_id', side_effect=[User(id="U67890", name="test", real_name="Test User", team="Testers"), User(id="U12345", name="johndoe", real_name="John Doe", team="team1")]), \
patch('bot.handle_message_deletion') as mock_handle_message_deletion:
await route_message(app, opsgenie_token, event)
mock_handle_message_deletion.assert_called_once_with(app, Channel(id="C12345", name="general", config={}), User(id="U12345", name="johndoe", real_name="John Doe", team="team1"), "1234567890.123456")
@pytest.mark.asyncio
async def test_route_message_command():
app = AsyncMock()
opsgenie_token = "dummy_token"
event = {
"channel": "C12345",
"user": "U67890",
"text": "set wait-time 10",
"ts": "1234567890.123456"
}
with patch('bot.get_channel_by_id', return_value=Channel(id="C12345", name="general", config={})), \
patch('bot.get_user_by_id', return_value=User(id="U67890", name="test", real_name="Test User", team="Testers")), \
patch('bot.is_command', return_value=True), \
patch('bot.process_command') as mock_process_command:
await route_message(app, opsgenie_token, event)
mock_process_command.assert_called_once_with(app, "set wait-time 10", Channel(id="C12345", name="general", config={}), User(id="U67890", name="test", real_name="Test User", team="Testers"), "1234567890.123456")
@pytest.mark.asyncio
async def test_route_message_thread_response():
app = AsyncMock()
opsgenie_token = "dummy_token"
event = {
"channel": "C12345",
"user": "U67890",
"text": "Hello, world!",
"ts": "1234567890.123456",
"thread_ts": "1234567890.123456"
}
with patch('bot.get_channel_by_id', return_value=Channel(id="C12345", name="general", config={})), \
patch('bot.get_user_by_id', return_value=User(id="U67890", name="test", real_name="Test User", team="Testers")), \
patch('bot.handle_thread_response') as mock_handle_thread_response:
await route_message(app, opsgenie_token, event)
mock_handle_thread_response.assert_called_once_with(app, Channel(id="C12345", name="general", config={}), User(id="U67890", name="test", real_name="Test User", team="Testers"), "1234567890.123456")
@pytest.mark.asyncio
async def test_route_message_channel_message():
app = AsyncMock()
opsgenie_token = "dummy_token"
event = {
"channel": "C12345",
"user": "U67890",
"text": "Hello, world!",
"ts": "1234567890.123456"
}
with patch('bot.get_channel_by_id', return_value=Channel(id="C12345", name="general", config={})), \
patch('bot.get_user_by_id', return_value=User(id="U67890", name="test", real_name="Test User", team="Testers")), \
patch('bot.handle_channel_message') as mock_handle_channel_message:
await route_message(app, opsgenie_token, event)
mock_handle_channel_message.assert_called_once_with(app, opsgenie_token, Channel(id="C12345", name="general", config={}), User(id="U67890", name="test", real_name="Test User", team="Testers"), "Hello, world!", "1234567890.123456")