def test_meetme(self): bot, plugin = self.callFTU(level=1000) plugin.connect() event = Event(event='MeetmeJoin', meetme='4201', usernum='1', calleridname='gawel', calleridnum='4242') plugin.handle_meetme(event, MagicMock()) event.update(usernum='2', calleridname='lukhas') plugin.handle_meetme(event, MagicMock()) event.update(usernum='4', calleridname='external call 0699999999') plugin.handle_meetme(event, MagicMock()) event.update(usernum='3', calleridname='gawel', event='MeetmeLeave') plugin.handle_meetme(event, MagicMock()) self.assertSent([ ('PRIVMSG #asterirc ' ':INFO gawel has join room 4201 (total in this room: 1)'), ('PRIVMSG #asterirc ' ':INFO lukhas has join room 4201 (total in this room: 2)'), ('PRIVMSG #asterirc ' ':INFO external call 069999 has join room 4201 ' '(total in this room: 3)'), ('PRIVMSG #asterirc ' ':INFO gawel has leave room 4201 (total in this room: 2)'), ]) bot.dispatch(':gawel!user@host PRIVMSG nono :!room list') self.assertSent( ['PRIVMSG gawel :Room 4201 (2): external call 069999, lukhas']) bot.dispatch(':gawel!user@host PRIVMSG nono :!room list 4201') self.assertSent( ['PRIVMSG gawel :Room 4201 (2): external call 069999, lukhas']) bot.dispatch(':gawel!user@host PRIVMSG nono :!room kick 4201 unknown') self.assertSent(['PRIVMSG gawel :No user matching query']) bot.dispatch(':gawel!user@host PRIVMSG nono :!room kick 4201 lu') self.assertSent(['PRIVMSG gawel :lukhas kicked from 4201.']) bot.dispatch(':gawel!user@host PRIVMSG nono :!room kick 4201 06') self.assertSent( ['PRIVMSG gawel :external call 069999 kicked from 4201.']) bot.dispatch(':gawel!user@host PRIVMSG nono :!room list') self.assertSent(['PRIVMSG gawel :Nobody here.']) event.update(event='MeetmeEnd') plugin.handle_meetme(event, MagicMock()) self.assertSent([ ('PRIVMSG #asterirc ' ':INFO room 4201 is closed.'), ])
def test_shutdown(self): bot, plugin = self.callFTU() plugin.connect() bot.notify('SIGINT') self.assertTrue(self.mocks['close'].called) event = MagicMock(manager=plugin.manager) plugin.handle_event(event, MagicMock()) plugin.handle_shutdown(event, MagicMock())
def callTFTU(self, **kwargs): kwargs = dict(dict(sizeWhenDone=200, leftUntilDone=100), **kwargs) if 'name' not in kwargs: kwargs['name'] = 'awesome torrent {id}'.format(**kwargs) if kwargs['leftUntilDone'] != 0: kwargs['status'] = 4 else: kwargs['status'] = 6 cli = MagicMock() cli.rpc_version = 15 return Torrent(cli, kwargs)
def test_add_cron(self): bot = self.callFTU(includes=['irc3.plugins.cron']) plugin = bot.get_plugin(cron.Crons) callback = MagicMock() bot.add_cron('* * * * *', callback) self.assertEqual(len(plugin), 1, plugin) self.assertFalse(callback.called) plugin.started = True bot.loop = MagicMock() bot.add_cron('* * * * *', callback) self.assertEqual(len(plugin), 2, plugin) self.assertTrue(bot.loop.call_at.called)
def test_fifo_fake_event_loop(self): bot = self.callFTU() plugin = bot.get_plugin(Fifo) plugin.loop = MagicMock() bot.test(':irc3!user@host JOIN #channel') channel_fd = plugin.fifos["#channel"] with open("/tmp/run/irc3/channel", "wb", 0) as f: f.write(b"test1\n") plugin.watch_fd(channel_fd, "#chanel") self.assertSent(['PRIVMSG #chanel :test1']) f.write(b"test2\r\n") plugin.watch_fd(channel_fd, "#chanel") self.assertSent(['PRIVMSG #chanel :test2']) f.write(b"test3\r\ntest4\n") plugin.watch_fd(channel_fd, "#chanel") self.assertSent( ['PRIVMSG #chanel :test3', 'PRIVMSG #chanel :test4']) for char in b"test5\n": f.write(bytes([char])) plugin.watch_fd(channel_fd, "#chanel") self.assertSent(['PRIVMSG #chanel :test5'])
def test_unignoreme(self): mock = MagicMock(name='MockGetUser') del self.config['loop'] self.callFTU() self.bot.get_user = mock self.bot.dispatch(':bar!id@host PRIVMSG #chan :!unignoreme') mock().set_setting.assert_called_with('nocompare', False) self.assertSent(['PRIVMSG #chan :Ok, enabled compare for bar'])
def test_get_local_from_database(self): mock = MagicMock() # mock get_setting @asyncio.coroutine def mock_get_setting(setting, default): assert setting == 'weatheruser' assert default == 'nick' return 'lastfmuser' mock.get_setting = mock_get_setting # we need to have a mock return the mocked user with get_setting mock_user = MagicMock(name='MockGetUser', return_value=mock) self.callFTU() self.bot.get_user = mock_user weather = self.bot.get_plugin('onebot.plugins.weather.WeatherPlugin') def wrap(): lastfmnick = yield from weather.get_local('nick') assert lastfmnick == 'lastfmuser' self.bot.loop.run_until_complete(wrap())
def test_setuser(self): mock = MagicMock(name='MockGetUser') del self.config['loop'] self.callFTU() self.bot.get_user = mock self.bot.dispatch(':bar!id@host PRIVMSG #chan :!setuser foo') mock().set_setting.assert_called_with('lastfmuser', 'foo') self.assertSent( ['PRIVMSG #chan :Ok, so you are ' 'https://last.fm/user/foo'])
def test_get_lastfm_nick_from_database(self): mock = MagicMock() # mock get_setting @asyncio.coroutine def mock_get_setting(setting, default): assert setting == 'lastfmuser' assert default == 'nick' return 'lastfmuser' mock.get_setting = mock_get_setting # we need to have a mock return the mocked user with get_setting mock_user = MagicMock(name='MockGetUser', return_value=mock) self.callFTU() self.bot.get_user = mock_user lastfm = self.bot.get_plugin('onebot.plugins.lastfm.LastfmPlugin') def wrap(): lastfmnick = yield from lastfm.get_lastfm_nick('nick') assert lastfmnick == 'lastfmuser' self.bot.loop.run_until_complete(wrap())
def test_add_remove_cron(self): bot = self.callFTU() plugin = bot.get_plugin(cron.Crons) plugin.connection_made() callback = MagicMock() c = bot.add_cron('* * * * *', callback) assert c.handle is not None assert len(plugin) == 1 assert not callback.called plugin.remove_cron(c) assert len(plugin) == 0
def test_ignoreme(self): mock = MagicMock(name='MockGetUser') del self.config['loop'] self.callFTU() self.bot.get_user = mock self.bot.dispatch(':bar!id@host PRIVMSG #chan :!ignoreme') mock().set_setting.assert_called_with('nocompare', True) self.assertSent([ 'PRIVMSG #chan :I will leave out bar from compare. ' 'Re-enable compare by using the unignoreme command' ])
def test_cron_raise(self): bot = self.callFTU() bot.loop = MagicMock() bot.include(__name__) plugin = bot.get_plugin(cron.Crons) plugin.log.setLevel(1000) plugin.connection_made() self.assertEqual(len(plugin), 1, plugin) self.assertTrue(bot.loop.call_at.call_count, 1) bot.loop.reset_mock() plugin.call_cron(plugin[0]) self.assertTrue(bot.loop.call_at.call_count, 1)
def test_cron(self): bot = self.callFTU() bot.loop = MagicMock() bot.include('mycrons') plugin = bot.get_plugin(cron.Crons) plugin.log.setLevel(1000) plugin.connection_made() self.assertEqual(len(plugin), 2, plugin) self.assertTrue(bot.loop.call_at.call_count, 2) bot.loop.reset_mock() plugin.call_cron(plugin[1]) self.assertTrue(bot.loop.call_at.call_count, 1) self.assertIn('*/2', str(plugin[0]))
def test_connection_made(self): bot = self.callFTU() bot.loop.call_later = MagicMock() bot.notify('connection_made') self.assertTrue(bot.loop.call_later.called)
def test_exception(self): bot = self.callFTU() bot.feeds.fetch = bot.feeds.parse = MagicMock(side_effect=KeyError()) bot.feeds.update()
def callFTU(self): conn = irc3.IrcConnection() conn.encoding = 'utf8' conn.factory = MagicMock() conn.connection_made(MagicMock()) return conn
fixture['track']['date']['#text'] = date.strftime('%D %b %Y, %h:%M') else: fixture['track'][0]['date']['uts'] = str( calendar.timegm(date.timetuple())) fixture['track'][0]['date']['#text'] = date.strftime('%D %b %Y, %h:%M') return fixture @asyncio.coroutine def get_lastfm_nick_mock(nick): yield from asyncio.sleep(0.001) return nick @freeze_time("2014-01-01") @patch('onebot.plugins.users.UsersPlugin', new=MagicMock()) class LastfmPluginTest(BotTestCase): """Test the LastFM plugin""" config = { 'includes': ['onebot.plugins.lastfm'], 'onebot.plugins.lastfm': { 'api_key': '', 'api_secret': '' }, 'onebot.plugins.users': { 'identified_by': 'mask' }, 'irc3.plugins.command': { 'antiflood': False },
def irc_conn(request): irc_conn = irc3.IrcConnection() irc_conn.encoding = 'utf8' irc_conn.factory = MagicMock() irc_conn.connection_made(MagicMock()) return irc_conn
def test_autojoin_delay(self): bot = self.callFTU(autojoins=['#foo'], autojoin_delay=3) bot.loop.call_later = MagicMock() bot.notify('connection_made') bot.dispatch(':hobana.freenode.net 422 irc3 :No MOTD.') self.assertTrue(bot.loop.call_later.called)