From 892de862907bd5ce7f3696848874241d01dd22b8 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 4 Sep 2018 23:40:37 +0200 Subject: [PATCH] api: fix case of boolean user's attributs (fixes #26113) As NullBooleanField does not support the allow_null init attribute, we must special case it and remember to never use BooleanField. --- src/authentic2/api_views.py | 6 +++++- tests/test_api.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/authentic2/api_views.py b/src/authentic2/api_views.py index b6fa431a..5e301294 100644 --- a/src/authentic2/api_views.py +++ b/src/authentic2/api_views.py @@ -339,7 +339,11 @@ class BaseUserSerializer(serializers.ModelSerializer): }) if not at.required: # setting an attribute to null will delete it - kwargs['allow_null'] = True + # NullBooleanField and BooleanField does not support allow_null + if field_class in (serializers.NullBooleanField, serializers.BooleanField): + field_class = serializers.NullBooleanField + else: + kwargs['allow_null'] = True # if not stated otherwise by the definition of the kind, string alike fields # accept blank values when not required if (issubclass(field_class, serializers.CharField) and 'allow_blank' not in diff --git a/tests/test_api.py b/tests/test_api.py index 240b0382..f1c82e16 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -151,6 +151,16 @@ def test_api_users_boolean_attribute(app, superuser): assert resp.json['boolean'] is True +def test_api_users_boolean_attribute_optional(app, superuser): + from authentic2.models import Attribute, AttributeValue + at = Attribute.objects.create( + kind='boolean', name='boolean', label='boolean', required=False) + superuser.attributes.boolean = True + app.authorization = ('Basic', (superuser.username, superuser.username)) + resp = app.get('/api/users/%s/' % superuser.uuid) + assert resp.json['boolean'] is True + + def test_api_users_list_by_authorized_service(app, superuser): from authentic2.models import Service -- 2.18.0