Esempio n. 1
0
 def setup(self):
     """Setup before each method"""
     self._dispatcher = GithubOutput(REGION, FUNCTION_NAME, CONFIG)
     remove_temp_secrets()
     output_name = self._dispatcher.output_cred_name(self.DESCRIPTOR)
     put_mock_creds(output_name, self.CREDS,
                    self._dispatcher.secrets_bucket, REGION, KMS_ALIAS)
Esempio n. 2
0
 def setup(self, provider_constructor):
     """Setup before each method"""
     provider = MagicMock()
     provider_constructor.return_value = provider
     provider.load_credentials = Mock(
         side_effect=lambda x: self.CREDS if x == self.DESCRIPTOR else None)
     self._provider = provider
     self._dispatcher = GithubOutput(None)
Esempio n. 3
0
 def setup(self):
     """Setup before each method"""
     self._mock_s3 = mock_s3()
     self._mock_s3.start()
     self._mock_kms = mock_kms()
     self._mock_kms.start()
     self._dispatcher = GithubOutput(None)
     remove_temp_secrets()
     output_name = self._dispatcher.output_cred_name(self.DESCRIPTOR)
     put_mock_creds(output_name, self.CREDS,
                    self._dispatcher.secrets_bucket, REGION, KMS_ALIAS)
Esempio n. 4
0
class TestGithubOutput(object):
    """Test class for GithubOutput"""
    DESCRIPTOR = 'unit_test_repo'
    SERVICE = 'github'
    CREDS = {
        'username': '******',
        'access_token': 'unit_test_access_token',
        'repository': 'unit_test_org/unit_test_repo',
        'labels': 'label1,label2'
    }

    def setup(self):
        """Setup before each method"""
        self._dispatcher = GithubOutput(REGION, ACCOUNT_ID, FUNCTION_NAME,
                                        None)
        remove_temp_secrets()
        output_name = self._dispatcher.output_cred_name(self.DESCRIPTOR)
        put_mock_creds(output_name, self.CREDS,
                       self._dispatcher.secrets_bucket, REGION, KMS_ALIAS)

    @patch('logging.Logger.info')
    @patch('requests.post')
    def test_dispatch_success(self, url_mock, log_mock):
        """GithubOutput - Dispatch Success"""
        url_mock.return_value.status_code = 200
        url_mock.return_value.json.return_value = dict()

        assert_true(self._dispatcher.dispatch(get_alert(), self.DESCRIPTOR))

        assert_equal(
            url_mock.call_args[0][0],
            'https://api.github.com/repos/unit_test_org/unit_test_repo/issues')
        assert_is_not_none(url_mock.call_args[1]['headers']['Authorization'])

        credentials = url_mock.call_args[1]['headers']['Authorization'].split(
            ' ')[-1]
        decoded_username_password = base64.b64decode(credentials)
        assert_equal(
            decoded_username_password,
            '{}:{}'.format(self.CREDS['username'], self.CREDS['access_token']))

        log_mock.assert_called_with('Successfully sent alert to %s:%s',
                                    self.SERVICE, self.DESCRIPTOR)

    @patch('logging.Logger.info')
    @patch('requests.post')
    def test_dispatch_success_with_labels(self, url_mock, log_mock):
        """GithubOutput - Dispatch Success with Labels"""
        url_mock.return_value.status_code = 200
        url_mock.return_value.json.return_value = dict()

        assert_true(self._dispatcher.dispatch(get_alert(), self.DESCRIPTOR))

        assert_equal(url_mock.call_args[1]['json']['labels'],
                     ['label1', 'label2'])
        log_mock.assert_called_with('Successfully sent alert to %s:%s',
                                    self.SERVICE, self.DESCRIPTOR)

    @patch('logging.Logger.error')
    @patch('requests.post')
    def test_dispatch_failure(self, url_mock, log_mock):
        """GithubOutput - Dispatch Failure, Bad Request"""
        json_error = {'message': 'error message', 'errors': ['error1']}
        url_mock.return_value.json.return_value = json_error
        url_mock.return_value.status_code = 400

        assert_false(self._dispatcher.dispatch(get_alert(), self.DESCRIPTOR))
        log_mock.assert_called_with('Failed to send alert to %s:%s',
                                    self.SERVICE, self.DESCRIPTOR)

    @patch('logging.Logger.error')
    def test_dispatch_bad_descriptor(self, log_mock):
        """GithubOutput - Dispatch Failure, Bad Descriptor"""
        assert_false(self._dispatcher.dispatch(get_alert(), 'bad_descriptor'))

        log_mock.assert_called_with('Failed to send alert to %s:%s',
                                    self.SERVICE, 'bad_descriptor')
Esempio n. 5
0
class TestGithubOutput(object):
    """Test class for GithubOutput"""
    DESCRIPTOR = 'unit_test_repo'
    SERVICE = 'github'
    OUTPUT = ':'.join([SERVICE, DESCRIPTOR])
    CREDS = {
        'username': '******',
        'access_token': 'unit_test_access_token',
        'repository': 'unit_test_org/unit_test_repo',
        'labels': 'label1,label2',
        'api': 'https://api.github.com',
    }

    @patch(
        'stream_alert.alert_processor.outputs.output_base.OutputCredentialsProvider'
    )
    def setup(self, provider_constructor):
        """Setup before each method"""
        provider = MagicMock()
        provider_constructor.return_value = provider
        provider.load_credentials = Mock(
            side_effect=lambda x: self.CREDS if x == self.DESCRIPTOR else None)
        self._provider = provider
        self._dispatcher = GithubOutput(None)

    @patch('logging.Logger.info')
    @patch('requests.post')
    def test_dispatch_success(self, url_mock, log_mock):
        """GithubOutput - Dispatch Success"""
        url_mock.return_value.status_code = 200
        url_mock.return_value.json.return_value = dict()

        assert_true(self._dispatcher.dispatch(get_alert(), self.OUTPUT))

        assert_equal(
            url_mock.call_args[0][0],
            'https://api.github.com/repos/unit_test_org/unit_test_repo/issues')
        assert_is_not_none(url_mock.call_args[1]['headers']['Authorization'])

        credentials = url_mock.call_args[1]['headers']['Authorization'].split(
            ' ')[-1]
        decoded_username_password = base64.b64decode(credentials)
        assert_equal(
            decoded_username_password,
            '{}:{}'.format(self.CREDS['username'], self.CREDS['access_token']))

        log_mock.assert_called_with('Successfully sent alert to %s:%s',
                                    self.SERVICE, self.DESCRIPTOR)

    @patch('logging.Logger.info')
    @patch('requests.post')
    def test_dispatch_success_with_labels(self, url_mock, log_mock):
        """GithubOutput - Dispatch Success with Labels"""
        url_mock.return_value.status_code = 200
        url_mock.return_value.json.return_value = dict()

        assert_true(self._dispatcher.dispatch(get_alert(), self.OUTPUT))

        assert_equal(url_mock.call_args[1]['json']['labels'],
                     ['label1', 'label2'])
        log_mock.assert_called_with('Successfully sent alert to %s:%s',
                                    self.SERVICE, self.DESCRIPTOR)

    @patch('logging.Logger.error')
    @patch('requests.post')
    def test_dispatch_failure(self, url_mock, log_mock):
        """GithubOutput - Dispatch Failure, Bad Request"""
        json_error = {'message': 'error message', 'errors': ['error1']}
        url_mock.return_value.json.return_value = json_error
        url_mock.return_value.status_code = 400

        assert_false(self._dispatcher.dispatch(get_alert(), self.OUTPUT))

        log_mock.assert_called_with('Failed to send alert to %s:%s',
                                    self.SERVICE, self.DESCRIPTOR)

    @patch('logging.Logger.error')
    def test_dispatch_bad_descriptor(self, log_mock):
        """GithubOutput - Dispatch Failure, Bad Descriptor"""
        assert_false(
            self._dispatcher.dispatch(
                get_alert(), ':'.join([self.SERVICE, 'bad_descriptor'])))

        log_mock.assert_called_with('Failed to send alert to %s:%s',
                                    self.SERVICE, 'bad_descriptor')