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.
This commit is contained in:
Benjamin Dauvergne 2018-09-04 23:40:37 +02:00
parent da31b8a52d
commit e1d73640b9
2 changed files with 15 additions and 1 deletions

View File

@ -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 is serializers.BooleanField:
field_class = serializers.NullBooleanField
elif field_class is not serializers.NullBooleanField:
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

View File

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