Skip to content

Commit

Permalink
username_autocomplete filter in legacy roles endpoint (#1940)
Browse files Browse the repository at this point in the history
* Add username_autocomplete to LegacyRoleFilter
* Add int test
* namespace__name__icontains filter
Issue: AAH-2782
  • Loading branch information
jerabekjiri authored Oct 18, 2023
1 parent 133f4c8 commit ca12993
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/2782.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``username_autocomplete`` filter to ``LegacyRole``.
10 changes: 10 additions & 0 deletions galaxy_ng/app/api/v1/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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
69 changes: 69 additions & 0 deletions galaxy_ng/tests/integration/api/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import json
import pytest
import subprocess

from urllib.parse import urlparse

Expand Down Expand Up @@ -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 """
Expand Down

0 comments on commit ca12993

Please sign in to comment.