aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cogs/test_antispam.py
blob: ce5472c71c23d3ef09dd43159b837c36bb7a759a (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
import unittest

from bot.cogs import antispam


class AntispamConfigurationValidationTests(unittest.TestCase):
    """Tests validation of the antispam cog configuration."""

    def test_default_antispam_config_is_valid(self):
        """The default antispam configuration is valid."""
        validation_errors = antispam.validate_config()
        self.assertEqual(validation_errors, {})

    def test_unknown_rule_returns_error(self):
        """Configuring an unknown rule returns an error."""
        self.assertEqual(
            antispam.validate_config({'invalid-rule': {}}),
            {'invalid-rule': "`invalid-rule` is not recognized as an antispam rule."}
        )

    def test_missing_keys_returns_error(self):
        """Not configuring required keys returns an error."""
        keys = (('interval', 'max'), ('max', 'interval'))
        for configured_key, unconfigured_key in keys:
            with self.subTest(
                configured_key=configured_key,
                unconfigured_key=unconfigured_key
            ):
                config = {'burst': {configured_key: 10}}
                error = f"Key `{unconfigured_key}` is required but not set for rule `burst`"

                self.assertEqual(
                    antispam.validate_config(config),
                    {'burst': error}
                )