Projet

Général

Profil

0001-api-allow-filtering-roles-list-57504.patch

Valentin Deniaud, 20 octobre 2021 10:52

Télécharger (4,02 ko)

Voir les différences:

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(-)
debian/control
31 31
    python3-ldap (>= 2.4),
32 32
    python3-jwcrypto (>= 0.3.1),
33 33
    python3-cryptography (>= 1.3.4),
34
    python3-django-filters (>= 1),
35
    python3-django-filters (<< 2.3),
34
    python3-django-filters,
36 35
    python3-pil,
37 36
    python3-tablib,
38 37
    python3-chardet,
setup.py
131 131
        'djangorestframework>=3.3,<3.10',
132 132
        'Markdown>=2.1',
133 133
        'python-ldap',
134
        'django-filter>1,<2.3',
134
        'django-filter',
135 135
        'pycryptodomex',
136 136
        'django-mellon>=1.22',
137 137
        'ldaptools',
src/authentic2/api_views.py
880 880
        )
881 881

  
882 882

  
883
class RolesFilter(FilterSet):
884
    class Meta:
885
        model = get_role_model()
886
        fields = {
887
            'uuid': ['exact'],
888
            'name': ['exact', 'iexact', 'icontains', 'startswith'],
889
            'slug': ['exact', 'iexact', 'icontains', 'startswith'],
890
        }
891

  
892

  
883 893
class RolesAPI(api_mixins.GetOrCreateMixinView, ExceptionHandlerMixin, ModelViewSet):
884 894
    permission_classes = (permissions.IsAuthenticated,)
885 895
    serializer_class = RoleSerializer
896
    filter_backends = api_settings.DEFAULT_FILTER_BACKENDS
897
    filterset_class = RolesFilter
886 898
    lookup_field = 'uuid'
887 899

  
888 900
    def get_queryset(self):
tests/test_api.py
1495 1495
            assert field in role_dict
1496 1496

  
1497 1497

  
1498
def test_api_filter_role_list(app, superuser):
1499
    app.authorization = ('Basic', (superuser.username, superuser.username))
1500
    role = Role.objects.create(name='Test role')
1501
    for i in range(10):
1502
        Role.objects.create(name=f'Prefixed test role {i}')
1503

  
1504
    resp = app.get('/api/roles/?name__icontains=test role')
1505
    assert len(resp.json['results']) == 11
1506

  
1507
    resp = app.get('/api/roles/?slug__icontains=test-role')
1508
    assert len(resp.json['results']) == 11
1509

  
1510
    resp = app.get('/api/roles/?name__startswith=Prefixed')
1511
    assert len(resp.json['results']) == 10
1512

  
1513
    resp = app.get('/api/roles/?slug__startswith=prefixed')
1514
    assert len(resp.json['results']) == 10
1515

  
1516
    resp = app.get('/api/roles/?uuid=%s' % role.uuid)
1517
    assert len(resp.json['results']) == 1
1518
    assert resp.json['results'][0]['name'] == 'Test role'
1519

  
1520
    resp = app.get('/api/roles/?name=Test role')
1521
    assert len(resp.json['results']) == 1
1522

  
1523
    resp = app.get('/api/roles/?name=test role')
1524
    assert len(resp.json['results']) == 0
1525

  
1526

  
1498 1527
def test_api_get_role_member_list(app, admin_ou1, user_ou1, role_ou1, role_random):
1499 1528
    app.authorization = ('Basic', (admin_ou1.username, admin_ou1.username))
1500 1529
    url = reverse('a2-api-role-members-list', kwargs={'role_uuid': role_ou1.uuid})
tox.ini
74 74
  oldldap: python-ldap<3
75 75
  py2: django-appconf<1.0.4
76 76
  py2: django-filter<2
77
  py3: django-filter<2.3
77
  py3: django-filter
78 78
  drf34: djangorestframework>=3.4,<3.4.1
79 79
  drf39: djangorestframework>=3.9.2,<3.10
80 80
usedevelop = True
81
-