#!/usr/bin/env python import unittest from unittest.mock import AsyncMock import os import re import sys from dataclasses import dataclass fakes_dir = os.path.join(os.path.dirname(__file__), 'fakes') assert(os.path.exists(fakes_dir)) sys.path.insert(0, fakes_dir) import autobanbot CHAT_ID = "Chat ID" USER_ID = "User ID" MESSAGE_ID = "Message ID" BOT_SPEC = ["kick_chat_member", "delete_message"] @dataclass class Chat: ban_member = AsyncMock() effective_name = "effective_chat_name" class User: id_count = 0 def __init__(self): self.id = User.id_count self.name = "User name" User.id_count += 1 @dataclass class Message: text = "Message text" delete = AsyncMock() chat = Chat() from_user = User() @dataclass class Update: message = Message() def make_update(msg: str) -> Update: update = Update() update.message.text = msg return update class TestAutoBanBot(unittest.IsolatedAsyncioTestCase): def setUp(self): self.regexes = autobanbot.load_config("config.json.example")['regexes'] async def test_not_bannable(self): 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) update.message.delete.reset_mock() update.message.chat.ban_member.reset_mock() await autobanbot.new_msg(update, None, self.regexes) update.message.delete.assert_not_called() update.message.chat.ban_member.assert_not_called() async def test_bannable(self): messages = [ "hitest123hello", "HiTeSt123HeLlO", "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__': unittest.main()