From f319aba7f6d6d44a2e2940160a4c61c8f2dbcad5 Mon Sep 17 00:00:00 2001 From: Jelle Janssens Date: Wed, 18 Dec 2024 14:49:34 +0000 Subject: [PATCH] Changed the error message when adding an infraction from ban to infraction as it is confusing --- src/helpers/ban.py | 2 +- tests/src/cmds/core/test_ban.py | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/helpers/ban.py b/src/helpers/ban.py index 433c1dd..0bf877c 100644 --- a/src/helpers/ban.py +++ b/src/helpers/ban.py @@ -280,7 +280,7 @@ async def add_infraction( f"Following is the reason given:\n>>> {reason}\n" ) except Forbidden as ex: - message = "Could not DM member due to privacy settings, however will still attempt to ban them..." + message = "Could not DM member due to privacy settings, however the infraction was still added." logger.warning(f"Forbidden, when trying to contact user with ID {member.id} about infraction.", exc_info=ex) except HTTPException as ex: message = "Here's a 400 Bad Request for you. Just like when you tried to ask me out, last week." diff --git a/tests/src/cmds/core/test_ban.py b/tests/src/cmds/core/test_ban.py index 8308ff7..6840133 100644 --- a/tests/src/cmds/core/test_ban.py +++ b/tests/src/cmds/core/test_ban.py @@ -4,6 +4,7 @@ from unittest.mock import AsyncMock, patch import pytest +from discord import Forbidden from src.cmds.core import ban from src.database.models import Ban, Infraction @@ -12,6 +13,13 @@ from tests import helpers +class MockResponse: + def __init__(self, status): + self.status = status + self.reason = 'Forbidden' + self.code = status + self.text = "Cannot send messages to this user" + class TestBanCog: """Test the `Ban` cog.""" @@ -167,6 +175,32 @@ async def test_warn_success(self, ctx, bot): # Assertions add_infraction_mock.assert_called_once_with(ctx.guild, user, 0, "Any valid reason", ctx.user) + @pytest.mark.asyncio + async def test_add_infraction_dm_forbidden(self, ctx, bot): + """Test add_infraction when DMing the user is forbidden.""" + ctx.user = helpers.MockMember(id=1, name="Test Moderator") + user = helpers.MockMember(id=2, name="Warned User") + user.send = AsyncMock(side_effect=Forbidden( + response=MockResponse(403), + message="Cannot send messages to this user" + )) + bot.get_member_or_user.return_value = user + + with patch('src.cmds.core.ban.add_infraction', new_callable=AsyncMock) as add_infraction_mock: + add_infraction_mock.return_value = SimpleResponse( + message="Could not DM member due to privacy settings, however the infraction was still added.", + delete_after=None + ) + + cog = ban.BanCog(bot) + await cog.warn.callback(cog, ctx, user, "Test reason") + + # Assertions + add_infraction_mock.assert_called_once_with(ctx.guild, user, 0, "Test reason", ctx.user) + ctx.respond.assert_called_once_with( + "Could not DM member due to privacy settings, however the infraction was still added.", delete_after=None + ) + @pytest.mark.asyncio async def test_warn_user_not_found(self, ctx, bot): ctx.user = helpers.MockMember(id=1, name="Test User")