Skip to content

Test that infraction reason sent to database is not truncated #2997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions tests/bot/exts/moderation/infraction/test_infractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ async def test_apply_ban_reason_truncation(self, post_infraction_mock, get_activ
self.cog.mod_log.ignore = Mock()
self.ctx.guild.ban = AsyncMock()

await self.cog.apply_ban(self.ctx, self.target, "foo bar" * 3000)
infraction_reason = "foo bar" * 3000

await self.cog.apply_ban(self.ctx, self.target, infraction_reason)
self.cog.apply_infraction.assert_awaited_once_with(
self.ctx, {"foo": "bar", "purge": ""}, self.target, ANY
)
Expand All @@ -46,10 +48,14 @@ async def test_apply_ban_reason_truncation(self, post_infraction_mock, get_activ
await action()
self.ctx.guild.ban.assert_awaited_once_with(
self.target,
reason=textwrap.shorten("foo bar" * 3000, 512, placeholder="..."),
reason=textwrap.shorten(infraction_reason, 512, placeholder="..."),
delete_message_days=0
)

# Assert that the reason sent to the database isn't truncated.
post_infraction_mock.assert_awaited_once()
self.assertEqual(post_infraction_mock.call_args.args[3], infraction_reason)

@patch("bot.exts.moderation.infraction._utils.post_infraction")
async def test_apply_kick_reason_truncation(self, post_infraction_mock):
"""Should truncate reason for `Member.kick`."""
Expand All @@ -59,14 +65,20 @@ async def test_apply_kick_reason_truncation(self, post_infraction_mock):
self.cog.mod_log.ignore = Mock()
self.target.kick = AsyncMock()

await self.cog.apply_kick(self.ctx, self.target, "foo bar" * 3000)
infraction_reason = "foo bar" * 3000

await self.cog.apply_kick(self.ctx, self.target, infraction_reason)
self.cog.apply_infraction.assert_awaited_once_with(
self.ctx, {"foo": "bar"}, self.target, ANY
)

action = self.cog.apply_infraction.call_args.args[-1]
await action()
self.target.kick.assert_awaited_once_with(reason=textwrap.shorten("foo bar" * 3000, 512, placeholder="..."))
self.target.kick.assert_awaited_once_with(reason=textwrap.shorten(infraction_reason, 512, placeholder="..."))

# Assert that the reason sent to the database isn't truncated.
post_infraction_mock.assert_awaited_once()
self.assertEqual(post_infraction_mock.call_args.args[3], infraction_reason)


@patch("bot.exts.moderation.infraction.infractions.constants.Roles.voice_verified", new=123456)
Expand Down