def test_process_exception_no_expected_errors(self, mock_logger, mock_set_custom_attribute): ExpectedErrorMiddleware('mock-response').process_exception( self.mock_request, self.mock_exception) mock_logger.info.assert_not_called() mock_set_custom_attribute.assert_not_called()
def test_get_response(self): expected_response = Mock() middleware = ExpectedErrorMiddleware(lambda _: expected_response) response = middleware(self.mock_request) assert response == expected_response
def test_process_exception_expected_error_with_overrides( self, log_error, log_stack_trace, mock_logger, mock_set_custom_attribute, ): expected_class = 'openedx.core.lib.tests.test_request_utils.CustomError1' expected_message = 'Test failure' with override_settings(EXPECTED_ERRORS=[{ 'MODULE_AND_CLASS': expected_class, 'IS_IGNORED': False, 'LOG_ERROR': log_error, 'LOG_STACK_TRACE': log_stack_trace, 'REASON_EXPECTED': 'Because', }]): ExpectedErrorMiddleware('mock-response').process_exception(self.mock_request, self.mock_exception) if log_error: exc_info = self.mock_exception if log_stack_trace else None mock_logger.info.assert_called_once_with( 'Expected error %s: %s: seen for path %s', expected_class, expected_message, '/test', exc_info=exc_info ) else: mock_logger.info.assert_not_called() mock_set_custom_attribute.assert_has_calls( [ call('checked_error_expected_from', 'middleware'), call('error_expected', True), ], any_order=True )
def test_process_exception_not_matching_expected_errors( self, mock_logger, mock_set_custom_attribute): ExpectedErrorMiddleware('mock-response').process_exception( self.mock_request, self.mock_exception) mock_logger.info.assert_not_called() mock_set_custom_attribute.assert_called_once_with( 'checked_error_expected_from', 'middleware')
def test_process_exception_with_empty_expected_errors( self, expected_errors_setting, mock_logger, mock_set_custom_attribute, ): with override_settings(EXPECTED_ERRORS=expected_errors_setting): ExpectedErrorMiddleware('mock-response').process_exception(self.mock_request, self.mock_exception) mock_logger.info.assert_not_called() mock_set_custom_attribute.assert_not_called()
def test_process_exception_with_plain_exception(self, mock_set_custom_attribute): mock_exception = Exception("Oops") ExpectedErrorMiddleware('mock-response').process_exception(self.mock_request, mock_exception) mock_set_custom_attribute.assert_has_calls([ call('error_expected', True), call('error_ignored_class', 'Exception'), call('error_ignored_message', 'Oops'), ])
def test_process_exception_called_multiple_times(self, use_same_exception, mock_set_custom_attribute): mock_first_exception = self.mock_exception mock_second_exception = mock_first_exception if use_same_exception else CustomError2("Oops") ExpectedErrorMiddleware('mock-response').process_exception(self.mock_request, mock_first_exception) ExpectedErrorMiddleware('mock-response').process_exception(self.mock_request, mock_second_exception) expected_calls = [ call('checked_error_expected_from', 'middleware'), call('error_expected', True), call('error_ignored_class', 'openedx.core.lib.tests.test_request_utils.CustomError1'), call('error_ignored_message', 'Test failure'), call('checked_error_expected_from', 'middleware'), ] if use_same_exception: expected_calls += [call('checked_error_expected_from', 'multiple')] else: expected_calls += [ call('unexpected_multiple_exceptions', 'openedx.core.lib.tests.test_request_utils.CustomError1'), ] mock_set_custom_attribute.assert_has_calls(expected_calls) assert mock_set_custom_attribute.call_count == len(expected_calls)
def test_process_exception_expected_error_with_defaults(self, mock_logger, mock_set_custom_attribute): ExpectedErrorMiddleware('mock-response').process_exception(self.mock_request, self.mock_exception) mock_logger.info.assert_not_called() mock_set_custom_attribute.assert_has_calls( [ call('checked_error_expected_from', 'middleware'), call('error_expected', True), call('error_ignored_class', 'openedx.core.lib.tests.test_request_utils.CustomError1'), call('error_ignored_message', 'Test failure'), ], any_order=True )