diff --git a/CHANGES/2782.feature b/CHANGES/2782.feature new file mode 100644 index 0000000000..0ca02e79c2 --- /dev/null +++ b/CHANGES/2782.feature @@ -0,0 +1 @@ +Added ``username_autocomplete`` filter to ``LegacyRole``. \ No newline at end of file diff --git a/galaxy_ng/app/api/v1/filtersets.py b/galaxy_ng/app/api/v1/filtersets.py index 8c734e7eaa..8555a82a45 100644 --- a/galaxy_ng/app/api/v1/filtersets.py +++ b/galaxy_ng/app/api/v1/filtersets.py @@ -95,6 +95,7 @@ class LegacyRoleFilter(filterset.FilterSet): tags = filters.CharFilter(method='tags_filter') tag = filters.CharFilter(method='tags_filter') autocomplete = filters.CharFilter(method='autocomplete_filter') + username_autocomplete = filters.CharFilter(method='username_autocomplete_filter') owner__username = filters.CharFilter(method='owner__username_filter') namespace = filters.CharFilter(method='namespace_filter') @@ -156,3 +157,12 @@ def autocomplete_filter(self, queryset, name, value): ) return queryset + + def username_autocomplete_filter(self, queryset, name, value): + + keywords = self.request.query_params.getlist('username_autocomplete') + + for keyword in keywords: + queryset = queryset.filter(namespace__name__icontains=keyword) + + return queryset diff --git a/galaxy_ng/tests/integration/api/test_community.py b/galaxy_ng/tests/integration/api/test_community.py index c5f29d5c8c..9aceb58f3c 100644 --- a/galaxy_ng/tests/integration/api/test_community.py +++ b/galaxy_ng/tests/integration/api/test_community.py @@ -3,6 +3,7 @@ import json import pytest +import subprocess from urllib.parse import urlparse @@ -525,6 +526,74 @@ def test_v1_autocomplete_search(ansible_config): cleanup_social_user(github_user2, ansible_config) +@pytest.mark.deployment_community +def test_v1_username_autocomplete_search(ansible_config): + """" + Tests v1 roles filtering by 'username_autocomplete' and + ansible-galaxy role search --author works as expected. + """ + + config = ansible_config("admin") + api_client = get_client( + config=config, + request_token=False, + require_auth=True + ) + + github_user = 'geerlingguy' + cleanup_social_user(github_user, ansible_config) + + # sync geerlingguy roles + pargs = json.dumps({"github_user": github_user, "limit": 5}).encode('utf-8') + resp = api_client('/api/v1/sync/', method='POST', args=pargs) + assert isinstance(resp, dict) + assert resp.get('task') is not None + wait_for_v1_task(resp=resp, api_client=api_client) + + # sync more roles + pargs = json.dumps({"limit": 5}).encode('utf-8') + resp = api_client('/api/v1/sync/', method='POST', args=pargs) + assert isinstance(resp, dict) + assert resp.get('task') is not None + wait_for_v1_task(resp=resp, api_client=api_client) + + # verify filtering works + resp = api_client(f'/api/v1/roles/?username_autocomplete={github_user}') + assert resp['count'] == 5 + for role in resp['results']: + assert role["username"] == github_user + + server = config.get("url") + + # test correct results with ansible-galaxy role search + cmd = [ + "ansible-galaxy" + + " role search" + + " --author " + + github_user + + " --server " + + server + ] + proc = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert b"Found 5 roles matching your search" in proc.stdout + + cmd = [ + "ansible-galaxy" + + " role search" + + " --author " + + github_user + + " adminer " + + " --server " + + server + ] + proc = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert b"Found 1 roles matching your search" in proc.stdout + assert b"geerlingguy.adminer" in proc.stdout + + # cleanup + cleanup_social_user(github_user, ansible_config) + + @pytest.mark.deployment_community def test_v1_role_pagination(ansible_config): """" Tests if v1 roles are auto-sorted by created """