Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

username_autocomplete filter in legacy roles endpoint #1940

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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``.
14 changes: 14 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,16 @@ 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(
Q(namespace__name__contains=keyword)
| Q(name__contains=keyword)
| Q(full_metadata__description__contains=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
Loading