def test_calls_stale_once_for_each_channel(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     mock_slacker.channels_by_name = {'leninists': 'C012839', 'stalinists': 'C102843'}
     self.destalinator.stale = mock.MagicMock(return_value=False)
     days = self.destalinator.config.archive_threshold
     self.destalinator.safe_archive_all(days)
     self.assertEqual(self.destalinator.stale.mock_calls, [mock.call('leninists', days), mock.call('stalinists', days)])
Beispiel #2
0
 def test_with_empty_ignore_channel_config(self):
     self.destalinator = destalinator.Destalinator(self.slacker,
                                                   self.slackbot,
                                                   activated=True)
     self.destalinator.config.config['ignore_channels'] = []
     self.destalinator.config.config['ignore_channel_patterns'] = []
     self.assertFalse(self.destalinator.ignore_channel('stalinists'))
 def test_skips_channel_with_only_restricted_users(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     mock_slacker.post_message.return_value = {}
     mock_slacker.archive.return_value = {'ok': True}
     mock_slacker.channel_has_only_restricted_members.return_value = True
     self.destalinator.safe_archive("stalinists")
     self.assertFalse(mock_slacker.archive.called)
 def test_calls_archive_method(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     mock_slacker.post_message.return_value = {}
     self.destalinator.archive = mock.MagicMock(return_value=True)
     mock_slacker.channel_has_only_restricted_members.return_value = False
     self.destalinator.safe_archive("stalinists")
     self.destalinator.archive.assert_called_once_with('stalinists')
Beispiel #5
0
    def __init__(self,
                 debug=False,
                 verbose=False,
                 slackbot_injected=None,
                 slacker_injected=None):
        self.debug = debug
        self.verbose = verbose
        self.config = config.Config()
        slackbot_token = os.getenv(self.config.slackbot_api_token_env_varname)
        api_token = os.getenv(self.config.api_token_env_varname)

        self.slackbot = slackbot_injected or slackbot.Slackbot(
            config.SLACK_NAME, token=slackbot_token)

        self.logger = logging.getLogger(__name__)
        utils.set_up_logger(self.logger,
                            log_level_env_var='DESTALINATOR_LOG_LEVEL',
                            log_to_slack_env_var='DESTALINATOR_LOG_TO_CHANNEL',
                            log_channel=self.config.log_channel,
                            slackbot=self.slackbot)

        self.destalinator_activated = False
        if os.getenv(self.config.destalinator_activated_env_varname):
            self.destalinator_activated = True
        self.logger.debug("destalinator_activated is %s",
                          self.destalinator_activated)

        self.slacker = slacker_injected or slacker.Slacker(
            config.SLACK_NAME, token=api_token, logger=self.logger)

        self.ds = destalinator.Destalinator(
            slacker=self.slacker,
            slackbot=self.slackbot,
            activated=self.destalinator_activated,
            logger=self.logger)
 def test_with_a_string_having_a_channel(self):
     self.destalinator = destalinator.Destalinator(self.slacker, self.slackbot, activated=True)
     self.slacker.channels_by_name = {'leninists': 'C012839', 'stalinists': 'C102843'}
     self.slacker.post_message = mock.MagicMock(return_value={})
     self.destalinator.post_marked_up_message('stalinists', "Really great message about #leninists.")
     self.slacker.post_message.assert_called_once_with('stalinists',
                                                       "Really great message about <#C012839|leninists>.")
Beispiel #7
0
 def test_skips_when_destalinator_not_activated(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker,
                                                   self.slackbot,
                                                   activated=False)
     mock_slacker.post_message.return_value = {}
     self.destalinator.archive("stalinists")
     self.assertFalse(mock_slacker.post_message.called)
Beispiel #8
0
 def test_with_all_sample_messages(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker,
                                                   self.slackbot,
                                                   activated=True)
     mock_slacker.get_channel_info.return_value = {'age': 60 * 86400}
     self.destalinator.get_messages = mock.MagicMock(
         return_value=sample_slack_messages)
     self.assertFalse(self.destalinator.stale('stalinists', 30))
Beispiel #9
0
 def test_with_many_matching_ignore_channel_patterns(self):
     self.destalinator = destalinator.Destalinator(self.slacker,
                                                   self.slackbot,
                                                   activated=True)
     self.destalinator.config.config['ignore_channel_patterns'] = [
         '^len', 'lin', '^st'
     ]
     self.assertTrue(self.destalinator.ignore_channel('stalinists'))
Beispiel #10
0
 def test_with_default_included_subtypes(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker,
                                                   self.slackbot,
                                                   activated=True)
     mock_slacker.get_channelid.return_value = "123456"
     mock_slacker.get_messages_in_time_range.return_value = sample_slack_messages
     self.assertEqual(len(self.destalinator.get_messages("general", 30)),
                      len(sample_slack_messages))
Beispiel #11
0
 def test_env_var_name_set_in_config(self):
     self.destalinator = destalinator.Destalinator(self.slacker,
                                                   self.slackbot,
                                                   activated=True)
     self.destalinator.config.config[
         'earliest_archive_date_env_varname'] = 'EARLIEST_ARCHIVE_DATE'
     self.assertEqual(self.destalinator.get_earliest_archive_date(),
                      target_archive_date)
Beispiel #12
0
def warn():

    sb = slackbot.Slackbot("rands-leadership",
                           slackbot_token_file="sb_token.txt")
    ds = destalinator.Destalinator("rands-leadership",
                                   slackbot=sb,
                                   api_token_file="api_token.txt")
    ds.warn_all(30)
Beispiel #13
0
def archive():

    sb = slackbot.Slackbot("rands-leadership",
                           slackbot_token_file="sb_token.txt")
    ds = destalinator.Destalinator("rands-leadership",
                                   slackbot=sb,
                                   api_token_file="api_token.txt")
    ds.safe_archive_all(60)
 def test_with_limited_included_subtypes(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     mock_slacker.get_channelid.return_value = "123456"
     mock_slacker.get_messages_in_time_range.return_value = sample_slack_messages
     self.assertEqual(
         len(self.destalinator.get_messages("general", 30)),
         sum(m.get('subtype', None) in (None, 'bot_message') for m in sample_slack_messages)
     )
 def test_warns_by_posting_message(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     mock_slacker.channel_has_only_restricted_members.return_value = False
     mock_slacker.get_messages_in_time_range.return_value = sample_slack_messages
     self.destalinator.warn("stalinists", 30)
     mock_slacker.post_message.assert_called_with("stalinists",
                                                  self.destalinator.warning_text,
                                                  message_type='channel_warning')
 def test_add_slack_channel_markup_hyphens(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     input_text = "Please find my #channel-with-hyphens references."
     mock_slacker.add_channel_markup.return_value = "<#EXA456|channel-with-hyphens>"
     self.assertEqual(
         self.destalinator.add_slack_channel_markup(input_text),
         "Please find my <#EXA456|channel-with-hyphens> references."
     )
 def test_add_slack_channel_markup_ignore_screaming(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     input_text = "Please find my #general channel reference and ignore my #HASHTAGSCREAMING thanks."
     mock_slacker.add_channel_markup.return_value = "<#ABC123|general>"
     self.assertEqual(
         self.destalinator.add_slack_channel_markup(input_text),
         "Please find my <#ABC123|general> channel reference and ignore my #HASHTAGSCREAMING thanks."
     )
 def test_add_slack_channel_markup_multiple(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     input_text = "Please find my #general multiple #general channel #general references."
     mock_slacker.add_channel_markup.return_value = "<#ABC123|general>"
     self.assertEqual(
         self.destalinator.add_slack_channel_markup(input_text),
         "Please find my <#ABC123|general> multiple <#ABC123|general> channel <#ABC123|general> references."
     )
Beispiel #19
0
 def test_calls_archive_method(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker,
                                                   self.slackbot,
                                                   activated=True)
     mock_slacker.post_message.return_value = {}
     mock_slacker.archive.return_value = {'ok': True}
     self.destalinator.archive("stalinists")
     mock_slacker.archive.assert_called_once_with('stalinists')
Beispiel #20
0
 def test_skips_ignored_channel(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker,
                                                   self.slackbot,
                                                   activated=True)
     mock_slacker.post_message.return_value = {}
     mock_slacker.archive.return_value = {'ok': True}
     self.destalinator.config.config['ignore_channels'] = ['stalinists']
     self.destalinator.archive("stalinists")
     self.assertFalse(mock_slacker.post_message.called)
 def test_skips_archiving_if_before_earliest_archive_date(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     mock_slacker.post_message.return_value = {}
     self.destalinator.archive = mock.MagicMock(return_value=True)
     mock_slacker.channel_has_only_restricted_members.return_value = False
     today = date.today()
     self.destalinator.earliest_archive_date = today + timedelta(days=1)
     self.destalinator.safe_archive("stalinists")
     self.assertFalse(self.destalinator.archive.called)
Beispiel #22
0
 def test_with_only_an_attachment_message(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker,
                                                   self.slackbot,
                                                   activated=True)
     mock_slacker.get_channel_info.return_value = {'age': 60 * 86400}
     self.destalinator.get_messages = mock.MagicMock(return_value=[
         m for m in sample_slack_messages if 'attachments' in m
     ])
     self.assertFalse(self.destalinator.stale('stalinists', 30))
Beispiel #23
0
 def test_with_empty_included_subtypes(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker,
                                                   self.slackbot,
                                                   activated=True)
     self.destalinator.config.config['included_subtypes'] = []
     mock_slacker.get_channelid.return_value = "123456"
     mock_slacker.get_messages_in_time_range.return_value = sample_slack_messages
     self.assertEqual(
         len(self.destalinator.get_messages("general", 30)),
         sum('subtype' not in m for m in sample_slack_messages))
 def test_announces_closure_with_closure_text(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     mock_slacker.post_message.return_value = {}
     mock_slacker.archive.return_value = {'ok': True}
     mock_slacker.get_channel_member_names.return_value = ['sridhar', 'jane']
     self.destalinator.archive("stalinists")
     self.assertIn(
         mock.call('stalinists', mock.ANY, message_type='channel_archive'),
         mock_slacker.post_message.mock_calls
     )
 def test_warns_with_cached_messages(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     days_ago = self.destalinator.now - (86400 * 30)
     self.destalinator.cache = {'C012839': {days_ago: sample_slack_messages}}
     mock_slacker.get_channelid.return_value = 'C012839'
     mock_slacker.channel_has_only_restricted_members.return_value = False
     self.destalinator.warn("stalinists", 30)
     mock_slacker.post_message.assert_called_with("stalinists",
                                                  self.destalinator.warning_text,
                                                  message_type='channel_warning')
 def test_calls_stale_once_for_each_channel_with_cache(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     mock_slacker.channels_by_name = {'leninists': 'C012839', 'stalinists': 'C102843'}
     mock_slacker.get_channelid.return_value = 'C012839'
     days_ago = self.destalinator.now - (86400 * 30)
     self.destalinator.cache = {'C012839': {days_ago: sample_slack_messages}}
     self.destalinator.stale = mock.MagicMock(return_value=False)
     days = self.destalinator.config.archive_threshold
     self.destalinator.safe_archive_all(days)
     self.assertEqual(self.destalinator.stale.mock_calls, [mock.call('leninists', days), mock.call('stalinists', days)])
Beispiel #27
0
 def test_archive_date_set_in_config(self):
     self.destalinator = destalinator.Destalinator(self.slacker,
                                                   self.slackbot,
                                                   activated=True)
     self.destalinator.config.config[
         'earliest_archive_date_env_varname'] = None
     self.destalinator.config.config[
         'earliest_archive_date'] = target_archive_date_string
     self.assertEqual(self.destalinator.get_earliest_archive_date(),
                      target_archive_date)
Beispiel #28
0
 def test_with_all_users_ignored(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker,
                                                   self.slackbot,
                                                   activated=True)
     self.destalinator.config.config['ignore_users'] = [
         m['user'] for m in sample_slack_messages if m.get('user')
     ]
     mock_slacker.get_channel_info.return_value = {'age': 60 * 86400}
     self.destalinator.get_messages = mock.MagicMock(
         return_value=sample_slack_messages)
     self.assertTrue(self.destalinator.stale('stalinists', 30))
 def test_announces_members_at_channel_closing(self, mock_slacker):
     self.destalinator = destalinator.Destalinator(mock_slacker, self.slackbot, activated=True)
     mock_slacker.post_message.return_value = {}
     mock_slacker.archive.return_value = {'ok': True}
     names = ['sridhar', 'jane']
     mock_slacker.get_channel_member_names.return_value = names
     self.destalinator.archive("stalinists")
     self.assertIn(
         mock.call('stalinists', MockValidator(lambda s: all(name in s for name in names)), message_type=mock.ANY),
         mock_slacker.post_message.mock_calls
     )
Beispiel #30
0
 def test_falls_back_to_past_date(self):
     self.destalinator = destalinator.Destalinator(self.slacker,
                                                   self.slackbot,
                                                   activated=True)
     self.destalinator.config.config[
         'earliest_archive_date_env_varname'] = None
     self.destalinator.config.config['earliest_archive_date'] = None
     self.assertEqual(
         self.destalinator.get_earliest_archive_date(),
         datetime.strptime(destalinator.PAST_DATE_STRING,
                           "%Y-%m-%d").date())