Ejemplo n.º 1
0
def test_wrapper_notice_override_destination(mockbot):
    message = line(mockbot,
                   ':[email protected] PRIVMSG #channel :test message')
    wrapper = bot.SopelWrapper(mockbot, message)
    wrapper.notice('Hi!', destination='#different')

    assert mockbot.backend.message_sent == rawlist('NOTICE #different :Hi!')
Ejemplo n.º 2
0
def test_wrapper_notice(mockbot):
    message = line(mockbot,
                   ':[email protected] PRIVMSG #channel :test message')
    wrapper = bot.SopelWrapper(mockbot, message)
    wrapper.notice('Hi!')

    assert mockbot.backend.message_sent == rawlist('NOTICE #channel :Hi!')
Ejemplo n.º 3
0
def test_wrapper_kick_override_destination(mockbot):
    message = line(mockbot,
                   ':[email protected] PRIVMSG #channel :test message')
    wrapper = bot.SopelWrapper(mockbot, message)
    wrapper.kick('SpamUser', channel='#another')

    assert mockbot.backend.message_sent == rawlist('KICK #another SpamUser')
Ejemplo n.º 4
0
def test_wrapper_kick(mockbot):
    message = line(mockbot,
                   ':[email protected] PRIVMSG #channel :test message')
    wrapper = bot.SopelWrapper(mockbot, message)
    wrapper.kick('SpamUser')

    assert mockbot.backend.message_sent == rawlist('KICK #channel SpamUser')
Ejemplo n.º 5
0
 def wrapper(
     self,
     mockbot: bot.Sopel,
     raw: str,
     pattern: Optional[str] = None,
 ) -> bot.SopelWrapper:
     trigger = self(mockbot, raw, pattern=pattern)
     return bot.SopelWrapper(mockbot, trigger)
Ejemplo n.º 6
0
def test_wrapper_action_override_destination(mockbot):
    message = line(mockbot,
                   ':[email protected] PRIVMSG #channel :test message')
    wrapper = bot.SopelWrapper(mockbot, message)
    wrapper.action('Hi!', destination='#different')

    assert mockbot.backend.message_sent == rawlist(
        'PRIVMSG #different :\x01ACTION Hi!\x01')
Ejemplo n.º 7
0
def test_wrapper_kick_error_channel(mockbot):
    message = line(mockbot,
                   ':[email protected] PRIVMSG OtherUser :test message')
    wrapper = bot.SopelWrapper(mockbot, message)
    with pytest.raises(RuntimeError):
        wrapper.kick('SpamUser')

    assert mockbot.backend.message_sent == []
Ejemplo n.º 8
0
def test_wrapper_reply_override_destination_reply_to(mockbot):
    message = line(mockbot,
                   ':[email protected] PRIVMSG #channel :test message')
    wrapper = bot.SopelWrapper(mockbot, message)
    wrapper.reply('Hi!', destination='#another', reply_to='Admin')

    assert mockbot.backend.message_sent == rawlist(
        'PRIVMSG #another :Admin: Hi!')
Ejemplo n.º 9
0
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'
Ejemplo n.º 10
0
    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
Ejemplo n.º 11
0
 def wrapper(self, mockbot, raw, pattern=None):
     trigger = self(mockbot, raw, pattern=pattern)
     return bot.SopelWrapper(mockbot, trigger)