|
| 1 | +import inspect |
| 2 | +import typing |
| 3 | +import unittest.mock |
| 4 | + |
| 5 | +import discord |
| 6 | +import pytest |
| 7 | +from discord.ext import commands |
| 8 | + |
| 9 | +from modmail.extensions.utils import error_handler |
| 10 | +from modmail.extensions.utils.error_handler import ErrorHandler |
| 11 | +from tests import mocks |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def cog(): |
| 16 | + """Pytest fixture for error_handler.""" |
| 17 | + return ErrorHandler(mocks.MockBot()) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def ctx(): |
| 22 | + """Pytest fixture for MockContext.""" |
| 23 | + return mocks.MockContext(channel=mocks.MockTextChannel()) |
| 24 | + |
| 25 | + |
| 26 | +def test_error_embed(): |
| 27 | + """Test the error embed method creates the correct embed.""" |
| 28 | + title = "Something very drastic went very wrong!" |
| 29 | + message = "seven southern seas are ready to collapse." |
| 30 | + embed = ErrorHandler.error_embed(title=title, message=message) |
| 31 | + |
| 32 | + assert embed.title == title |
| 33 | + assert embed.description == message |
| 34 | + assert embed.colour == error_handler.ERROR_COLOUR |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize( |
| 38 | + ["exception_or_str", "expected_str"], |
| 39 | + [ |
| 40 | + [commands.NSFWChannelRequired(mocks.MockTextChannel()), "NSFW Channel Required"], |
| 41 | + [commands.CommandNotFound(), "Command Not Found"], |
| 42 | + ["someWEIrdName", "some WE Ird Name"], |
| 43 | + ], |
| 44 | +) |
| 45 | +def test_get_title_from_name(exception_or_str: typing.Union[Exception, str], expected_str: str): |
| 46 | + """Test the regex works properly for the title from name.""" |
| 47 | + result = ErrorHandler.get_title_from_name(exception_or_str) |
| 48 | + assert expected_str == result |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize( |
| 52 | + ["error", "title", "description"], |
| 53 | + [ |
| 54 | + [ |
| 55 | + commands.UserInputError("some interesting information."), |
| 56 | + "User Input Error", |
| 57 | + "some interesting information.", |
| 58 | + ], |
| 59 | + [ |
| 60 | + commands.MissingRequiredArgument(inspect.Parameter("SomethingSpecial", kind=1)), |
| 61 | + "Missing Required Argument", |
| 62 | + "SomethingSpecial is a required argument that is missing.", |
| 63 | + ], |
| 64 | + [ |
| 65 | + commands.GuildNotFound("ImportantGuild"), |
| 66 | + "Guild Not Found", |
| 67 | + 'Guild "ImportantGuild" not found.', |
| 68 | + ], |
| 69 | + ], |
| 70 | +) |
| 71 | +@pytest.mark.asyncio |
| 72 | +async def test_handle_user_input_error( |
| 73 | + cog: ErrorHandler, ctx: mocks.MockContext, error: commands.UserInputError, title: str, description: str |
| 74 | +): |
| 75 | + """Test user input errors are handled properly. Does not test with BadUnionArgument.""" |
| 76 | + embed = await cog.handle_user_input_error(ctx=ctx, error=error, reset_cooldown=False) |
| 77 | + |
| 78 | + assert title == embed.title |
| 79 | + assert description == embed.description |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.asyncio |
| 83 | +async def test_handle_bot_missing_perms(cog: ErrorHandler): |
| 84 | + """ |
| 85 | +
|
| 86 | + Test error_handler.handle_bot_missing_perms. |
| 87 | +
|
| 88 | + There are some cases here where the bot is unable to send messages, and that should be clear. |
| 89 | + """ |
| 90 | + ... |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.asyncio |
| 94 | +async def test_handle_check_failure(cog: ErrorHandler): |
| 95 | + """ |
| 96 | + Test check failures. |
| 97 | +
|
| 98 | + In some cases, this method should result in calling a bot_missing_perms method |
| 99 | + because the bot cannot send messages. |
| 100 | + """ |
| 101 | + ... |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.asyncio |
| 105 | +async def test_on_command_error(cog: ErrorHandler): |
| 106 | + """Test the general command error method.""" |
| 107 | + ... |
| 108 | + |
| 109 | + |
| 110 | +class TestErrorHandler: |
| 111 | + """ |
| 112 | + Test class for the error handler. The problem here is a lot of the errors need to be raised. |
| 113 | +
|
| 114 | + Thankfully, most of them do not have extra attributes that we use, and can be easily faked. |
| 115 | + """ |
| 116 | + |
| 117 | + errors = { |
| 118 | + commands.CommandError: [ |
| 119 | + commands.ConversionError, |
| 120 | + { |
| 121 | + commands.UserInputError: [ |
| 122 | + commands.MissingRequiredArgument, |
| 123 | + commands.TooManyArguments, |
| 124 | + { |
| 125 | + commands.BadArgument: [ |
| 126 | + commands.MessageNotFound, |
| 127 | + commands.MemberNotFound, |
| 128 | + commands.GuildNotFound, |
| 129 | + commands.UserNotFound, |
| 130 | + commands.ChannelNotFound, |
| 131 | + commands.ChannelNotReadable, |
| 132 | + commands.BadColourArgument, |
| 133 | + commands.RoleNotFound, |
| 134 | + commands.BadInviteArgument, |
| 135 | + commands.EmojiNotFound, |
| 136 | + commands.GuildStickerNotFound, |
| 137 | + commands.PartialEmojiConversionFailure, |
| 138 | + commands.BadBoolArgument, |
| 139 | + commands.ThreadNotFound, |
| 140 | + ] |
| 141 | + }, |
| 142 | + commands.BadUnionArgument, |
| 143 | + commands.BadLiteralArgument, |
| 144 | + { |
| 145 | + commands.ArgumentParsingError: [ |
| 146 | + commands.UnexpectedQuoteError, |
| 147 | + commands.InvalidEndOfQuotedStringError, |
| 148 | + commands.ExpectedClosingQuoteError, |
| 149 | + ] |
| 150 | + }, |
| 151 | + ] |
| 152 | + }, |
| 153 | + commands.CommandNotFound, |
| 154 | + { |
| 155 | + commands.CheckFailure: [ |
| 156 | + commands.CheckAnyFailure, |
| 157 | + commands.PrivateMessageOnly, |
| 158 | + commands.NoPrivateMessage, |
| 159 | + commands.NotOwner, |
| 160 | + commands.MissingPermissions, |
| 161 | + commands.BotMissingPermissions, |
| 162 | + commands.MissingRole, |
| 163 | + commands.BotMissingRole, |
| 164 | + commands.MissingAnyRole, |
| 165 | + commands.BotMissingAnyRole, |
| 166 | + commands.NSFWChannelRequired, |
| 167 | + ] |
| 168 | + }, |
| 169 | + commands.DisabledCommand, |
| 170 | + commands.CommandInvokeError, |
| 171 | + commands.CommandOnCooldown, |
| 172 | + commands.MaxConcurrencyReached, |
| 173 | + ] |
| 174 | + } |
0 commit comments