Skip to content

Commit b1fed88

Browse files
authored
Merge pull request #2208 from sopel-irc/reddit-shortlink-overlap
reddit: fix matching twice for reddit.com without www.
2 parents a8dcb76 + e876a48 commit b1fed88

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

sopel/modules/reddit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
r'/r/\S+?/comments/(?P<submission>[\w-]+)'
3333
r'(?:/?(?:[\w%]+/(?P<comment>[\w-]+))?)'
3434
)
35-
short_post_url = r'https?://(redd\.it|reddit\.com)/(?P<submission>[\w-]+)'
35+
short_post_url = r'https?://(redd\.it|reddit\.com)/(?P<submission>[\w-]+)/?$'
3636
user_url = r'%s/u(?:ser)?/([\w-]+)' % domain
3737
image_url = r'https?://i\.redd\.it/\S+'
3838
video_url = r'https?://v\.redd\.it/([\w-]+)'
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""Tests for Sopel's ``reddit`` plugin"""
2+
from __future__ import generator_stop
3+
4+
import pytest
5+
6+
from sopel.trigger import PreTrigger
7+
8+
9+
TMP_CONFIG = """
10+
[core]
11+
owner = Admin
12+
nick = Sopel
13+
enable =
14+
reddit
15+
host = irc.libera.chat
16+
"""
17+
18+
19+
@pytest.fixture
20+
def bot(botfactory, configfactory):
21+
settings = configfactory('default.ini', TMP_CONFIG)
22+
return botfactory.preloaded(settings, ['reddit'])
23+
24+
25+
MATCHING_URLS = (
26+
# URLs the reddit plugin is expected to handle
27+
# Should match ONCE each, no more, no less
28+
'https://redd.it/123456',
29+
'https://redd.it/123456/',
30+
'https://reddit.com/123456',
31+
'https://reddit.com/123456/',
32+
'https://reddit.com/r/subname',
33+
'https://reddit.com/r/subname/',
34+
'https://www.reddit.com/r/subname',
35+
'https://www.reddit.com/r/subname/',
36+
'https://reddit.com/r/subname/comments/123456',
37+
'https://reddit.com/r/subname/comments/123456/',
38+
'https://reddit.com/r/subname/comments/123456?param=value',
39+
'https://reddit.com/r/subname/comments/123456/?param=value',
40+
'https://www.reddit.com/r/subname/comments/123456',
41+
'https://www.reddit.com/r/subname/comments/123456/',
42+
'https://reddit.com/r/subname/comments/123456/post_title_slug/234567',
43+
'https://reddit.com/r/subname/comments/123456/post_title_slug/234567/',
44+
'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567',
45+
'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567/',
46+
'https://reddit.com/r/subname/comments/123456/post_title_slug/234567/?context=1337',
47+
'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567/?context=1337',
48+
)
49+
50+
51+
NON_MATCHING_URLS = (
52+
# we don't allow for query parameters on subreddit links (yet?)
53+
'https://reddit.com/r/subname?param=value',
54+
'https://reddit.com/r/subname/?param=value',
55+
'https://www.reddit.com/r/subname?param=value',
56+
'https://www.reddit.com/r/subname/?param=value',
57+
# "shortlink" style allegedly seen in the wild, but not currently recognized
58+
'https://reddit.com/comments/123456',
59+
'https://reddit.com/comments/123456/',
60+
)
61+
62+
63+
@pytest.mark.parametrize('link', MATCHING_URLS)
64+
def test_url_matching(link, bot):
65+
line = PreTrigger(bot.nick, ':[email protected] PRIVMSG #channel {}'.format(link))
66+
matches = bot.rules.get_triggered_rules(bot, line)
67+
68+
assert len([match for match in matches if match[0].get_plugin_name() == 'reddit']) == 1
69+
70+
71+
@pytest.mark.parametrize('link', NON_MATCHING_URLS)
72+
def test_url_non_matching(link, bot):
73+
line = PreTrigger(bot.nick, ':[email protected] PRIVMSG #channel {}'.format(link))
74+
matches = bot.rules.get_triggered_rules(bot, line)
75+
76+
assert len([match for match in matches if match[0].get_plugin_name() == 'reddit']) == 0

0 commit comments

Comments
 (0)