Projet

Général

Profil

0002-custom_user-user-DRF-field-to-serializer-custom-attr.patch

Benjamin Dauvergne, 07 août 2019 10:41

Télécharger (4,86 ko)

Voir les différences:

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(-)
src/authentic2/custom_user/models.py
270 270

  
271 271
    def to_json(self):
272 272
        d = {}
273
        attributes_map = get_attributes_map()
273 274
        for av in AttributeValue.objects.with_owner(self):
274
            d[str(av.attribute.name)] = av.to_python()
275
            attribute = attributes_map[av.attribute_id]
276
            drf_field = attribute.get_drf_field()
277
            d[str(attribute.name)] = drf_field.to_representation(av.to_python())
275 278
        d.update({
276 279
            'uuid': self.uuid,
277 280
            'username': self.username,
tests/test_api.py
16 16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 17

  
18 18

  
19
import datetime
19 20
import json
20 21
import pytest
21 22
import random
......
33 34

  
34 35
from authentic2.a2_rbac.models import Role
35 36
from authentic2.a2_rbac.utils import get_default_ou
36
from authentic2.models import Service
37
from authentic2.models import Service, Attribute, AttributeValue
37 38
from authentic2.utils import good_next_url
38 39

  
39 40
from utils import login, basic_authorization_header, get_link_from_mail
......
47 48
    resp = logged_app.get('/api/user/')
48 49
    assert isinstance(resp.json, dict)
49 50
    assert 'username' in resp.json
50
    assert 'username' in resp.json
51 51

  
52 52

  
53 53
def test_api_user(client):
54 54
    # create an user, an ou role, a service and a service role
55 55
    ou = get_default_ou()
56 56

  
57
    Attribute.objects.create(kind='birthdate', name='birthdate', label='birthdate', required=True)
57 58
    User = get_user_model()
58 59
    user = User.objects.create(ou=ou, username='john.doe', first_name=u'Jôhn',
59 60
                               last_name=u'Doe', email='john.doe@example.net')
61
    user.attributes.birthdate = datetime.date(2019, 2, 2)
60 62
    user.set_password('password')
61 63
    user.save()
62 64

  
......
83 85
    assert set(data.keys()) == set(['uuid', 'username', 'first_name',
84 86
                                    'ou__slug', 'ou__uuid', 'ou__name',
85 87
                                    'last_name', 'email', 'roles', 'services',
86
                                    'is_superuser', 'ou'])
88
                                    'is_superuser', 'ou', 'birthdate'])
87 89
    assert data['uuid'] == user.uuid
88 90
    assert data['username'] == user.username
89 91
    assert data['first_name'] == user.first_name
......
94 96
    assert data['ou__name'] == ou.name
95 97
    assert data['ou__slug'] == ou.slug
96 98
    assert data['ou__uuid'] == ou.uuid
99
    assert data['birthdate'] == '2019-02-02'
97 100
    assert isinstance(data['roles'], list)
98 101
    assert len(data['roles']) == 2
99 102
    for role in data['roles']:
......
301 304

  
302 305

  
303 306
def test_api_users_boolean_attribute(app, superuser):
304
    from authentic2.models import Attribute, AttributeValue
305 307
    at = Attribute.objects.create(
306 308
            kind='boolean', name='boolean', label='boolean', required=True)
307 309
    superuser.attributes.boolean = True
......
311 313

  
312 314

  
313 315
def test_api_users_boolean_attribute_optional(app, superuser):
314
    from authentic2.models import Attribute, AttributeValue
315 316
    at = Attribute.objects.create(
316 317
        kind='boolean', name='boolean', label='boolean', required=False)
317 318
    superuser.attributes.boolean = True
......
365 366

  
366 367
def test_api_users_create(settings, app, api_user):
367 368
    from django.contrib.auth import get_user_model
368
    from authentic2.models import Attribute, AttributeValue
369 369

  
370 370
    at = Attribute.objects.create(kind='title', name='title', label='title')
371 371
    app.authorization = ('Basic', (api_user.username, api_user.username))
......
562 562

  
563 563

  
564 564
def test_api_users_create_send_mail(app, settings, superuser, rf):
565
    from authentic2.models import Attribute
566

  
567 565
    # Use case is often that Email is the main identifier
568 566
    settings.A2_EMAIL_IS_UNIQUE = True
569 567
    Attribute.objects.create(kind='title', name='title', label='title')
570
-