Projet

Général

Profil

0001-misc-add-belgian-national-register-number-validation.patch

Frédéric Péters, 23 octobre 2021 19:14

Télécharger (3,46 ko)

Voir les différences:

Subject: [PATCH] misc: add belgian national register number validation
 (#45039)

 tests/test_widgets.py | 30 ++++++++++++++++++++++++++++++
 wcs/qommon/form.py    |  8 ++++++++
 wcs/qommon/misc.py    | 24 ++++++++++++++++++++++++
 3 files changed, 62 insertions(+)
tests/test_widgets.py
1006 1006
    assert widget.has_error()
1007 1007

  
1008 1008

  
1009
def test_wcsextrastringwidget_belgian_nrn_validation():
1010
    class FakeField:
1011
        pass
1012

  
1013
    fakefield = FakeField()
1014
    fakefield.validation = {'type': 'nrn-be'}
1015

  
1016
    # regular cases
1017
    for value in ('85073003328', '17073003384', '40000095579', '00000100364', '40000100133'):
1018
        widget = WcsExtraStringWidget('test', value='foo', required=False)
1019
        widget.field = fakefield
1020
        mock_form_submission(req, widget, {'test': value})
1021
        assert not widget.has_error()
1022

  
1023
    # failing cases
1024
    for value in (
1025
        '8507300332',  # too short
1026
        '850730033281',  # too long
1027
        '8507300332A',  # not just digits
1028
        '85143003377',  # invalid month
1029
        '85073203365',  # invalid day
1030
        '85073003329',  # invalid checksum (<2000)
1031
        '17073003385',  # invalid checksum (≥2000)
1032
    ):
1033
        widget = WcsExtraStringWidget('test', value='foo', required=False)
1034
        widget.field = fakefield
1035
        mock_form_submission(req, widget, {'test': value})
1036
        assert widget.has_error()
1037

  
1038

  
1009 1039
def test_wcsextrastringwidget_iban_validation():
1010 1040
    class FakeField:
1011 1041
        pass
wcs/qommon/form.py
1112 1112
                    'function': 'validate_nir',
1113 1113
                },
1114 1114
            ),
1115
            (
1116
                'nrn-be',
1117
                {
1118
                    'title': _('National register number (Belgium)'),
1119
                    'error_message': _('Invalid national register number'),
1120
                    'function': 'validate_belgian_nrn',
1121
                },
1122
            ),
1115 1123
            (
1116 1124
                'iban',
1117 1125
                {
wcs/qommon/misc.py
842 842
    return int(nir_key) == 97 - int(string_value[:13]) % 97
843 843

  
844 844

  
845
def validate_belgian_nrn(string_value):
846
    # https://fr.wikipedia.org/wiki/Numéro_de_registre_national
847
    if not string_value:
848
        return False
849
    if len(string_value) != 11:
850
        return False
851
    if not is_ascii_digit(string_value):
852
        return False
853
    _year, month, day, _index, checksum = (
854
        string_value[:2],
855
        string_value[2:4],
856
        string_value[4:6],
857
        string_value[6:9],
858
        string_value[9:11],
859
    )
860
    if int(month) > 12:
861
        return False
862
    if int(day) > 31:
863
        return False
864
    return (97 - int(string_value[:9]) % 97 == int(checksum)) or (
865
        97 - int('2' + string_value[:9]) % 97 == int(checksum)
866
    )
867

  
868

  
845 869
IBAN_LENGTH = {
846 870
    # from https://www.iban.com/structure
847 871
    'AD': 24,
848
-