"""Tests for the Sentry Connector.""" from unittest.mock import patch import sentry_sdk from owsresponse import response from account.connectors import sentry @patch('account.connectors.sentry.FlaskIntegration', return_value='flask_integration') @patch('account.connectors.sentry.sentry_sdk.init', return_value='sentry_client') @patch('account.connectors.sentry.config') def test_sentry_enabled(mock_config, mock_sentry_client, mock_flask_integration): """Test sentry.sentry_client is setup when config.SENTRY is set.""" mock_config.SENTRY = 'SENTRY' sentry_client = sentry.get_client() assert sentry.sentry_client mock_sentry_client.assert_called_with( 'SENTRY', integrations=[mock_flask_integration.return_value] ) assert sentry_client == sentry_sdk @patch('account.connectors.sentry.FlaskIntegration', return_value='flask_integration') @patch('account.connectors.sentry.sentry_sdk.init', return_value='sentry_client') @patch('account.connectors.sentry.config') def test_sentry_disabled(mock_config, mock_sentry_client, mock_flask_integration): """Test sentry.sentry_client initialized with dsn of None.""" mock_config.SENTRY = None sentry_client = sentry.get_client() # last call is None mock_sentry_client.assert_called_with(None, integrations=[mock_flask_integration.return_value]) # sentry_client is returned assert sentry_client == sentry_sdk @patch('account.connectors.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, stack=True, extra={ 'message': error_response.message, 'errors': error_response.errors, 'status': error_response.status, }, )