def test_report_reasons_only(self): rule = Rule({ 'report_reasons (only)': 'abcde', 'action': 'approve' }) comment = helpers.comment() comment.user_reports = [ ['abcde', 1], ] mod = ModqueueModerator(comment) assert mod.moderate(rule), "User reports (only) not passing when only a match exists" comment = helpers.comment() comment.user_reports = [ ['abcde', 1], ['edcba', 1] ] comment.mod_reports = [] mod = ModqueueModerator(comment) self.assertFalse(mod.moderate(rule), "User reports (only) passing when a match exists alongside a non-match") comment = helpers.comment() comment.user_reports = [ ['fghij', 1], ['edcba', 1] ] comment.mod_reports = [] mod = ModqueueModerator(comment) self.assertFalse(mod.moderate(rule), "User reports (only) are passing when the list does not contain the value")
def test_negation(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({'id': 'abcde', 'action': 'remove'}) assert mod.moderate(rule), "Doesn't match when validity is default" rule = Rule({'id': ['abcde', 'fghij'], 'action': 'remove'}) assert mod.moderate( rule), "Doesn't match lists when validity is default" rule = Rule({'~id': 'abcde', 'action': 'remove'}) self.assertFalse(mod.moderate(rule), "Matches when validity is false") rule = Rule({'id': 'test', 'action': 'remove'}) self.assertFalse( mod.moderate(rule), "Matches when validity is default and there's no actual match") rule = Rule({'~id': 'test', 'action': 'remove'}) assert mod.moderate( rule ), "Doesn't match when validity is false, but there's no actual match" rule = Rule({'~id': ['fghij', 'klmno'], 'action': 'remove'}) assert mod.moderate( rule), "Matches when a list of values is given but none match" rule = Rule({'~id': ['abcde', 'fghij'], 'action': 'remove'}) self.assertFalse( mod.moderate(rule), "Matches when a list of values is given and one matches")
def test_author_checks(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({'author': {'post_karma': '> 5'}, 'action': 'remove'}) assert mod.moderate(rule), "basic author checks are failing" rule = Rule({'author': {'post_karma': '> 15'}, 'action': 'remove'}) self.assertFalse(mod.moderate(rule), "basic author checks are throwing false positive")
def test_starts_with(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({'body (starts-with)': 'Hello', 'action': 'remove'}) assert mod.moderate(rule), "starts-with match failing to match" comment.body = 'Wassup, buddy?' self.assertFalse(mod.moderate(rule), "starts-with match matching as false positive")
def test_full_exact_regex(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({ 'body (full-exact, regex)': 'Hello,? world!?', 'action': 'remove' }) assert mod.moderate( rule), "full_exact match with regex failing to match"
def test_is_edited(self): comment = helpers.comment() rule = Rule({'is_edited': True, 'action': 'approve'}) comment.edited = 1595932445.0 mod = Moderator(comment) assert mod.moderate(rule), "is_edited not matching" comment.edited = False self.assertFalse(mod.moderate(rule), "is_edited matching as false positive")
def test_multiple_values(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({'id': ['abcde', 'fghij'], 'action': 'remove'}) assert mod.moderate(rule), "Matches fail with multiple values" rule = Rule({'id': ['fghij', 'abcde'], 'action': 'remove'}) assert mod.moderate( rule ), "Matches fail with multiple values, when correct value is not first"
def test_is_top_level(self): rule = Rule({ 'is_top_level': True, 'action': 'approve' }) comment = helpers.comment() comment.depth = 0 mod = CommentModerator(comment) assert mod.moderate(rule), "is_top_level not matching correctly" comment.depth = 1 self.assertFalse(mod.moderate(rule), "is_top_level matching as a false positive")
def test_moderators_exempt_default(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({'id': 'abcde', 'action': 'remove'}) # Default, non-mod comment.author.moderated = MagicMock(return_value=[]) assert mod.moderate( rule), "Default moderators_exempt is exempting normal redditors" # Default, mod comment.author.moderated = MagicMock(return_value=[comment.subreddit]) self.assertFalse(mod.moderate(rule), "Default moderators_exempt is not exempting mods")
def test_regex_match(self): comment = helpers.comment() comment.body = 'Hello, foo! How are you?' mod = Moderator(comment) rule = Rule({ 'body (full-exact, regex)': 'Hello, [A-za-z]+! How are you\\?', 'action': 'remove' }) assert mod.moderate(rule), "Regex match failing to match" comment.body = 'Hello, Foo Bar! How are you?' self.assertFalse(mod.moderate(rule), "Regex match matching as false positive")
def test_include(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({'body (includes)': 'Ell', 'action': 'remove'}) assert mod.moderate(rule), "include match failing to match" rule = Rule({'body (includes)': 'lo, ', 'action': 'remove'}) assert mod.moderate( rule), "include match failing to match with word boundaries" comment.body = "Hello world" self.assertFalse(mod.moderate(rule), "include match matching as a false positive")
def test_report_reasons_contains(self): rule = Rule({ 'report_reasons': 'abcde', 'action': 'approve' }) comment = helpers.comment() comment.user_reports = [ ['abcde', 1], ['edcba', 1] ] comment.mod_reports = [] mod = ModqueueModerator(comment) assert mod.moderate(rule), "User reports not passing when the list contains the value" comment = helpers.comment() comment.user_reports = [ ['fghij', 1], ['edcba', 1] ] comment.mod_reports = [] mod = ModqueueModerator(comment) self.assertFalse(mod.moderate(rule), "User reports are passing when the list does not contain the value")
def test_reports(self): rule = Rule({'reports': 2, 'action': 'approve'}) comment = helpers.comment() comment.user_reports = [['abcde', 1], ['edcba', 1]] comment.mod_reports = [] mod = Moderator(comment) assert mod.moderate( rule ), "Reports (count) not passing when the list contains same number of values" comment = helpers.comment() comment.user_reports = [['fghij', 1]] comment.mod_reports = [['edcba', 1]] mod = Moderator(comment) assert mod.moderate( rule ), "Reports (count) are not passing when the list contains the appropriate value between user and mod reports" comment.mod_reports = [] mod = Moderator(comment) self.assertFalse(mod.moderate(rule), "Reports (count) matching as false positive")
def test_match(self): comment = helpers.comment() comment.body = "comment with id %s" % comment.id rule = Rule({ 'id': comment.id, 'body (full-exact)': 'comment with id {{match-id}}', 'action': 'approve' }) mod = Moderator(comment) assert mod.moderate(rule), 'Match not injecting itself' comment.id = 'fghij' self.assertFalse(mod.moderate(rule), 'Match injecting incorrectly')
def test_ignore_blockquotes(self): rule = Rule({'ignore_blockquotes': True}) comment = helpers.comment() mod = Moderator(comment) # test # test <-- removed # <-- removed # test <-- removed # test comment.body = "test\n test\n \n test\ntest" self.assertEqual(len(mod.checks.body.__wrapped__(mod, rule, [])), 9) comment.body = "test ```test\ntest\n test\n test\ntest``` test" self.assertEqual(len(mod.checks.body.__wrapped__(mod, rule, [])), 10)
def test_parent_comment(self): comment = helpers.comment() parent_comment = helpers.comment() comment.parent = MagicMock(return_value=parent_comment) mod = CommentModerator(comment) rule = Rule({ 'parent_comment': { 'id': 'test', 'action': 'approve' } }) comment.depth = 0 self.assertFalse(mod.moderate(rule), "parent_comment matches even at depth=0") comment.depth = 1 comment.id = 'test' self.assertFalse(mod.moderate(rule), "parent_comment matching on OG comment attributes") comment.id = 'abcde' parent_comment.id = 'test' parent_comment.mod.approve.reset_mock() assert mod.moderate(rule), "parent_comment not matching" parent_comment.mod.approve.assert_called()
def test_is_banned(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({'author': {'is_banned': True}, 'action': 'approve'}) banned_mock = MagicMock(return_value=[ 1 ]) #is_banned just checks for any items in the array comment.subreddit.banned = banned_mock assert mod.moderate( rule), "is_banned not matching when one value is returned for bans" banned_mock.assert_called_with(redditor=comment.author.name) banned_mock.return_value = [] self.assertFalse(mod.moderate(rule), "is_banned matching as false positive")
def test_is_submitter(self): rule = Rule({ 'author': { 'is_submitter': True }, 'action': 'approve' }) comment = helpers.comment() comment.author.id = 'abcde' comment.submission.author.id = 'abcde' mod = CommentModerator(comment) assert mod.moderate(rule), "is_submitted not matching correctly" comment.author.id = 'fghij' self.assertFalse(mod.moderate(rule), "is_submitter matching on a false positive")
def test_lowercase_checks(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({'id (full-text)': 'AbCdE', 'action': 'remove'}) assert mod.moderate( rule), "full-text doesn't match when some letters are uppercase" rule = Rule({ 'id (full-text, case-sensitive)': 'AbCdE', 'action': 'remove' }) self.assertFalse( mod.moderate(rule), "full-text matches even when some letters are uppercase and case-sensitive is on" )
def test_multiple_checks(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({'id+body (full-exact)': 'abcde', 'action': 'remove'}) assert mod.moderate(rule), "Matches fail when checks are combined" rule = Rule({ 'id+body (full-exact)': 'Hello, world!', 'action': 'remove' }) assert mod.moderate(rule), "Matches fail when checks are combined" rule = Rule({'id+body (full-exact)': 'not there', 'action': 'remove'}) self.assertFalse(mod.moderate(rule), "Matches when checks are combined")
def test_contains_calls_full_text(self): rule = Rule({ 'report_reasons': 'abcde', 'action': 'approve' }) comment = helpers.comment() comment.user_reports = [ ['fghij', 1], ] mod = ModqueueModerator(comment) old_full_exact = ModqueueModerator.full_exact ModqueueModerator.full_exact = MagicMock(return_value=True) mod.moderate(rule) mod.full_exact.assert_called() ModqueueModerator.full_exact = old_full_exact
def test_body_shorter_than(self): comment = helpers.comment() rule = Rule({ 'body_shorter_than': len(comment.body) + 1, 'action': 'approve' }) mod = Moderator(comment) assert mod.moderate(rule), "body_shorter_than not matching" comment.body = comment.body + 'a' self.assertFalse(mod.moderate(rule), 'body_shorter_than matching when lengths are equal') comment.body = comment.body + 'a' self.assertFalse( mod.moderate(rule), 'body_shorter_than matching when the body is too long')
def test_full_text(self): comment = helpers.comment() comment.body = ' .# Hello, world! # ..' mod = Moderator(comment) rule = Rule({'body (full-text)': 'Hello, world', 'action': 'remove'}) assert mod.moderate(rule), "full-text match failing to match" comment.body = ' .# Hello, world' assert mod.moderate( rule), "full-text match failing with just leading symbols" comment.body = ' Hello, world! # ..' assert mod.moderate( rule), "full-text match failing with just following symbols" comment.body = ' .# Hello, world! # Cya! ..' self.assertFalse(mod.moderate(rule), "full-text match matching as false positive")
def test_satisfy_any_threshold(self): comment = helpers.comment() type(comment.author).comment_karma = PropertyMock(return_value=150) type(comment.author).link_karma = PropertyMock(return_value=100) mod = Moderator(comment) rule = Rule({ 'author': { 'comment_karma': '> 50', 'post_karma': '< 50' }, 'action': 'approve' }) self.assertFalse( mod.moderate(rule), 'Karma rules are passing when false, regardless of satisfy_any_threshold' ) rule = Rule({ 'author': { 'comment_karma': '> 50', 'post_karma': '< 50', 'satisfy_any_threshold': True }, 'action': 'approve' }) assert mod.moderate( rule ), 'Karma rules are not passing when satisfy_any_threshold is True' rule = Rule({ 'author': { 'comment_karma': '< 50', 'post_karma': '< 50', 'satisfy_any_threshold': True }, 'action': 'approve' }) self.assertFalse( mod.moderate(rule), 'Karma rules matching as false positive when satisfy_any_threshold is True' )
def test_body_longer_than(self): comment = helpers.comment() comment.body = "Hello, world" rule = Rule({ 'body_longer_than': len(comment.body) - 1, # Removing 2 because the "!" will be removed 'action': 'approve' }) mod = Moderator(comment) assert mod.moderate(rule), "body_longer_than not matching" comment.body = comment.body[:-1] self.assertFalse(mod.moderate(rule), 'body_longer_than matching when lengths are equal') comment.body = comment.body[:-1] self.assertFalse( mod.moderate(rule), 'body_longer_than matching when the body is too short')
def test_placeholders(self): comment = helpers.comment() comment.body = "Hello, %s" % comment.author.name mod = Moderator(comment) rule = Rule({ 'body (full-exact)': 'Hello, {{author}}', 'action': 'remove' }) assert mod.moderate(rule), "Author placeholder is not replaced" comment.body = "Hello, %s, how are you %s?" % (comment.author.name, comment.author.name) rule = Rule({ 'body (full-exact)': 'Hello, {{author}}, how are you {{author}}?', 'action': 'remove' }) assert mod.moderate( rule ), "Author placeholder is not replaced when there are multiple matches"
def test_report_reasons_with_mod_reports(self): rule = Rule({ 'report_reasons': 'abcde', 'action': 'approve' }) comment = helpers.comment() comment.author.moderated = MagicMock(return_value=[comment.subreddit]) comment.user_reports = [ ['fghij', 1], ] comment.mod_reports = [ ['abcde', 1] ] mod = ModqueueModerator(comment) assert mod.moderate(rule), "Report reasons don't match against mod reports" rule = Rule({ 'report_reasons': 'abcde', 'action': 'approve', 'moderators_exempt': True }) self.assertFalse(mod.moderate(rule), "Report reasons include mod reports even when moderators_exempt is true")
def test_moderators_exempt_set(self): comment = helpers.comment() comment.author.moderated = MagicMock(return_value=[comment.subreddit]) mod = Moderator(comment) # exempt True, mod rule = Rule({ 'id': 'abcde', 'moderators_exempt': True, 'action': 'remove' }) self.assertFalse( mod.moderate(rule), "Moderators are not exempted when moderators_exempt is true") # exempt False, mod rule = Rule({ 'id': 'abcde', 'moderators_exempt': False, 'action': 'remove' }) assert mod.moderate( rule ), "Moderators are exempted even when moderators_exempt is false"
def test_includes_regex(self): comment = helpers.comment() mod = Moderator(comment) rule = Rule({'body (includes, regex)': 'Hello!?', 'action': 'remove'}) assert mod.moderate(rule), "include match with regex failing to match"