Projet

Général

Profil

0001-use-convert_value_from_anything-even-for-empty-value.patch

Thomas Noël, 27 mars 2018 09:14

Télécharger (9,09 ko)

Voir les différences:

Subject: [PATCH] use convert_value_from_anything even for empty values
 (#22793)

 tests/test_hobo_notify.py   | 32 +++++++++++++++++++++++++++++
 tests/test_workflows.py     | 49 +++++++++++++++++++++++++++------------------
 wcs/ctl/hobo_notify.py      |  2 +-
 wcs/fields.py               |  4 ++++
 wcs/qommon/saml2.py         |  2 +-
 wcs/wf/backoffice_fields.py |  2 +-
 wcs/wf/profile.py           |  2 +-
 7 files changed, 70 insertions(+), 23 deletions(-)
tests/test_hobo_notify.py
544 544
    assert user.is_admin is True
545 545
    assert set(user.roles) == set([old_role.id])
546 546

  
547
    for bad_birthdate in ('baddate', None, ''):
548
        notification = {
549
            u'@type': u'provision',
550
            u'issuer': 'http://idp.example.net/idp/saml/metadata',
551
            u'audience': [u'test'],
552
            u'objects': {
553
                u'@type': 'user',
554
                u'data': [
555
                    {
556
                        u'uuid': u'a' * 32,
557
                        u'first_name': u'John',
558
                        u'last_name': u'Doe',
559
                        u'email': u'john.doe@example.net',
560
                        u'zipcode': u'13600',
561
                        u'birthdate': bad_birthdate,
562
                        u'is_superuser': True,
563
                        u'roles': [
564
                            {
565
                                u'uuid': u'xyz',
566
                                u'name': u'Service état civil',
567
                                u'description': u'etc.',
568
                            },
569
                        ],
570
                    }
571
                ]
572
            }
573
        }
574
        CmdHoboNotify.process_notification(notification)
575
        assert User.count() == 1
576
        user = User.select()[0]
577
        assert user.name_identifiers == ['a'*32]
578
        assert user.form_data['_birthdate'] is None
547 579

  
548 580
def notify_of_exception(exc_info, context):
549 581
    raise Exception(exc_info)
tests/test_workflows.py
2879 2879
    for date_value in (
2880 2880
            '20/03/2018',
2881 2881
            '=utils.make_date("20/03/2018")',
2882
            '=utils.make_date("20/03/2018").timetuple()'):
2882
            '=utils.make_date("20/03/2018").timetuple()',
2883
            None, ''):
2883 2884

  
2884 2885
        # check local value
2885 2886
        item.fields = [{'field_id': 'bar', 'value': date_value}]
2886 2887
        item.perform(formdata)
2887 2888
        assert User.get(user.id).form_data.get('3') == 'Plop'
2888
        assert User.get(user.id).form_data.get('4').tm_year == 2018
2889
        if date_value:
2890
            assert User.get(user.id).form_data.get('4').tm_year == 2018
2891
        else:
2892
            assert User.get(user.id).form_data.get('4') is None
2889 2893

  
2890 2894
        with mock.patch('wcs.wf.profile.http_patch_request') as http_patch_request:
2891 2895
            http_patch_request.return_value = (None, 200, '', None)
2892 2896
            get_response().process_after_jobs()
2893 2897
            assert http_patch_request.call_count == 1
2894
            assert http_patch_request.call_args[0][1] == '{"bar": "2018-03-20"}'
2898
            if date_value:
2899
                assert http_patch_request.call_args[0][1] == '{"bar": "2018-03-20"}'
2900
            else:
2901
                assert http_patch_request.call_args[0][1] == '{"bar": null}'
2895 2902

  
2896 2903
def test_set_backoffice_field(http_requests, two_pubs):
2897 2904
    Workflow.wipe()
......
3055 3062
    assert formdata.data['bo1'].get_content() == 'HELLO WORLD'
3056 3063

  
3057 3064
    # check wrong value
3058
    del formdata.data['bo1']
3059
    formdata.store()
3060
    assert not 'bo1' in formdata.data
3065
    for value in ('="HELLO"', None, '', 'BAD'):
3066
        if 'bo1' in formdata.data:
3067
            del formdata.data['bo1']
3068
            formdata.store()
3069
        assert not 'bo1' in formdata.data
3070

  
3071
        item = SetBackofficeFieldsWorkflowStatusItem()
3072
        item.parent = st1
3073
        item.fields = [{'field_id': 'bo1', 'value': value}]
3074
        item.perform(formdata)
3061 3075

  
3062
    item = SetBackofficeFieldsWorkflowStatusItem()
3063
    item.parent = st1
3064
    item.fields = [{'field_id': 'bo1', 'value': '="HELLO"'}]
3065
    item.perform(formdata)
3066

  
3067
    formdata = formdef.data_class().get(formdata.id)
3068
    assert formdata.data.get('bo1') is None
3076
        formdata = formdef.data_class().get(formdata.id)
3077
        assert formdata.data.get('bo1') is None
3069 3078

  
3070 3079
    # check wrong field
3071 3080
    item = SetBackofficeFieldsWorkflowStatusItem()
......
3230 3239
    formdata = formdef.data_class().get(formdata.id)
3231 3240
    assert datetime.date(*formdata.data['bo1'][:3]) == datetime.date(2017, 3, 23)
3232 3241

  
3233
    item = SetBackofficeFieldsWorkflowStatusItem()
3234
    item.parent = st1
3235
    item.fields = [{'field_id': 'bo1', 'value': "plop"}]
3236
    item.perform(formdata)
3242
    for value in ('plop', None, ''):   # invalid values => None
3243
        item = SetBackofficeFieldsWorkflowStatusItem()
3244
        item.parent = st1
3245
        item.fields = [{'field_id': 'bo1', 'value': value}]
3237 3246

  
3238
    formdata = formdef.data_class().get(formdata.id)
3239
    assert formdata.data['bo1'] is None
3247
        formdata.data['bo1'] == 'not_none'
3248
        item.perform(formdata)
3249
        formdata = formdef.data_class().get(formdata.id)
3250
        assert formdata.data['bo1'] is None
3240 3251

  
3241 3252
def test_set_backoffice_field_immediate_use(http_requests, two_pubs):
3242 3253
    Workflow.wipe()
wcs/ctl/hobo_notify.py
189 189
                        if not field.id.startswith('_'):
190 190
                            continue
191 191
                        field_value = o.get(field.id[1:])
192
                        if field_value and field.convert_value_from_anything:
192
                        if field.convert_value_from_anything:
193 193
                            field_value = field.convert_value_from_anything(field_value)
194 194
                        user.form_data[field.id] = field_value
195 195
                    user.name_identifiers = [uuid]
wcs/fields.py
788 788

  
789 789
    @classmethod
790 790
    def convert_value_from_anything(cls, value):
791
        if value is None:
792
            return None
791 793
        if hasattr(value, 'base_filename'):
792 794
            upload = PicklableUpload(value.base_filename,
793 795
                                     value.content_type or 'application/octet-stream')
......
972 974

  
973 975
    @classmethod
974 976
    def convert_value_from_anything(cls, value):
977
        if value is None:
978
            return None
975 979
        try:
976 980
            date_value = evalutils.make_date(value).timetuple()
977 981
        except ValueError:
wcs/qommon/saml2.py
493 493
                continue
494 494
            field_value = d[key]
495 495
            field = dict_fields.get(field_id)
496
            if field and field_value and field.convert_value_from_anything:
496
            if field and field.convert_value_from_anything:
497 497
                field_value = field.convert_value_from_anything(field_value)
498 498
            if user.form_data.get(field_id) != field_value:
499 499
                user.form_data[field_id] = field_value
wcs/wf/backoffice_fields.py
100 100
                get_publisher().notify_of_exception(sys.exc_info())
101 101
                continue
102 102

  
103
            if new_value and formdef_field.convert_value_from_anything:
103
            if formdef_field.convert_value_from_anything:
104 104
                try:
105 105
                    new_value = formdef_field.convert_value_from_anything(new_value)
106 106
                except ValueError:
wcs/wf/profile.py
156 156
        for field in user_formdef.fields:
157 157
            if field.varname in new_data:
158 158
                field_value = new_data.get(field.varname)
159
                if field and field_value and field.convert_value_from_anything:
159
                if field and field.convert_value_from_anything:
160 160
                    field_value = field.convert_value_from_anything(field_value)
161 161
                new_user_data[field.id] = field_value
162 162
                # also change initial value to the converted one, as the
163
-