"""Tests for the Stream Connector.""" from unittest.mock import MagicMock from owsresponse import response, status as ows_status from stream.exceptions import ApiKeyException, FeedConfigException, InputException from notifications.connectors import sentry, stream def test_capture_exceptions_wrapper(): """Test that the decorated function is called.""" decorated_function = MagicMock(return_value=response.Response()) args = (1, 2, 3) kwargs = {'d': 4, 'e': 5, 'f': 6} function = stream.capture_exceptions(decorated_function) result = function(*args, **kwargs) decorated_function.assert_called_with(*args, **kwargs) assert result.status == ows_status.OK def test_capture_exceptions_wrapper_api_key_exception(mocker): """Test error response if an ApiKeyException is raised.""" sentry_mock = mocker.patch.object(sentry, 'send_response_to_sentry') decorated_function = MagicMock(side_effect=ApiKeyException('error')) function = stream.capture_exceptions(decorated_function) result = function() assert result.status == ows_status.UNAUTHORIZED sentry_mock.assert_called_once() def test_capture_exceptions_wrapper_feed_config_exception(mocker): """Test error response if a FeedConfigException is raised.""" sentry_mock = mocker.patch.object(sentry, 'send_response_to_sentry') decorated_function = MagicMock(side_effect=FeedConfigException('error')) function = stream.capture_exceptions(decorated_function) result = function() assert result.status == ows_status.NOT_FOUND sentry_mock.assert_called_once() def test_capture_exceptions_wrapper_input_exception(mocker): """Test error response if an InputException is raised.""" sentry_mock = mocker.patch.object(sentry, 'send_response_to_sentry') decorated_function = MagicMock(side_effect=InputException('error')) function = stream.capture_exceptions(decorated_function) result = function() assert result.status == ows_status.BAD_REQUEST sentry_mock.assert_called_once() def test_capture_exceptions_wrapper_other_exception(mocker): """Test error response if an other type of exception is raised.""" sentry_mock = mocker.patch.object(sentry, 'send_response_to_sentry') decorated_function = MagicMock(side_effect=Exception('error')) body = {'a': 'a', 'b': 'b'} function = stream.capture_exceptions(decorated_function) result = function('1', '2', body) assert result.status == ows_status.INTERNAL_ERROR sentry_mock.assert_called_once()