2018-07-24 13:31:04 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import unittest
|
2024-06-13 16:24:19 +00:00
|
|
|
from unittest.mock import MagicMock
|
2024-06-13 14:45:27 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
2024-06-13 16:24:19 +00:00
|
|
|
from dataclasses import dataclass
|
2024-06-13 14:45:27 +00:00
|
|
|
fakes_dir = os.path.join(os.path.dirname(__file__), 'fakes')
|
|
|
|
assert(os.path.exists(fakes_dir))
|
|
|
|
sys.path.insert(0, fakes_dir)
|
2018-07-24 13:31:04 +00:00
|
|
|
import autobanbot
|
|
|
|
|
|
|
|
CHAT_ID = "Chat ID"
|
|
|
|
USER_ID = "User ID"
|
|
|
|
MESSAGE_ID = "Message ID"
|
|
|
|
BOT_SPEC = ["kick_chat_member", "delete_message"]
|
|
|
|
|
|
|
|
|
2024-06-13 16:24:19 +00:00
|
|
|
@dataclass
|
|
|
|
class Chat:
|
|
|
|
ban_member = MagicMock()
|
|
|
|
effective_name = "effective_chat_name"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class User:
|
|
|
|
id = 0
|
|
|
|
name = "User name"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Message:
|
|
|
|
text = "Message text"
|
|
|
|
delete = MagicMock()
|
|
|
|
chat = Chat()
|
|
|
|
from_user = User()
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Update:
|
|
|
|
message = Message()
|
|
|
|
|
|
|
|
|
|
|
|
def make_update(msg: str) -> Update:
|
|
|
|
update = Update()
|
|
|
|
update.message.text = msg
|
|
|
|
return update
|
|
|
|
|
|
|
|
|
2024-06-13 14:45:27 +00:00
|
|
|
class TestAutoBanBot(unittest.IsolatedAsyncioTestCase):
|
2018-07-24 13:31:04 +00:00
|
|
|
def setUp(self):
|
2024-06-13 16:24:19 +00:00
|
|
|
self.regexes = list(map(lambda r: re.compile(r, re.I), ["test123", "(t\\.me\\/|\\@)[a-z]+bot"]))
|
2018-07-24 13:31:04 +00:00
|
|
|
|
2024-06-13 14:45:27 +00:00
|
|
|
async def test_not_bannable(self):
|
2024-06-13 16:24:19 +00:00
|
|
|
messages = ["not bannable message", "siematest_123elo", "i am not a bot", "t.me, i'm not a bot"]
|
|
|
|
for msg in messages:
|
|
|
|
with self.subTest(msg=msg):
|
|
|
|
update = make_update(msg)
|
|
|
|
await autobanbot.new_msg(update, None, self.regexes)
|
|
|
|
update.message.delete.assert_not_called()
|
|
|
|
update.message.chat.ban_member.assert_not_called()
|
2018-07-24 13:31:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|