api: allow filtering roles list (#57504)
This commit is contained in:
parent
95a7417ed9
commit
85411f3376
|
@ -31,8 +31,7 @@ Depends: ${misc:Depends}, ${python3:Depends},
|
|||
python3-ldap (>= 2.4),
|
||||
python3-jwcrypto (>= 0.3.1),
|
||||
python3-cryptography (>= 1.3.4),
|
||||
python3-django-filters (>= 1),
|
||||
python3-django-filters (<< 2.3),
|
||||
python3-django-filters,
|
||||
python3-pil,
|
||||
python3-tablib,
|
||||
python3-chardet,
|
||||
|
|
2
setup.py
2
setup.py
|
@ -131,7 +131,7 @@ setup(
|
|||
'djangorestframework>=3.3,<3.10',
|
||||
'Markdown>=2.1',
|
||||
'python-ldap',
|
||||
'django-filter>1,<2.3',
|
||||
'django-filter',
|
||||
'pycryptodomex',
|
||||
'django-mellon>=1.22',
|
||||
'ldaptools',
|
||||
|
|
|
@ -880,9 +880,21 @@ class UsersAPI(api_mixins.GetOrCreateMixinView, HookMixin, ExceptionHandlerMixin
|
|||
)
|
||||
|
||||
|
||||
class RolesFilter(FilterSet):
|
||||
class Meta:
|
||||
model = get_role_model()
|
||||
fields = {
|
||||
'uuid': ['exact'],
|
||||
'name': ['exact', 'iexact', 'icontains', 'startswith'],
|
||||
'slug': ['exact', 'iexact', 'icontains', 'startswith'],
|
||||
}
|
||||
|
||||
|
||||
class RolesAPI(api_mixins.GetOrCreateMixinView, ExceptionHandlerMixin, ModelViewSet):
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
serializer_class = RoleSerializer
|
||||
filter_backends = api_settings.DEFAULT_FILTER_BACKENDS
|
||||
filterset_class = RolesFilter
|
||||
lookup_field = 'uuid'
|
||||
|
||||
def get_queryset(self):
|
||||
|
|
|
@ -1495,6 +1495,35 @@ def test_api_get_role_list(app, admin_ou1, role_ou1, role_random):
|
|||
assert field in role_dict
|
||||
|
||||
|
||||
def test_api_filter_role_list(app, superuser):
|
||||
app.authorization = ('Basic', (superuser.username, superuser.username))
|
||||
role = Role.objects.create(name='Test role')
|
||||
for i in range(10):
|
||||
Role.objects.create(name=f'Prefixed test role {i}')
|
||||
|
||||
resp = app.get('/api/roles/?name__icontains=test role')
|
||||
assert len(resp.json['results']) == 11
|
||||
|
||||
resp = app.get('/api/roles/?slug__icontains=test-role')
|
||||
assert len(resp.json['results']) == 11
|
||||
|
||||
resp = app.get('/api/roles/?name__startswith=Prefixed')
|
||||
assert len(resp.json['results']) == 10
|
||||
|
||||
resp = app.get('/api/roles/?slug__startswith=prefixed')
|
||||
assert len(resp.json['results']) == 10
|
||||
|
||||
resp = app.get('/api/roles/?uuid=%s' % role.uuid)
|
||||
assert len(resp.json['results']) == 1
|
||||
assert resp.json['results'][0]['name'] == 'Test role'
|
||||
|
||||
resp = app.get('/api/roles/?name=Test role')
|
||||
assert len(resp.json['results']) == 1
|
||||
|
||||
resp = app.get('/api/roles/?name=test role')
|
||||
assert len(resp.json['results']) == 0
|
||||
|
||||
|
||||
def test_api_get_role_member_list(app, admin_ou1, user_ou1, role_ou1, role_random):
|
||||
app.authorization = ('Basic', (admin_ou1.username, admin_ou1.username))
|
||||
url = reverse('a2-api-role-members-list', kwargs={'role_uuid': role_ou1.uuid})
|
||||
|
|
Loading…
Reference in New Issue