Exemplo n.º 1
0
    def test_can_compose_slack_message(self):
        """Test that the slack message is built correctly."""
        violation_data = """        
            {
                "role": "READER",
                "email": "",
                "bucket": "test-bucket-world-readable-123",
                "domain": "",
                "entity": "allUsers"
            }
        """

        violation = {
            'violation_data': json.loads(violation_data),
            'resource_id': '123',
            'rule_name': 'Public buckets (allUsers)',
            'rule_index': 0L,
            'violation_type': 'BUCKET_VIOLATION',
            'id': 1L,
            'resource_type': 'bucket'
        }

        with mock.patch.object(slack_webhook.SlackWebhook, '__init__',
                               lambda x: None):
            slack_notifier = slack_webhook.SlackWebhook()
            slack_notifier.resource = 'buckets_acl_violations'
            actual_output = slack_notifier._compose(violation=violation)

            expected_output = "*type*:\t`buckets_acl_violations`\n*details*:\n\t*bucket*:\t\t`test-bucket-world-readable-123`\n\t*domain*:\t\t`n/a`\n\t*email*:\t\t`n/a`\n\t*entity*:\t\t`allUsers`\n\t*role*:\t\t`READER`"

            self.assertEqual(expected_output.strip(), actual_output.strip())
Exemplo n.º 2
0
    def test_no_url_no_run_notifier(self):
        """Test that no url for Slack notifier will skip running."""
        with mock.patch.object(slack_webhook.SlackWebhook, '__init__',
                               lambda x: None):
            slack_notifier = slack_webhook.SlackWebhook()
            slack_notifier.notification_config = {}
            slack_notifier._compose = mock.MagicMock()
            slack_notifier.run()

            slack_notifier._compose.assert_not_called()
Exemplo n.º 3
0
    def test_dump_slack_output_for_string_returns_string(self):
        violation_data = 'Test violation data string'
        with mock.patch.object(slack_webhook.SlackWebhook, '__init__',
                               lambda x: None):
            slack_notifier = slack_webhook.SlackWebhook()
            actual_output = slack_notifier._dump_slack_output(violation_data)

            expected_output = '\t' + '`' + str(violation_data) + '`\n'

            self.assertEqual(expected_output.strip(), actual_output.strip())