2018-07-24 13:31:04 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import unittest
|
2024-06-13 19:28:13 +00:00
|
|
|
from unittest.mock import AsyncMock
|
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
|
|
|
|
|
2024-06-15 08:03:15 +00:00
|
|
|
|
|
|
|
ALLOWED_CHAT_ID = 123
|
|
|
|
DISALLOWED_CHAT_ID = 789
|
2018-07-24 13:31:04 +00:00
|
|
|
|
|
|
|
|
2024-06-13 16:24:19 +00:00
|
|
|
@dataclass
|
|
|
|
class Chat:
|
2024-06-15 08:03:15 +00:00
|
|
|
id: int = 0
|
2024-06-13 19:28:13 +00:00
|
|
|
ban_member = AsyncMock()
|
2024-06-13 16:24:19 +00:00
|
|
|
effective_name = "effective_chat_name"
|
|
|
|
|
|
|
|
|
|
|
|
class User:
|
2024-06-13 19:28:13 +00:00
|
|
|
id_count = 0
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.id = User.id_count
|
|
|
|
self.name = "User name"
|
|
|
|
User.id_count += 1
|
2024-06-13 16:24:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Message:
|
|
|
|
text = "Message text"
|
2024-06-13 19:28:13 +00:00
|
|
|
delete = AsyncMock()
|
2024-06-13 16:24:19 +00:00
|
|
|
chat = Chat()
|
|
|
|
from_user = User()
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Update:
|
|
|
|
message = Message()
|
2024-06-20 14:42:48 +00:00
|
|
|
edited_message = None
|
2024-06-13 16:24:19 +00:00
|
|
|
|
|
|
|
|
2024-06-15 08:03:15 +00:00
|
|
|
def make_update(msg: str, chat_id: int) -> Update:
|
2024-06-13 16:24:19 +00:00
|
|
|
update = Update()
|
|
|
|
update.message.text = msg
|
2024-06-15 08:03:15 +00:00
|
|
|
update.message.chat.id = chat_id
|
2024-06-13 16:24:19 +00:00
|
|
|
return update
|
|
|
|
|
2024-06-20 14:42:48 +00:00
|
|
|
def make_edited_message(msg: str, chat_id: int) -> Update:
|
|
|
|
update = Update()
|
|
|
|
update.message = None
|
|
|
|
update.edited_message = Message()
|
|
|
|
update.edited_message.text = msg
|
|
|
|
update.edited_message.chat.id = chat_id
|
|
|
|
return update
|
|
|
|
|
2024-06-13 16:24:19 +00:00
|
|
|
|
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-15 08:03:15 +00:00
|
|
|
config = autobanbot.load_config("config.json.example")
|
|
|
|
self.regexes = config['regexes']
|
|
|
|
self.allowed_chats = config['allowedChats']
|
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:
|
2024-06-20 14:42:48 +00:00
|
|
|
with self.subTest("new message", message=msg):
|
2024-06-15 08:03:15 +00:00
|
|
|
update = make_update(msg, ALLOWED_CHAT_ID)
|
2024-06-20 14:42:48 +00:00
|
|
|
self.assertIsNone(update.edited_message)
|
2024-06-13 19:28:13 +00:00
|
|
|
update.message.delete.reset_mock()
|
|
|
|
update.message.chat.ban_member.reset_mock()
|
2024-06-15 08:03:15 +00:00
|
|
|
await autobanbot.new_msg(update, None, self.regexes, self.allowed_chats)
|
2024-06-13 16:24:19 +00:00
|
|
|
update.message.delete.assert_not_called()
|
|
|
|
update.message.chat.ban_member.assert_not_called()
|
2024-06-20 14:42:48 +00:00
|
|
|
with self.subTest("message edit", message=msg):
|
|
|
|
update = make_edited_message(msg, ALLOWED_CHAT_ID)
|
|
|
|
self.assertIsNone(update.message)
|
|
|
|
update.edited_message.delete.reset_mock()
|
|
|
|
update.edited_message.chat.ban_member.reset_mock()
|
|
|
|
await autobanbot.new_msg(update, None, self.regexes, self.allowed_chats)
|
|
|
|
update.edited_message.delete.assert_not_called()
|
|
|
|
update.edited_message.chat.ban_member.assert_not_called()
|
2018-07-24 13:31:04 +00:00
|
|
|
|
2024-06-15 04:47:55 +00:00
|
|
|
async def test_bannable(self):
|
2024-06-13 19:28:13 +00:00
|
|
|
messages = [
|
2024-06-15 04:47:55 +00:00
|
|
|
"hitest123hello",
|
|
|
|
"HiTeSt123HeLlO",
|
2024-06-13 19:28:13 +00:00
|
|
|
"t.me/whatever_bot?not bannable message",
|
|
|
|
"https://www.t.me/ohhaaaaaaaibot/siematest123elo",
|
|
|
|
"@Hello_BoT"
|
|
|
|
]
|
|
|
|
for msg in messages:
|
2024-06-15 08:03:15 +00:00
|
|
|
with self.subTest("Allowed chat causes a ban", message=msg):
|
|
|
|
update = make_update(msg, ALLOWED_CHAT_ID)
|
2024-06-20 14:42:48 +00:00
|
|
|
self.assertIsNone(update.edited_message)
|
2024-06-13 19:28:13 +00:00
|
|
|
update.message.delete.reset_mock()
|
|
|
|
update.message.chat.ban_member.reset_mock()
|
2024-06-15 08:03:15 +00:00
|
|
|
await autobanbot.new_msg(update, None, self.regexes, self.allowed_chats)
|
2024-06-13 19:28:13 +00:00
|
|
|
update.message.delete.assert_called_once()
|
|
|
|
update.message.chat.ban_member.assert_called_once_with(update.message.from_user.id)
|
2024-06-15 08:03:15 +00:00
|
|
|
with self.subTest("Unknown chat is ignored", message=msg):
|
|
|
|
update = make_update(msg, DISALLOWED_CHAT_ID)
|
2024-06-20 14:42:48 +00:00
|
|
|
self.assertIsNone(update.edited_message)
|
2024-06-15 08:03:15 +00:00
|
|
|
update.message.delete.reset_mock()
|
|
|
|
update.message.chat.ban_member.reset_mock()
|
|
|
|
await autobanbot.new_msg(update, None, self.regexes, self.allowed_chats)
|
|
|
|
update.message.delete.assert_not_called()
|
|
|
|
update.message.chat.ban_member.assert_not_called()
|
2024-06-20 14:42:48 +00:00
|
|
|
with self.subTest("Edit in allowed chat causes a ban", message=msg):
|
|
|
|
update = make_edited_message(msg, ALLOWED_CHAT_ID)
|
|
|
|
self.assertIsNone(update.message)
|
|
|
|
update.edited_message.delete.reset_mock()
|
|
|
|
update.edited_message.chat.ban_member.reset_mock()
|
|
|
|
await autobanbot.new_msg(update, None, self.regexes, self.allowed_chats)
|
|
|
|
update.edited_message.delete.assert_called_once()
|
|
|
|
update.edited_message.chat.ban_member.assert_called_once_with(update.edited_message.from_user.id)
|
|
|
|
with self.subTest("Edit in unknown chat is ignored", message=msg):
|
|
|
|
update = make_edited_message(msg, DISALLOWED_CHAT_ID)
|
|
|
|
self.assertIsNone(update.message)
|
|
|
|
update.edited_message.delete.reset_mock()
|
|
|
|
update.edited_message.chat.ban_member.reset_mock()
|
|
|
|
await autobanbot.new_msg(update, None, self.regexes, self.allowed_chats)
|
|
|
|
update.edited_message.delete.assert_not_called()
|
|
|
|
update.edited_message.chat.ban_member.assert_not_called()
|
2024-06-13 19:28:13 +00:00
|
|
|
|
2018-07-24 13:31:04 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|