|
| 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