-
-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2629 from Exirel/fix-no-rate-limit
bot, rules: fix rate limiting rules without a rate limit
- Loading branch information
Showing
4 changed files
with
144 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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 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 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 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) | ||
|
||
|