def test_clone_post(create_bot): # Test cloned post test_submission = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="title_test", body="asd1234") test.get_reddit().inbox.add_message( "mod1", "/clone_post --subreddit=testsub123 --sticky --title=test2 --sub_link=%s" % test_submission.shortlink) test.advance_time_10m() _, body = test.get_user("mod1").inbox[-1] # Get first line fline = body.split("\n")[0] # Get id target_sub = None id = fline.split(" ")[1] for sub in test.cache_submissions.values(): if sub.shortlink == id: target_sub = sub # Check for content assert "asd1234" in target_sub.body # Edit the original body test_submission.edit("asd5678") test.advance_time_10m() # Check for content assert "asd5678" in target_sub.body
def test_notify(create_bot): wiki_trigger_words = """ [Setup] word_list = ["12345", "abcdef", "aaa"] ignore_users = ["JohnDoe2"] """ sub = test.get_subreddit(TEST_SUBREDDIT) # Update flair posts control panel sub.edit_wiki("control_panel", enable_flair_posts) sub.edit_wiki("word_notifier", wiki_trigger_words) # Tell the bot to update the control panel test.get_reddit().inbox.add_message( "mod1", "/update_control_panel --subreddit %s" % TEST_SUBREDDIT) # Give some time to the bot to get the new wiki configuration test.advance_time_60s() test.FakeSubmission( subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="title_test", body="blabla qweqw") test.advance_time_10m() submission = test.FakeSubmission( subreddit_name="some_other_sub", author_name="JohnDoe1", title="title_test", body="12345 oqiwjeoqiw aaa xxxabcdefxxx") test.advance_time_10m() sub = test.get_subreddit(TEST_SUBREDDIT) # Check that modmail has been sent assert(len(sub.modmail) == 1) submission.add_comment("user1", "12345 1234") test.advance_time_10m() # Check that modmail has been sent assert(len(sub.modmail) == 2)
def test_usernotes(create_bot): sub = test.get_subreddit(TEST_SUBREDDIT) unotes = sub.get_wiki("usernotes") unotes.set_content(open("tests/test_usernotes.json", "r").read(), "me") submission = test.FakeSubmission( subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="title_test", body="12345 oqiwjeoqiw aaa xxxabcdefxxx") test.advance_time_10m() # Add a mod report submission.report("test1", "mod1") submission.report("test2", "invalid_mod") submission.report("test3") test.advance_time_10m() mod1 = test.get_user("mod1") assert(len(test.get_user("bot_owner").inbox) == 2) assert(len(mod1.inbox) == 0) submission.report("/get_notes", "invalid_mod") assert(len(test.get_user("bot_owner").inbox) == 3) submission.report("/get_notes", "mod1") assert(len(mod1.inbox) == 1) assert("Usernotes not found" in mod1.inbox[0][0]) mod1.inbox = [] submission2 = test.FakeSubmission( subreddit_name=TEST_SUBREDDIT, author_name="RoCirclejerk", title="title_test", body="12345 oqiwjeoqiw aaa xxxabcdefxxx") test.advance_time_10m() submission2.report("/get_notes", "mod1") assert(len(mod1.inbox) == 1) assert("test1" in mod1.inbox[0][1])
def test_changed_title(create_bot): wiki_flair_posts = """ [Setup] minimum_overlap_percent = 60 domains = ["google.com", "blabla.co.uk"] """ sub = test.get_subreddit(TEST_SUBREDDIT) # Update control panel and plugin wiki sub.edit_wiki("control_panel", enable_flair_posts) sub.edit_wiki("changed_title", wiki_flair_posts) # Tell the bot to update the control panel test.get_reddit().inbox.add_message( "mod1", "/update_control_panel --subreddit %s" % TEST_SUBREDDIT) # Give some time to the bot to get the new wiki configuration test.advance_time_60s() # Create fake articles test.FakeURL("https://google.com/myarticle1", "I don't like mosquitoes because they suck blood") test.FakeURL("https://google.com/myarticle2", "qwerty asdfg zxcvb poiuy lkjhg mnbvc") # Test it test_submission1 = test.FakeSubmission( subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="AAAA BBBB CCCC DDDD EEEE FFFF", url="https://google.com/myarticle1") test_submission2 = test.FakeSubmission( subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="qwerty asdfg zxcvb poiuy lkjhg mnbvc", url="https://google.com/myarticle2") test.advance_time_10m() assert (len(test_submission1.reports) == 1) assert (len(test_submission2.reports) == 0)
def test_flair_warning(create_bot): wiki_flair_posts = """ [Setup] message_intervals = 5, 7, 9, 10, 15, 20, 25 autoflair = 1 message = message ${MESSAGE_NO}/${MAX_MESSAGES}/${SUBMISSION_LINK} [autoflair_test1] title_contains = ["testa", "testb"] flair_css_class = "testflair" """ sub = test.get_subreddit(TEST_SUBREDDIT) # Tell the bot to update the control panel test.get_reddit().inbox.add_message( "mod1", "/update_control_panel --subreddit %s" % TEST_SUBREDDIT) # Update flair posts control panel sub.edit_wiki("control_panel", enable_flair_posts) sub.edit_wiki("flair_posts", wiki_flair_posts) # Tell the bot to update the control panel test.get_reddit().inbox.add_message( "mod1", "/update_control_panel --subreddit %s" % TEST_SUBREDDIT) # Give some time to the bot to get the new wiki configuration test.advance_time_60s() # Create a new submissinon that we will be testing against test_submission = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="title_test") # Give the bot time to send all messages test.advance_time_30m() user = test.get_user("JohnDoe1") # Check that 7 messages have been sent assert (len(user.inbox) == 7) # Check each inbox message msg_no = 1 while len(user.inbox) != 0: _, text = user.inbox[0] assert ("message %d/7/%s" % (msg_no, test_submission.shortlink) in text) msg_no += 1 user.inbox = user.inbox[1:]
def test_webhook(create_bot): wiki_cfg = """ [Setup] modlog = http://webhook1 submissions = http://webhook2 """ sub = test.get_subreddit(TEST_SUBREDDIT) # Update control panel and plugin wiki sub.edit_wiki("control_panel", enable_streamer) sub.edit_wiki("webhook_streamer", wiki_cfg) # Tell the bot to update the control panel test.get_reddit().inbox.add_message( "mod1", "/update_control_panel --subreddit %s" % TEST_SUBREDDIT) # Give some time to the bot to get the new wiki configuration test.advance_time_60s() # Check that the webhook target is None first # (aka does not exist because no messages have been sent) assert (test.get_webhook("http://webhook1") == None) # Add modlog item sub.add_modlog("mod1", "dummy_target", "test", "blabla", "test description") # Check again assert (len(test.get_webhook("http://webhook1").messages) == 1) test.advance_time_60s() # Check that the webhook target is None first # (aka does not exist because no messages have been sent) assert (test.get_webhook("http://webhook2") == None) # Create a new submissinon to test submission webhooks too test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="AAAA BBBB CCCC DDDD EEEE FFFF") test.advance_time_60s() # Check again assert (len(test.get_webhook("http://webhook2").messages) == 1)
def test_sched_post(create_bot): enable_sched_posts = """ [Enabled Plugins] schedule_posts """ sub = test.get_subreddit(TEST_SUBREDDIT) test.set_time(22 * 60 * 60) test_submission = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="title_test", body="asd1234") wiki_sched_posts = r""" [post_at_12AM] title=test1 test2 ${DAY}.${MONTH}.${YEAR} body=aaa bbb ccc interval= 0 0 * * * * [post_at_1AM] title=test3 test4 wikibody=post1AM interval= 0 1 * * * * [post_at_2AM] title=test5 test6 clonepost=%s interval= 0 2 * * * * """ % test_submission.permalink sub.edit_wiki("post1AM", "xx1") # Update control panel and plugin wiki sub.edit_wiki("control_panel", enable_sched_posts) sub.edit_wiki("schedule_posts", wiki_sched_posts) # Tell the bot to update the control panel test.get_reddit().inbox.add_message( "mod1", "/update_control_panel --subreddit %s" % TEST_SUBREDDIT) test.advance_time_10m() test.advance_time_1h() test.advance_time_1h() test.advance_time_1h() test.advance_time_1h() post_12am = None post_1am = None post_2am = None # Get the posts for post in test.cache_submissions.values(): if post.title.startswith("test1 test2"): post_12am = post if post.title == "test3 test4": post_1am = post if post.title == "test5 test6": post_2am = post assert post_12am assert post_1am assert post_2am assert post_12am.created_utc - 86400 < 60 assert post_1am.created_utc - 90000 < 60 assert post_2am.created_utc - 93600 < 60
def test_auto_flair(create_bot): wiki_flair_posts = """ [Setup] autoflair = 1 [autoflair_test1] title_contains = ["test1", "test2"] flair_css_class = "testflair1" [autoflair_test2] title_contains = ["test3", "test4"] flair_css_class = "testflair2" [autoflair_test3] body_contains = ["test5", "test6"] flair_css_class = "testflair3" [autoflair_test4] domain = ["redditbot.com", "pula.ro"] flair_css_class = "testflair4" [autoflair_test5] title_contains = ["prio1", "prio2"] flair_css_class = "testflair5" priority = 50 [autoflair_test6] title_contains = ["prio1", "prio2"] flair_css_class = "testflair6" priority = 40 """ sub = test.get_subreddit(TEST_SUBREDDIT) # Update flair posts control panel sub.edit_wiki("control_panel", enable_flair_posts) sub.edit_wiki("flair_posts", wiki_flair_posts) # Tell the bot to update the control panel test.get_reddit().inbox.add_message( "mod1", "/update_control_panel --subreddit %s" % TEST_SUBREDDIT) # Give some time to the bot to get the new wiki configuration test.advance_time_60s() subreddit = test.get_subreddit(TEST_SUBREDDIT) subreddit.set_flairs([ "testflair1", "testflair2", "testflair3", "testflair4", "testflair5", "testflair6" ]) # Test titles test_submission1 = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="blabla test1 blabla") test_submission2 = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe2", title="blabla test4 blabla") # Give the bot time to send all messages for _ in range(30): test.advance_time_60s() assert (test_submission1.flairs.set_flair_id == "testflair1") assert (test_submission2.flairs.set_flair_id == "testflair2") # Test body test_submission3 = test.FakeSubmission( subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="blabla 1234 blabla", body="qoweiqoiejqoiwjq test5 pqioejoqij") # Give the bot time to send all messages for _ in range(30): test.advance_time_60s() assert (test_submission3.flairs.set_flair_id == "testflair3") # Test domain test_submission4 = test.FakeSubmission( subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="blabla 1234 blabla", url="https://www.redditbot.com/12345.123") # Give the bot time to send all messages test.advance_time_30m() assert (test_submission4.flairs.set_flair_id == "testflair4") # Test priority test_submission5 = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="blabla prio2 blabla") # Give the bot time to send all messages test.advance_time_30m() assert (test_submission5.flairs.set_flair_id == "testflair6")
def test_corner_cases(create_bot): wiki_flair_posts = """ [Setup] message_intervals = 2, 30 message = message ${MESSAGE_NO}/${MAX_MESSAGES}/${SUBMISSION_LINK} """ sub = test.get_subreddit(TEST_SUBREDDIT) # Update flair posts control panel sub.edit_wiki("control_panel", enable_flair_posts) sub.edit_wiki("flair_posts", wiki_flair_posts) # Tell the bot to update the control panel test.get_reddit().inbox.add_message( "mod1", "/update_control_panel --subreddit %s" % TEST_SUBREDDIT) # Give some time to the bot to get the new wiki configuration test.advance_time_10m() # Create new submissions test_submission1 = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe123", title="random title") test_submission2 = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe123", title="random title") test_submission3 = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe123", title="random title") # Advance a few minutes so that one message is sent test.advance_time_10m() # Remove it by a mod test_submission1.delete_by_mod() # Remove it by the author test_submission2.delete_by_author() # Add flair by user test_submission3.set_link_flair_text("asdfg") # Give the bot time to remove the posts from queues test.advance_time_30m() test.advance_time_30m() user = test.get_user("JohnDoe123") # Check that 2 messages have been sent assert (len(user.inbox) == 3) # Try to exceed minimum trigger time test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="granular", title="random title") test.advance_time(test.GLOBAL_TIME + 61) assert (len(test.get_user("granular").inbox) == 0)
def test_repost_detector(create_bot): wiki_flair_posts = """ [Setup] minimum_word_length = 3 minimum_nb_words = 5 min_overlap_percent = 50 ignore_users = ["AutoModerator"] """ sub = test.get_subreddit(TEST_SUBREDDIT) # Update control panel and plugin wiki sub.edit_wiki("control_panel", enable_flair_posts) sub.edit_wiki("repost_detector", wiki_flair_posts) # Tell the bot to update the control panel test.get_reddit().inbox.add_message( "mod1", "/update_control_panel --subreddit %s" % TEST_SUBREDDIT) # Give some time to the bot to get the new wiki configuration test.advance_time_60s() # Create a new submissinon that we will be testing against test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="AAAA BBBB CCCC DDDD EEEE FFFF") test.advance_time_30m() # Create another submission test_submission2 = test.FakeSubmission( subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="AAAA BBBB CCCC DDDD EEEE GGGG") test.advance_time_10m() assert (len(test_submission2.reports) == 1) test.advance_time_30m() # Test short word elimination test_submission3 = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="AAAA BBBB CCCC DDDD") assert (len(test_submission3.reports) == 0) # Jump in time one month test.advance_time(2592000) test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="AAAB BBBC CCCD DDDE EEEG GGGF") # Give the chance to remove a post from storage due to being too old test.advance_time_30m() # Test user ignore test_submission4 = test.FakeSubmission( subreddit_name=TEST_SUBREDDIT, author_name="automoderator", title="AAAA BBBB CCCC DDDD EEEE GGGG") test.advance_time_10m() assert (len(test_submission4.reports) == 0)
def test_yt_post(create_bot): sub = test.get_subreddit(TEST_SUBREDDIT) # Update control panel and plugin wiki sub.edit_wiki("control_panel", yt_plugin) sub.edit_wiki( "youtube", r""" [ch1] id = 101 message = got a post by some channel ${AUTHOR} ${KIND} [ch2] id = 102 message = got a post by some channel ${AUTHOR} ${KIND} report = blah [ch3] id = 103 message = got a post by some channel ${AUTHOR} ${KIND} delete = True """) # Tell the bot to update the control panel test.get_reddit().inbox.add_message( "mod1", "/update_control_panel --subreddit %s" % TEST_SUBREDDIT) # Give some time to the bot to get the new wiki configuration test.advance_time_10m() ### # Test1 ### test_submission1 = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="asdfg", url="https://youtube.com/watch?v=1") test.advance_time_30s() # Check conditions are successful for t1 assert (len(sub.modmail) == 1) assert (test_submission1.deleted == False) assert (len(test_submission1.reports) == 0) ### # Test2 ### test_submission2 = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="asdfg", url="https://youtube.com/watch?v=2") test.advance_time_30s() # Check conditions are successful for t2 assert (len(sub.modmail) == 2) assert (test_submission2.deleted == False) assert (len(test_submission2.reports) == 1) ### # Test3 ### test_submission3 = test.FakeSubmission(subreddit_name=TEST_SUBREDDIT, author_name="JohnDoe1", title="asdfg", url="https://youtube.com/watch?v=3") test.advance_time_30s() # Check conditions are successful for t3 assert (len(sub.modmail) == 3) assert (test_submission3.deleted == True) assert (len(test_submission3.reports) == 0)