aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cogs/test_security.py
blob: efa7a50b16f61a8280da6242a5aa84060674b95e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import logging
import unittest
from unittest.mock import MagicMock

from discord.ext.commands import NoPrivateMessage

from bot.cogs 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.TestCase):
    """Tests loading the `Security` cog."""

    def test_security_cog_load(self):
        """Cog loading logs a message at `INFO` level."""
        bot = MagicMock()
        with self.assertLogs(logger='bot.cogs.security', level=logging.INFO) as cm:
            security.setup(bot)
            bot.add_cog.assert_called_once()

        [line] = cm.output
        self.assertIn("Cog loaded: Security", line)