aboutsummaryrefslogtreecommitdiffstats
path: root/tests/base.py
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-02-25 10:04:49 -0800
committerGravatar MarkKoz <[email protected]>2020-02-25 10:04:49 -0800
commit8f73270cb9f15f47613b378cb3f648c9a0e12b6d (patch)
tree5254a28f073e65d3811af2fd64d6c8a9ed1ed6e6 /tests/base.py
parentError handler: handle CommandOnCooldown errors (diff)
parentMerge pull request #781 from python-discord/bug/utils/bot-1c/reminder-unsched... (diff)
Merge remote-tracking branch 'origin/master' into feat/backend/b131/error-handling
Diffstat (limited to 'tests/base.py')
-rw-r--r--tests/base.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/base.py b/tests/base.py
index 029a249ed..88693f382 100644
--- a/tests/base.py
+++ b/tests/base.py
@@ -1,6 +1,12 @@
import logging
import unittest
from contextlib import contextmanager
+from typing import Dict
+
+import discord
+from discord.ext import commands
+
+from tests import helpers
class _CaptureLogHandler(logging.Handler):
@@ -65,3 +71,31 @@ class LoggingTestCase(unittest.TestCase):
standard_message = self._truncateMessage(base_message, record_message)
msg = self._formatMessage(msg, standard_message)
self.fail(msg)
+
+
+class CommandTestCase(unittest.TestCase):
+ """TestCase with additional assertions that are useful for testing Discord commands."""
+
+ @helpers.async_test
+ async def assertHasPermissionsCheck(
+ self,
+ cmd: commands.Command,
+ permissions: Dict[str, bool],
+ ) -> None:
+ """
+ Test that `cmd` raises a `MissingPermissions` exception if author lacks `permissions`.
+
+ Every permission in `permissions` is expected to be reported as missing. In other words, do
+ not include permissions which should not raise an exception along with those which should.
+ """
+ # Invert permission values because it's more intuitive to pass to this assertion the same
+ # permissions as those given to the check decorator.
+ permissions = {k: not v for k, v in permissions.items()}
+
+ ctx = helpers.MockContext()
+ ctx.channel.permissions_for.return_value = discord.Permissions(**permissions)
+
+ with self.assertRaises(commands.MissingPermissions) as cm:
+ await cmd.can_run(ctx)
+
+ self.assertCountEqual(permissions.keys(), cm.exception.missing_perms)