Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bot/rules/attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def apply(

if total_recent_attachments > config['max']:
return (
f"sent {total_recent_attachments} attachments in {config['max']}s",
f"sent {total_recent_attachments} attachments in {config['interval']}s",
(last_message.author,),
relevant_messages
)
Expand Down
76 changes: 76 additions & 0 deletions tests/bot/rules/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import unittest
from abc import ABCMeta, abstractmethod
from typing import Callable, Dict, Iterable, List, NamedTuple, Tuple

from tests.helpers import MockMessage


class DisallowedCase(NamedTuple):
"""Encapsulation for test cases expected to fail."""
recent_messages: List[MockMessage]
culprits: Iterable[str]
n_violations: int


class RuleTest(unittest.TestCase, metaclass=ABCMeta):
"""
Abstract class for antispam rule test cases.

Tests for specific rules should inherit from `RuleTest` and implement
`relevant_messages` and `get_report`. Each instance should also set the
`apply` and `config` attributes as necessary.

The execution of test cases can then be delegated to the `run_allowed`
and `run_disallowed` methods.
"""

apply: Callable # The tested rule's apply function
config: Dict[str, int]
Comment thread
kwzrd marked this conversation as resolved.
Resolved

async def run_allowed(self, cases: Tuple[List[MockMessage], ...]) -> None:
"""Run all `cases` against `self.apply` expecting them to pass."""
for recent_messages in cases:
last_message = recent_messages[0]

with self.subTest(
last_message=last_message,
recent_messages=recent_messages,
config=self.config,
):
self.assertIsNone(
await self.apply(last_message, recent_messages, self.config)
)

async def run_disallowed(self, cases: Tuple[DisallowedCase, ...]) -> None:
"""Run all `cases` against `self.apply` expecting them to fail."""
for case in cases:
recent_messages, culprits, n_violations = case
last_message = recent_messages[0]
relevant_messages = self.relevant_messages(case)
desired_output = (
self.get_report(case),
culprits,
relevant_messages,
)

with self.subTest(
last_message=last_message,
recent_messages=recent_messages,
relevant_messages=relevant_messages,
n_violations=n_violations,
config=self.config,
):
self.assertTupleEqual(
await self.apply(last_message, recent_messages, self.config),
desired_output,
)

@abstractmethod
def relevant_messages(self, case: DisallowedCase) -> Iterable[MockMessage]:
"""Give expected relevant messages for `case`."""
raise NotImplementedError

@abstractmethod
def get_report(self, case: DisallowedCase) -> str:
"""Give expected error report for `case`."""
raise NotImplementedError
97 changes: 35 additions & 62 deletions tests/bot/rules/test_attachments.py
Original file line number Diff line number Diff line change
@@ -1,98 +1,71 @@
import unittest
from typing import List, NamedTuple, Tuple
from typing import Iterable

from bot.rules import attachments
from tests.bot.rules import DisallowedCase, RuleTest
from tests.helpers import MockMessage, async_test


class Case(NamedTuple):
recent_messages: List[MockMessage]
culprit: Tuple[str]
total_attachments: int


def msg(author: str, total_attachments: int) -> MockMessage:
def make_msg(author: str, total_attachments: int) -> MockMessage:
"""Builds a message with `total_attachments` attachments."""
return MockMessage(author=author, attachments=list(range(total_attachments)))


class AttachmentRuleTests(unittest.TestCase):
class AttachmentRuleTests(RuleTest):
"""Tests applying the `attachments` antispam rule."""

def setUp(self):
self.config = {"max": 5}
self.apply = attachments.apply
self.config = {"max": 5, "interval": 10}

@async_test
async def test_allows_messages_without_too_many_attachments(self):
"""Messages without too many attachments are allowed as-is."""
cases = (
[msg("bob", 0), msg("bob", 0), msg("bob", 0)],
[msg("bob", 2), msg("bob", 2)],
[msg("bob", 2), msg("alice", 2), msg("bob", 2)],
[make_msg("bob", 0), make_msg("bob", 0), make_msg("bob", 0)],
[make_msg("bob", 2), make_msg("bob", 2)],
[make_msg("bob", 2), make_msg("alice", 2), make_msg("bob", 2)],
)

for recent_messages in cases:
last_message = recent_messages[0]

with self.subTest(
last_message=last_message,
recent_messages=recent_messages,
config=self.config
):
self.assertIsNone(
await attachments.apply(last_message, recent_messages, self.config)
)
await self.run_allowed(cases)

@async_test
async def test_disallows_messages_with_too_many_attachments(self):
"""Messages with too many attachments trigger the rule."""
cases = (
Case(
[msg("bob", 4), msg("bob", 0), msg("bob", 6)],
DisallowedCase(
[make_msg("bob", 4), make_msg("bob", 0), make_msg("bob", 6)],
("bob",),
10
10,
),
Case(
[msg("bob", 4), msg("alice", 6), msg("bob", 2)],
DisallowedCase(
[make_msg("bob", 4), make_msg("alice", 6), make_msg("bob", 2)],
("bob",),
6
6,
),
Case(
[msg("alice", 6)],
DisallowedCase(
[make_msg("alice", 6)],
("alice",),
6
6,
),
(
[msg("alice", 1) for _ in range(6)],
DisallowedCase(
[make_msg("alice", 1) for _ in range(6)],
("alice",),
6
6,
),
)

for recent_messages, culprit, total_attachments in cases:
last_message = recent_messages[0]
relevant_messages = tuple(
msg
for msg in recent_messages
if (
msg.author == last_message.author
and len(msg.attachments) > 0
)
await self.run_disallowed(cases)

def relevant_messages(self, case: DisallowedCase) -> Iterable[MockMessage]:
last_message = case.recent_messages[0]
return tuple(
msg
for msg in case.recent_messages
if (
msg.author == last_message.author
and len(msg.attachments) > 0
)
)

with self.subTest(
last_message=last_message,
recent_messages=recent_messages,
relevant_messages=relevant_messages,
total_attachments=total_attachments,
config=self.config
):
desired_output = (
f"sent {total_attachments} attachments in {self.config['max']}s",
culprit,
relevant_messages
)
self.assertTupleEqual(
await attachments.apply(last_message, recent_messages, self.config),
desired_output
)
def get_report(self, case: DisallowedCase) -> str:
return f"sent {case.n_violations} attachments in {self.config['interval']}s"
56 changes: 56 additions & 0 deletions tests/bot/rules/test_burst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from typing import Iterable

from bot.rules import burst
from tests.bot.rules import DisallowedCase, RuleTest
from tests.helpers import MockMessage, async_test


def make_msg(author: str) -> MockMessage:
"""
Init a MockMessage instance with author set to `author`.

This serves as a shorthand / alias to keep the test cases visually clean.
"""
return MockMessage(author=author)


class BurstRuleTests(RuleTest):
"""Tests the `burst` antispam rule."""

def setUp(self):
self.apply = burst.apply
self.config = {"max": 2, "interval": 10}

@async_test
async def test_allows_messages_within_limit(self):
"""Cases which do not violate the rule."""
cases = (
[make_msg("bob"), make_msg("bob")],
[make_msg("bob"), make_msg("alice"), make_msg("bob")],
)

await self.run_allowed(cases)

@async_test
async def test_disallows_messages_beyond_limit(self):
"""Cases where the amount of messages exceeds the limit, triggering the rule."""
cases = (
DisallowedCase(
[make_msg("bob"), make_msg("bob"), make_msg("bob")],
("bob",),
3,
),
DisallowedCase(
[make_msg("bob"), make_msg("bob"), make_msg("alice"), make_msg("bob")],
("bob",),
3,
),
)

await self.run_disallowed(cases)

def relevant_messages(self, case: DisallowedCase) -> Iterable[MockMessage]:
return tuple(msg for msg in case.recent_messages if msg.author in case.culprits)

def get_report(self, case: DisallowedCase) -> str:
return f"sent {case.n_violations} messages in {self.config['interval']}s"
59 changes: 59 additions & 0 deletions tests/bot/rules/test_burst_shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from typing import Iterable

from bot.rules import burst_shared
from tests.bot.rules import DisallowedCase, RuleTest
from tests.helpers import MockMessage, async_test


def make_msg(author: str) -> MockMessage:
"""
Init a MockMessage instance with the passed arg.

This serves as a shorthand / alias to keep the test cases visually clean.
"""
return MockMessage(author=author)


class BurstSharedRuleTests(RuleTest):
"""Tests the `burst_shared` antispam rule."""

def setUp(self):
self.apply = burst_shared.apply
self.config = {"max": 2, "interval": 10}

@async_test
async def test_allows_messages_within_limit(self):
"""
Cases that do not violate the rule.

There really isn't more to test here than a single case.
"""
cases = (
[make_msg("spongebob"), make_msg("patrick")],
)

await self.run_allowed(cases)

@async_test
async def test_disallows_messages_beyond_limit(self):
"""Cases where the amount of messages exceeds the limit, triggering the rule."""
cases = (
DisallowedCase(
[make_msg("bob"), make_msg("bob"), make_msg("bob")],
{"bob"},
3,
),
DisallowedCase(
[make_msg("bob"), make_msg("bob"), make_msg("alice"), make_msg("bob")],
{"bob", "alice"},
4,
),
)

await self.run_disallowed(cases)

def relevant_messages(self, case: DisallowedCase) -> Iterable[MockMessage]:
return case.recent_messages

def get_report(self, case: DisallowedCase) -> str:
return f"sent {case.n_violations} messages in {self.config['interval']}s"
Loading