From 76d9f637aa9e38febb45bee759406e1ce36aaae6 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 7 Aug 2019 10:41:25 +0200 Subject: [PATCH 2/2] custom_user: user DRF field to serializer custom attributes to JSON (#24401) --- src/authentic2/custom_user/models.py | 5 ++++- tests/test_api.py | 14 ++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/authentic2/custom_user/models.py b/src/authentic2/custom_user/models.py index bf37e20d..ac028b8f 100644 --- a/src/authentic2/custom_user/models.py +++ b/src/authentic2/custom_user/models.py @@ -270,8 +270,11 @@ class User(AbstractBaseUser, PermissionMixin): def to_json(self): d = {} + attributes_map = get_attributes_map() for av in AttributeValue.objects.with_owner(self): - d[str(av.attribute.name)] = av.to_python() + attribute = attributes_map[av.attribute_id] + drf_field = attribute.get_drf_field() + d[str(attribute.name)] = drf_field.to_representation(av.to_python()) d.update({ 'uuid': self.uuid, 'username': self.username, diff --git a/tests/test_api.py b/tests/test_api.py index 86694a72..c2370416 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -16,6 +16,7 @@ # along with this program. If not, see . +import datetime import json import pytest import random @@ -33,7 +34,7 @@ from django_rbac.utils import get_role_model, get_ou_model from authentic2.a2_rbac.models import Role from authentic2.a2_rbac.utils import get_default_ou -from authentic2.models import Service +from authentic2.models import Service, Attribute, AttributeValue from authentic2.utils import good_next_url from utils import login, basic_authorization_header, get_link_from_mail @@ -47,16 +48,17 @@ def test_api_user_simple(logged_app): resp = logged_app.get('/api/user/') assert isinstance(resp.json, dict) assert 'username' in resp.json - assert 'username' in resp.json def test_api_user(client): # create an user, an ou role, a service and a service role ou = get_default_ou() + Attribute.objects.create(kind='birthdate', name='birthdate', label='birthdate', required=True) User = get_user_model() user = User.objects.create(ou=ou, username='john.doe', first_name=u'Jôhn', last_name=u'Doe', email='john.doe@example.net') + user.attributes.birthdate = datetime.date(2019, 2, 2) user.set_password('password') user.save() @@ -83,7 +85,7 @@ def test_api_user(client): assert set(data.keys()) == set(['uuid', 'username', 'first_name', 'ou__slug', 'ou__uuid', 'ou__name', 'last_name', 'email', 'roles', 'services', - 'is_superuser', 'ou']) + 'is_superuser', 'ou', 'birthdate']) assert data['uuid'] == user.uuid assert data['username'] == user.username assert data['first_name'] == user.first_name @@ -94,6 +96,7 @@ def test_api_user(client): assert data['ou__name'] == ou.name assert data['ou__slug'] == ou.slug assert data['ou__uuid'] == ou.uuid + assert data['birthdate'] == '2019-02-02' assert isinstance(data['roles'], list) assert len(data['roles']) == 2 for role in data['roles']: @@ -301,7 +304,6 @@ def test_api_email_unset_verification(settings, app, admin, simple_user): def test_api_users_boolean_attribute(app, superuser): - from authentic2.models import Attribute, AttributeValue at = Attribute.objects.create( kind='boolean', name='boolean', label='boolean', required=True) superuser.attributes.boolean = True @@ -311,7 +313,6 @@ def test_api_users_boolean_attribute(app, superuser): 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 @@ -365,7 +366,6 @@ def test_api_users_list_search_text(app, superuser): def test_api_users_create(settings, app, api_user): from django.contrib.auth import get_user_model - from authentic2.models import Attribute, AttributeValue at = Attribute.objects.create(kind='title', name='title', label='title') app.authorization = ('Basic', (api_user.username, api_user.username)) @@ -562,8 +562,6 @@ def test_api_users_create_email_is_unique(settings, app, superuser): def test_api_users_create_send_mail(app, settings, superuser, rf): - from authentic2.models import Attribute - # Use case is often that Email is the main identifier settings.A2_EMAIL_IS_UNIQUE = True Attribute.objects.create(kind='title', name='title', label='title') -- 2.22.0