def test_kick_not_empty_no_skip_paused(self): bot = common.make_bot() jobs = {} for i in range(0, 10): job = mock.MagicMock() if i == 0: job.next_run_time = self.ZERO_DT else: if i % 2 == 0: job.next_run_time = self.IN_ONE_HOUR_DT else: job.next_run_time = None job.id = str(i) jobs[job.id] = job bot.scheduler.get_jobs.return_value = list(jobs.values()) bot.scheduler.submitted_jobs = {} bot.scheduler.submitted_jobs["1"] = self.ZERO_DT m = common.make_message(text="periodics run all", to_me=True, user_id="me") h = schedule.RunAllHandler(bot, m) h.run(handler.HandlerMatch("skip_paused=false")) m.reply_text.assert_called_with("Kicked 10 jobs and skipped 0 jobs.", prefixed=False, threaded=True)
def test_watcher(self): bot = common.make_bot() client = mock.MagicMock() bot.clients.gerrit_mqtt_client = client watcher = gerrit.Watcher(bot) watcher.run() client.run.assert_called_once()
def setUp(self): super(StatusWatcherTest, self).setUp() self.bot = common.make_bot() self.bot.started_at = datetime.now() self.hook = status.StatusApplication(self.bot) self.slack_client = mock.MagicMock() self.maxDiff = None
def test_add_alias(self): bot = common.make_bot(simple_config=True) m = common.make_message(text="alias add b c", to_me=True, user_id="me") h = alias.AddHandler(bot, m) h.run(handler.HandlerMatch("b c")) self.assertEqual(bot.brain.storage, {'user:me': {'aliases': {'c': 'b'}}})
def test_expected_handled(self): bot = common.make_bot(simple_config=True) m = common.make_message(text="brain dump bob", to_me=True) self.assertTrue(brain.DumpHandler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="brain no list", to_me=True) self.assertFalse(brain.DumpHandler.handles(m, c.TARGETED, bot.config))
def test_message_from_channels(self): bot = common.make_bot() message = mock.MagicMock() message.body = munch.Munch() message.body.channel_name = 'my_channel' message.body.channel_id = 'my_channel' a = auth.message_from_channels(['my_channel']) a(bot, message)
def test_expected_handled(self): bot = common.make_bot() m = common.make_message(text="help me", to_me=True) self.assertTrue(help_me.Handler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="help anyone", to_me=True) self.assertEqual(help_me.Handler.handles(m, c.TARGETED, bot.config), None)
def test_expected_handled(self): bot = common.make_bot() m = common.make_message(text="ldap list", to_me=True) self.assertTrue(ldap.ListHandler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="ldap not list, lol", to_me=True) self.assertEqual( ldap.ListHandler.handles(m, c.TARGETED, bot.config), None)
def test_expected_handled(self): bot = common.make_bot() m = common.make_message(text="shutdown", to_me=True) self.assertTrue(shutdown.Handler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="please, kill yourself", to_me=True) self.assertEqual(shutdown.Handler.handles(m, c.TARGETED, bot.config), None)
def test_expected_handled(self): bot = common.make_bot() m = common.make_message(text="version", to_me=True) self.assertTrue(version.Handler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="vrsn", to_me=True) self.assertEqual(version.Handler.handles(m, c.TARGETED, bot.config), None)
def test_message_from_channels_bad(self): bot = common.make_bot() message = mock.MagicMock() message.body = munch.Munch() message.body.channel_id = 'blah' message.body.channel_name = 'blah' a = auth.message_from_channels(['my_channel']) self.assertRaises(excp.NotAuthorized, a, bot, message)
def test_remove_alias(self): bot = common.make_bot(simple_config=True) bot.brain = common.MockBrain({'user:me': {'aliases': {'c': 'b'}}}) m = common.make_message(text="alias remove c", to_me=True, user_id="me") h = alias.RemoveHandler(bot, m) h.run(handler.HandlerMatch("c")) self.assertEqual(bot.brain.storage, {'user:me': {'aliases': {}}})
def test_restart_check_bot_really_restarted(self): bot = common.make_bot() m = common.make_message(text="restart", to_me=True, user_id="me") h = restart.Handler(bot, m) h.run(handler.HandlerMatch()) self.assertEqual(e.Event.RESTART, bot.dead.value)
def test_expected_handled(self): bot = common.make_bot() m = common.make_message(text="periodics show", to_me=True) self.assertTrue(schedule.ShowHandler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="periodics do now show", to_me=True) self.assertEqual( schedule.ShowHandler.handles(m, c.TARGETED, bot.config), None)
def test_expected_handled(self): bot = common.make_bot() m = common.make_message(text="periodics pause 1", to_me=True) self.assertTrue( schedule.PauseHandler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="do something else", to_me=True) self.assertEqual( schedule.PauseHandler.handles(m, c.TARGETED, bot.config), None)
def test_expected_handled(self): bot = common.make_bot() m = common.make_message(text="tell me a joke", to_me=True) self.assertTrue(tell_joke.Handler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="please, don't tell me anything", to_me=True) self.assertEqual( tell_joke.Handler.handles(m, c.TARGETED, bot.config), None)
def test_expected_handled(self): bot = common.make_bot() m = common.make_message(text="restart", to_me=True) self.assertTrue( restart.Handler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="not restart", to_me=True) self.assertEqual( restart.Handler.handles(m, c.TARGETED, bot.config), None)
def test_help_me_call(self): bot = common.make_bot() m = common.make_message(text="consult me", to_me=True, user_id="me") with requests_mock.mock() as req_m: req_m.get(help_me.Handler.buzz_url, text="you have been helped") h = help_me.Handler(bot, m) h.run(handler.HandlerMatch()) m.reply_text.assert_called_once()
def test_openstack_runs_default(self): bot = common.make_bot() bot.topo_loader.env_names = tuple(['test']) m = common.make_message(text="openstack server show", to_me=True, user_id="me") h = openstack.DescribeServerHandler(bot, m) h.run(handler.HandlerMatch('blah-svrnm')) bot.topo_loader.load_one.assert_called()
def test_expected_handled(self): bot = common.make_bot(simple_config=True) m = common.make_message(text="alias add b c", to_me=True) self.assertTrue(alias.AddHandler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="alias remove b c", to_me=True) self.assertTrue(alias.RemoveHandler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="alias clear", to_me=True) self.assertTrue(alias.ClearHandler.handles(m, c.TARGETED, bot.config))
def test_periodic_no_run_missing(self): bot = common.make_bot() jobs = {} bot.scheduler.get_job.side_effect = lambda job_id: jobs.get(job_id) m = common.make_message(text="periodics run one 1", to_me=True, user_id="me") h = schedule.RunOneHandler(bot, m) self.assertRaises(excp.NotFound, h.run, handler.HandlerMatch("1"))
def test_expected_handled(self): bot = common.make_bot() m = common.make_message(text="ldap describe user", to_me=True) self.assertTrue( ldap.DescribeUserHandler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="ldap do anything please", to_me=True) self.assertEqual( ldap.DescribeUserHandler.handles(m, c.TARGETED, bot.config), None)
def test_clear_alias(self): bot = common.make_bot(simple_config=True) bot.brain = common.MockBrain({ 'user:me': {'aliases': {'c': 'b', 'e': 'f'}}, }) m = common.make_message(text="alias clear", to_me=True, user_id="me") h = alias.ClearHandler(bot, m) h.run(handler.HandlerMatch()) self.assertEqual(bot.brain.storage, {'user:me': {'aliases': {}}})
def test_openstack_handles_messages(self): bot = common.make_bot() m = common.make_message(text="openstack server show", to_me=True) self.assertTrue( openstack.DescribeServerHandler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="openstack not server show", to_me=True) self.assertEqual( openstack.DescribeServerHandler.handles(m, c.TARGETED, bot.config), None)
def test_info_with_metadata(self): bot = common.make_bot() m = common.make_message(text="what are you?", to_me=True, user_id="me") h = what_are_you.Handler(bot, m) with mock.patch('pkginfo.get_metadata'): h.run(handler.HandlerMatch()) m.reply_text.assert_not_called() m.reply_attachments.assert_called_once()
def test_restart_get_message(self): bot = common.make_bot() m = common.make_message(text="restart", to_me=True, user_id="me") h = restart.Handler(bot, m) h.run(handler.HandlerMatch()) m.reply_text.assert_called_once_with( 'Restart acknowledged. Be back in a bit!', prefixed=False, threaded=True)
def test_expected_handled(self): bot = common.make_bot() m = common.make_message(text="stock", to_me=True) self.assertTrue(stock.Handler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="stock gddy", to_me=True) self.assertTrue(stock.Handler.handles(m, c.TARGETED, bot.config)) m = common.make_message(text="stocks", to_me=True) self.assertEqual(stock.Handler.handles(m, c.TARGETED, bot.config), None)
def test_and_auth(self): bot = common.make_bot() message = mock.MagicMock() message.body = munch.Munch() message.body.channel_name = 'not_my_channel' message.body.channel_id = 'not_my_channel' a = auth.no_auth() a(bot, message) b = a & auth.message_from_channels(['my_channel']) self.assertRaises(excp.NotAuthorized, b, bot, message)
def test_uptime_is_not_zero(self, mock_now): bot = common.make_bot() bot.started_at = datetime.now(tz=pytz.timezone('UTC')) bot.config.tz = 'UTC' mock_now.return_value = bot.started_at m = common.make_message(text="uptime", to_me=True, user_id="me") h = uptime.Handler(bot, m) h.run(handler.HandlerMatch()) m.reply_text.assert_called_once_with( 'I have been alive for 0 seconds.', prefixed=False, threaded=True)
def test_version_with_distribution(self, get_dist): bot = common.make_bot() get_dist.return_value = munch.Munch({'version': '1.0.0'}) m = common.make_message(text="version", to_me=True, user_id="me") h = version.Handler(bot, m) h.run(handler.HandlerMatch()) m.reply_text.assert_called_once_with('I am padre version `1.0.0`.', prefixed=False, threaded=True)