async def test_deleteSpam(self): with self.assertRaises(ValueError): ash = AntiSpamHandler(commands.Bot(command_prefix="!"), delete_spam={}) ash = AntiSpamHandler(commands.Bot(command_prefix="!"), delete_spam=True)
async def test_initialization(self): # Test AntiSpamHandler type assertion with self.assertRaises(TypeError): # noinspection PyTypeChecker AntiSpamTracker(1, 2) AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3) with self.assertRaises(TypeError): # noinspection PyArgumentList AntiSpamTracker() ash = AntiSpamHandler(get_mocked_bot()) message_interval = ash.options.get("message_interval") ast = AntiSpamTracker(ash, 3) self.assertEqual(message_interval, ast.valid_global_interval) ast = AntiSpamTracker(ash, 3, 15000) self.assertNotEqual(message_interval, ast.valid_global_interval) self.assertEqual(15000, ast.valid_global_interval) self.assertEqual(False, bool(ast.user_tracking)) self.assertEqual(ast.punish_min_amount, 3) with self.assertRaises(TypeError): # noinspection PyTypeChecker AntiSpamTracker(ash, 3, dict())
async def asyncSetUp(self): """ Simply setup our Ash obj before usage """ self.ash = AntiSpamHandler(get_mocked_bot(name="bot", id=98987)) self.ash.guilds = Guild(None, 12, Static.DEFAULTS) self.ash.guilds = Guild(None, 15, Static.DEFAULTS)
async def test_verboseType(self): with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), verbose_level="x") ash = AntiSpamHandler(commands.Bot(command_prefix="!"), verbose_level=logging.INFO) self.assertIsNotNone(ash)
async def test_verboseLevelRange(self): with self.assertRaises(ValueError, msg="Invalid verbose level (To low)"): AntiSpamHandler(commands.Bot(command_prefix="!"), verbose_level=-1) with self.assertRaises(ValueError, msg="Invalid verbose level (To high)"): AntiSpamHandler(commands.Bot(command_prefix="!"), verbose_level=6)
async def asyncSetUp(self): """ Simply setup our Ash obj before usage """ self.ash = AntiSpamHandler( MockedMember(name="bot", member_id=98987, mock_type="bot").to_mock()) self.ash.guilds = Guild(None, 12, Static.DEFAULTS) self.ash.guilds = Guild(None, 15, Static.DEFAULTS)
async def test_messageAccuracyRange(self): with self.assertRaises( ValueError, msg="Invalid message_duplicate_accuracy (To low)"): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_accuracy=0) with self.assertRaises( ValueError, msg="Invalid message_duplicate_accuracy (To high)"): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_accuracy=101)
async def test_userBanMessage(self): with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), user_ban_message=1) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), user_ban_message=tuple()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), user_ban_message=[1]) AntiSpamHandler(commands.Bot(command_prefix="!"), user_ban_message="hi") AntiSpamHandler(commands.Bot(command_prefix="!"), user_ban_message=dict())
async def test_guildKickMessage(self): with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), guild_kick_message=1) with self.assertRaises(ValueError): AntiSpamHandler( commands.Bot(command_prefix="!"), guild_kick_message=tuple() ) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), guild_kick_message=[1]) AntiSpamHandler(commands.Bot(command_prefix="!"), guild_kick_message="hi") AntiSpamHandler(commands.Bot(command_prefix="!"), guild_kick_message=dict())
async def test_guildWarnMessageType(self): with self.assertRaises( ValueError, msg="guild_warn_message should raise, given int"): AntiSpamHandler(commands.Bot(command_prefix="!"), guild_warn_message=1) with self.assertRaises( ValueError, msg="guild_warn_message should raise, given bool"): AntiSpamHandler(commands.Bot(command_prefix="!"), guild_warn_message=True) AntiSpamHandler(commands.Bot(command_prefix="!"), guild_warn_message="Test") AntiSpamHandler(commands.Bot(command_prefix="!"), guild_warn_message={"title": "Hello!"})
async def test_updateCache(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3) self.assertEqual(False, bool(ast.user_tracking)) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(True, bool(ast.user_tracking)) ast.user_tracking = {} # overwrite so we can test more self.assertEqual(False, bool(ast.user_tracking)) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": False}) self.assertEqual(False, bool(ast.user_tracking)) with self.assertRaises(TypeError): # noinspection PyTypeChecker ast.update_cache(1, dict()) with self.assertRaises(TypeError): # noinspection PyTypeChecker ast.update_cache(get_mocked_message(), 1) ast.user_tracking = {} self.assertEqual(0, len(ast.user_tracking)) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(1, len(ast.user_tracking)) self.assertEqual(1, len(ast.user_tracking[123456789])) self.assertEqual(2, len(ast.user_tracking[123456789][12345]))
async def test_messageDuplicateCount(self): with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_count="1") with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_count=dict()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_count=tuple()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_count=[1]) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_count=2.0) AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_count=2) AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_count=None)
async def test_messageInterval(self): with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_interval="1") with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_interval=dict()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_interval=tuple()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_interval=[1]) AntiSpamHandler(commands.Bot(command_prefix="!"), message_interval=1000) AntiSpamHandler(commands.Bot(command_prefix="!"), message_interval=None) with self.assertRaises(BaseASHException): AntiSpamHandler(commands.Bot(command_prefix="!"), message_interval=999)
async def test_messageDuplicateAccuracy(self): with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_accuracy="1") with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_accuracy=dict()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_accuracy=tuple()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_accuracy=[1]) ash = AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_accuracy=2) AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_accuracy=None) self.assertIsInstance(ash.options.get("message_duplicate_accuracy"), float) AntiSpamHandler(commands.Bot(command_prefix="!"), message_duplicate_accuracy=2.0)
async def test_propagateRoleIgnoring(self): """ Tests if the propagate method ignores the correct roles """ ash = AntiSpamHandler(get_mocked_member(name="bot", id="87678"), ignore_roles=[151515]) result = await ash.propagate(get_mocked_message()) self.assertEqual(result["status"], "Ignoring this role: 151515")
async def test_getGuildValidInterval(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3) self.assertEqual(ast._get_guild_valid_interval(123456789), 30000) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(ast._get_guild_valid_interval(123456789), 30000) ast.user_tracking[123456789]["valid_interval"] = 15000 self.assertEqual(ast._get_guild_valid_interval(123456789), 15000)
async def test_propagateRoleIgnoring(self): """ Tests if the propagate method ignores the correct roles """ ash = AntiSpamHandler(MockedMember(name="bot", member_id=87678).to_mock(), ignore_roles=[151515]) print(MockedMember(mock_type="bot").to_mock().user.id) result = await ash.propagate(MockedMessage().to_mock()) self.assertEqual(result["status"], "Ignoring this role: 151515")
async def test_removeOutdatedTimestamps(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3, 50) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(ast.get_user_count(get_mocked_message()), 1) ast.remove_outdated_timestamps(123456789, 12345) self.assertEqual(ast.get_user_count(get_mocked_message()), 1) await asyncio.sleep(0.06) ast.remove_outdated_timestamps(123456789, 12345) self.assertEqual(ast.get_user_count(get_mocked_message()), 0)
async def test_removePunishment(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 5) ast.remove_punishments(get_mocked_message()) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(1, ast.get_user_count(get_mocked_message())) ast.remove_punishments(get_mocked_message()) with self.assertRaises(UserNotFound): ast.get_user_count(get_mocked_message())
async def test_warnOnly(self): ash = AntiSpamHandler(MockedMember(mock_type="bot").to_mock(), warn_only=True) self.assertEqual(ash.options["warn_only"], True) self.assertEqual(self.ash.options["warn_only"], False) ash = AntiSpamHandler(MockedMember(mock_type="bot").to_mock(), warn_only=None) self.assertEqual(ash.options["warn_only"], False) with self.assertRaises(ValueError): AntiSpamHandler(MockedMember(mock_type="bot").to_mock(), warn_only=1) with self.assertRaises(ValueError): AntiSpamHandler(MockedMember(mock_type="bot").to_mock(), warn_only="1") with self.assertRaises(ValueError): AntiSpamHandler(MockedMember(mock_type="bot").to_mock(), warn_only=dict()) with self.assertRaises(ValueError): AntiSpamHandler(MockedMember(mock_type="bot").to_mock(), warn_only=[])
async def test_verboseAssignment(self): ash = AntiSpamHandler( commands.Bot(command_prefix="!"), verbose_level=logging.NOTSET ) self.assertEqual(ash.logger.level, 0) ash = AntiSpamHandler( commands.Bot(command_prefix="!"), verbose_level=logging.DEBUG ) self.assertEqual(ash.logger.level, 10) ash = AntiSpamHandler( commands.Bot(command_prefix="!"), verbose_level=logging.INFO ) self.assertEqual(ash.logger.level, 20) ash = AntiSpamHandler( commands.Bot(command_prefix="!"), verbose_level=logging.WARNING ) self.assertEqual(ash.logger.level, 30) ash = AntiSpamHandler( commands.Bot(command_prefix="!"), verbose_level=logging.ERROR ) self.assertEqual(ash.logger.level, 40) ash = AntiSpamHandler( commands.Bot(command_prefix="!"), verbose_level=logging.CRITICAL ) self.assertEqual(ash.logger.level, 50)
def __init__(self, bot): self.bot = bot self.bot.handler = AntiSpamHandler(self.bot) self.bot.handler.add_ignored_item(370120271367110656, "member") #NullPointer self.bot.handler.add_ignored_item(786334287968337981, "member") #Me self.bot.handler.add_ignored_item(382687991958601729, "member") #Prime Iridium self.bot.handler.add_ignored_item(705641763062415431, "member") #Micah self.bot.handler.add_ignored_item(490690957642301442, "member") #Matthew Tibbits self.bot.handler.add_ignored_item(264445053596991498, "guild") #Top.gg
async def test_cleanCache(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3, 50) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(1, len(ast.user_tracking[123456789][12345])) ast.clean_cache() self.assertEqual(len(ast.user_tracking), 1) self.assertEqual(1, len(ast.user_tracking[123456789][12345])) await asyncio.sleep(0.06) # This should now fully clean the cache ast.clean_cache() self.assertEqual(ast.user_tracking, dict())
async def test_propagateGuildPerms(self): ash = AntiSpamHandler(get_mocked_bot()) message = get_mocked_message() message.guild.me.guild_permissions.kick_members = False message.guild.me.guild_permissions.ban_members = True with self.assertRaises(MissingGuildPermissions, msg="Invalid kick_members perms"): await ash.propagate(message) message.guild.me.guild_permissions.kick_members = True message.guild.me.guild_permissions.ban_members = False with self.assertRaises(MissingGuildPermissions, msg="Invalid ban_members perms"): await ash.propagate(message) message.guild.me.guild_permissions.ban_members = True await ash.propagate(message)
def setUp(self): """ Simply setup our Guild obj before usage """ self.ash = AntiSpamHandler(commands.Bot(command_prefix="!")) self.ash.guilds = Guild(None, 12, Static.DEFAULTS, logger=logging.getLogger(__name__)) self.ash.guilds = Guild(None, 15, Static.DEFAULTS, logger=logging.getLogger(__name__)) self.ash.guilds[0] = User(None, 20, 15, Static.DEFAULTS, logger=logging.getLogger(__name__))
async def test_noPunishMode(self): # SETUP ash = AntiSpamHandler(get_mocked_bot(), no_punish=True) # WHEN / TESTING data = [] for num in range(6): return_value = await ash.propagate( get_mocked_message(message_id=num)) data.append(return_value) # THEN / ASSERTIONS self.assertEqual(len(data), 6) # TODO Fix this while fixing #39 self.assertEqual(data[0]["should_be_punished_this_message"], False) self.assertEqual(data[2]["should_be_punished_this_message"], False) self.assertEqual(data[3]["should_be_punished_this_message"], True) self.assertEqual(data[5]["should_be_punished_this_message"], True)
async def test_ignoreBots(self): with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_bots="1") with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_bots=dict()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_bots=tuple()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_bots=1) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_bots=[1]) AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_bots=True) AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_bots=None)
async def test_banThreshold(self): with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ban_threshold="1") with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ban_threshold=dict()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ban_threshold=tuple()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ban_threshold=[1]) AntiSpamHandler(commands.Bot(command_prefix="!"), ban_threshold=1) AntiSpamHandler(commands.Bot(command_prefix="!"), ban_threshold=None)
async def test_ignoreRoles(self): with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_roles="1") with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_roles=dict()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_roles=tuple()) with self.assertRaises(ValueError): AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_roles=1) AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_roles=[1, "test role"]) AntiSpamHandler(commands.Bot(command_prefix="!"), ignore_roles=None)
async def test_botAssignment(self): with self.assertRaises(ValueError): AntiSpamHandler(None) with self.assertRaises(ValueError): AntiSpamHandler(1) with self.assertRaises(ValueError): AntiSpamHandler("1") with self.assertRaises(ValueError): AntiSpamHandler({1: 2}) with self.assertRaises(ValueError): AntiSpamHandler([1, None]) with self.assertRaises(ValueError): AntiSpamHandler(True) with self.assertRaises(ValueError): AntiSpamHandler(False) AntiSpamHandler(commands.Bot(command_prefix="!"))