diff --git a/zerver/tests/test_logging_handlers.py b/zerver/tests/test_logging_handlers.py index 1b587a0cc8..533e6e4e07 100644 --- a/zerver/tests/test_logging_handlers.py +++ b/zerver/tests/test_logging_handlers.py @@ -96,12 +96,12 @@ class AdminNotifyHandlerTest(ZulipTestCase): None) record.request = captured_request # type: ignore # this field is dynamically added - report = self.run_handler(record) - self.assertIn("user_email", report) - self.assertIn("message", report) - self.assertIn("stack_trace", report) - self.assertEqual(report['stack_trace'], 'message\nmoremesssage\nmore') - self.assertEqual(report['message'], 'message') + report = self.run_handler(record) + self.assertIn("user_email", report) + self.assertIn("message", report) + self.assertIn("stack_trace", report) + self.assertEqual(report['stack_trace'], 'message\nmoremesssage\nmore') + self.assertEqual(report['message'], 'message') def test_request(self) -> None: """A normal request is handled properly""" @@ -119,76 +119,76 @@ class AdminNotifyHandlerTest(ZulipTestCase): 'message', {}, captured_exc_info) record.request = captured_request # type: ignore # this field is dynamically added - report = self.run_handler(record) - self.assertIn("user_email", report) - self.assertIn("message", report) - self.assertIn("stack_trace", report) + report = self.run_handler(record) + self.assertIn("user_email", report) + self.assertIn("message", report) + self.assertIn("stack_trace", report) - # Test that `add_request_metadata` throwing an exception is fine - with patch("zerver.logging_handlers.traceback.print_exc"): - with patch("zerver.logging_handlers.add_request_metadata", - side_effect=Exception("Unexpected exception!")): - report = self.run_handler(record) - self.assertNotIn("user_email", report) - self.assertIn("message", report) - self.assertEqual(report["stack_trace"], "See /var/log/zulip/errors.log") - - # Check anonymous user is handled correctly - record.request.user = AnonymousUser() # type: ignore # this field is dynamically added - report = self.run_handler(record) - self.assertIn("host", report) - self.assertIn("user_email", report) - self.assertIn("message", report) - self.assertIn("stack_trace", report) - - # Now simulate a DisallowedHost exception - def get_host_error() -> None: - raise Exception("Get Host Failure!") - orig_get_host = record.request.get_host # type: ignore # this field is dynamically added - record.request.get_host = get_host_error # type: ignore # this field is dynamically added - report = self.run_handler(record) - record.request.get_host = orig_get_host # type: ignore # this field is dynamically added - self.assertIn("host", report) - self.assertIn("user_email", report) - self.assertIn("message", report) - self.assertIn("stack_trace", report) - - # Test an exception_filter exception - with patch("zerver.logging_handlers.get_exception_reporter_filter", - return_value=15): - record.request.method = "POST" # type: ignore # this field is dynamically added + # Test that `add_request_metadata` throwing an exception is fine + with patch("zerver.logging_handlers.traceback.print_exc"): + with patch("zerver.logging_handlers.add_request_metadata", + side_effect=Exception("Unexpected exception!")): report = self.run_handler(record) - record.request.method = "GET" # type: ignore # this field is dynamically added - self.assertIn("host", report) - self.assertIn("user_email", report) - self.assertIn("message", report) - self.assertIn("stack_trace", report) + self.assertNotIn("user_email", report) + self.assertIn("message", report) + self.assertEqual(report["stack_trace"], "See /var/log/zulip/errors.log") - # Test the catch-all exception handler doesn't throw - with patch('zerver.lib.error_notify.notify_server_error', + # Check anonymous user is handled correctly + record.request.user = AnonymousUser() # type: ignore # this field is dynamically added + report = self.run_handler(record) + self.assertIn("host", report) + self.assertIn("user_email", report) + self.assertIn("message", report) + self.assertIn("stack_trace", report) + + # Now simulate a DisallowedHost exception + def get_host_error() -> None: + raise Exception("Get Host Failure!") + orig_get_host = record.request.get_host # type: ignore # this field is dynamically added + record.request.get_host = get_host_error # type: ignore # this field is dynamically added + report = self.run_handler(record) + record.request.get_host = orig_get_host # type: ignore # this field is dynamically added + self.assertIn("host", report) + self.assertIn("user_email", report) + self.assertIn("message", report) + self.assertIn("stack_trace", report) + + # Test an exception_filter exception + with patch("zerver.logging_handlers.get_exception_reporter_filter", + return_value=15): + record.request.method = "POST" # type: ignore # this field is dynamically added + report = self.run_handler(record) + record.request.method = "GET" # type: ignore # this field is dynamically added + self.assertIn("host", report) + self.assertIn("user_email", report) + self.assertIn("message", report) + self.assertIn("stack_trace", report) + + # Test the catch-all exception handler doesn't throw + with patch('zerver.lib.error_notify.notify_server_error', + side_effect=Exception("queue error")): + self.handler.emit(record) + with self.settings(STAGING_ERROR_NOTIFICATIONS=False): + with patch('zerver.logging_handlers.queue_json_publish', side_effect=Exception("queue error")): self.handler.emit(record) - with self.settings(STAGING_ERROR_NOTIFICATIONS=False): - with patch('zerver.logging_handlers.queue_json_publish', - side_effect=Exception("queue error")): - self.handler.emit(record) - # Test no exc_info - record.exc_info = None + # Test no exc_info + record.exc_info = None + report = self.run_handler(record) + self.assertIn("host", report) + self.assertIn("user_email", report) + self.assertIn("message", report) + self.assertEqual(report["stack_trace"], 'No stack trace available') + + # Test arbitrary exceptions from request.user + record.request.user = None # type: ignore # this field is dynamically added + with patch("zerver.logging_handlers.traceback.print_exc"): report = self.run_handler(record) - self.assertIn("host", report) - self.assertIn("user_email", report) - self.assertIn("message", report) - self.assertEqual(report["stack_trace"], 'No stack trace available') - - # Test arbitrary exceptions from request.user - record.request.user = None # type: ignore # this field is dynamically added - with patch("zerver.logging_handlers.traceback.print_exc"): - report = self.run_handler(record) - self.assertIn("host", report) - self.assertIn("user_email", report) - self.assertIn("message", report) - self.assertIn("stack_trace", report) + self.assertIn("host", report) + self.assertIn("user_email", report) + self.assertIn("message", report) + self.assertIn("stack_trace", report) class LoggingConfigTest(TestCase): @staticmethod