From 85411f33768770c699ab319a7163d7d8134f4123 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 20 Oct 2021 10:09:48 +0200 Subject: [PATCH] api: allow filtering roles list (#57504) --- debian/control | 3 +-- setup.py | 2 +- src/authentic2/api_views.py | 12 ++++++++++++ tests/test_api.py | 29 +++++++++++++++++++++++++++++ tox.ini | 2 +- 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/debian/control b/debian/control index 91900253..882f3ea1 100644 --- a/debian/control +++ b/debian/control @@ -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, diff --git a/setup.py b/setup.py index 1723e294..61c6794e 100755 --- a/setup.py +++ b/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', diff --git a/src/authentic2/api_views.py b/src/authentic2/api_views.py index e13a607b..04406d94 100644 --- a/src/authentic2/api_views.py +++ b/src/authentic2/api_views.py @@ -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): diff --git a/tests/test_api.py b/tests/test_api.py index 30fc0d31..a7fd4e7b 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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}) diff --git a/tox.ini b/tox.ini index ed334276..4ac505b7 100644 --- a/tox.ini +++ b/tox.ini @@ -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 -- 2.30.2