"""Tests for the Sentry Connector.""" from unittest.mock import patch from oto import response from activity_detector.utils import sentry @patch('activity_detector.utils.sentry.sentry_sdk.init') @patch('activity_detector.utils.sentry.base_config') def test_sentry_enabled(mock_config, mock_sentry_sdk_init): """Test sentry.sentry_client is setup when config.SENTRY is set.""" mock_config.SENTRY_DSN = 'SENTRY' sentry.get_client() assert sentry.sentry_client mock_sentry_sdk_init.assert_called_with('SENTRY') @patch('sentry_sdk.init') @patch('activity_detector.utils.sentry.base_config') def test_sentry_disabled(mock_config, mock_sentry_sdk_init): """Test sentry.sentry_client initialized with dsn of None.""" mock_config.SENTRY_DSN = None sentry.get_client() mock_sentry_sdk_init.assert_called_with(None) @patch('activity_detector.utils.sentry.sentry_client') def test_send_response_to_sentry(mock_sentry_client): """Test sending an error message to Sentry.""" error_response = response.Response( message='response message', errors={'error_code': 'error_message'}, status=204) sentry_message = 'sentry message' # when sentry client exists, sentry message is sent mock_sentry_client.__bool__.return_value = True sentry.send_response_to_sentry(error_response, sentry_message) mock_sentry_client.capture_message.assert_called_with( message=sentry_message)