aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar Jannes Jonkers <[email protected]>2020-05-11 21:56:39 +0200
committerGravatar Jannes Jonkers <[email protected]>2020-05-11 21:56:39 +0200
commitddfe583d0b1e72f98855f628ff01b72c82fa491d (patch)
treea9a3972959e7f6e4b1fbacf4b6c1be8cdda6f9a2 /tests
parentAntiMalware Tests - Removed exact log content checks (diff)
AntiMalware Refactor - Moved embed descriptions into constants, added tests for embed descriptions
Diffstat (limited to 'tests')
-rw-r--r--tests/bot/cogs/test_antimalware.py48
1 files changed, 30 insertions, 18 deletions
diff --git a/tests/bot/cogs/test_antimalware.py b/tests/bot/cogs/test_antimalware.py
index a2ce9a740..fab063201 100644
--- a/tests/bot/cogs/test_antimalware.py
+++ b/tests/bot/cogs/test_antimalware.py
@@ -4,7 +4,7 @@ from unittest.mock import AsyncMock, Mock
from discord import NotFound
from bot.cogs import antimalware
-from bot.constants import AntiMalware as AntiMalwareConfig, Channels, STAFF_ROLES, URLs
+from bot.constants import AntiMalware as AntiMalwareConfig, Channels, STAFF_ROLES
from tests.helpers import MockAttachment, MockBot, MockMessage, MockRole
MODULE = "bot.cogs.antimalware"
@@ -63,7 +63,7 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase):
self.message.delete.assert_not_called()
- async def test_python_file_redirect_embed(self):
+ async def test_python_file_redirect_embed_description(self):
"""A message containing a .py file should result in an embed redirecting the user to our paste site"""
attachment = MockAttachment(filename="python.py")
self.message.attachments = [attachment]
@@ -74,32 +74,44 @@ class AntiMalwareCogTests(unittest.IsolatedAsyncioTestCase):
args, kwargs = self.message.channel.send.call_args
embed = kwargs.pop("embed")
- self.assertEqual(args[0], f"Hey {self.message.author.mention}!")
- self.assertEqual(embed.description, (
- "It looks like you tried to attach a Python file - "
- f"please use a code-pasting service such as {URLs.site_schema}{URLs.site_paste}"
- ))
+ self.assertEqual(embed.description, antimalware.PY_EMBED_DESCRIPTION)
- async def test_txt_file_redirect_embed(self):
+ async def test_txt_file_redirect_embed_description(self):
+ """A message containing a .txt file should result in the correct embed."""
attachment = MockAttachment(filename="python.txt")
self.message.attachments = [attachment]
self.message.channel.send = AsyncMock()
+ antimalware.TXT_EMBED_DESCRIPTION = Mock()
+ antimalware.TXT_EMBED_DESCRIPTION.format.return_value = "test"
await self.cog.on_message(self.message)
+ self.message.channel.send.assert_called_once()
args, kwargs = self.message.channel.send.call_args
embed = kwargs.pop("embed")
cmd_channel = self.bot.get_channel(Channels.bot_commands)
- self.assertEqual(args[0], f"Hey {self.message.author.mention}!")
- self.assertEqual(embed.description, (
- "**Uh-oh!** It looks like your message got zapped by our spam filter. "
- "We currently don't allow `.txt` attachments, so here are some tips to help you travel safely: \n\n"
- "• If you attempted to send a message longer than 2000 characters, try shortening your message "
- "to fit within the character limit or use a pasting service (see below) \n\n"
- "• If you tried to show someone your code, you can use codeblocks \n(run `!code-blocks` in "
- f"{cmd_channel.mention} for more information) or use a pasting service like: "
- f"\n\n{URLs.site_schema}{URLs.site_paste}"
- ))
+ self.assertEqual(embed.description, antimalware.TXT_EMBED_DESCRIPTION.format.return_value)
+ antimalware.TXT_EMBED_DESCRIPTION.format.assert_called_with(cmd_channel_mention=cmd_channel.mention)
+
+ async def test_other_disallowed_extention_embed_description(self):
+ """Test the description for a non .py/.txt disallowed extension."""
+ attachment = MockAttachment(filename="python.disallowed")
+ self.message.attachments = [attachment]
+ self.message.channel.send = AsyncMock()
+ antimalware.DISALLOWED_EMBED_DESCRIPTION = Mock()
+ antimalware.DISALLOWED_EMBED_DESCRIPTION.format.return_value = "test"
+
+ await self.cog.on_message(self.message)
+ self.message.channel.send.assert_called_once()
+ args, kwargs = self.message.channel.send.call_args
+ embed = kwargs.pop("embed")
+ meta_channel = self.bot.get_channel(Channels.meta)
+
+ self.assertEqual(embed.description, antimalware.DISALLOWED_EMBED_DESCRIPTION.format.return_value)
+ antimalware.DISALLOWED_EMBED_DESCRIPTION.format.assert_called_with(
+ blocked_extensions_str=".disallowed",
+ meta_channel_mention=meta_channel.mention
+ )
async def test_removing_deleted_message_logs(self):
"""Removing an already deleted message logs the correct message"""