"""Tests for the Sentry Connector.""" from unittest.mock import MagicMock, patch from oto import response from reporting.connectors import sentry @patch('reporting.connectors.sentry.sentry_sdk.init') @patch('reporting.connectors.sentry.config') def test_sentry_enabled(mock_config, mock_init): """Test sentry SDK is initialised with DSN when config.SENTRY is set.""" mock_config.SENTRY = 'SENTRY' sentry.init_sentry() mock_init.assert_called_with(dsn='SENTRY') @patch('reporting.connectors.sentry.sentry_sdk.init') @patch('reporting.connectors.sentry.config') def test_sentry_disabled(mock_config, mock_init): """Test sentry SDK is initialised with dsn of None when unset.""" mock_config.SENTRY = None sentry.init_sentry() mock_init.assert_called_with(dsn=None) @patch('reporting.connectors.sentry.sentry_sdk.capture_message') @patch('reporting.connectors.sentry.sentry_sdk.new_scope') def test_send_response_to_sentry(mock_new_scope, mock_capture_message): """Test sending an error message to Sentry.""" mock_scope = MagicMock() mock_new_scope.return_value.__enter__.return_value = mock_scope mock_new_scope.return_value.__exit__.return_value = False error_response = response.Response( message='response message', errors={'error_code': 'error_message'}, status=204, ) sentry_message = 'sentry message' sentry.send_response_to_sentry(error_response, sentry_message) mock_capture_message.assert_called_with(sentry_message) mock_scope.set_extra.assert_any_call('message', error_response.message) mock_scope.set_extra.assert_any_call('errors', error_response.errors) mock_scope.set_extra.assert_any_call('status', error_response.status)