Projet

Général

Profil

0001-change-serialization-format-for-attribute-kind-strin.patch

Benjamin Dauvergne, 24 mars 2017 14:03

Télécharger (5,9 ko)

Voir les différences:

Subject: [PATCH] change serialization format for attribute kind string, title
 and boolean (fixes #15607)

 src/authentic2/attribute_kinds.py                  |  6 ++-
 .../0017_modify_boolean_serialization.py           | 60 ++++++++++++++++++++++
 tests/test_api.py                                  |  2 +-
 tests/test_auth_oidc.py                            |  8 +--
 4 files changed, 69 insertions(+), 7 deletions(-)
 create mode 100644 src/authentic2/migrations/0017_modify_boolean_serialization.py
src/authentic2/attribute_kinds.py
45 45
        'label': _('boolean'),
46 46
        'name': 'boolean',
47 47
        'field_class': forms.BooleanField,
48
        'serialize': lambda x: str(int(bool(x))),
49
        'deserialize': lambda x: bool(int(x)),
48 50
    },
49 51
]
50 52

  
......
105 107
def get_kind(kind):
106 108
    d = get_attribute_kinds()[kind]
107 109
    d.setdefault('default', None)
108
    d.setdefault('serialize', json.dumps)
109
    d.setdefault('deserialize', json.loads)
110
    d.setdefault('serialize', lambda x: x)
111
    d.setdefault('deserialize', lambda x: x)
110 112
    return d
src/authentic2/migrations/0017_modify_boolean_serialization.py
1
# -*- coding: utf-8 -*-
2
import json
3

  
4
from django.db import migrations
5

  
6

  
7
def clean_null(apps, schema_editor):
8
    AttributeValue = apps.get_model('authentic2', 'AttributeValue')
9
    AttributeValue.objects.filter(attribute__required=True, content='null').update(content='""')
10
    AttributeValue.objects.filter(attribute__required=False, content='null').delete()
11
    AttributeValue.objects.filter(attribute__required=False, content='""').delete()
12

  
13

  
14
def noop(apps, schema_editor):
15
    pass
16

  
17

  
18
def modify_string_serialization(apps, schema_editor):
19
    AttributeValue = apps.get_model('authentic2', 'AttributeValue')
20
    for atv in AttributeValue.objects.filter(attribute__kind__in=['string', 'title']):
21
        b = json.loads(atv.content)
22
        assert isinstance(b, unicode)
23
        atv.content = b
24
        atv.save()
25

  
26

  
27
def reverse_modify_string_serialization(apps, schema_editor):
28
    AttributeValue = apps.get_model('authentic2', 'AttributeValue')
29
    for atv in AttributeValue.objects.filter(attribute__kind__in=['string', 'title']):
30
        atv.content = json.dumps(atv.content)
31
        atv.save()
32

  
33

  
34
def modify_boolean_serialization(apps, schema_editor):
35
    AttributeValue = apps.get_model('authentic2', 'AttributeValue')
36
    for atv in AttributeValue.objects.filter(attribute__kind='boolean'):
37
        b = json.loads(atv.content)
38
        atv.content = str(int(bool(b)))
39
        atv.save()
40

  
41

  
42
def reverse_modify_boolean_serialization(apps, schema_editor):
43
    AttributeValue = apps.get_model('authentic2', 'AttributeValue')
44
    for atv in AttributeValue.objects.filter(attribute__kind='boolean'):
45
        b = bool(int(atv.content))
46
        atv.content = json.dumps(b)
47
        atv.save()
48

  
49

  
50
class Migration(migrations.Migration):
51

  
52
    dependencies = [
53
        ('authentic2', '0016_attribute_disabled'),
54
    ]
55

  
56
    operations = [
57
        migrations.RunPython(clean_null, noop),
58
        migrations.RunPython(modify_string_serialization, reverse_modify_string_serialization),
59
        migrations.RunPython(modify_boolean_serialization, reverse_modify_boolean_serialization),
60
    ]
tests/test_api.py
193 193
        assert new_user.last_name == resp.json['last_name']
194 194
        assert AttributeValue.objects.with_owner(new_user).count() == 3
195 195
        assert AttributeValue.objects.with_owner(new_user).filter(attribute=at).exists()
196
        assert (json.loads(AttributeValue.objects.with_owner(new_user).get(attribute=at).content) ==
196
        assert (AttributeValue.objects.with_owner(new_user).get(attribute=at).content ==
197 197
                payload['title'])
198 198
        resp2 = app.get('/api/users/%s/' % resp.json['uuid'])
199 199
        assert resp.json == resp2.json
tests/test_auth_oidc.py
272 272
    assert user.email == 'john.doe@example.com'
273 273
    assert user.attributes.first_name == 'John'
274 274
    assert user.attributes.last_name == 'Doe'
275
    assert AttributeValue.objects.filter(content='"John"', verified=True).count() == 1
276
    assert AttributeValue.objects.filter(content='"Doe"', verified=False).count() == 1
275
    assert AttributeValue.objects.filter(content='John', verified=True).count() == 1
276
    assert AttributeValue.objects.filter(content='Doe', verified=False).count() == 1
277 277

  
278 278
    with oidc_provider_mock(oidc_provider, code, extra_user_info={'family_name_verified': True}):
279 279
        response = app.get(login_callback_url, params={'code': code, 'state': query['state']})
280
    assert AttributeValue.objects.filter(content='"Doe"', verified=False).count() == 0
281
    assert AttributeValue.objects.filter(content='"Doe"', verified=True).count() == 1
280
    assert AttributeValue.objects.filter(content='Doe', verified=False).count() == 0
281
    assert AttributeValue.objects.filter(content='Doe', verified=True).count() == 1
282 282

  
283 283
    response = app.get(reverse('account_management'))
284 284
    with utils.check_log(caplog, 'revoked token from OIDC'):
285
-