async def test_on_response(): # Tests on_response, _attach_message_log, and _get_author for IRC listener = IRCListener(None, 'chitanda', 'irc.freenode.fake') listener.nickname = 'chitanda' await on_response(Response(None, listener, '#chan', 'hello')) assert listener.message_log['#chan'].popleft() == '<chitanda> hello'
async def test_on_message_self_message(method): bot = Mock() bot.handle_message.return_value = coroutine(lambda **kwargs: None)() listener = IRCListener(bot, 'zad', 'irc.freenode.fake') listener.nickname = 'zad' await getattr(listener, method)(1, 'zad', 2) bot.handle_message.assert_not_called()
async def test_on_join(test_db): listener = IRCListener(None, "chitanda", "irc.freenode.fake") listener.nickname = "chitanda" listener.on_join = AsyncMock(return_value=True) await on_join(listener, "#channel", "chitanda") with database() as (conn, cursor): cursor.execute(""" SELECT 1 FROM irc_channels WHERE name = "#channel" AND server = "irc.freenode.fake" AND active = 1 """) assert cursor.fetchone()
async def test_on_join_other_user(test_db): listener = IRCListener(None, 'chitanda', 'irc.freenode.fake') listener.nickname = 'chitanda' listener.on_join = coroutine(lambda *args: True) await on_join(listener, '#channel', 'azul') with database() as (conn, cursor): cursor.execute( """ SELECT 1 FROM irc_channels WHERE name = "#channel" AND server = "irc.freenode.fake" AND active = 1 """ ) assert not cursor.fetchone()
async def test_on_part_other_user(test_db): listener = IRCListener(None, "chitanda", "irc.freenode.fake") listener.nickname = "chitanda" listener.on_part = AsyncMock(return_value=True) with database() as (conn, cursor): cursor.execute(""" INSERT INTO irc_channels (name, server) VALUES ("#channel", "irc.freenode.fake") """) conn.commit() await on_part(listener, "#channel", "azul", None) cursor.execute(""" SELECT 1 FROM irc_channels WHERE name = "#channel" AND server = "irc.freenode.fake" AND active = 1 """) assert cursor.fetchone()
async def test_is_admin_not_authenticated(monkeypatch): listener = IRCListener(None, 'chitanda', 'irc.freenode.fake') monkeypatch.setattr('chitanda.listeners.irc.config', {'admins': { str(listener): ['azul'] }}) with patch.object(listener, 'whois', return_value=Future()) as whois: whois.return_value.set_result({'identified': False, 'account': None}) assert not await listener.is_admin('azul')
async def test_on_part_other_user(test_db): listener = IRCListener(None, 'chitanda', 'irc.freenode.fake') listener.nickname = 'chitanda' listener.on_part = coroutine(lambda *args: True) with database() as (conn, cursor): cursor.execute( """ INSERT INTO irc_channels (name, server) VALUES ("#channel", "irc.freenode.fake") """ ) conn.commit() await on_part(listener, '#channel', 'azul', None) cursor.execute( """ SELECT 1 FROM irc_channels WHERE name = "#channel" AND server = "irc.freenode.fake" AND active = 1 """ ) assert cursor.fetchone()
def _connect_irc(self): for hostname, server in config['irc_servers'].items(): logger.info(f'Connecting to IRC server: {hostname}.') self.irc_listeners[hostname] = IRCListener(self, server['nickname'], hostname) asyncio.ensure_future(self.irc_listeners[hostname].connect( hostname, server['port'], tls=server['tls'], tls_verify=server['tls_verify'], ))
async def test_perform(monkeypatch): listener = IRCListener(None, 'a', 'irc.freenode.fake') monkeypatch.setattr( 'chitanda.listeners.irc.config', {'irc_servers': { listener.hostname: { 'perform': ['per1', 'per2'] } }}, ) with patch.object(listener, 'raw', return_value=Future()) as raw: raw.return_value.set_result(123) await listener._perform() assert raw.call_args_list == [call('per1\r\n'), call('per2\r\n')] assert listener.performed is True
def test_repr(): listener = IRCListener(None, 'a', 'irc.freenode.fake') assert '*****@*****.**' == repr(listener)