Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_index_is_down(self, mock_index):
"""Test returns 'DOWN' + status 500 when index raises an exception."""
mock_index.search.side_effect = RuntimeError
response, status_code, _ = health_check()
self.assertEqual(response, 'DOWN', "Response content should be DOWN")
self.assertEqual(status_code, status.HTTP_500_INTERNAL_SERVER_ERROR,
"Should return 500 status code.")
def test_index_returns_no_result(self, mock_index):
"""Test returns 'DOWN' + status 500 when index returns no results."""
mock_index.search.return_value = dict(metadata={}, results=[])
response, status_code, _ = health_check()
self.assertEqual(response, 'DOWN', "Response content should be DOWN")
self.assertEqual(status_code, status.HTTP_500_INTERNAL_SERVER_ERROR,
"Should return 500 status code.")
def test_500(self, mock_abs):
"""A 500 response should be returned."""
# Raise a general exception from the get_abs_page controller.
mock_abs.side_effect = AbsException
"""Disable logging to avoid messy output during testing"""
self.app.logger.disabled = True
with self.assertRaises(AbsException):
response = self.client.get('/abs/1234.5678')
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR)
self.assertIn('text/html', response.content_type)
def test_index_connection_error(self, mock_search):
"""When an IndexConnectionError occurs, an error page is displayed."""
mock_search.side_effect = IndexConnectionError
response = self.client.get('/?searchtype=title&query=foo')
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR)
self.assertIn('text/html', response.content_type)
Search result response data.
int
HTTP status code.
dict
Headers to add to the response.
"""
try:
document_set = index.SearchSession.search( # type: ignore
SimpleQuery(search_field="all", value="theory")
)
except Exception:
return "DOWN", status.HTTP_500_INTERNAL_SERVER_ERROR, {}
if document_set["results"]:
return "OK", status.HTTP_200_OK, {}
return "DOWN", status.HTTP_500_INTERNAL_SERVER_ERROR, {}
def handle_internal_server_error(error: InternalServerError) -> Response:
"""Render the base 500 error page."""
if isinstance(error, HTTPException):
rendered = jsonify({'code': error.code, 'error': error.description})
else:
logger.error('Caught unhandled exception: %s', error)
rendered = jsonify({'code': status.HTTP_500_INTERNAL_SERVER_ERROR,
'error': 'Unexpected error'})
response: Response = make_response(rendered)
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return response
def handle_internal_server_error(error: InternalServerError) -> Response:
"""Render the base 500 error page."""
if isinstance(error, HTTPException):
rendered = jsonify({'code': error.code, 'error': error.description})
else:
logger.error('Caught unhandled exception: %s', error)
rendered = jsonify({'code': status.HTTP_500_INTERNAL_SERVER_ERROR,
'error': 'Unexpected error'})
response: Response = make_response(rendered)
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return response