Пример #1
0
 def setUp(self):
     self.helga = Helga(load=False)
Пример #2
0
class HelgaTestCase(TestCase):

    def setUp(self):
        self.helga = Helga(load=False)

    def test_join_channel(self):
        self.helga.join_channel('#all')

        assert '#all' in self.helga.channels

    def test_leave_channel(self):
        self.helga.join_channel('#all')
        self.helga.leave_channel('#all')

        assert '#all' not in self.helga.channels

    def test_set_topic(self):
        self.helga.set_topic('#all', 'everything is broken')

        assert self.helga.topics['#all'] == 'everything is broken'

    def test_nick(self):
        self.helga.client = Mock()
        self.helga.client.nickname = 'foo'

        assert self.helga.nick == 'foo'

    def test_nick_no_client(self):
        assert self.helga.nick == ''

    def test_get_current_nick_unknown_user(self):
        assert self.helga.get_current_nick('foo') == 'foo'

    def test_get_current_nick_is_current(self):
        self.helga.users = {'foo': ('foobar',)}

        assert self.helga.get_current_nick('foo') == 'foo'

    def test_get_current_nick_is_old_nick(self):
        self.helga.users = {'foo': ('foobar',)}

        assert self.helga.get_current_nick('foobar') == 'foo'

    def test_update_user_nick_adds_new_user(self):
        self.helga.update_user_nick(None, 'foo')

        assert self.helga.users['foo'] == set(['foo'])

    def test_update_user_nick_no_changes(self):
        user_set = {'foo': set(['foo'])}
        self.helga.users = user_set
        self.helga.update_user_nick('foo', 'foo')

        assert self.helga.users == user_set

    def test_update_user_nick_remaps(self):
        old = {'foobar': set(['foobar'])}
        new = {'foo': set(['foo', 'foobar'])}
        self.helga.users = old
        self.helga.update_user_nick('foobar', 'foo')

        assert self.helga.users == new

    def setup_handle_message(self, pre_dispatch_ret, dispatch_ret):
        self.helga.extensions = Mock()
        self.helga.extensions.dispatch.return_value = dispatch_ret

        self.helga.extensions.pre_dispatch.return_value = pre_dispatch_ret

        self.helga.client = Mock()

    def test_handle_message_does_nothing(self):
        self.setup_handle_message((None, 'baz'), None)
        self.helga.handle_message('foo', 'bar', 'baz', True)

        assert not self.helga.client.msg.called

    def test_handle_message_pre_dispatch_skips_extensions(self):
        self.setup_handle_message(('OK', 'baz'), None)
        self.helga.handle_message('foo', 'bar', 'baz', True)

        assert not self.helga.extensions.dispatch.called

    def test_handle_message_sends_client_message_to_correct_channel(self):
        # Public
        self.setup_handle_message(('OK', 'baz'), None)
        self.helga.handle_message('foo', 'bar', 'baz', True)
        self.helga.client.msg.assertCalledWith('bar', 'OK')

        # Private
        self.setup_handle_message(('OK', 'baz'), None)
        self.helga.handle_message('foo', 'bar', 'baz', False)
        self.helga.client.msg.assertCalledWith('foo', 'OK')

    def test_handle_message_formats_output(self):
        self.setup_handle_message(('OK: %(nick)s - %(botnick)s - %(channel)s', 'baz'), None)
        self.helga.client.nickname = 'helga'
        self.helga.handle_message('foo', 'bar', 'baz', True)

        self.helga.client.msg.assertCalledWith('bar', 'OK: foo - helga - bar')

    def test_handle_message_runs_extensions(self):
        self.setup_handle_message((None, 'baz'), 'EXT')
        self.helga.handle_message('foo', 'bar', 'baz', True)

        assert self.helga.extensions.dispatch.called
        self.helga.client.msg.assertCalledWith('bar', 'EXT')
Пример #3
0
import time

import smokesignal
from twisted.words.protocols import irc

from helga import settings
from helga.bot import Helga
from helga.log import setup_logger

logger = setup_logger(__name__)
helga = Helga()


class Message(object):
    """
    Just creates a dict of things that is passed around to helga's internals
    """
    def __init__(self, from_nick, channel, message, is_public):
        self.from_nick = from_nick
        self.on_channel = channel
        self.resp_channel = channel if is_public else from_nick
        self.is_public = is_public
        self.message = message
        self.response = []  # support multi-line responses

    def format_response(self, **kwargs):
        response = self.response
        resp_fmt = {
            'nick': self.from_nick,
            'channel': self.on_channel,
            'norm_channel': self.on_channel.replace('#', ''),
Пример #4
0
class HelgaTestCase(TestCase):
    def setUp(self):
        self.helga = Helga(load=False)

    def test_join_channel(self):
        self.helga.join_channel('#all')

        assert '#all' in self.helga.channels

    def test_leave_channel(self):
        self.helga.join_channel('#all')
        self.helga.leave_channel('#all')

        assert '#all' not in self.helga.channels

    def test_nick(self):
        self.helga.client = Mock()
        self.helga.client.nickname = 'foo'

        assert self.helga.nick == 'foo'

    def test_nick_no_client(self):
        assert self.helga.nick == ''

    def test_get_current_nick_unknown_user(self):
        assert self.helga.get_current_nick('foo') == 'foo'

    def test_get_current_nick_is_current(self):
        self.helga.users = {'foo': ('foobar', )}

        assert self.helga.get_current_nick('foo') == 'foo'

    def test_get_current_nick_is_old_nick(self):
        self.helga.users = {'foo': ('foobar', )}

        assert self.helga.get_current_nick('foobar') == 'foo'

    def test_update_user_nick_adds_new_user(self):
        self.helga.update_user_nick(None, 'foo')

        assert self.helga.users['foo'] == set(['foo'])

    def test_update_user_nick_no_changes(self):
        user_set = {'foo': set(['foo'])}
        self.helga.users = user_set
        self.helga.update_user_nick('foo', 'foo')

        assert self.helga.users == user_set

    def test_update_user_nick_remaps(self):
        old = {'foobar': set(['foobar'])}
        new = {'foo': set(['foo', 'foobar'])}
        self.helga.users = old
        self.helga.update_user_nick('foobar', 'foo')

        assert self.helga.users == new

    def setup_process(self, preprocess_ret, process_ret):
        self.helga.extensions = Mock()
        self.helga.client = Mock()

        def set_process_ret(message):
            message.response = process_ret

        def set_preprocess_ret(message):
            message.response = preprocess_ret

        self.helga.extensions.process.return_value = set_process_ret
        self.helga.extensions.preprocess.return_value = set_preprocess_ret

    def test_process_does_nothing(self):
        msg = Mock()
        msg.has_response = False

        self.setup_process(None, None)
        self.helga.process(msg)

        assert not self.helga.client.msg.called

    def test_process_preprocess_skips_extensions(self):
        msg = Mock()
        msg.has_response = True

        self.setup_process('OK', None)
        self.helga.process(msg)

        assert not self.helga.extensions.process.called

    def test_process_runs_extensions(self):
        msg = Mock()
        msg.response = None

        self.setup_process(None, None)

        def process(m):
            m.has_response = True
            m.response = 'foo'

        self.helga.extensions.process = process

        msg.format_response.return_value = 'foo'
        msg.resp_channel = '#bots'
        msg.has_repsonse = False

        self.helga.process(msg)

        self.helga.client.msg.assertCalledWith('#bots', 'foo')
Пример #5
0
 def setUp(self):
     self.helga = Helga(load=False)
Пример #6
0
class HelgaTestCase(TestCase):

    def setUp(self):
        self.helga = Helga(load=False)

    def test_join_channel(self):
        self.helga.join_channel('#all')

        assert '#all' in self.helga.channels

    def test_leave_channel(self):
        self.helga.join_channel('#all')
        self.helga.leave_channel('#all')

        assert '#all' not in self.helga.channels

    def test_nick(self):
        self.helga.client = Mock()
        self.helga.client.nickname = 'foo'

        assert self.helga.nick == 'foo'

    def test_nick_no_client(self):
        assert self.helga.nick == ''

    def test_get_current_nick_unknown_user(self):
        assert self.helga.get_current_nick('foo') == 'foo'

    def test_get_current_nick_is_current(self):
        self.helga.users = {'foo': ('foobar',)}

        assert self.helga.get_current_nick('foo') == 'foo'

    def test_get_current_nick_is_old_nick(self):
        self.helga.users = {'foo': ('foobar',)}

        assert self.helga.get_current_nick('foobar') == 'foo'

    def test_update_user_nick_adds_new_user(self):
        self.helga.update_user_nick(None, 'foo')

        assert self.helga.users['foo'] == set(['foo'])

    def test_update_user_nick_no_changes(self):
        user_set = {'foo': set(['foo'])}
        self.helga.users = user_set
        self.helga.update_user_nick('foo', 'foo')

        assert self.helga.users == user_set

    def test_update_user_nick_remaps(self):
        old = {'foobar': set(['foobar'])}
        new = {'foo': set(['foo', 'foobar'])}
        self.helga.users = old
        self.helga.update_user_nick('foobar', 'foo')

        assert self.helga.users == new

    def setup_process(self, preprocess_ret, process_ret):
        self.helga.extensions = Mock()
        self.helga.client = Mock()

        def set_process_ret(message):
            message.response = process_ret

        def set_preprocess_ret(message):
            message.response = preprocess_ret

        self.helga.extensions.process.return_value = set_process_ret
        self.helga.extensions.preprocess.return_value = set_preprocess_ret

    def test_process_does_nothing(self):
        msg = Mock()
        msg.has_response = False

        self.setup_process(None, None)
        self.helga.process(msg)

        assert not self.helga.client.msg.called

    def test_process_preprocess_skips_extensions(self):
        msg = Mock()
        msg.has_response = True

        self.setup_process('OK', None)
        self.helga.process(msg)

        assert not self.helga.extensions.process.called

    def test_process_runs_extensions(self):
        msg = Mock()
        msg.response = None

        self.setup_process(None, None)

        def process(m):
            m.has_response = True
            m.response = 'foo'

        self.helga.extensions.process = process

        msg.format_response.return_value = 'foo'
        msg.resp_channel = '#bots'
        msg.has_repsonse = False

        self.helga.process(msg)

        self.helga.client.msg.assertCalledWith('#bots', 'foo')