def test_send_digest_handles_invalid_env_var(self, morning_digest, log, time_for_user_digest):
        """
        Verify that we gracefully handle a miconfigured digest_users variable.
        """
        # Setup scenario
        os.environ['DIGEST_USERS'] = ''

        # Run code
        send_digest_to_all_slack_users()

        # Verify expectations
        self.assertFalse(morning_digest.called)
        self.assertEquals(0, morning_digest.return_value.post_to_slack.call_count)
        log.warning.assert_called_once_with('No slack users selected')
    def test_send_digest_to_all_slack_users(self, post_to_slack, time_for_user_digest):
        """
        Verify that we construct and send a digest to all slack users.
        """
        # Setup scenario
        slack_user_2 = G(SlackUser, expects_morning_digest=True)
        os.environ['DIGEST_USERS'] = '*'

        # Run code
        send_digest_to_all_slack_users()

        # Verify expectations
        post_to_slack.assert_has_calls([call(self.slack_user_1), call(slack_user_2)], any_order=True)
        self.assertEquals(2, post_to_slack.call_count)
    def test_send_digest_to_subset_of_slack_users_based_on_env_var(self, post_to_slack, time_for_user_digest):
        """
        Verify that we construct and send a digest to only the slack users specified by DIGEST_USERS.
        """
        # Setup scenario
        slack_user_2 = G(SlackUser, expects_morning_digest=True)
        G(SlackUser, expects_morning_digest=True)
        os.environ['DIGEST_USERS'] = ','.join([self.slack_user_1.username, slack_user_2.username])

        # Run code
        send_digest_to_all_slack_users()

        # Verify expectations
        post_to_slack.assert_has_calls([call(self.slack_user_1), call(slack_user_2)], any_order=True)
        self.assertEquals(2, post_to_slack.call_count)
    def test_send_digest_handles_exception(self, morning_digest, log, time_for_user_digest):
        """
        Verify that we gracefully handle an exception when posting a digest.
        """
        # Setup scenario
        os.environ['DIGEST_USERS'] = '*'
        e = Exception()
        morning_digest.return_value.post_to_slack.side_effect = e

        # Run code
        send_digest_to_all_slack_users()

        # Verify expectations
        self.assertTrue(morning_digest.called)
        self.assertEquals(1, morning_digest.return_value.post_to_slack.call_count)
        log.error.assert_called_once_with(
            'Could not send digest to "{0}"; exception: {1}'.format(self.slack_user_1.username, str(e)))
    def test_send_digest_to_subset_of_slack_users_based_on_tz(self, post_to_slack, time_for_user_digest):
        """
        Verify that we construct and send a digest to only the slack users specified by DIGEST_USERS.
        """
        # Setup scenario
        slack_user_2 = G(SlackUser, expects_morning_digest=True)
        os.environ['DIGEST_USERS'] = ','.join([self.slack_user_1.username, slack_user_2.username])

        def side_effect(u):
            # Only the first user is in the appropriate timezone
            return (u == self.slack_user_1)

        time_for_user_digest.side_effect = side_effect

        # Run code
        send_digest_to_all_slack_users()

        # Verify expectations
        post_to_slack.assert_called_once_with(self.slack_user_1)
 def handle(self, *args, **options):
     send_digest_to_all_slack_users()