async def test_initialization(self): # Test AntiSpamHandler type assertion with self.assertRaises(TypeError): # noinspection PyTypeChecker AntiSpamTracker(1, 2) AntiSpamTracker( AntiSpamHandler(MockedMember(mock_type="bot").to_mock()), 3) with self.assertRaises(TypeError): # noinspection PyArgumentList AntiSpamTracker() ash = AntiSpamHandler(MockedMember(mock_type="bot").to_mock()) 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 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")
def to_mock(self): """Returns an AsyncMock matching the spec for this class""" # we still have to set stuff manually but changing values is nicer mock = AsyncMock(name="Message Mock", spec=discord.Message) # Author section mock.author = MockedMember( name=self.author["name"], member_id=self.author["id"], is_bot=self.author["is_bot"], ).to_mock() # Guild options and 'is_in_guild' are mutually exclusive if not self.is_in_guild: mock.guild = None else: mock.guild = MockedGuild(name=self.guild["name"], guild_id=self.guild["id"]).to_mock() mock.channel = MockedChannel().to_mock() mock.created_at = datetime.datetime.now() mock.id = self.message_id mock.content = self.content mock.clean_content = self.clean_content return mock
async def test_statefulLoading(self): data = read_json("unittests") test_ash = await AntiSpamHandler.load_from_dict( MockedMember(name="bot", member_id=98987, mock_type="bot").to_mock(), data) result = await test_ash.save_to_dict() self.assertEqual(data, result)
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_getGuildValidInterval(self): ast = AntiSpamTracker( AntiSpamHandler(MockedMember(mock_type="bot").to_mock()), 3) self.assertEqual(ast._get_guild_valid_interval(123456789), 30000) ast.update_cache(MockedMessage().to_mock(), {"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)
def to_mock(self): """Returns an AsyncMock matching the spec for this class""" # we still have to set stuff manually but changing values is nicer mock = AsyncMock(name="Guild Mock", spec=discord.Guild) mock.name = self.name mock.id = self.id mock.me = MockedMember(name="Bot", member_id=54321).to_mock() mock.me.top_role.position = 100 # Bot should have higher role by default return mock
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_removeOutdatedTimestamps(self): ast = AntiSpamTracker( AntiSpamHandler(MockedMember(mock_type="bot").to_mock()), 3, 50) ast.update_cache(MockedMessage().to_mock(), {"should_be_punished_this_message": True}) self.assertEqual(ast.get_user_count(MockedMessage().to_mock()), 1) ast.remove_outdated_timestamps(123456789, 12345) self.assertEqual(ast.get_user_count(MockedMessage().to_mock()), 1) await asyncio.sleep(0.06) ast.remove_outdated_timestamps(123456789, 12345) self.assertEqual(ast.get_user_count(MockedMessage().to_mock()), 0)
async def test_removePunishment(self): ast = AntiSpamTracker( AntiSpamHandler(MockedMember(mock_type="bot").to_mock()), 5) ast.remove_punishments(MockedMessage().to_mock()) ast.update_cache(MockedMessage().to_mock(), {"should_be_punished_this_message": True}) self.assertEqual(1, ast.get_user_count(MockedMessage().to_mock())) ast.remove_punishments(MockedMessage().to_mock()) with self.assertRaises(UserNotFound): ast.get_user_count(MockedMessage().to_mock())
async def test_resetKickCounter(self): # GIVEN / SETUP user = User( MockedMember(mock_type="bot").to_mock(), 123454321, 12, Static.DEFAULTS) self.ash.guilds[0].users = user self.assertEqual(1, len(self.ash.guilds[0].users)) self.assertEqual(0, self.ash.guilds[0].users[0].kick_count) self.ash.guilds[0].users[0].kick_count += 1 self.assertEqual(1, self.ash.guilds[0].users[0].kick_count) # WHEN / TESTING self.ash.reset_user_count(123454321, 12, Static.KICKCOUNTER) # THEN / ASSERTIONS self.assertEqual(0, self.ash.guilds[0].users[0].kick_count)
async def test_cleanCache(self): ast = AntiSpamTracker( AntiSpamHandler(MockedMember(mock_type="bot").to_mock()), 3, 50) ast.update_cache(MockedMessage().to_mock(), {"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(MockedMember(mock_type="bot").to_mock()) message = MockedMessage().to_mock() 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)
async def test_resetCountersRaises(self): # SETUP user = User( MockedMember(mock_type="bot").to_mock(), 123454321, 12, Static.DEFAULTS) self.ash.guilds[0].users = user # ASSERTIONS / TESTING with self.assertRaises(LogicError): self.ash.reset_user_count(123454321, 12, "Who knows") # Invalid guild, should work silently self.ash.reset_user_count(123454321, 15, "Who knows") # Invalid user, should work silently self.ash.reset_user_count(1234, 12, "Who knows") # Invalid both, should work silently self.ash.reset_user_count(1234, 15, "Who knows")
async def test_noPunishMode(self): # SETUP ash = AntiSpamHandler(MockedMember(mock_type="bot").to_mock(), no_punish=True) # WHEN / TESTING data = [] for num in range(6): return_value = await ash.propagate( MockedMessage(message_id=num).to_mock()) 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_updateCache(self): ast = AntiSpamTracker( AntiSpamHandler(MockedMember(mock_type="bot").to_mock()), 3) ast.update_cache( MockedMessage(is_in_guild=False).to_mock(), {"should_be_punished_this_message": True}, ) self.assertEqual(False, bool(ast.user_tracking)) ast.update_cache(MockedMessage().to_mock(), {"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(MockedMessage().to_mock(), {"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(MockedMessage().to_mock(), 1) ast.user_tracking = {} self.assertEqual(0, len(ast.user_tracking)) ast.update_cache(MockedMessage().to_mock(), {"should_be_punished_this_message": True}) ast.update_cache(MockedMessage().to_mock(), {"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_getUserCount(self): ast = AntiSpamTracker( AntiSpamHandler(MockedMember(mock_type="bot").to_mock()), 3) ast.update_cache(MockedMessage().to_mock(), {"should_be_punished_this_message": True}) user_count = ast.get_user_count(MockedMessage().to_mock()) self.assertEqual(user_count, 1) for i in range(3): ast.update_cache(MockedMessage().to_mock(), {"should_be_punished_this_message": True}) user_count = ast.get_user_count(MockedMessage().to_mock()) self.assertEqual(user_count, 4) with self.assertRaises(UserNotFound): message = MockedMessage(guild_id=15).to_mock() ast.get_user_count(message) with self.assertRaises(UserNotFound): message = MockedMessage(author_id=25).to_mock() ast.get_user_count(message)
async def test_isSpamming(self): ast = AntiSpamTracker( AntiSpamHandler(MockedMember(mock_type="bot").to_mock()), 5) self.assertEqual(ast.is_spamming(MockedMessage().to_mock()), False) ast.update_cache(MockedMessage().to_mock(), {"should_be_punished_this_message": True}) self.assertEqual(ast.is_spamming(MockedMessage().to_mock()), False) for i in range(3): ast.update_cache(MockedMessage().to_mock(), {"should_be_punished_this_message": True}) # Cap is 5, should have 4 messages rn self.assertEqual(ast.is_spamming(MockedMessage().to_mock()), False) self.assertEqual(4, len(ast.user_tracking[123456789][12345])) ast.update_cache(MockedMessage().to_mock(), {"should_be_punished_this_message": True}) self.assertEqual(ast.is_spamming(MockedMessage().to_mock()), True) ast.update_cache(MockedMessage().to_mock(), {"should_be_punished_this_message": True}) self.assertEqual(ast.is_spamming(MockedMessage().to_mock()), True)
async def test_ASHRandomVar(self): with self.assertRaises(TypeError): AntiSpamHandler(MockedMember(mock_type="bot").to_mock(), lol=1) with self.assertRaises(TypeError): self.ash.add_custom_guild_options(1234, testing=1)