Exemple #1
0
class TestKomandutput(object):
    """Test class for KomandOutput"""
    DESCRIPTOR = 'unit_test_komand'
    SERVICE = 'komand'
    CREDS = {
        'url': 'http://komand.foo.bar',
        'komand_auth_token': 'mocked_auth_token'
    }

    def setup(self):
        """Setup before each method"""
        self._dispatcher = KomandOutput(REGION, ACCOUNT_ID, 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)

    @patch('logging.Logger.info')
    @patch('requests.post')
    def test_dispatch_existing_container(self, post_mock, log_mock):
        """KomandOutput - Dispatch Success"""
        post_mock.return_value.status_code = 200

        assert_true(
            self._dispatcher.dispatch(descriptor=self.DESCRIPTOR,
                                      alert=get_alert()))

        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_container_failure(self, post_mock, log_mock):
        """KomandOutput - Dispatch Failure"""
        post_mock.return_value.status_code = 400
        json_error = {'message': 'error message', 'errors': ['error1']}
        post_mock.return_value.json.return_value = json_error

        assert_false(
            self._dispatcher.dispatch(descriptor=self.DESCRIPTOR,
                                      alert=get_alert()))

        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_error_mock):
        """KomandOutput - Dispatch Failure, Bad Descriptor"""
        assert_false(
            self._dispatcher.dispatch(descriptor='bad_descriptor',
                                      alert=get_alert()))

        log_error_mock.assert_called_with('Failed to send alert to %s:%s',
                                          self.SERVICE, 'bad_descriptor')
Exemple #2
0
class TestKomandutput(object):
    """Test class for KomandOutput"""
    DESCRIPTOR = 'unit_test_komand'
    SERVICE = 'komand'
    OUTPUT = ':'.join([SERVICE, DESCRIPTOR])
    CREDS = {'url': 'http://komand.foo.bar',
             'komand_auth_token': 'mocked_auth_token'}

    @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 = KomandOutput(None)

    @patch('logging.Logger.info')
    @patch('requests.post')
    def test_dispatch_existing_container(self, post_mock, log_mock):
        """KomandOutput - Dispatch Success"""
        post_mock.return_value.status_code = 200

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

        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_container_failure(self, post_mock, log_mock):
        """KomandOutput - Dispatch Failure"""
        post_mock.return_value.status_code = 400
        json_error = {'message': 'error message', 'errors': ['error1']}
        post_mock.return_value.json.return_value = json_error

        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_error_mock):
        """KomandOutput - Dispatch Failure, Bad Descriptor"""
        assert_false(
            self._dispatcher.dispatch(get_alert(), ':'.join([self.SERVICE, 'bad_descriptor'])))

        log_error_mock.assert_called_with('Failed to send alert to %s:%s',
                                          self.SERVICE, 'bad_descriptor')
class TestKomandutput(object):
    """Test class for KomandOutput"""
    DESCRIPTOR = 'unit_test_komand'
    SERVICE = 'komand'
    OUTPUT = ':'.join([SERVICE, DESCRIPTOR])
    CREDS = {
        'url': 'http://komand.foo.bar',
        'komand_auth_token': 'mocked_auth_token'
    }

    @patch.dict('os.environ', MOCK_ENV)
    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 = KomandOutput(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)

    def teardown(self):
        """Teardown after each method"""
        self._mock_s3.stop()
        self._mock_kms.stop()

    @patch('logging.Logger.info')
    @patch('requests.post')
    def test_dispatch_existing_container(self, post_mock, log_mock):
        """KomandOutput - Dispatch Success"""
        post_mock.return_value.status_code = 200

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

        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_container_failure(self, post_mock, log_mock):
        """KomandOutput - Dispatch Failure"""
        post_mock.return_value.status_code = 400
        json_error = {'message': 'error message', 'errors': ['error1']}
        post_mock.return_value.json.return_value = json_error

        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_error_mock):
        """KomandOutput - Dispatch Failure, Bad Descriptor"""
        assert_false(
            self._dispatcher.dispatch(
                get_alert(), ':'.join([self.SERVICE, 'bad_descriptor'])))

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