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_no_form_data(self, mock_index):
"""No form data has been submitted."""
request_data = MultiDict()
response_data, code, headers = advanced.search(request_data)
self.assertEqual(code, status.HTTP_200_OK, "Response should be OK.")
self.assertIn('form', response_data, "Response should include form.")
self.assertEqual(mock_index.search.call_count, 0,
"No search should be attempted")
def test_no_params(self, mock_index):
"""Request with no parameters."""
params = MultiDict({})
data, code, headers = api.search(params)
self.assertEqual(code, status.HTTP_200_OK, "Returns 200 OK")
self.assertIn("results", data, "Results are returned")
self.assertIn("query", data, "Query object is returned")
expected_fields = api_domain.get_required_fields() \
+ api_domain.get_default_extra_fields()
self.assertEqual(set(data["query"].include_fields),
set(expected_fields),
"Default set of fields is included")
def test_single_field_term(self, mock_index):
"""Form data are present."""
mock_index.search.return_value = dict(metadata={}, results=[])
request_data = MultiDict({
'searchtype': 'title',
'query': 'foo title'
})
response_data, code, headers = simple.search(request_data)
self.assertEqual(mock_index.search.call_count, 1,
"A search should be attempted")
call_args, call_kwargs = mock_index.search.call_args
self.assertIsInstance(call_args[0], SimpleQuery,
"An SimpleQuery is passed to the search index")
self.assertEqual(code, status.HTTP_200_OK, "Response should be OK.")
def test_index_returns_result(self, mock_index):
"""Test returns 'OK' + status 200 when index returns results."""
mock_index.search.return_value = dict(metadata={}, results=[dict()])
response, status_code, _ = health_check()
self.assertEqual(response, 'OK', "Response content should be OK")
self.assertEqual(status_code, status.HTTP_200_OK,
"Should return 200 status code.")
month['yymm'] = f"{month['year']}-{month['month']:02}" # type: ignore
month['url'] = url_for('browse.list_articles', # type: ignore
context=archive_id,
subcontext=f"{month['year']}{month['month']:02}")
response_data: Dict[str, Any] = {
'archive_id': archive_id,
'archive': archive,
'months': month_listing['month_counts'],
'listing': month_listing,
'year': str(year),
'stats_by_year': stats_by_year(archive_id, archive, years_operating(archive), year)
}
response_headers: Dict[str, Any] = {}
response_status = status.HTTP_200_OK
return response_data, response_status, response_headers
def tb_recent() -> Response:
"""Get the recent trackbacks that have been posted across the site."""
response, code, headers = tb_page.get_recent_tb_page(request.form)
if code == status.HTTP_200_OK:
return render_template('tb/recent.html', **response), code, headers # type: ignore
raise InternalServerError('Unexpected error')
'paging': paging(count, skipn, shown,
subject_or_category, time_period),
'viewing_all': shown >= count,
'template': type_to_template[list_type]
})
response_data.update(more_fewer(shown, count, shown >= count))
def author_query(article: DocMetadata, query: str)->str:
return str(url_for('search_archive',
searchtype='author',
archive=article.primary_archive.id,
query=query))
response_data['url_for_author_search'] = author_query
return response_data, status.HTTP_200_OK, response_headers
except AbsDeletedException as e:
raise AbsNotFound(data={'reason': 'deleted',
'arxiv_id_latest': arxiv_identifier.id,
'message': e})
except IdentifierIsArchiveException as e:
raise AbsNotFound(data={'reason': 'is_archive',
'arxiv_id': arxiv_id,
'archive_name': e})
except IdentifierException:
raise AbsNotFound(data={'arxiv_id': arxiv_id})
except AbsException as e:
raise InternalServerError(
'There was a problem. If this problem persists, please contact '
'help@arxiv.org.') from e
response_status = status.HTTP_200_OK
not_modified = _check_request_headers(
abs_meta, response_data, response_headers)
if not_modified:
return {}, status.HTTP_304_NOT_MODIFIED, response_headers
return response_data, response_status, response_headers
def get_main_stats_page() -> Response:
"""Minimal rendering of the main stats page."""
response_data: Dict[str, Any] = {}
return response_data, status.HTTP_200_OK, {}
def set_parameters_in_cookie(response: Response) -> Response:
"""Set request parameters in the cookie, to use as future defaults."""
if response.status_code == status.HTTP_200_OK:
data = {param: request.args[param] for param in PARAMS_TO_PERSIST
if param in request.args}
response.set_cookie(PARAMS_COOKIE_NAME, json.dumps(data))
return response