From 8a610d1ad56a059a7689f184eb372bbe8deaaaf2 Mon Sep 17 00:00:00 2001 From: dgw Date: Mon, 29 Nov 2021 00:45:14 -0600 Subject: [PATCH] Backport pull request #2208 reddit: fix matching twice for reddit.com without www. Modified new test file from master to contain the required header lines (future imports & coding comment) for 7.1.x branch. --- sopel/modules/reddit.py | 2 +- test/modules/test_modules_reddit.py | 77 +++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 test/modules/test_modules_reddit.py diff --git a/sopel/modules/reddit.py b/sopel/modules/reddit.py index bcad3d468a..35b03bc4e0 100644 --- a/sopel/modules/reddit.py +++ b/sopel/modules/reddit.py @@ -44,7 +44,7 @@ r'/r/\S+?/comments/(?P[\w-]+)' r'(?:/?(?:[\w%]+/(?P[\w-]+))?)' ) -short_post_url = r'https?://(redd\.it|reddit\.com)/(?P[\w-]+)' +short_post_url = r'https?://(redd\.it|reddit\.com)/(?P[\w-]+)/?$' user_url = r'%s/u(?:ser)?/([\w-]+)' % domain image_url = r'https?://i\.redd\.it/\S+' video_url = r'https?://v\.redd\.it/([\w-]+)' diff --git a/test/modules/test_modules_reddit.py b/test/modules/test_modules_reddit.py new file mode 100644 index 0000000000..5905779d60 --- /dev/null +++ b/test/modules/test_modules_reddit.py @@ -0,0 +1,77 @@ +# coding=utf-8 +"""Tests for Sopel's ``reddit`` plugin""" +from __future__ import absolute_import, division, print_function, unicode_literals + +import pytest + +from sopel.trigger import PreTrigger + + +TMP_CONFIG = """ +[core] +owner = Admin +nick = Sopel +enable = + reddit +host = irc.libera.chat +""" + + +@pytest.fixture +def bot(botfactory, configfactory): + settings = configfactory('default.ini', TMP_CONFIG) + return botfactory.preloaded(settings, ['reddit']) + + +MATCHING_URLS = ( + # URLs the reddit plugin is expected to handle + # Should match ONCE each, no more, no less + 'https://redd.it/123456', + 'https://redd.it/123456/', + 'https://reddit.com/123456', + 'https://reddit.com/123456/', + 'https://reddit.com/r/subname', + 'https://reddit.com/r/subname/', + 'https://www.reddit.com/r/subname', + 'https://www.reddit.com/r/subname/', + 'https://reddit.com/r/subname/comments/123456', + 'https://reddit.com/r/subname/comments/123456/', + 'https://reddit.com/r/subname/comments/123456?param=value', + 'https://reddit.com/r/subname/comments/123456/?param=value', + 'https://www.reddit.com/r/subname/comments/123456', + 'https://www.reddit.com/r/subname/comments/123456/', + 'https://reddit.com/r/subname/comments/123456/post_title_slug/234567', + 'https://reddit.com/r/subname/comments/123456/post_title_slug/234567/', + 'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567', + 'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567/', + 'https://reddit.com/r/subname/comments/123456/post_title_slug/234567/?context=1337', + 'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567/?context=1337', +) + + +NON_MATCHING_URLS = ( + # we don't allow for query parameters on subreddit links (yet?) + 'https://reddit.com/r/subname?param=value', + 'https://reddit.com/r/subname/?param=value', + 'https://www.reddit.com/r/subname?param=value', + 'https://www.reddit.com/r/subname/?param=value', + # "shortlink" style allegedly seen in the wild, but not currently recognized + 'https://reddit.com/comments/123456', + 'https://reddit.com/comments/123456/', +) + + +@pytest.mark.parametrize('link', MATCHING_URLS) +def test_url_matching(link, bot): + line = PreTrigger(bot.nick, ':User!user@irc.libera.chat PRIVMSG #channel {}'.format(link)) + matches = bot.rules.get_triggered_rules(bot, line) + + assert len([match for match in matches if match[0].get_plugin_name() == 'reddit']) == 1 + + +@pytest.mark.parametrize('link', NON_MATCHING_URLS) +def test_url_non_matching(link, bot): + line = PreTrigger(bot.nick, ':User!user@irc.libera.chat PRIVMSG #channel {}'.format(link)) + matches = bot.rules.get_triggered_rules(bot, line) + + assert len([match for match in matches if match[0].get_plugin_name() == 'reddit']) == 0