"""Tests Sentry connectors and helpers.""" from unittest.mock import Mock, call, patch from httpx import HTTPStatusError, Request, Response from royalties.connectors.sentry import ( init_sentry, send_server_error_to_sentry, send_to_sentry, ) @patch('sentry_sdk.init') def test_sentry_init_sentry_enabled(init_sentry_mock): """Test sentry.sentry_client is setup when config.SENTRY is set.""" init_sentry('SENTRY') init_sentry_mock.assert_called() @patch('sentry_sdk.init') def test_sentry_init_sentry_disabled(init_sentry_mock): """Test sentry.sentry_client is setup when config.SENTRY is NOT set.""" init_sentry('') init_sentry_mock.assert_not_called() @patch('royalties.connectors.sentry.sentry_sdk') @patch('royalties.connectors.sentry.configure_scope') def test_send_to_sentry(mock_configure_scope, mock_sentry_sdk): """Verify that scope is configured and a message is captured.""" mock_scope = Mock() mock_configure_scope.return_value.__enter__.return_value = mock_scope send_to_sentry( message='Test Message', errors='Test Error', status=418, sentry_message='Teapot Error', ) expected_calls = [ call.set_extra('message', 'Test Message'), call.set_extra('errors', 'Test Error'), call.set_extra('status', 418), ] mock_scope.assert_has_calls(expected_calls, any_order=True) mock_sentry_sdk.capture_message.assert_called_once_with('Teapot Error') @patch('royalties.connectors.sentry.send_to_sentry') def test_send_server_error_handles_5xx_error(mock_send_to_sentry): """Verify an HTTPStatusError with a 500 status code is sent to Sentry.""" mock_request = Request(method='GET', url='http://test.com') mock_response = Response(status_code=500, request=mock_request) exception = HTTPStatusError( message='Server Error', request=mock_request, response=mock_response ) send_server_error_to_sentry( exception=exception, sentry_message='Server failed', message='A server error occurred', ) mock_send_to_sentry.assert_called_once_with( message='A server error occurred', errors=str(exception), status=500, sentry_message='Server failed', ) @patch('royalties.connectors.sentry.send_to_sentry') def test_send_server_error_ignores_4xx_error(mock_send_to_sentry): """Verify an HTTPStatusError with a 404 status code is NOT sent to Sentry.""" mock_request = Request(method='GET', url='http://test.com') mock_response = Response(status_code=404, request=mock_request) exception = HTTPStatusError( message='Not Found', request=mock_request, response=mock_response ) send_server_error_to_sentry( exception=exception, sentry_message='This should not be sent', message='A client error occurred', ) mock_send_to_sentry.assert_not_called() @patch('royalties.connectors.sentry.send_to_sentry') def test_send_server_error_handles_other_exceptions(mock_send_to_sentry): """Verify an unexpected non-HTTP exception is sent to Sentry.""" exception = Exception('Something went wrong') send_server_error_to_sentry( exception=exception, sentry_message='Unexpected error happened', message='An unexpected error occurred', ) mock_send_to_sentry.assert_called_once_with( message='An unexpected error occurred', errors=str(exception), status=500, sentry_message='Unexpected error happened', )