diff options
| author | 2020-04-16 11:15:16 +0300 | |
|---|---|---|
| committer | 2020-04-16 11:15:16 +0300 | |
| commit | 81f6efc2f4e9e157e2f7fb9f191ea410af066632 (patch) | |
| tree | b78c7a3c0f45ab33db886de3871c84f90c303722 | |
| parent | (Test Helpers): Added `__ge__` function to `MockRole` for comparing. (diff) | |
(Infraction Tests): Created reason shortening tests for ban and kick.
Diffstat (limited to '')
| -rw-r--r-- | tests/bot/cogs/moderation/test_infractions.py | 54 | 
1 files changed, 54 insertions, 0 deletions
| diff --git a/tests/bot/cogs/moderation/test_infractions.py b/tests/bot/cogs/moderation/test_infractions.py new file mode 100644 index 000000000..39ea93952 --- /dev/null +++ b/tests/bot/cogs/moderation/test_infractions.py @@ -0,0 +1,54 @@ +import textwrap +import unittest +from unittest.mock import AsyncMock, Mock, patch + +from bot.cogs.moderation.infractions import Infractions +from tests.helpers import MockBot, MockContext, MockGuild, MockMember, MockRole + + +class ShorteningTests(unittest.IsolatedAsyncioTestCase): +    """Tests for ban and kick command reason shortening.""" + +    def setUp(self): +        self.bot = MockBot() +        self.cog = Infractions(self.bot) +        self.user = MockMember(id=1234, top_role=MockRole(id=3577, position=10)) +        self.target = MockMember(id=1265, top_role=MockRole(id=9876, position=0)) +        self.guild = MockGuild(id=4567) +        self.ctx = MockContext(bot=self.bot, author=self.user, guild=self.guild) + +    @patch("bot.cogs.moderation.utils.has_active_infraction") +    @patch("bot.cogs.moderation.utils.post_infraction") +    async def test_apply_ban_reason_shortening(self, post_infraction_mock, has_active_mock): +        """Should truncate reason for `ctx.guild.ban`.""" +        has_active_mock.return_value = False +        post_infraction_mock.return_value = {"foo": "bar"} + +        self.cog.apply_infraction = AsyncMock() +        self.bot.get_cog.return_value = AsyncMock() +        self.cog.mod_log.ignore = Mock() + +        await self.cog.apply_ban(self.ctx, self.target, "foo bar" * 3000) +        ban = self.cog.apply_infraction.call_args[0][3] +        self.assertEqual( +            ban.cr_frame.f_locals["kwargs"]["reason"], +            textwrap.shorten("foo bar" * 3000, 512, placeholder=" ...") +        ) +        # Await ban to avoid warning +        await ban + +    @patch("bot.cogs.moderation.utils.post_infraction") +    async def test_apply_kick_reason_shortening(self, post_infraction_mock) -> None: +        """Should truncate reason for `Member.kick`.""" +        post_infraction_mock.return_value = {"foo": "bar"} + +        self.cog.apply_infraction = AsyncMock() +        self.cog.mod_log.ignore = Mock() + +        await self.cog.apply_kick(self.ctx, self.target, "foo bar" * 3000) +        kick = self.cog.apply_infraction.call_args[0][3] +        self.assertEqual( +            kick.cr_frame.f_locals["kwargs"]["reason"], +            textwrap.shorten("foo bar" * 3000, 512, placeholder="...") +        ) +        await kick | 
