def test_call_rule_rate_limited_user(mockbot): items = [] # setup def testrule(bot, trigger): bot.say('hi') items.append(1) return "Return Value" rule_hello = rules.Rule( [re.compile(r'(hi|hello|hey|sup)')], plugin='testplugin', label='testrule', handler=testrule, rate_limit=100, threaded=False, ) # trigger line = ':[email protected] PRIVMSG #channel :hello' pretrigger = trigger.PreTrigger(mockbot.nick, line) # match matches = list(rule_hello.match(mockbot, pretrigger)) match = matches[0] # trigger and wrapper rule_trigger = trigger.Trigger(mockbot.settings, pretrigger, match, account=None) wrapper = bot.SopelWrapper(mockbot, rule_trigger) # call rule mockbot.call_rule(rule_hello, wrapper, rule_trigger) # assert the rule has been executed assert mockbot.backend.message_sent == rawlist('PRIVMSG #channel :hi') assert items == [1] # assert the rule is now rate limited assert rule_hello.is_rate_limited(Identifier('Test')) assert not rule_hello.is_channel_rate_limited('#channel') assert not rule_hello.is_global_rate_limited() # call rule again mockbot.call_rule(rule_hello, wrapper, rule_trigger) # assert no new message assert mockbot.backend.message_sent == rawlist( 'PRIVMSG #channel :hi'), 'There must not be any new message sent' assert items == [1], 'There must not be any new item'
def __call__( self, mockbot: bot.Sopel, raw: str, pattern: Optional[str] = None, ) -> trigger.Trigger: match = re.match(pattern or r'.*', raw) if match is None: raise ValueError( 'Cannot create a Trigger without a matching pattern') url_schemes = mockbot.settings.core.auto_url_schemes pretrigger = trigger.PreTrigger( mockbot.nick, raw, url_schemes=url_schemes, identifier_factory=mockbot.make_identifier, ) return trigger.Trigger(mockbot.settings, pretrigger, match)
def test(configfactory, botfactory, ircfactory): test_config = TEST_CONFIG.format( name='NickName', admin=admin, owner=owner, ) settings = configfactory('default.cfg', test_config) url_schemes = settings.core.auto_url_schemes mockbot = botfactory(settings) server = ircfactory(mockbot) server.channel_joined('#Sopel') if not hasattr(tested_func, 'commands'): raise AssertionError('Function is not a command.') loader.clean_callable(tested_func, settings) test_rule = plugins.rules.Command.from_callable(settings, tested_func) parse_results = list(test_rule.parse(msg)) assert parse_results, "Example did not match any command." match = parse_results[0] sender = mockbot.nick if privmsg else "#channel" hostmask = "%s!%s@%s" % (mockbot.nick, "UserName", "example.com") # TODO enable message tags full_message = ':{} PRIVMSG {} :{}'.format(hostmask, sender, msg) pretrigger = trigger.PreTrigger( mockbot.nick, full_message, url_schemes=url_schemes) test_trigger = trigger.Trigger(mockbot.settings, pretrigger, match) pattern = re.compile(r'^%s: ' % re.escape(mockbot.nick)) # setup module module = sys.modules[tested_func.__module__] if hasattr(module, 'setup'): module.setup(mockbot) def isnt_ignored(value): """Return True if value doesn't match any re in ignore list.""" return not any( re.match(ignored_line, value) for ignored_line in ignore) expected_output_count = 0 for _i in range(repeat): expected_output_count += len(results) wrapper = bot.SopelWrapper(mockbot, test_trigger) tested_func(wrapper, test_trigger) output_triggers = ( trigger.PreTrigger( mockbot.nick, message.decode('utf-8'), url_schemes=url_schemes, ) for message in wrapper.backend.message_sent ) output_texts = ( # subtract "Sopel: " when necessary pattern.sub('', output_trigger.args[-1]) for output_trigger in output_triggers ) outputs = [text for text in output_texts if isnt_ignored(text)] # output length assert len(outputs) == expected_output_count # output content for expected, output in zip(results, outputs): if use_regexp: message = ( "Output does not match the regex:\n" "Pattern: %s\n" "Output: %s" ) % (expected, output) if not re.match(expected, output): raise AssertionError(message) else: assert expected == output
def __call__(self, mockbot, raw, pattern=None): url_schemes = mockbot.settings.core.auto_url_schemes return trigger.Trigger( mockbot.settings, trigger.PreTrigger(mockbot.nick, raw, url_schemes=url_schemes), re.match(pattern or r'.*', raw))
def __call__(self, mockbot, raw, pattern=None): return trigger.Trigger(mockbot.settings, trigger.PreTrigger(mockbot.nick, raw), re.match(pattern or r'.*', raw))
def line(sopel, raw): return trigger.Trigger(sopel.settings, trigger.PreTrigger(sopel.nick, raw), re.match('.*', raw))