Not working tests, but at least a small step forward

This commit is contained in:
Michał Rudowicz 2024-06-13 16:45:27 +02:00
parent 779292196e
commit e343b54dcf
3 changed files with 26 additions and 75 deletions

View File

@ -0,0 +1,4 @@
#!/ust/bin/env python3
class Update:
pass

12
fakes/telegram/ext.py Normal file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env python3
class Application:
pass
class MessageHandler:
pass
class filters:
pass

View File

@ -2,6 +2,12 @@
import unittest import unittest
from unittest.mock import Mock 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 import autobanbot
CHAT_ID = "Chat ID" CHAT_ID = "Chat ID"
@ -9,85 +15,14 @@ USER_ID = "User ID"
MESSAGE_ID = "Message ID" MESSAGE_ID = "Message ID"
BOT_SPEC = ["kick_chat_member", "delete_message"] 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: class TestAutoBanBot(unittest.IsolatedAsyncioTestCase):
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):
def setUp(self): 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): async def test_not_bannable(self):
bot = Mock(spec=BOT_SPEC) await autobanbot.new_msg(None, None, self.regexes)
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)
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__': if __name__ == '__main__':
unittest.main() unittest.main()