diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/bot/rules/test_mentions.py | 58 | ||||
| -rw-r--r-- | tests/helpers.py | 22 | 
2 files changed, 75 insertions, 5 deletions
| diff --git a/tests/bot/rules/test_mentions.py b/tests/bot/rules/test_mentions.py index f8805ac48..e1f904917 100644 --- a/tests/bot/rules/test_mentions.py +++ b/tests/bot/rules/test_mentions.py @@ -1,15 +1,32 @@ -from typing import Iterable +from typing import Iterable, Optional + +import discord  from bot.rules import mentions  from tests.bot.rules import DisallowedCase, RuleTest -from tests.helpers import MockMember, MockMessage +from tests.helpers import MockMember, MockMessage, MockMessageReference -def make_msg(author: str, total_user_mentions: int, total_bot_mentions: int = 0) -> MockMessage: -    """Makes a message with `total_mentions` mentions.""" +def make_msg( +    author: str, +    total_user_mentions: int, +    total_bot_mentions: int = 0, +    *, +    reference: Optional[MockMessageReference] = None +) -> MockMessage: +    """Makes a message from `author` with `total_user_mentions` user mentions and `total_bot_mentions` bot mentions."""      user_mentions = [MockMember() for _ in range(total_user_mentions)]      bot_mentions = [MockMember(bot=True) for _ in range(total_bot_mentions)] -    return MockMessage(author=author, mentions=user_mentions+bot_mentions) + +    mentions = user_mentions + bot_mentions +    if reference is not None: +        # For the sake of these tests we assume that all references are mentions. +        mentions.append(reference.resolved.author) +        msg_type = discord.MessageType.reply +    else: +        msg_type = discord.MessageType.default + +    return MockMessage(author=author, mentions=mentions, reference=reference, type=msg_type)  class TestMentions(RuleTest): @@ -56,6 +73,16 @@ class TestMentions(RuleTest):                  ("bob",),                  3,              ), +            DisallowedCase( +                [make_msg("bob", 3, reference=MockMessageReference())], +                ("bob",), +                3, +            ), +            DisallowedCase( +                [make_msg("bob", 3, reference=MockMessageReference(reference_author_is_bot=True))], +                ("bob",), +                3 +            )          )          await self.run_disallowed(cases) @@ -71,6 +98,27 @@ class TestMentions(RuleTest):          await self.run_allowed(cases) +    async def test_ignore_reply_mentions(self): +        """Messages with an allowed amount of mentions in the content, also containing reply mentions.""" +        cases = ( +            [ +                make_msg("bob", 2, reference=MockMessageReference()) +            ], +            [ +                make_msg("bob", 2, reference=MockMessageReference(reference_author_is_bot=True)) +            ], +            [ +                make_msg("bob", 2, reference=MockMessageReference()), +                make_msg("bob", 0, reference=MockMessageReference()) +            ], +            [ +                make_msg("bob", 2, reference=MockMessageReference(reference_author_is_bot=True)), +                make_msg("bob", 0, reference=MockMessageReference(reference_author_is_bot=True)) +            ] +        ) + +        await self.run_allowed(cases) +      def relevant_messages(self, case: DisallowedCase) -> Iterable[MockMessage]:          last_message = case.recent_messages[0]          return tuple( diff --git a/tests/helpers.py b/tests/helpers.py index 17214553c..687e15b96 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -492,6 +492,28 @@ class MockAttachment(CustomMockMixin, unittest.mock.MagicMock):      spec_set = attachment_instance +message_reference_instance = discord.MessageReference( +    message_id=unittest.mock.MagicMock(id=1), +    channel_id=unittest.mock.MagicMock(id=2), +    guild_id=unittest.mock.MagicMock(id=3) +) + + +class MockMessageReference(CustomMockMixin, unittest.mock.MagicMock): +    """ +    A MagicMock subclass to mock MessageReference objects. + +    Instances of this class will follow the specification of `discord.MessageReference` instances. +    For more information, see the `MockGuild` docstring. +    """ +    spec_set = message_reference_instance + +    def __init__(self, *, reference_author_is_bot: bool = False, **kwargs): +        super().__init__(**kwargs) +        referenced_msg_author = MockMember(name="bob", bot=reference_author_is_bot) +        self.resolved = MockMessage(author=referenced_msg_author) + +  class MockMessage(CustomMockMixin, unittest.mock.MagicMock):      """      A MagicMock subclass to mock Message objects. | 
