From e343b54dcf6b52dd7d03db28fea9f36467dde84b Mon Sep 17 00:00:00 2001 From: Michal Rudowicz Date: Thu, 13 Jun 2024 16:45:27 +0200 Subject: [PATCH] Not working tests, but at least a small step forward --- fakes/telegram/__init__.py | 4 ++ fakes/telegram/ext.py | 12 ++++++ tests.py | 85 +++++--------------------------------- 3 files changed, 26 insertions(+), 75 deletions(-) create mode 100644 fakes/telegram/__init__.py create mode 100644 fakes/telegram/ext.py diff --git a/fakes/telegram/__init__.py b/fakes/telegram/__init__.py new file mode 100644 index 0000000..c6c32d8 --- /dev/null +++ b/fakes/telegram/__init__.py @@ -0,0 +1,4 @@ +#!/ust/bin/env python3 + +class Update: + pass diff --git a/fakes/telegram/ext.py b/fakes/telegram/ext.py new file mode 100644 index 0000000..00510a1 --- /dev/null +++ b/fakes/telegram/ext.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 + +class Application: + pass + + +class MessageHandler: + pass + + +class filters: + pass diff --git a/tests.py b/tests.py index c7be551..0dc2b94 100644 --- a/tests.py +++ b/tests.py @@ -2,6 +2,12 @@ import unittest from unittest.mock import Mock +import os +import re +import sys +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" @@ -9,85 +15,14 @@ USER_ID = "User ID" MESSAGE_ID = "Message ID" BOT_SPEC = ["kick_chat_member", "delete_message"] -class Member: - def __init__(self, user_id, first_name, last_name, username): - self.id = user_id - self.first_name = first_name - self.last_name = last_name - self.username = username -class Message: - def __init__(self, user_id, first_name, last_name, username): - self.new_chat_members = [Member(user_id, first_name, last_name, username)] - self.message_id = MESSAGE_ID - -class Chat: - def __init__(self): - self.id = CHAT_ID - -class Update: - def __init__(self, user_id, first_name, last_name, username): - self.message = Message(user_id, first_name, last_name, username) - self.effective_chat = Chat() - -class TestAutoBanBot(unittest.TestCase): +class TestAutoBanBot(unittest.IsolatedAsyncioTestCase): def setUp(self): - self.regexes = autobanbot.load_config()["regexes"] + self.regexes = map(lambda r: re.compile(r, re.I), ["test123", "(t\.me\/|\@)[a-z]+bot"]) - def banned_join(self, user_id, first_name, last_name, username): - bot = Mock(spec=BOT_SPEC) - self.assertTrue(autobanbot.hello( - bot, Update(user_id, first_name, last_name, username), self.regexes - )) - bot.kick_chat_member.assert_called_once_with(CHAT_ID, USER_ID) - bot.delete_message.assert_called_once_with(CHAT_ID, MESSAGE_ID) + async def test_not_bannable(self): + await autobanbot.new_msg(None, None, self.regexes) - def legit_join(self, user_id, first_name, last_name, username): - bot = Mock(spec=BOT_SPEC) - self.assertFalse(autobanbot.hello( - bot, Update(user_id, first_name, last_name, username), self.regexes - )) - bot.kick_chat_member.assert_not_called() - bot.delete_message.assert_not_called() - - def test_spam_joins(self): - updates = [ - "READY-MADE TELEGRAM ACCOUNTS", - "ready-made TeLeGrAm AcCoUnTs", - "there are ready-made TeLeGrAm AcCoUnTs for sale!", - "253239090 hi hello 473157472" - ] - for update in updates: - with self.subTest(first_name=update): - self.banned_join(USER_ID, update, "", "") - with self.subTest(last_name=update): - self.banned_join(USER_ID, "", update, "") - with self.subTest(last_name=update, first_name=None): - self.banned_join(USER_ID, None, update, "") - with self.subTest(last_name=None, first_name=update): - self.banned_join(USER_ID, update, None, "") - with self.subTest(last_name=update, first_name=update): - self.banned_join(USER_ID, update, update, "") - - def test_non_spam_joins(self): - updates = [ - "telegram account", - "abc", - "ready-made telegram naccounts", - "completely legit name", - "" - ] - for update in updates: - with self.subTest(first_name=update): - self.legit_join(USER_ID, update, "", "") - with self.subTest(last_name=update): - self.legit_join(USER_ID, "", update, "") - with self.subTest(first_name=update, last_name=None): - self.legit_join(USER_ID, update, None, "") - with self.subTest(last_name=update, first_name=None): - self.legit_join(USER_ID, None, update, "") - with self.subTest(last_name=update, first_name=update): - self.legit_join(USER_ID, update, update, "") if __name__ == '__main__': unittest.main()