Skip to content

Commit

Permalink
Catch invalid sort param exception
Browse files Browse the repository at this point in the history
Providing a sort param like `sort=title xx desc` or `sort=title xx`
causes an exception due to an uncaught `ValidationError`
  • Loading branch information
amercader authored and smotornyuk committed Jun 29, 2017
1 parent b499f8a commit afd03e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
26 changes: 18 additions & 8 deletions ckan/controllers/group.py
Expand Up @@ -171,14 +171,24 @@ def index(self):
context['user_id'] = c.userobj.id
context['user_is_admin'] = c.userobj.sysadmin

data_dict_global_results = {
'all_fields': False,
'q': q,
'sort': sort_by,
'type': group_type or 'group',
}
global_results = self._action('group_list')(context,
data_dict_global_results)
try:
data_dict_global_results = {
'all_fields': False,
'q': q,
'sort': sort_by,
'type': group_type or 'group',
}
global_results = self._action('group_list')(context,
data_dict_global_results)
except ValidationError as e:
if e.error_dict and e.error_dict.get('message'):
msg = e.error_dict['message']
else:
msg = str(e)
h.flash_error(msg)
c.page = h.Page([], 0)
return render(self._index_template(group_type),
extra_vars={'group_type': group_type})

data_dict_page_results = {
'all_fields': True,
Expand Down
14 changes: 14 additions & 0 deletions ckan/tests/controllers/test_group.py
Expand Up @@ -53,6 +53,20 @@ def test_page_thru_list_of_groups_preserves_sort_order(self):
assert groups[-1]['title'] not in response2
assert groups[0]['title'] in response2

def test_invalid_sort_param_does_not_crash(self):
app = self._get_test_app()
group_url = url_for(controller='group',
action='index',
sort='title desc nope')

app.get(url=group_url)

group_url = url_for(controller='group',
action='index',
sort='title nope desc nope')

app.get(url=group_url)


def _get_group_new_page(app):
user = factories.User()
Expand Down

0 comments on commit afd03e4

Please sign in to comment.