diff options
-rw-r--r-- | bot/cogs/antispam.py | 6 | ||||
-rw-r--r-- | tests/cogs/test_antispam.py | 30 |
2 files changed, 34 insertions, 2 deletions
diff --git a/bot/cogs/antispam.py b/bot/cogs/antispam.py index 69367b40b..482965b9b 100644 --- a/bot/cogs/antispam.py +++ b/bot/cogs/antispam.py @@ -1,5 +1,6 @@ import asyncio import logging +from collections.abc import Mapping from dataclasses import dataclass, field from datetime import datetime, timedelta from operator import itemgetter @@ -245,16 +246,17 @@ class AntiSpam: await deletion_context.upload_messages(self.bot.user.id, self.mod_log) -def validate_config() -> bool: +def validate_config(rules: Mapping = AntiSpamConfig.rules) -> dict: """Validates the antispam configs.""" validation_errors = {} - for name, config in AntiSpamConfig.rules.items(): + for name, config in rules.items(): if name not in RULE_FUNCTION_MAPPING: log.error( f"Unrecognized antispam rule `{name}`. " f"Valid rules are: {', '.join(RULE_FUNCTION_MAPPING)}" ) validation_errors[name] = f"`{name}` is not recognized as an antispam rule." + continue for required_key in ('interval', 'max'): if required_key not in config: log.error( diff --git a/tests/cogs/test_antispam.py b/tests/cogs/test_antispam.py new file mode 100644 index 000000000..67900b275 --- /dev/null +++ b/tests/cogs/test_antispam.py @@ -0,0 +1,30 @@ +import pytest + +from bot.cogs import antispam + + +def test_default_antispam_config_is_valid(): + validation_errors = antispam.validate_config() + assert not validation_errors + + + ('config', 'expected'), + ( + ( + {'invalid-rule': {}}, + {'invalid-rule': "`invalid-rule` is not recognized as an antispam rule."} + ), + ( + {'burst': {'interval': 10}}, + {'burst': "Key `max` is required but not set for rule `burst`"} + ), + ( + {'burst': {'max': 10}}, + {'burst': "Key `interval` is required but not set for rule `burst`"} + ) + ) +) +def test_invalid_antispam_config_returns_validation_errors(config, expected): + validation_errors = antispam.validate_config(config) + assert validation_errors == expected |