Esempio n. 1
0
    async def test_removing_deleted_message_logs(self):
        """Removing an already deleted message logs the correct message"""
        attachment = MockAttachment(filename="python.disallowed")
        self.message.attachments = [attachment]
        self.message.delete = AsyncMock(side_effect=NotFound(response=Mock(status=""), message=""))

        with self.assertLogs(logger=antimalware.log, level="INFO"):
            await self.cog.on_message(self.message)
        self.message.delete.assert_called_once()
Esempio n. 2
0
async def fetch_message(ctx, message_link):
    try:
        channel_id = extract_channel_id(message_link)
        channel = ctx.guild.get_channel(channel_id)

        if channel:
            return await channel.fetch_message(extract_message_id(message_link))
        else:
            raise NotFound(f'Channel {channel_id} not found.')
    except (IndexError, ValueError, Forbidden, NotFound):
        return None
Esempio n. 3
0
    async def test_take_action_delete_failure(self, mod_log_property):
        """Shouldn't send any messages if the token message can't be deleted."""
        cog = TokenRemover(self.bot)
        mod_log_property.return_value = mock.create_autospec(ModLog,
                                                             spec_set=True,
                                                             instance=True)
        self.msg.delete.side_effect = NotFound(MagicMock(), MagicMock())

        token = mock.create_autospec(Token, spec_set=True, instance=True)
        await cog.take_action(self.msg, token)

        self.msg.delete.assert_called_once_with()
        self.msg.channel.send.assert_not_awaited()
Esempio n. 4
0
    async def test_format_userid_log_message_unknown(
        self,
        unknown_user_log_message,
    ):
        """Should correctly format the user ID portion when the actual user it belongs to is unknown."""
        token = Token("NDcyMjY1OTQzMDYyNDEzMzMy", "XsySD_",
                      "s45jqDV_Iisn-symw0yDRrk_jf4")
        unknown_user_log_message.format.return_value = " Partner"
        msg = MockMessage(id=555, content="hello world")
        msg.guild.get_member.return_value = None
        msg.guild.fetch_member.side_effect = NotFound(mock.Mock(status=404),
                                                      "Not found")

        return_value = await TokenRemover.format_userid_log_message(msg, token)

        self.assertEqual(return_value,
                         (unknown_user_log_message.format.return_value, False))
        unknown_user_log_message.format.assert_called_once_with(
            user_id=472265943062413332)
Esempio n. 5
0
    async def test_send_private_embed(self):
        """Should DM the user and return `True` on success or `False` on failure."""
        embed = Embed(title="Test", description="Test val")

        test_case = namedtuple("test_case", ["expected_output", "raised_exception"])
        test_cases = [
            test_case(True, None),
            test_case(False, HTTPException(AsyncMock(), AsyncMock())),
            test_case(False, Forbidden(AsyncMock(), AsyncMock())),
            test_case(False, NotFound(AsyncMock(), AsyncMock()))
        ]

        for case in test_cases:
            with self.subTest(expected=case.expected_output, raised=case.raised_exception):
                self.user.send.reset_mock(side_effect=True)
                self.user.send.side_effect = case.raised_exception

                result = await utils.send_private_embed(self.user, embed)

                self.assertEqual(result, case.expected_output)
                self.user.send.assert_awaited_once_with(embed=embed)