Skip to content

Commit

Permalink
SPC-130 SPC-131 Search users by ID (#50)
Browse files Browse the repository at this point in the history
* SPC-130 Add users to main nav

* SPC-131 Search users by ID

* SPC-131 Test chain user_list action

* SPC-131 Fix tests
  • Loading branch information
jonathansberry committed Jul 31, 2023
1 parent d5b88dc commit b0b41c6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
15 changes: 15 additions & 0 deletions ckanext/spectrum/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import ckan.plugins.toolkit as toolkit
from ckan.plugins.toolkit import ValidationError, _
from ckan.logic import NotFound


log = logging.getLogger(__name__)

Expand Down Expand Up @@ -46,6 +48,19 @@ def package_create(next_action, context, data_dict):
return next_action(context, data_dict)


@toolkit.chained_action
def user_list(next_action, context, data_dict):
try:
user_from_id = toolkit.get_action('user_show')(
context,
{'id': data_dict.get('q', '')}
)
data_dict['q'] = user_from_id.get('name')
except toolkit.ObjectNotFound:
pass
return next_action(context, data_dict)


@toolkit.chained_action
def user_create(next_action, context, data_dict):
if not data_dict.get('password'):
Expand Down
1 change: 1 addition & 0 deletions ckanext/spectrum/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def get_auth_functions(self):
# IActions
def get_actions(self):
return {
'user_list': spectrum_actions.user_list,
'user_create': spectrum_actions.user_create,
'dataset_duplicate': spectrum_actions.dataset_duplicate,
'package_create': spectrum_actions.package_create,
Expand Down
3 changes: 2 additions & 1 deletion ckanext/spectrum/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
{% block header_site_navigation_tabs %}
{{ h.build_nav_main(
('home.index', _('Home')),
('dataset.search', _('Datasets')) ) }}
('dataset.search', _('Datasets')),
('user.index', _('Users')) ) }}
{% endblock %}

{% block header_wrapper %}
Expand Down
36 changes: 36 additions & 0 deletions ckanext/spectrum/tests/test_action_user_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest
import ckan.tests.factories as factories
from ckan.tests.helpers import call_action


@pytest.mark.ckan_config('ckan.plugins', "spectrum")
@pytest.mark.usefixtures('clean_db', 'with_plugins')
class TestListUsers():

def test_empty_query(self):
users = [factories.User(name=f"{i}01dec4a-6cc9-49cd-91ea-cc0e09ba620d") for i in range(3)]
response = call_action(
'user_list',
q=''
)
user_ids_created = {u['id'] for u in users}
user_ids_found = {u['id'] for u in response}
assert user_ids_created == user_ids_found

def test_search_by_id(self):
users = [factories.User(name=f"{i}01dec4a-6cc9-49cd-91ea-cc0e09ba620d") for i in range(3)]
response = call_action(
'user_list',
q=users[1]['id']
)
user_ids_found = [u['id'] for u in response]
assert user_ids_found == [users[1]['id']]

def test_search_by_non_id(self):
users = [factories.User(name=f"{i}01dec4a-6cc9-49cd-91ea-cc0e09ba620d") for i in range(3)]
response = call_action(
'user_list',
q=users[2]['name']
)
user_ids_found = [u['id'] for u in response]
assert user_ids_found == [users[2]['id']]

0 comments on commit b0b41c6

Please sign in to comment.