async def test_unsubscribe(self): r = roost.Roost(mocks.Context()) r.subunify = True w = Mock() w.read_string.return_value = mocks.promise('a') r.r.unsubscribe = Mock(return_value=mocks.promise()) await r.unsubscribe(w) r.r.unsubscribe.assert_called_with([ ('a', '*', ''), ('a.d', '*', ''), ('a.d.d', '*', ''), ('a.d.d.d', '*', ''), ('una', '*', ''), ('una.d', '*', ''), ('una.d.d', '*', ''), ('una.d.d.d', '*', ''), ('ununa', '*', ''), ('ununa.d', '*', ''), ('ununa.d.d', '*', ''), ('ununa.d.d.d', '*', ''), ('unununa', '*', ''), ('unununa.d', '*', ''), ('unununa.d.d', '*', ''), ('unununa.d.d.d', '*', ''), ])
async def test_include(self): i = irccloud.IRCCloud(None) i.context = Mock() i.context.ui = Mock() i.context.ui.redisplay = Mock() i._get = Mock(return_value=mocks.promise({ 'success': False, 'message': 'ouch', })) with self.assertLogs(): await i.include('http://foo/') i._get.assert_called_with('http://foo/') i._get = Mock(return_value=mocks.promise([{ 'type': 'buffer_msg', 'cid': 2, 'eid': 2, 'from': 'user', 'msg': 'message body', }])) i.drop_cache = Mock() i.redisplay = Mock() await i.include('http://foo/') self.assertEqual(1, len(i.messages)) i.drop_cache.assert_called() i.redisplay.assert_called_with(i.messages[0], i.messages[0])
async def test_unicode_insert(self): e = snipe.editor.Editor(None) e.read_string = Mock(return_value=mocks.promise( 'LATIN CAPITAL LETTER A')) await e.insert_unicode() self.assertEqual('A', str(e.buf)) e.read_string.return_value = mocks.promise('42') await e.insert_unicode() self.assertEqual('AB', str(e.buf))
async def test_reconnect(self): i = irccloud.IRCCloud(None) i.disconnect = Mock(return_value=mocks.promise()) i.new_task = True i.start = Mock(return_value=mocks.promise()) await i.reconnect() i.disconnect.assert_called() i.start.assert_called()
async def test_subscribe_fie(self): r = roost.Roost(mocks.Context()) w = Mock() w.read_filename.return_value = mocks.promise('filename') r.load_subs = Mock(return_value=mocks.promise()) with patch('os.path.exists', return_value=False): await r.subscribe_file(w) r.load_subs.assert_called_with('filename')
async def test_reconnect(self): r = roost.Roost(mocks.Context()) r.new_task = Mock() r.new_task.is_done.return_value = False r.disconnect = Mock(return_value=mocks.promise()) r.start = Mock(return_value=mocks.promise()) await r.reconnect() r.disconnect.assert_called() r.start.assert_called()
async def test_react_add_remove(self): s = slack.Slack(None, name='test') m = slack.SlackMessage(s, {'type': 'message', 'channel': 'foo'}) o = object() m.react = Mock(return_value=mocks.promise()) await m.add_reaction(o) m.react.assert_called_with(o, 'reactions.add') m.react = Mock(return_value=mocks.promise()) await m.remove_reaction(o) m.react.assert_called_with(o, 'reactions.remove')
async def test_backoff(self): with patch('snipe.imbroglio.sleep') as sleep: sleep.return_value = mocks.promise(None) self.assertEqual(.5, (await slack.Slack.backoff(0))) sleep.assert_not_called() self.assertEqual(1.0, (await slack.Slack.backoff(.5))) sleep.assert_called_with(.5)
async def test_send(self): i = irccloud.IRCCloud(None) with self.assertRaises(util.SnipeException) as ar: await i.send('', '') self.assertEqual('nowhere to send the message', str(ar.exception)) with self.assertRaises(util.SnipeException) as ar: await i.send('host nick', '') self.assertEqual('unknown server name host', str(ar.exception)) i.connections = { 0: { 'hostname': 'host.domain0', 'cid': 0, }, 1: { 'hostname': 'host.domain1', 'cid': 1, }, } with self.assertRaises(util.SnipeException) as ar: await i.send('host nick', '') self.assertEqual( "ambiguous server name host matches " "('host.domain0', 'host.domain1')", str(ar.exception)) i.connections = { 0: { 'hostname': 'host.domain', 'cid': 0, }, } i.say = Mock(return_value=mocks.promise()) await i.send('host nick', 'body') i.say.assert_called_with(0, '*', '/msg nick body') i.say = Mock(return_value=mocks.promise()) await i.send('host', 'body') i.say.assert_called_with(0, '*', '/msg * body') i.say = Mock(return_value=mocks.promise()) with self.assertRaises(RuntimeError): await i.send('host nick', 'body\nbody')
async def test_new_message(self): r = roost.Roost(mocks.Context()) o = object() r.construct_and_maybe_decrypt = Mock(return_value=mocks.promise(o)) r.add_message = Mock() await r.new_message({}) r.construct_and_maybe_decrypt.assert_called_with({}) r.add_message.assert_called_with(o)
async def test_method(self): s = slack.Slack(None, name='test') s.token = 'TOKEN' s._post = Mock(return_value=mocks.promise('foo')) self.assertEqual( 'foo', (await s.method('method'))) s._post.assert_called_with('method', token='TOKEN')
async def test_incoming(self): i = irccloud.IRCCloud(None) # this seems to cause a RunTimeError to the effect that the # mock is never awaited, except coverage says that the # exception is definitely propagated appropriately i.process_message = Mock(return_value=mocks.promise( exception=KeyError)) with self.assertLogs(): await i.incoming({}) o = object() i.process_message = Mock(return_value=mocks.promise(o)) i.drop_cache = Mock() i.redisplay = Mock() await i.incoming(o) i.process_message.assert_called_with([], o) i.drop_cache.assert_called() i.redisplay.assert_called_with(o, o)
async def test_edit_message(self): s = slack.Slack(None, name='test') m = slack.SlackMessage( s, {'type': 'message', 'channel': 'foo', 'ts': 0.0}) window = Mock() window.cursor = None window.read_string.return_value = mocks.promise('foo') with self.assertRaises(Exception): await m.edit_message(window) s.method = Mock(return_value=mocks.promise({'ok': True})) window.read_string.return_value = mocks.promise('\nfoo') await m.edit_message(window) window.read_string.assert_called() s.method.assert_called_with( 'chat.update', channel='foo', ts=0.0, text='foo')
async def test_react(self): s = slack.Slack(None, name='test') s.emoji = {} m = slack.SlackMessage( s, {'type': 'message', 'channel': 'foo', 'ts': 0.0}) window = Mock() window.read_oneof.return_value = mocks.promise('foo') await m.react(window, 'reactions.add') window.read_oneof.assert_called() m.data['reactions'] = [{'name': 'star'}] window.read_oneof.return_value = mocks.promise('star') s.method = Mock(return_value=mocks.promise({'ok': True})) await m.react(window, 'reactions.add') self.assertEqual(['star'], s.used_emoji) s.method.assert_called_with( 'reactions.add', name='star', channel='foo', timestamp=0.0)
async def test_dump_subscriptions(self): r = roost.Roost(mocks.Context()) r.r.subscriptions = Mock(return_value=mocks.promise([{ 'class': 'class', 'instance': 'instance', 'recipient': '', }])) w = Mock() await r.dump_subscriptions(w) w.show.assert_called_with('class instance *')
async def test_say(self): i = irccloud.IRCCloud(None) i.websocket = Mock() i.websocket.write.return_value = mocks.promise() x = i.reqid await i.say('x', 'y', 'z') i.websocket.write.assert_called_with({ '_method': 'say', '_reqid': x + 1, 'cid': 'x', 'to': 'y', 'msg': 'z', })
async def test_start(self): c = context.Context() ui = Mock() ui.get_erasechar.return_value = chr(8) c.backends = Mock() c.backends.start.return_value = mocks.promise(None) with patch('snipe.util.Configurable') as Configurable: await c.start(ui) self.assertIs(c.ui, ui) self.assertIs(ui.context, c) self.assertIs(c.erasechar, chr(8)) Configurable.immanentize.assert_called() ui.initial.assert_called() c.backends.start.assert_called()
async def test_disconnect(self): i = irccloud.IRCCloud(None) i.reap_tasks = Mock() i.shutdown = Mock() await i.disconnect() i.reap_tasks.assert_called() i.shutdown.assert_not_called() i.reap_tasks = Mock() i.shutdown = Mock(return_value=mocks.promise()) i.new_task = True await i.disconnect() i.reap_tasks.assert_called() i.shutdown.assert_called() self.assertIsNone(i.new_task)
async def test_load_subs(self): r = roost.Roost(mocks.Context()) r.r.subscribe = Mock(return_value=mocks.promise()) with patch('os.path.exists', return_value=False), \ patch('snipe.roost.open', return_value=io.StringIO( 'class,instance,*\n')) as o: await r.load_subs('foo') o.assert_not_called() with patch('os.path.exists', return_value=True), \ patch('snipe.roost.open', return_value=io.StringIO( 'class,instance,@ATHENA.MIT.EDU\n')): await r.load_subs('foo') r.r.subscribe.assert_called_with([['class', 'instance', '*']])
async def test_process_message_misc(self): s = slack.Slack(None, name='test') l = [] self.assertIsNone( await s.process_message(l, { 'type': list(s.IGNORED_TYPE_PREFIXES)[0] + 'foo'})) self.assertIsNone( await s.process_message(l, { 'type': list(s.IGNORED_TYPES)[0]})) self.assertIsNone( await s.process_message(l, { 'type': 'team_migration_started'})) with self.assertRaises(slack.SlackReconnectException): await s.process_message(s.messages, { 'type': 'team_migration_started'}) s.emoji_update = Mock(return_value=mocks.promise()) self.assertIsNone( await s.process_message(l, { 'type': 'emoji_changed'})) s.emoji_update.assert_called()
async def test_insert_file(self): e = snipe.editor.Editor(None) e.read_filename = Mock(return_value=mocks.promise(__file__)) await e.insert_file() self.assertEqual( '# -*- encoding: utf-8 -*-', str(e.buf).splitlines()[0])
async def test_send(self): r = roost.Roost(mocks.Context()) r.r.send = Mock(return_value=mocks.promise()) await r.send('-c class -i instance -s sig -O opcode', 'body') self.assertEqual( r.r.send.call_args[0][0], { 'class': 'class', 'instance': 'instance', 'recipient': '', 'opcode': 'opcode', 'signature': 'sig', 'message': 'body', }) r.r.send = Mock(return_value=mocks.promise()) await r.send('-c class -R -s sig recipient', 'body') self.assertEqual( r.r.send.call_args[0][0], { 'class': 'class', 'instance': 'PERSONAL', 'recipient': 'recipient', 'opcode': '', 'signature': 'sig', 'message': 'obql', }) r.r.send = Mock(return_value=mocks.promise()) with self.assertRaises(RuntimeError): await r.send('-c class -s sig -C r1 r2', 'body') self.assertEqual( r.r.send.call_args[0][0], { 'class': 'class', 'instance': 'PERSONAL', 'recipient': 'r2', 'opcode': '', 'signature': 'sig', 'message': 'CC: r1 r2\nbody', }) with patch( 'snipe.imbroglio.process_filter', return_value=mocks.promise((0, 'foo'))): r.r.send = Mock(return_value=mocks.promise()) await r.send('-c class -x -s sig recipient', 'body') self.assertEqual( { 'class': 'class', 'instance': 'PERSONAL', 'recipient': 'recipient', 'opcode': 'crypt', 'signature': 'sig', 'message': 'foo', }, r.r.send.call_args[0][0]) with patch( 'snipe.imbroglio.process_filter', return_value=mocks.promise((1, 'foo'))), \ self.assertRaises(Exception): r.r.send = Mock(return_value=mocks.promise()) await r.send('-c class -x -s sig recipient', 'body')
async def test_emoji_update(self): s = slack.Slack(None, name='test') s.method = Mock(return_value=mocks.promise({'foo': 'bar'})) await s.emoji_update() s.method.assert_called_with('emoji.list') self.assertEqual(s.emoji, {'foo': 'bar'})
async def test_process_message(self): i = irccloud.IRCCloud(None) l = [] self.assertIsNone(await i.process_message(l, None)) i.last_eid = -1 i.buffers[1] = {} for mtype in ( 'idle', 'banned', 'socket_closed', 'num_invites', 'stat_user', 'backlog_starts', 'end_of_backlog', 'you_joined_channel', 'self_details', 'your_unique_id', 'cap_ls', 'cap_req', 'cap_ack', 'user_account', 'heartbeat_echo', ): ml = [] self.assertIsNone(await i.process_message(ml, { 'type': mtype, 'eid': 0, })) self.assertFalse(ml) d = {} self.assertIsNone(await i.process_message([], { 'type': 'header', 'eid': 0, 'header': d, })) self.assertIs(d, i.header.get('header')) i.include = Mock(return_value=mocks.promise()) self.assertIsNone(await i.process_message( [], { 'type': 'oob_include', 'eid': 0, 'url': 'http://foo/', })) i.include.assert_called_with('http://foo/') self.assertIsNone(i.message_set) self.assertIsNone(await i.process_message([], { 'type': 'oob_skipped', 'eid': 0, })) self.assertEqual(set(), i.message_set) self.assertIsNone(await i.process_message([], { 'type': 'backlog_complete', 'eid': 0, })) self.assertIsNone(i.message_set) self.assertIsNone(await i.process_message( [], { 'type': 'makeserver', 'eid': 0, 'cid': 0, 'foo': 'bar', })) self.assertEqual('bar', i.connections[0]['foo']) self.assertIsNone(await i.process_message( [], { 'type': 'status_changed', 'eid': 0, 'cid': 0, 'new_status': 'foo', 'fail_info': 'bar', })) self.assertEqual('foo', i.connections[0]['status']) self.assertEqual('bar', i.connections[0]['fail_info']) self.assertIsNone(await i.process_message( [], { 'type': 'isupport_params', 'eid': 0, 'cid': 0, 'foo': 'bar', })) self.assertEqual('bar', i.servers[0]['foo']) self.assertIsNone(await i.process_message( [], { 'type': 'makebuffer', 'eid': 0, 'bid': 0, 'foo': 'bar', })) self.assertEqual('bar', i.buffers[0]['foo']) self.assertIsNone(await i.process_message( [], { 'type': 'channel_init', 'eid': 0, 'bid': 0, 'foo': 'bar', })) self.assertEqual('bar', i.channels[0]['foo']) self.assertIsNotNone(await i.process_message( l, { 'type': 'buffer_msg', 'bid': 1, 'cid': 2, 'eid': 2, 'from': 'user', 'msg': 'message body', })) i.since_id = 2 self.assertIsNone(await i.process_message( l, { 'type': 'buffer_msg', 'bid': 1, 'cid': 2, 'eid': 1, 'from': 'user', 'msg': 'message body', })) i.message_set = set(float(m) for m in l) self.assertIsNone(await i.process_message( l, { 'type': 'buffer_msg', 'bid': 1, 'cid': 2, 'eid': 2, 'from': 'user', 'msg': 'message body', })) i.since_id = 0 self.assertIsNotNone(await i.process_message( l, { 'type': 'buffer_msg', 'bid': 1, 'cid': 2, 'eid': 1, 'from': 'user', 'msg': 'message body', })) self.assertEqual(2, len(l)) self.assertEqual(1, l[0].data['eid']) self.assertEqual(2, l[1].data['eid'])
async def test_construct_and_maybe_decrypt(self): r = roost.Roost(mocks.Context()) self.assertEqual(0, len(r._destinations)) self.assertEqual(0, len(r._senders)) m = await r.construct_and_maybe_decrypt({ 'message': 'body', 'receiveTime': 0.0, 'sender': 'sender', 'class': 'class', 'instance': 'instance', 'recipient': '', 'opcode': '', 'signature': 'sig', 'time': 0.0, }) self.assertEqual('body', m.body) self.assertEqual(2, len(r._destinations)) self.assertEqual(1, len(r._senders)) with patch( 'snipe.imbroglio.process_filter', return_value=mocks.promise((0, 'foo\n**END**\n'))): m = await r.construct_and_maybe_decrypt({ 'message': 'body', 'receiveTime': 0.0, 'sender': 'sender', 'class': 'class', 'instance': 'instance', 'recipient': '', 'opcode': 'crypt', 'signature': 'sig', 'time': 0.0, }) self.assertEqual('zcrypt', m.transformed) self.assertEqual('foo\n', m.body) with patch( 'snipe.imbroglio.process_filter', return_value=mocks.promise((1, 'foo\n**END**\n'))), \ self.assertLogs() as l: m = await r.construct_and_maybe_decrypt({ 'message': 'body', 'receiveTime': 0.0, 'sender': 'sender', 'class': 'class', 'instance': 'instance', 'recipient': '', 'opcode': 'crypt', 'signature': 'sig', 'time': 0.0, }) (m,) = l.output self.assertRegex( m, r'ERROR:Roost.[0-9a-f]+:roost: zcrypt -D -c class returned 1') with patch( 'snipe.imbroglio.process_filter', return_value=mocks.promise(exception=Exception)), \ self.assertLogs() as l: m = await r.construct_and_maybe_decrypt({ 'message': 'body', 'receiveTime': 0.0, 'sender': 'sender', 'class': 'class', 'instance': 'instance', 'recipient': '', 'opcode': 'crypt', 'signature': 'sig', 'time': 0.0, }) (m,) = l.output m = m.splitlines()[0] self.assertRegex( m, 'ERROR:Roost.[0-9a-f]+:zcrypt, decrypting')