class RandomChannelPromoter(ModuleBase): """Module that promotes a random channel every Saturday.""" NAME = 'Feature random channels' def __init__(self, flask_app: Flask, config: Config): """Initialize the object.""" self.default_channel = config.slack_announcement_channel self.bot = Bot(WebClient(config.slack_api_token), config.slack_notification_channel) def get_job_args(self) -> Dict[str, Any]: """Get job configuration arguments for apscheduler.""" return { 'trigger': 'cron', 'day_of_week': 'sat', 'hour': 12, 'name': self.NAME } def do_it(self): """Select and post random channels to #general.""" channels = list( filter(lambda c: not c['is_archived'], self.bot.get_channels())) rand_channel = choice(channels) channel_id, channel_name = rand_channel['id'], rand_channel['name'] self.bot.send_to_channel( 'Featured channel of the week: ' + f'<#{channel_id}|{channel_name}>!', self.default_channel) logging.info(f'Featured #{channel_name}')
class TestBot(TestCase): """Test Case for Bot class.""" def setUp(self): """Set up the test case environment.""" self.mock_sc = mock.MagicMock(WebClient) self.bot = Bot(self.mock_sc, "#general") def test_send_dm(self): """Test the Bot class method send_dm().""" self.mock_sc.chat_postMessage = mock.MagicMock(return_value=OK_RESP) self.bot.send_dm("Hahahaha", "UD8UCTN05") self.mock_sc.chat_postMessage.assert_called_with( text="Hahahaha", channel="UD8UCTN05", as_user=True ) def test_send_dm_failure(self): """Test send_dm() when the Slack API call fails.""" self.mock_sc.chat_postMessage = mock.MagicMock(return_value=BAD_RESP) try: self.bot.send_dm("Hahahaha", "UD8UCTN05") except SlackAPIError as e: assert e.error == "Error" finally: self.mock_sc.chat_postMessage.assert_called_with( text="Hahahaha", channel="UD8UCTN05", as_user=True ) def test_send_to_channel(self): """Test the Bot class method send_to_channel().""" self.mock_sc.chat_postMessage = mock.MagicMock(return_value=OK_RESP) self.bot.send_to_channel("Hahahaha", "#random") self.mock_sc.chat_postMessage.assert_called_with( text="Hahahaha", attachments=[], channel="#random" ) def test_send_to_channel_failure(self): """Test send_to_channel() when the Slack API call fails.""" self.mock_sc.chat_postMessage = mock.MagicMock(return_value=BAD_RESP) try: self.bot.send_to_channel("Hahahaha", "#random") except SlackAPIError as e: assert e.error == "Error" finally: self.mock_sc.chat_postMessage.assert_called_with( text="Hahahaha", attachments=[], channel="#random" ) def test_send_event_notif(self): """Test send_event_notif().""" self.mock_sc.chat_postMessage.return_value = OK_RESP self.bot.send_event_notif("Good work everyone") self.mock_sc.chat_postMessage.assert_called_with( text="Good work everyone", attachments=[], channel=self.bot.slack_channel ) def test_send_event_notif_error(self): """Test send_event_notif() that errors out.""" self.mock_sc.chat_postMessage.return_value = BAD_RESP self.bot.send_event_notif("Good work everyone") self.mock_sc.chat_postMessage.assert_called_with( text="Good work everyone", attachments=[], channel=self.bot.slack_channel ) def test_get_channels_error(self): """Test get_channels() with errors.""" resp = {'ok': False, 'error': 'bad bad bad'} self.mock_sc.conversations_list.return_value = resp with self.assertRaises(SlackAPIError): self.bot.get_channels() def test_get_channels(self): """Test get_channel_names() method.""" resp = {'ok': True, 'channels': [{'name': 'happy'}]} self.mock_sc.conversations_list = mock.MagicMock(return_values=resp) names = self.bot.get_channel_names() self.assertEqual(len(names), 0) def test_get_channel_users(self): """Test the bot method get_channel_users().""" ids = ["U12314", "U42839", "U31055"] self.mock_sc.conversations_members.return_value = {'ok': True, 'members': [ "U12314", "U42839", "U31055" ]} assert self.bot.get_channel_users("C1234441") == ids self.mock_sc.conversations_members.assert_called_with( channel="C1234441" ) def test_get_channel_users_failure(self): """Test get_channel_users() when Slack API call fails.""" self.mock_sc.conversations_members =\ mock.MagicMock(return_value={"ok": False, "error": "Error"}) with self.assertRaises(SlackAPIError): self.bot.get_channel_users('C1234441') self.mock_sc.conversations_members.assert_called_with( channel='C1234441' ) def test_create_same_channel_thrice(self): """Test create_channel() thrice, with the third call throwing up.""" name: str = "#rocket2" self.mock_sc.channels_create.return_value = {"ok": True, "name": name} assert self.bot.create_channel(name) == name try: self.mock_sc.channels_create.return_value =\ {"ok": False, "error": "name_taken"} assert self.bot.create_channel(name) == name self.mock_sc.channels_create.return_value =\ {"ok": False, "error": "invalid_name"} self.bot.create_channel(name) except SlackAPIError as e: assert e.error == "invalid_name"
app = Flask(__name__) # HTTP security header middleware for Flask talisman = Talisman(app) talisman.force_https = False github_interface = make_github_interface(config) command_parser = make_command_parser(config, github_interface) github_webhook_handler = make_github_webhook_handler(github_interface, config) slack_events_handler = make_slack_events_handler(config) slack_events_adapter = SlackEventAdapter(config.slack_signing_secret, "/slack/events", app) sched = Scheduler(BackgroundScheduler(timezone="America/Los_Angeles"), (app, config)) sched.start() bot = Bot(WebClient(config.slack_api_token), config.slack_notification_channel) bot.send_to_channel('rocket2 has restarted successfully! :clap: :clap:', config.slack_notification_channel) @app.route('/') def check(): """Display a Rocket status image.""" logging.debug('Served check()') return "🚀" @app.route('/slack/commands', methods=['POST']) def handle_commands(): """Handle rocket slash commands.""" logging.info("Slash command received") timestamp = request.headers.get("X-Slack-Request-Timestamp") slack_signature = request.headers.get("X-Slack-Signature")