api: allow filtering roles list (#57504)

This commit is contained in:
Valentin Deniaud 2021-10-20 10:09:48 +02:00
parent 95a7417ed9
commit 85411f3376
5 changed files with 44 additions and 4 deletions

3
debian/control vendored
View File

@ -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,

View File

@ -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',

View File

@ -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):

View File

@ -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})

View File

@ -74,7 +74,7 @@ deps =
oldldap: python-ldap<3
py2: django-appconf<1.0.4
py2: django-filter<2
py3: django-filter<2.3
py3: django-filter
drf34: djangorestframework>=3.4,<3.4.1
drf39: djangorestframework>=3.9.2,<3.10
usedevelop = True