Projet

Général

Profil

0001-api_views-provide-a-default-slug-for-ous-22250.patch

Paul Marillonnet, 16 septembre 2020 08:53

Télécharger (4,74 ko)

Voir les différences:

Subject: [PATCH] api_views: provide a default slug for ous (#22250)

 src/authentic2/api_views.py | 21 +++++++++++++-
 tests/test_api.py           | 58 +++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 1 deletion(-)
src/authentic2/api_views.py
25 25
from django.contrib.auth.hashers import identify_hasher
26 26
from django.core.exceptions import MultipleObjectsReturned
27 27
from django.utils.translation import ugettext as _
28
from django.utils.text import slugify
28 29
from django.utils.encoding import force_text
29 30
from django.utils.dateparse import parse_datetime
30 31
from django.views.decorators.vary import vary_on_headers
......
887 888
role_memberships = RoleMembershipsAPI.as_view()
888 889

  
889 890

  
891
class SlugFromNameDefault:
892
    requires_context = False
893
    serializer_instance = None
894

  
895
    def set_context(self, instance):
896
        self.serializer_instance = instance
897

  
898
    def __call__(self):
899
        name = self.serializer_instance.context['request'].data.get('name', '')
900
        return slugify(name)
901

  
902

  
890 903
class BaseOrganizationalUnitSerializer(serializers.ModelSerializer):
904
    slug = serializers.SlugField(
905
            required=False,
906
            allow_blank=False,
907
            max_length=127,
908
            default=SlugFromNameDefault(),
909
        )
891 910
    class Meta:
892 911
        model = get_ou_model()
893 912
        fields = '__all__'
894 913

  
895 914

  
896
class OrganizationalUnitAPI(ExceptionHandlerMixin, ModelViewSet):
915
class OrganizationalUnitAPI(api_mixins.GetOrCreateMixinView, ExceptionHandlerMixin, ModelViewSet):
897 916
    permission_classes = (DjangoPermission('a2_rbac.search_organizationalunit'),)
898 917
    serializer_class = BaseOrganizationalUnitSerializer
899 918
    lookup_field = 'uuid'
tests/test_api.py
30 30
from django.core import mail
31 31
from django.urls import reverse
32 32
from django.utils.encoding import force_text
33
from django.utils.text import slugify
33 34
from django.utils.timezone import now
34 35
from django.utils.http import urlencode
35 36

  
......
1254 1255
    assert role.ou == get_default_ou()
1255 1256

  
1256 1257

  
1258
def test_api_post_ou_no_slug(app, superuser):
1259
    app.authorization = ('Basic', (superuser.username, superuser.username))
1260
    OU = get_ou_model()
1261

  
1262
    ou_data = {
1263
        'name': 'Some Organizational Unit',
1264
        }
1265
    resp = app.post_json('/api/ous/', params=ou_data)
1266
    uuid = resp.json['uuid']
1267
    ou = OU.objects.get(uuid=uuid)
1268
    assert ou.id != get_default_ou().id
1269
    assert ou.slug == slugify(ou.name)
1270
    assert ou.slug == slugify(ou_data['name'])
1271
    # another call with same ou name
1272
    ou_data = {
1273
        'name': 'Some Organizational Unit',
1274
        }
1275
    resp = app.post_json('/api/ous/', params=ou_data, status=400)
1276
    assert resp.json['errors']['__all__'] == [
1277
            "The fields name must make a unique set.",
1278
            "The fields slug must make a unique set."
1279
        ]
1280
    # no slug no name
1281
    ou_data = {
1282
        'id': 42,
1283
        }
1284
    resp = app.post_json('/api/ous/', params=ou_data, status=400)
1285
    assert resp.json['errors']['name'] == ['This field is required.']
1286

  
1287

  
1288
def test_api_post_ou_get_or_create(app, superuser):
1289
    app.authorization = ('Basic', (superuser.username, superuser.username))
1290
    OU = get_ou_model()
1291
    # first get-or-create? -> create
1292
    ou_data = {
1293
        'name': 'Some Organizational Unit',
1294
        }
1295
    resp = app.post_json('/api/ous/', params=ou_data)
1296
    uuid = resp.json['uuid']
1297
    ou = OU.objects.get(uuid=uuid)
1298
    # second get-or-create? -> get
1299
    ou_data = {
1300
        'name': 'Some Organizational Unit',
1301
        'slug': ou.slug,
1302
        }
1303
    resp = app.post_json('/api/ous/?get_or_create=slug', params=ou_data)
1304
    assert resp.json['uuid'] == ou.uuid
1305
    # update-or-create? -> update
1306
    ou_data = {
1307
        'name': 'Another name',
1308
        'slug': ou.slug,
1309
        }
1310
    resp = app.post_json('/api/ous/?update_or_create=slug', params=ou_data)
1311
    assert resp.json['uuid'] == ou.uuid
1312
    assert OU.objects.get(uuid=resp.json['uuid']).name == ou_data['name']
1313

  
1314

  
1257 1315
def test_api_post_role_unauthorized(app, simple_user, ou1):
1258 1316
    app.authorization = ('Basic', (simple_user.username, simple_user.username))
1259 1317
    Role = get_role_model()
1260
-