Unit tests for banning
This commit is contained in:
parent
1d111f003f
commit
2d4bf992de
37
tests.py
37
tests.py
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import AsyncMock
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
@ -19,20 +19,23 @@ BOT_SPEC = ["kick_chat_member", "delete_message"]
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Chat:
|
class Chat:
|
||||||
ban_member = MagicMock()
|
ban_member = AsyncMock()
|
||||||
effective_name = "effective_chat_name"
|
effective_name = "effective_chat_name"
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class User:
|
class User:
|
||||||
id = 0
|
id_count = 0
|
||||||
name = "User name"
|
|
||||||
|
def __init__(self):
|
||||||
|
self.id = User.id_count
|
||||||
|
self.name = "User name"
|
||||||
|
User.id_count += 1
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Message:
|
class Message:
|
||||||
text = "Message text"
|
text = "Message text"
|
||||||
delete = MagicMock()
|
delete = AsyncMock()
|
||||||
chat = Chat()
|
chat = Chat()
|
||||||
from_user = User()
|
from_user = User()
|
||||||
|
|
||||||
|
@ -50,17 +53,37 @@ def make_update(msg: str) -> Update:
|
||||||
|
|
||||||
class TestAutoBanBot(unittest.IsolatedAsyncioTestCase):
|
class TestAutoBanBot(unittest.IsolatedAsyncioTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.regexes = list(map(lambda r: re.compile(r, re.I), ["test123", "(t\\.me\\/|\\@)[a-z]+bot"]))
|
self.regexes = list(map(lambda r: re.compile(r, re.I), [
|
||||||
|
"test123",
|
||||||
|
"(?:@|(?:(?:(?:https?://)?t(?:elegram)?)\.me\/))(\w{4,})bot"
|
||||||
|
]))
|
||||||
|
|
||||||
async def test_not_bannable(self):
|
async def test_not_bannable(self):
|
||||||
messages = ["not bannable message", "siematest_123elo", "i am not a bot", "t.me, i'm not a bot"]
|
messages = ["not bannable message", "siematest_123elo", "i am not a bot", "t.me, i'm not a bot"]
|
||||||
for msg in messages:
|
for msg in messages:
|
||||||
with self.subTest(msg=msg):
|
with self.subTest(msg=msg):
|
||||||
update = make_update(msg)
|
update = make_update(msg)
|
||||||
|
update.message.delete.reset_mock()
|
||||||
|
update.message.chat.ban_member.reset_mock()
|
||||||
await autobanbot.new_msg(update, None, self.regexes)
|
await autobanbot.new_msg(update, None, self.regexes)
|
||||||
update.message.delete.assert_not_called()
|
update.message.delete.assert_not_called()
|
||||||
update.message.chat.ban_member.assert_not_called()
|
update.message.chat.ban_member.assert_not_called()
|
||||||
|
|
||||||
|
async def test_not_bannable(self):
|
||||||
|
messages = [
|
||||||
|
"t.me/whatever_bot?not bannable message",
|
||||||
|
"https://www.t.me/ohhaaaaaaaibot/siematest123elo",
|
||||||
|
"@Hello_BoT"
|
||||||
|
]
|
||||||
|
for msg in messages:
|
||||||
|
with self.subTest(msg=msg):
|
||||||
|
update = make_update(msg)
|
||||||
|
update.message.delete.reset_mock()
|
||||||
|
update.message.chat.ban_member.reset_mock()
|
||||||
|
await autobanbot.new_msg(update, None, self.regexes)
|
||||||
|
update.message.delete.assert_called_once()
|
||||||
|
update.message.chat.ban_member.assert_called_once_with(update.message.from_user.id)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue