auto-ban-bot/tests.py

95 lines
3.4 KiB
Python
Raw Normal View History

2018-07-24 13:31:04 +00:00
#!/usr/bin/env python
import unittest
from unittest.mock import Mock
import autobanbot
CHAT_ID = "Chat ID"
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):
def setUp(self):
self.regexes = autobanbot.load_config()["regexes"]
2018-07-24 13:31:04 +00:00
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
2018-07-24 13:31:04 +00:00
))
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
2018-07-24 13:31:04 +00:00
))
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()