From a39ce559e70d84ef9a03c5839a4988c79ceeb876 Mon Sep 17 00:00:00 2001 From: mbaruh Date: Sat, 22 Oct 2022 22:40:02 +0300 Subject: Move Security cog to backend extension --- bot/exts/backend/security.py | 30 +++++++++++++++++++ bot/exts/filters/security.py | 30 ------------------- tests/bot/exts/backend/test_security.py | 53 +++++++++++++++++++++++++++++++++ tests/bot/exts/filters/test_security.py | 53 --------------------------------- 4 files changed, 83 insertions(+), 83 deletions(-) create mode 100644 bot/exts/backend/security.py delete mode 100644 bot/exts/filters/security.py create mode 100644 tests/bot/exts/backend/test_security.py delete mode 100644 tests/bot/exts/filters/test_security.py diff --git a/bot/exts/backend/security.py b/bot/exts/backend/security.py new file mode 100644 index 000000000..27e4d9752 --- /dev/null +++ b/bot/exts/backend/security.py @@ -0,0 +1,30 @@ +from discord.ext.commands import Cog, Context, NoPrivateMessage + +from bot.bot import Bot +from bot.log import get_logger + +log = get_logger(__name__) + + +class Security(Cog): + """Security-related helpers.""" + + def __init__(self, bot: Bot): + self.bot = bot + self.bot.check(self.check_not_bot) # Global commands check - no bots can run any commands at all + self.bot.check(self.check_on_guild) # Global commands check - commands can't be run in a DM + + def check_not_bot(self, ctx: Context) -> bool: + """Check if the context is a bot user.""" + return not ctx.author.bot + + def check_on_guild(self, ctx: Context) -> bool: + """Check if the context is in a guild.""" + if ctx.guild is None: + raise NoPrivateMessage("This command cannot be used in private messages.") + return True + + +async def setup(bot: Bot) -> None: + """Load the Security cog.""" + await bot.add_cog(Security(bot)) diff --git a/bot/exts/filters/security.py b/bot/exts/filters/security.py deleted file mode 100644 index 27e4d9752..000000000 --- a/bot/exts/filters/security.py +++ /dev/null @@ -1,30 +0,0 @@ -from discord.ext.commands import Cog, Context, NoPrivateMessage - -from bot.bot import Bot -from bot.log import get_logger - -log = get_logger(__name__) - - -class Security(Cog): - """Security-related helpers.""" - - def __init__(self, bot: Bot): - self.bot = bot - self.bot.check(self.check_not_bot) # Global commands check - no bots can run any commands at all - self.bot.check(self.check_on_guild) # Global commands check - commands can't be run in a DM - - def check_not_bot(self, ctx: Context) -> bool: - """Check if the context is a bot user.""" - return not ctx.author.bot - - def check_on_guild(self, ctx: Context) -> bool: - """Check if the context is in a guild.""" - if ctx.guild is None: - raise NoPrivateMessage("This command cannot be used in private messages.") - return True - - -async def setup(bot: Bot) -> None: - """Load the Security cog.""" - await bot.add_cog(Security(bot)) diff --git a/tests/bot/exts/backend/test_security.py b/tests/bot/exts/backend/test_security.py new file mode 100644 index 000000000..c3985c609 --- /dev/null +++ b/tests/bot/exts/backend/test_security.py @@ -0,0 +1,53 @@ +import unittest + +from discord.ext.commands import NoPrivateMessage + +from bot.exts.backend import security +from tests.helpers import MockBot, MockContext + + +class SecurityCogTests(unittest.TestCase): + """Tests the `Security` cog.""" + + def setUp(self): + """Attach an instance of the cog to the class for tests.""" + self.bot = MockBot() + self.cog = security.Security(self.bot) + self.ctx = MockContext() + + def test_check_additions(self): + """The cog should add its checks after initialization.""" + self.bot.check.assert_any_call(self.cog.check_on_guild) + self.bot.check.assert_any_call(self.cog.check_not_bot) + + def test_check_not_bot_returns_false_for_humans(self): + """The bot check should return `True` when invoked with human authors.""" + self.ctx.author.bot = False + self.assertTrue(self.cog.check_not_bot(self.ctx)) + + def test_check_not_bot_returns_true_for_robots(self): + """The bot check should return `False` when invoked with robotic authors.""" + self.ctx.author.bot = True + self.assertFalse(self.cog.check_not_bot(self.ctx)) + + def test_check_on_guild_raises_when_outside_of_guild(self): + """When invoked outside of a guild, `check_on_guild` should cause an error.""" + self.ctx.guild = None + + with self.assertRaises(NoPrivateMessage, msg="This command cannot be used in private messages."): + self.cog.check_on_guild(self.ctx) + + def test_check_on_guild_returns_true_inside_of_guild(self): + """When invoked inside of a guild, `check_on_guild` should return `True`.""" + self.ctx.guild = "lemon's lemonade stand" + self.assertTrue(self.cog.check_on_guild(self.ctx)) + + +class SecurityCogLoadTests(unittest.IsolatedAsyncioTestCase): + """Tests loading the `Security` cog.""" + + async def test_security_cog_load(self): + """Setup of the extension should call add_cog.""" + bot = MockBot() + await security.setup(bot) + bot.add_cog.assert_awaited_once() diff --git a/tests/bot/exts/filters/test_security.py b/tests/bot/exts/filters/test_security.py deleted file mode 100644 index 007b7b1eb..000000000 --- a/tests/bot/exts/filters/test_security.py +++ /dev/null @@ -1,53 +0,0 @@ -import unittest - -from discord.ext.commands import NoPrivateMessage - -from bot.exts.filters import security -from tests.helpers import MockBot, MockContext - - -class SecurityCogTests(unittest.TestCase): - """Tests the `Security` cog.""" - - def setUp(self): - """Attach an instance of the cog to the class for tests.""" - self.bot = MockBot() - self.cog = security.Security(self.bot) - self.ctx = MockContext() - - def test_check_additions(self): - """The cog should add its checks after initialization.""" - self.bot.check.assert_any_call(self.cog.check_on_guild) - self.bot.check.assert_any_call(self.cog.check_not_bot) - - def test_check_not_bot_returns_false_for_humans(self): - """The bot check should return `True` when invoked with human authors.""" - self.ctx.author.bot = False - self.assertTrue(self.cog.check_not_bot(self.ctx)) - - def test_check_not_bot_returns_true_for_robots(self): - """The bot check should return `False` when invoked with robotic authors.""" - self.ctx.author.bot = True - self.assertFalse(self.cog.check_not_bot(self.ctx)) - - def test_check_on_guild_raises_when_outside_of_guild(self): - """When invoked outside of a guild, `check_on_guild` should cause an error.""" - self.ctx.guild = None - - with self.assertRaises(NoPrivateMessage, msg="This command cannot be used in private messages."): - self.cog.check_on_guild(self.ctx) - - def test_check_on_guild_returns_true_inside_of_guild(self): - """When invoked inside of a guild, `check_on_guild` should return `True`.""" - self.ctx.guild = "lemon's lemonade stand" - self.assertTrue(self.cog.check_on_guild(self.ctx)) - - -class SecurityCogLoadTests(unittest.IsolatedAsyncioTestCase): - """Tests loading the `Security` cog.""" - - async def test_security_cog_load(self): - """Setup of the extension should call add_cog.""" - bot = MockBot() - await security.setup(bot) - bot.add_cog.assert_awaited_once() -- cgit v1.2.3