-
-
Notifications
You must be signed in to change notification settings - Fork 409
bot, rules: fix rate limiting rules without a rate limit #2629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,9 @@ | |
|
|
||
| if typing.TYPE_CHECKING: | ||
| from sopel.config import Config | ||
| from sopel.tests.factories import BotFactory, IRCFactory, UserFactory | ||
| from sopel.tests.factories import ( | ||
| BotFactory, ConfigFactory, IRCFactory, TriggerFactory, UserFactory, | ||
| ) | ||
| from sopel.tests.mocks import MockIRCServer | ||
|
|
||
|
|
||
|
|
@@ -81,17 +83,17 @@ def ignored(): | |
|
|
||
|
|
||
| @pytest.fixture | ||
| def tmpconfig(configfactory): | ||
| def tmpconfig(configfactory: ConfigFactory) -> Config: | ||
| return configfactory('test.cfg', TMP_CONFIG) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mockbot(tmpconfig, botfactory): | ||
| def mockbot(tmpconfig: Config, botfactory: BotFactory) -> bot.Sopel: | ||
| return botfactory(tmpconfig) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mockplugin(tmpdir): | ||
| def mockplugin(tmpdir) -> plugins.handlers.PyFilePlugin: | ||
| root = tmpdir.mkdir('loader_mods') | ||
| mod_file = root.join('mockplugin.py') | ||
| mod_file.write(MOCK_MODULE_CONTENT) | ||
|
|
@@ -676,7 +678,7 @@ def url_callback_http(bot, trigger, match): | |
| # call_rule | ||
|
|
||
| @pytest.fixture | ||
| def match_hello_rule(mockbot, triggerfactory): | ||
| def match_hello_rule(mockbot: bot.Sopel, triggerfactory: TriggerFactory): | ||
| """Helper for generating matches to each `Rule` in the following tests""" | ||
| def _factory(rule_hello): | ||
| # trigger | ||
|
|
@@ -694,7 +696,25 @@ def _factory(rule_hello): | |
| return _factory | ||
|
|
||
|
|
||
| def test_call_rule(mockbot, match_hello_rule): | ||
| @pytest.fixture | ||
| def multimatch_hello_rule(mockbot: bot.Sopel, triggerfactory: TriggerFactory): | ||
| def _factory(rule_hello): | ||
| # trigger | ||
| line = ':[email protected] PRIVMSG #channel :hello hello hello' | ||
|
|
||
| trigger = triggerfactory(mockbot, line) | ||
| pretrigger = trigger._pretrigger | ||
|
|
||
| for match in rule_hello.match(mockbot, pretrigger): | ||
| wrapper = bot.SopelWrapper(mockbot, trigger) | ||
| yield match, trigger, wrapper | ||
| return _factory | ||
|
|
||
|
|
||
| def test_call_rule( | ||
| mockbot: bot.Sopel, | ||
| match_hello_rule: typing.Callable, | ||
| ) -> None: | ||
| # setup | ||
| items = [] | ||
|
|
||
|
|
@@ -721,9 +741,10 @@ def testrule(bot, trigger): | |
| assert items == [1] | ||
|
|
||
| # assert the rule is not rate limited | ||
| assert not rule_hello.is_user_rate_limited(Identifier('Test')) | ||
| assert not rule_hello.is_channel_rate_limited('#channel') | ||
| assert not rule_hello.is_global_rate_limited() | ||
| at_time = datetime.now(timezone.utc) | ||
| assert not rule_hello.is_user_rate_limited(Identifier('Test'), at_time) | ||
| assert not rule_hello.is_channel_rate_limited('#channel', at_time) | ||
| assert not rule_hello.is_global_rate_limited(at_time) | ||
|
|
||
| match, rule_trigger, wrapper = match_hello_rule(rule_hello) | ||
|
|
||
|
|
@@ -738,6 +759,36 @@ def testrule(bot, trigger): | |
| assert items == [1, 1] | ||
|
|
||
|
|
||
| def test_call_rule_multiple_matches( | ||
| mockbot: bot.Sopel, | ||
| multimatch_hello_rule: typing.Callable, | ||
| ) -> None: | ||
| # setup | ||
| items = [] | ||
|
|
||
| def testrule(bot, trigger): | ||
| bot.say('hi') | ||
| items.append(1) | ||
| return "Return Value" | ||
|
|
||
| find_hello = rules.FindRule( | ||
| [re.compile(r'(hi|hello|hey|sup)')], | ||
| plugin='testplugin', | ||
| label='testrule', | ||
| handler=testrule) | ||
|
|
||
| for match, rule_trigger, wrapper in multimatch_hello_rule(find_hello): | ||
| mockbot.call_rule(find_hello, wrapper, rule_trigger) | ||
|
|
||
| # assert the rule has been executed three times now | ||
| assert mockbot.backend.message_sent == rawlist( | ||
| 'PRIVMSG #channel :hi', | ||
| 'PRIVMSG #channel :hi', | ||
| 'PRIVMSG #channel :hi', | ||
| ) | ||
| assert items == [1, 1, 1] | ||
|
|
||
|
|
||
| def test_call_rule_rate_limited_user(mockbot, match_hello_rule): | ||
| items = [] | ||
|
|
||
|
|
@@ -767,9 +818,10 @@ def testrule(bot, trigger): | |
| assert items == [1] | ||
|
|
||
| # assert the rule is now rate limited | ||
| assert rule_hello.is_user_rate_limited(Identifier('Test')) | ||
| assert not rule_hello.is_channel_rate_limited('#channel') | ||
| assert not rule_hello.is_global_rate_limited() | ||
| at_time = datetime.now(timezone.utc) | ||
| assert rule_hello.is_user_rate_limited(Identifier('Test'), at_time) | ||
| assert not rule_hello.is_channel_rate_limited('#channel', at_time) | ||
| assert not rule_hello.is_global_rate_limited(at_time) | ||
|
|
||
| match, rule_trigger, wrapper = match_hello_rule(rule_hello) | ||
|
|
||
|
|
@@ -852,9 +904,10 @@ def testrule(bot, trigger): | |
| assert items == [1] | ||
|
|
||
| # assert the rule is now rate limited | ||
| assert not rule_hello.is_user_rate_limited(Identifier('Test')) | ||
| assert rule_hello.is_channel_rate_limited('#channel') | ||
| assert not rule_hello.is_global_rate_limited() | ||
| at_time = datetime.now(timezone.utc) | ||
| assert not rule_hello.is_user_rate_limited(Identifier('Test'), at_time) | ||
| assert rule_hello.is_channel_rate_limited('#channel', at_time) | ||
| assert not rule_hello.is_global_rate_limited(at_time) | ||
|
|
||
| match, rule_trigger, wrapper = match_hello_rule(rule_hello) | ||
|
|
||
|
|
@@ -897,9 +950,10 @@ def testrule(bot, trigger): | |
| assert items == [1] | ||
|
|
||
| # assert the rule is now rate limited | ||
| assert not rule_hello.is_user_rate_limited(Identifier('Test')) | ||
| assert rule_hello.is_channel_rate_limited('#channel') | ||
| assert not rule_hello.is_global_rate_limited() | ||
| at_time = datetime.now(timezone.utc) | ||
| assert not rule_hello.is_user_rate_limited(Identifier('Test'), at_time) | ||
| assert rule_hello.is_channel_rate_limited('#channel', at_time) | ||
| assert not rule_hello.is_global_rate_limited(at_time) | ||
|
|
||
| match, rule_trigger, wrapper = match_hello_rule(rule_hello) | ||
|
|
||
|
|
@@ -942,9 +996,10 @@ def testrule(bot, trigger): | |
| assert items == [1] | ||
|
|
||
| # assert the rule is now rate limited | ||
| assert not rule_hello.is_user_rate_limited(Identifier('Test')) | ||
| assert not rule_hello.is_channel_rate_limited('#channel') | ||
| assert rule_hello.is_global_rate_limited() | ||
| at_time = datetime.now(timezone.utc) | ||
| assert not rule_hello.is_user_rate_limited(Identifier('Test'), at_time) | ||
| assert not rule_hello.is_channel_rate_limited('#channel', at_time) | ||
| assert rule_hello.is_global_rate_limited(at_time) | ||
|
|
||
| match, rule_trigger, wrapper = match_hello_rule(rule_hello) | ||
|
|
||
|
|
@@ -987,9 +1042,10 @@ def testrule(bot, trigger): | |
| assert items == [1] | ||
|
|
||
| # assert the rule is now rate limited | ||
| assert not rule_hello.is_user_rate_limited(Identifier('Test')) | ||
| assert not rule_hello.is_channel_rate_limited('#channel') | ||
| assert rule_hello.is_global_rate_limited() | ||
| at_time = datetime.now(timezone.utc) | ||
| assert not rule_hello.is_user_rate_limited(Identifier('Test'), at_time) | ||
| assert not rule_hello.is_channel_rate_limited('#channel', at_time) | ||
| assert rule_hello.is_global_rate_limited(at_time) | ||
|
|
||
| match, rule_trigger, wrapper = match_hello_rule(rule_hello) | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.