Skip to content

Commit

Permalink
Backport pull request #2216
Browse files Browse the repository at this point in the history
reddit, test: even more of the comments link format is optional
  • Loading branch information
dgw committed Dec 11, 2021
1 parent 4fb17d4 commit 19d7fde
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 78 deletions.
2 changes: 1 addition & 1 deletion sopel/modules/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
subreddit_url = r'%s/r/([\w-]+)/?$' % domain
post_or_comment_url = (
domain +
r'/r/\S+?/comments/(?P<submission>[\w-]+)'
r'(?:/r/\S+?)?/comments/(?P<submission>[\w-]+)'
r'(?:/?(?:[\w%]+/(?P<comment>[\w-]+))?)'
)
short_post_url = r'https?://(redd\.it|reddit\.com)/(?P<submission>[\w-]+)/?$'
Expand Down
166 changes: 89 additions & 77 deletions test/modules/test_modules_reddit.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,89 @@
# 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
# 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
('auto_subreddit_info', 'https://reddit.com/r/subname'),
('auto_subreddit_info', 'https://reddit.com/r/subname/'),
('auto_subreddit_info', 'https://www.reddit.com/r/subname'),
('auto_subreddit_info', 'https://www.reddit.com/r/subname/'),
('post_or_comment_info', 'https://redd.it/123456'),
('post_or_comment_info', 'https://redd.it/123456/'),
('post_or_comment_info', 'https://reddit.com/123456'),
('post_or_comment_info', 'https://reddit.com/123456/'),
('post_or_comment_info', 'https://reddit.com/comments/123456'),
('post_or_comment_info', 'https://reddit.com/comments/123456/'),
('post_or_comment_info', 'https://www.reddit.com/comments/123456'),
('post_or_comment_info', 'https://www.reddit.com/comments/123456/'),
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456'),
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456/'),
('post_or_comment_info', 'https://www.reddit.com/comments/123456?param=value'),
('post_or_comment_info', 'https://www.reddit.com/comments/123456/?param=value'),
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456?param=value'),
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456/?param=value'),
('post_or_comment_info', 'https://www.reddit.com/r/subname/comments/123456'),
('post_or_comment_info', 'https://www.reddit.com/r/subname/comments/123456/'),
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456/post_title_slug/234567'),
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456/post_title_slug/234567/'),
('post_or_comment_info', 'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567'),
('post_or_comment_info', 'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567/'),
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456/post_title_slug/234567/?context=1337'),
('post_or_comment_info', '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',
)


@pytest.mark.parametrize('rule_name, link', MATCHING_URLS)
def test_url_matching(link, rule_name, bot):
line = PreTrigger(bot.nick, ':User!user@irc.libera.chat PRIVMSG #channel {}'.format(link))
matched_rules = [
# we can ignore matches that don't come from the plugin under test
match[0] for match in bot.rules.get_triggered_rules(bot, line)
if match[0].get_plugin_name() == 'reddit'
]

assert len(matched_rules) == 1
assert matched_rules[0].get_rule_label() == rule_name


@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))
matched_rules = [
# we can ignore matches that don't come from the plugin under test
match[0] for match in bot.rules.get_triggered_rules(bot, line)
if match[0].get_plugin_name() == 'reddit'
]

assert len(matched_rules) == 0

0 comments on commit 19d7fde

Please sign in to comment.