"""Tests for the Sentry Connector.""" from unittest.mock import patch from oto import response import sentry_sdk from product_configuration.connectors import sentry @patch( 'product_configuration.connectors.sentry.FlaskIntegration', return_value='flask_integration') @patch( 'product_configuration.connectors.sentry.sentry_sdk.init', return_value='sentry_client') @patch('product_configuration.connectors.sentry.config') def test_sentry_enabled(mock_config, mock_sentry_client, mock_flask_integration): """Test sentry.sentry_client is set up when config.SENTRY_DSN is set.""" mock_config.SENTRY_DSN = 'SENTRY_DSN' sentry_client = sentry.get_client() assert sentry.sentry_client mock_sentry_client.assert_called_with( dsn='SENTRY_DSN', integrations=[mock_flask_integration.return_value]) assert sentry_client == sentry_sdk @patch( 'product_configuration.connectors.sentry.FlaskIntegration', return_value='flask_integration') @patch( 'product_configuration.connectors.sentry.sentry_sdk.init', return_value='sentry_client') @patch('product_configuration.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_DSN = None sentry_client = sentry.get_client() mock_sentry_client.assert_called_with( dsn=None, integrations=[mock_flask_integration.return_value]) assert sentry_client == sentry_sdk @patch('product_configuration.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' 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})