Repository navigation
Add test cases for antispam rules, fix bug in attachments rule & test #733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SebastiaanZ
merged 15 commits into
python-discord:master
from
kwzrd:unittest-antispam-rules
Feb 21, 2020
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1f17d0e
Add unit test case for burst antispam rule
kwzrd b0713be
Add unit test case for burst shared antispam rule
kwzrd f15f318
Add unit test case for chars antispam rule
kwzrd 4bfe30d
Add unit test case for discord emojis antispam rule
kwzrd f32ffaf
Add unit test case for role mentions antispam rule
kwzrd 4199b12
Fix incorrect config key in attachments antispam rule
kwzrd aedbd6f
Refactor msg helper function name to make_msg
kwzrd 8456331
Adjust multi-line docstrings to prevailing style
kwzrd 93d19f3
Implement RuleTest ABC
kwzrd b89f9c5
Adjust existing tests to inherit from RuleTest ABC
kwzrd 9758e32
Make RuleTest use ABCMeta
kwzrd 7aa1098
Merge branch 'master' into unittest-antispam-rules
jb3 54e8c98
Add unit test for duplicates antispam rule
kwzrd cae7f25
Add unit test for newlines antispam rule
kwzrd 54ffa4e
Merge branch 'master' into unittest-antispam-rules
SebastiaanZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] | ||
|
|
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.