Projet

Général

Profil

0001-enforce-birthdate-1900-through-UI-and-API-fixes-2686.patch

Benjamin Dauvergne, 02 octobre 2018 17:43

Télécharger (5,78 ko)

Voir les différences:

Subject: [PATCH] enforce birthdate > 1900 through UI and API (fixes @26867)

 src/authentic2/attribute_kinds.py | 21 +++++--
 tests/test_attribute_kinds.py     | 92 ++++++++++++++++++++++++++++++-
 2 files changed, 106 insertions(+), 7 deletions(-)
src/authentic2/attribute_kinds.py
32 32
    def __init__(self, *args, **kwargs):
33 33
        options = kwargs.setdefault('options', {})
34 34
        options['endDate'] = '-1d'
35
        options['startDate'] = '1900-01-01'
35 36
        super(BirthdateWidget, self).__init__(*args, **kwargs)
36 37

  
37 38

  
39
def validate_birthdate(value):
40
    if value and not (datetime.date(1900, 1, 1) <= value < datetime.date.today()):
41
        raise ValidationError(_('birthdate must be in the past and greater or equal than 1900-01-01.'))
42

  
43

  
38 44
class BirthdateField(forms.DateField):
39 45
    widget = BirthdateWidget
46
    default_validators = [
47
        validate_birthdate,
48
    ]
40 49

  
41
    def clean(self, value):
42
        value = super(BirthdateField, self).clean(value)
43
        if value and value >= datetime.date.today():
44
            raise ValidationError(_('birthdate must be in the past.'))
45
        return value
50

  
51
class BirthdateRestField(serializers.DateField):
52
    default_validators = [
53
        validate_birthdate,
54
    ]
46 55

  
47 56

  
48 57
@to_iter
......
137 146
        'field_class': BirthdateField,
138 147
        'serialize': lambda x: x.isoformat(),
139 148
        'deserialize': lambda x: x and datetime.datetime.strptime(x, '%Y-%m-%d').date(),
140
        'rest_framework_field_class': serializers.DateField,
149
        'rest_framework_field_class': BirthdateRestField,
141 150
    },
142 151
    {
143 152
        'label': _('french postcode'),
tests/test_attribute_kinds.py
1 1
# -*- coding: utf-8 -*-
2
import datetime
3

  
2 4
from authentic2.custom_user.models import User
3 5
from authentic2.models import Attribute
4 6

  
......
168 170
    response = form.submit().follow()
169 171
    assert 'john.doe@example.com' in response
170 172
    url = get_link_from_mail(mailoutbox[0])
171
    response = app.get(url)
172 173

  
174
    response = app.get(url)
173 175
    form = response.form
174 176
    form.set('first_name', 'John')
175 177
    form.set('last_name', 'Doe')
......
279 281
    app.post_json('/api/users/', params=payload)
280 282
    assert qs.get().attributes.phone_number == ''
281 283
    qs.delete()
284

  
285

  
286
def test_birthdate(db, app, admin, mailoutbox, freezer):
287
    freezer.move_to('2018-01-01')
288
    Attribute.objects.create(name='birthdate', label='birthdate', kind='birthdate',
289
                             asked_on_registration=True)
290
    qs = User.objects.filter(first_name='John')
291

  
292
    response = app.get('/accounts/register/')
293
    form = response.form
294
    form.set('email', 'john.doe@example.com')
295
    response = form.submit().follow()
296
    assert 'john.doe@example.com' in response
297
    url = get_link_from_mail(mailoutbox[0])
298
    response = app.get(url)
299

  
300
    form = response.form
301
    form.set('first_name', 'John')
302
    form.set('last_name', 'Doe')
303
    form.set('birthdate', '2018-01-01')
304
    form.set('password1', '12345abcdA')
305
    form.set('password2', '12345abcdA')
306
    response = form.submit()
307
    assert response.pyquery.find('.form-field-error #id_birthdate')
308

  
309
    form.set('birthdate', '2017-12-31')
310
    form.set('password1', '12345abcdA')
311
    form.set('password2', '12345abcdA')
312
    form.submit().follow()
313
    assert qs.get().attributes.birthdate == datetime.date(2017, 12, 31)
314
    qs.delete()
315

  
316
    response = app.get(url)
317
    form = response.form
318
    form.set('first_name', 'John')
319
    form.set('last_name', 'Doe')
320
    form.set('birthdate', '1899-12-31')
321
    form.set('password1', '12345abcdA')
322
    form.set('password2', '12345abcdA')
323
    response = form.submit()
324
    assert response.pyquery.find('.form-field-error #id_birthdate')
325

  
326
    form.set('birthdate', '1900-01-01')
327
    form.set('password1', '12345abcdA')
328
    form.set('password2', '12345abcdA')
329
    form.submit().follow()
330
    assert qs.get().attributes.birthdate == datetime.date(1900, 1, 1)
331
    qs.delete()
332

  
333

  
334
def test_birthdate_api(db, app, admin, mailoutbox, freezer):
335
    freezer.move_to('2018-01-01')
336
    Attribute.objects.create(name='birthdate', label='birthdate', kind='birthdate',
337
                             asked_on_registration=True)
338
    qs = User.objects.filter(first_name='John')
339
    app.authorization = ('Basic', (admin.username, admin.username))
340

  
341
    payload = {
342
        'first_name': 'John',
343
        'last_name': 'Doe',
344
        'birthdate': '2018-01-01',
345
    }
346
    app.post_json('/api/users/', params=payload, status=400)
347

  
348
    payload = {
349
        'first_name': 'John',
350
        'last_name': 'Doe',
351
        'birthdate': '2017-12-31',
352
    }
353
    response = app.post_json('/api/users/', params=payload)
354
    assert qs.get().attributes.birthdate == datetime.date(2017, 12, 31)
355
    qs.delete()
356

  
357
    payload = {
358
        'first_name': 'John',
359
        'last_name': 'Doe',
360
        'birthdate': '1899-12-31',
361
    }
362
    app.post_json('/api/users/', params=payload, status=400)
363

  
364
    payload = {
365
        'first_name': 'John',
366
        'last_name': 'Doe',
367
        'birthdate': '1900-01-01',
368
    }
369
    app.post_json('/api/users/', params=payload)
370
    assert qs.get().attributes.birthdate == datetime.date(1900, 1, 1)
371
    qs.delete()
282
-