aboutsummaryrefslogtreecommitdiffstats
path: root/tests/base.py
diff options
context:
space:
mode:
authorGravatar Sebastiaan Zeeff <[email protected]>2020-02-24 01:09:58 +0100
committerGravatar Sebastiaan Zeeff <[email protected]>2020-02-24 01:09:58 +0100
commit0826d89f59d3f9be120f3a67412574271ef18b4c (patch)
treebd2b3de216cd0568ac14716443d5771802d2c299 /tests/base.py
parentUpdate Dockerfile to use python:3.8-slim (diff)
parentMerge pull request #711 from python-discord/bug/backend/b704/ready-missing-cache (diff)
Merge branch 'master' into python38-migration
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 21a57716a..21613110e 100644
--- a/tests/base.py
+++ b/tests/base.py
@@ -1,5 +1,11 @@
import logging
from contextlib import contextmanager
+from typing import Dict
+
+import discord
+from discord.ext import commands
+
+from tests import helpers
class _CaptureLogHandler(logging.Handler):
@@ -69,3 +75,31 @@ class LoggingTestsMixin:
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)