Antispam rules: add unit test for mentions, improve unit tests for links and attachments, fix bug in attachments rule#655
Merged
Conversation
SebastiaanZ
reviewed
Nov 9, 2019
Testing methodology was adjusted in upstream repo. Merging the relevant changes.
…t twice in the sum of attachments
…s, pass config to subTest
MarkKoz
requested changes
Dec 3, 2019
Contributor
MarkKoz
left a comment
There was a problem hiding this comment.
test_disallows_messages_with_too_many_attachments should be now acting like a regression test against that duplicate message bug for attachments, right?
MarkKoz
approved these changes
Dec 4, 2019
Akarys42
approved these changes
Dec 13, 2019
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR addresses several problems.
bot.rules.mentions, using theMockMessagehelper to correctly represent mockeddiscord.Messageinstances. Closes Write unit tests forbot/rules/mentions.py#601. Gives 100% coverage. See0eade46.
bot.rules.linksandbot.rules.attachmentsto useMockMessagehelper instead of anamedtuple("FakeMessage", ["author", "content"])that was a nasty oversimplification of actual Message instances. After Enhancements fortests.helpersand our test suite #660 the mocking system is now more safe as it prevents setting attributes that do not exist for the mocked objects. See 2a9b1fc and a2617d1.Additionally, we have discovered that there is an inconsistency / bug in the behaviour of
bot.rules.attachments. Snippets below explain.All rules have the same function signature, taking the following args:
The antispam cog calls them in
on_messageby providing themessagethat triggers the event, and a history of messages up to a certain point in time. This history, however, includes themessageinstance itself. So let's saylast_message == recent_messages[0]. Within the rule, the first thing we do is build a tuplerelevant_messageswhich is made up of all messages m wherem.author == last_message.author, and so this is the only way thelast_messagearg is used - to see which user we're checking (slight deviation in burst rule which applies to all users):The
attachmentsrule however deviates from this pattern by always including thelast_messageinrelevant_messages, and then looping overrecent_messages. The result of this is that thelast_messagepotentially gets included twice, and its attachments then count twice in the sum.Therefore, a test case that has
last_message == recent_messages[0]will fail, for example... fails with ...
... and also the
relevant_messagescontaining a duplicate.So why wasn't the rule failing on the unit test written for it?
The unit test incorrectly makes the assumption here and here that
last_messageis not present inrecent_messages, therefore making the same mistake as the rule itself.This PR therefore also contains the following.
attachmentsrule to understandlast_messageto be part ofrecent_messagesand buildrelevant_messagesaccordingly. See 54598dd.on_messageevent calling the rule. See eef447a.attachmentsrule against multiple authors, not just a hardcodedlemon. See 01731a8.Always happy to hear any feedback.