diff options
| author | 2020-04-23 15:51:58 +0200 | |
|---|---|---|
| committer | 2020-04-23 16:15:18 +0200 | |
| commit | b20bb7471b8d1d01f217f0620f8597bf1bae4456 (patch) | |
| tree | aee2d18da0bf8b2578f46d5bccf0ef4e30217d2a /tests | |
| parent | Allow `!eval` in help channel categories (diff) | |
Simplify `in_whitelisted_context` decorator API
The API of the `in_whitelisted_context` decorator was a bit clunky:
- The long parameter names frequently required multiline decorators
- Despite `#bot-commands` being the defacto default, it needed to be passed
- The name of the function, `in_whitelisted_context` is fairly long in itself
To shorten the call length of the decorator, the parameter names were shortened by dropping the `whitelisted_` prefix. This means that the parameter names are now just `channels`, `categories`, and `roles`. This already means that all current usages of the decorator are reduced to one line.
In addition, `#bot-commands` has now been made the default redirect channel for the decorator. This means that if no `redirect` was passed, users will be redirected to `bot-commands` to use the command. If needed, `None` (or any falsey value) can be passed to disable redirection. Passing another channel id will trigger that channel to be used as the redirection target instead of bot-commands.
Finally, the name of the decorator was shortened to `in_whitelist`, which already communicates what it is supposed to do.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/bot/cogs/test_information.py | 4 | ||||
| -rw-r--r-- | tests/bot/test_decorators.py | 60 | 
2 files changed, 32 insertions, 32 deletions
| diff --git a/tests/bot/cogs/test_information.py b/tests/bot/cogs/test_information.py index 4a36fe030..6dace1080 100644 --- a/tests/bot/cogs/test_information.py +++ b/tests/bot/cogs/test_information.py @@ -7,7 +7,7 @@ import discord  from bot import constants  from bot.cogs import information -from bot.decorators import InWhitelistedContextCheckFailure +from bot.decorators import InWhitelistCheckFailure  from tests import helpers @@ -525,7 +525,7 @@ class UserCommandTests(unittest.TestCase):          ctx = helpers.MockContext(author=self.author, channel=helpers.MockTextChannel(id=100))          msg = "Sorry, but you may only use this command within <#50>." -        with self.assertRaises(InWhitelistedContextCheckFailure, msg=msg): +        with self.assertRaises(InWhitelistCheckFailure, msg=msg):              asyncio.run(self.cog.user_info.callback(self.cog, ctx))      @unittest.mock.patch("bot.cogs.information.Information.create_user_embed", new_callable=unittest.mock.AsyncMock) diff --git a/tests/bot/test_decorators.py b/tests/bot/test_decorators.py index fae7c0c52..645051fec 100644 --- a/tests/bot/test_decorators.py +++ b/tests/bot/test_decorators.py @@ -2,15 +2,15 @@ import collections  import unittest  import unittest.mock -from bot.decorators import InWhitelistedContextCheckFailure, in_whitelisted_context +from bot.decorators import InWhitelistCheckFailure, in_whitelist  from tests import helpers  WhitelistedContextTestCase = collections.namedtuple("WhitelistedContextTestCase", ("kwargs", "ctx")) -class InWhitelistedContextTests(unittest.TestCase): -    """Tests for the `in_whitelisted_context` check.""" +class InWhitelistTests(unittest.TestCase): +    """Tests for the `in_whitelist` check."""      @classmethod      def setUpClass(cls): @@ -23,43 +23,43 @@ class InWhitelistedContextTests(unittest.TestCase):          cls.staff_role = helpers.MockRole(id=121212)          cls.staff_member = helpers.MockMember(roles=(cls.staff_role,)) -        cls.whitelisted_channels = (cls.bot_commands.id,) -        cls.whitelisted_categories = (cls.help_channel.category_id,) -        cls.whitelisted_roles = (cls.staff_role.id,) +        cls.channels = (cls.bot_commands.id,) +        cls.categories = (cls.help_channel.category_id,) +        cls.roles = (cls.staff_role.id,)      def test_predicate_returns_true_for_whitelisted_context(self):          """The predicate should return `True` if a whitelisted context was passed to it."""          test_cases = (              # Commands issued in whitelisted channels by members without whitelisted roles              WhitelistedContextTestCase( -                kwargs={"whitelisted_channels": self.whitelisted_channels}, +                kwargs={"channels": self.channels},                  ctx=helpers.MockContext(channel=self.bot_commands, author=self.non_staff_member)              ), -            # `redirect_channel` should be added implicitly to the `whitelisted_channels` +            # `redirect` should be added implicitly to the `channels`              WhitelistedContextTestCase( -                kwargs={"redirect_channel": self.bot_commands.id}, +                kwargs={"redirect": self.bot_commands.id},                  ctx=helpers.MockContext(channel=self.bot_commands, author=self.non_staff_member)              ),              # Commands issued in a whitelisted category by members without whitelisted roles              WhitelistedContextTestCase( -                kwargs={"whitelisted_categories": self.whitelisted_categories}, +                kwargs={"categories": self.categories},                  ctx=helpers.MockContext(channel=self.help_channel, author=self.non_staff_member)              ),              # Command issued by a staff member in a non-whitelisted channel/category              WhitelistedContextTestCase( -                kwargs={"whitelisted_roles": self.whitelisted_roles}, +                kwargs={"roles": self.roles},                  ctx=helpers.MockContext(channel=self.non_whitelisted_channel, author=self.staff_member)              ),              # With all kwargs provided              WhitelistedContextTestCase(                  kwargs={ -                    "whitelisted_channels": self.whitelisted_channels, -                    "whitelisted_categories": self.whitelisted_categories, -                    "whitelisted_roles": self.whitelisted_roles, -                    "redirect_channel": self.bot_commands, +                    "channels": self.channels, +                    "categories": self.categories, +                    "roles": self.roles, +                    "redirect": self.bot_commands,                  },                  ctx=helpers.MockContext(channel=self.help_channel, author=self.staff_member)              ), @@ -69,31 +69,31 @@ class InWhitelistedContextTests(unittest.TestCase):              # patch `commands.check` with a no-op lambda that just returns the predicate passed to it              # so we can test the predicate that was generated from the specified kwargs.              with unittest.mock.patch("bot.decorators.commands.check", new=lambda predicate: predicate): -                predicate = in_whitelisted_context(**test_case.kwargs) +                predicate = in_whitelist(**test_case.kwargs)              with self.subTest(test_case=test_case):                  self.assertTrue(predicate(test_case.ctx))      def test_predicate_raises_exception_for_non_whitelisted_context(self): -        """The predicate should raise `InWhitelistedContextCheckFailure` for a non-whitelisted context.""" +        """The predicate should raise `InWhitelistCheckFailure` for a non-whitelisted context."""          test_cases = ( -            # Failing check with `redirect_channel` +            # Failing check with `redirect`              WhitelistedContextTestCase(                  kwargs={ -                    "whitelisted_categories": self.whitelisted_categories, -                    "whitelisted_channels": self.whitelisted_channels, -                    "whitelisted_roles": self.whitelisted_roles, -                    "redirect_channel": self.bot_commands.id, +                    "categories": self.categories, +                    "channels": self.channels, +                    "roles": self.roles, +                    "redirect": self.bot_commands.id,                  },                  ctx=helpers.MockContext(channel=self.non_whitelisted_channel, author=self.non_staff_member)              ), -            # Failing check without `redirect_channel` +            # Failing check without `redirect`              WhitelistedContextTestCase(                  kwargs={ -                    "whitelisted_categories": self.whitelisted_categories, -                    "whitelisted_channels": self.whitelisted_channels, -                    "whitelisted_roles": self.whitelisted_roles, +                    "categories": self.categories, +                    "channels": self.channels, +                    "roles": self.roles,                  },                  ctx=helpers.MockContext(channel=self.non_whitelisted_channel, author=self.non_staff_member)              ), @@ -102,14 +102,14 @@ class InWhitelistedContextTests(unittest.TestCase):          for test_case in test_cases:              # Create expected exception message based on whether or not a redirect channel was provided              expected_message = "Sorry, but you are not allowed to use that command here." -            if test_case.kwargs.get("redirect_channel"): -                expected_message += f" Please use the <#{test_case.kwargs['redirect_channel']}> channel instead." +            if test_case.kwargs.get("redirect"): +                expected_message += f" Please use the <#{test_case.kwargs['redirect']}> channel instead."              # patch `commands.check` with a no-op lambda that just returns the predicate passed to it              # so we can test the predicate that was generated from the specified kwargs.              with unittest.mock.patch("bot.decorators.commands.check", new=lambda predicate: predicate): -                predicate = in_whitelisted_context(**test_case.kwargs) +                predicate = in_whitelist(**test_case.kwargs)              with self.subTest(test_case=test_case): -                with self.assertRaises(InWhitelistedContextCheckFailure, msg=expected_message): +                with self.assertRaises(InWhitelistCheckFailure, msg=expected_message):                      predicate(test_case.ctx) | 
