From 73e16b5dd6aafbdd9c8bdc3f8607eda91af2af53 Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Mon, 26 Mar 2018 17:02:41 +0200 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(-) diff --git a/tests/test_hobo_notify.py b/tests/test_hobo_notify.py index dad592c8..30bc6568 100644 --- a/tests/test_hobo_notify.py +++ b/tests/test_hobo_notify.py @@ -544,6 +544,38 @@ def test_process_notification_user_provision(pub): assert user.is_admin is True assert set(user.roles) == set([old_role.id]) + for bad_birthdate in ('baddate', None, ''): + notification = { + u'@type': u'provision', + u'issuer': 'http://idp.example.net/idp/saml/metadata', + u'audience': [u'test'], + u'objects': { + u'@type': 'user', + u'data': [ + { + u'uuid': u'a' * 32, + u'first_name': u'John', + u'last_name': u'Doe', + u'email': u'john.doe@example.net', + u'zipcode': u'13600', + u'birthdate': bad_birthdate, + u'is_superuser': True, + u'roles': [ + { + u'uuid': u'xyz', + u'name': u'Service état civil', + u'description': u'etc.', + }, + ], + } + ] + } + } + CmdHoboNotify.process_notification(notification) + assert User.count() == 1 + user = User.select()[0] + assert user.name_identifiers == ['a'*32] + assert user.form_data['_birthdate'] is None def notify_of_exception(exc_info, context): raise Exception(exc_info) diff --git a/tests/test_workflows.py b/tests/test_workflows.py index f579f85c..ae736964 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -2879,19 +2879,26 @@ def test_profile(two_pubs): for date_value in ( '20/03/2018', '=utils.make_date("20/03/2018")', - '=utils.make_date("20/03/2018").timetuple()'): + '=utils.make_date("20/03/2018").timetuple()', + None, ''): # check local value item.fields = [{'field_id': 'bar', 'value': date_value}] item.perform(formdata) assert User.get(user.id).form_data.get('3') == 'Plop' - assert User.get(user.id).form_data.get('4').tm_year == 2018 + if date_value: + assert User.get(user.id).form_data.get('4').tm_year == 2018 + else: + assert User.get(user.id).form_data.get('4') is None with mock.patch('wcs.wf.profile.http_patch_request') as http_patch_request: http_patch_request.return_value = (None, 200, '', None) get_response().process_after_jobs() assert http_patch_request.call_count == 1 - assert http_patch_request.call_args[0][1] == '{"bar": "2018-03-20"}' + if date_value: + assert http_patch_request.call_args[0][1] == '{"bar": "2018-03-20"}' + else: + assert http_patch_request.call_args[0][1] == '{"bar": null}' def test_set_backoffice_field(http_requests, two_pubs): Workflow.wipe() @@ -3055,17 +3062,19 @@ def test_set_backoffice_field_file(http_requests, two_pubs): assert formdata.data['bo1'].get_content() == 'HELLO WORLD' # check wrong value - del formdata.data['bo1'] - formdata.store() - assert not 'bo1' in formdata.data + for value in ('="HELLO"', None, '', 'BAD'): + if 'bo1' in formdata.data: + del formdata.data['bo1'] + formdata.store() + assert not 'bo1' in formdata.data + + item = SetBackofficeFieldsWorkflowStatusItem() + item.parent = st1 + item.fields = [{'field_id': 'bo1', 'value': value}] + item.perform(formdata) - item = SetBackofficeFieldsWorkflowStatusItem() - item.parent = st1 - item.fields = [{'field_id': 'bo1', 'value': '="HELLO"'}] - item.perform(formdata) - - formdata = formdef.data_class().get(formdata.id) - assert formdata.data.get('bo1') is None + formdata = formdef.data_class().get(formdata.id) + assert formdata.data.get('bo1') is None # check wrong field item = SetBackofficeFieldsWorkflowStatusItem() @@ -3230,13 +3239,15 @@ def test_set_backoffice_field_date(two_pubs): formdata = formdef.data_class().get(formdata.id) assert datetime.date(*formdata.data['bo1'][:3]) == datetime.date(2017, 3, 23) - item = SetBackofficeFieldsWorkflowStatusItem() - item.parent = st1 - item.fields = [{'field_id': 'bo1', 'value': "plop"}] - item.perform(formdata) + for value in ('plop', None, ''): # invalid values => None + item = SetBackofficeFieldsWorkflowStatusItem() + item.parent = st1 + item.fields = [{'field_id': 'bo1', 'value': value}] - formdata = formdef.data_class().get(formdata.id) - assert formdata.data['bo1'] is None + formdata.data['bo1'] == 'not_none' + item.perform(formdata) + formdata = formdef.data_class().get(formdata.id) + assert formdata.data['bo1'] is None def test_set_backoffice_field_immediate_use(http_requests, two_pubs): Workflow.wipe() diff --git a/wcs/ctl/hobo_notify.py b/wcs/ctl/hobo_notify.py index 66a3f28e..39201837 100644 --- a/wcs/ctl/hobo_notify.py +++ b/wcs/ctl/hobo_notify.py @@ -189,7 +189,7 @@ class CmdHoboNotify(Command): if not field.id.startswith('_'): continue field_value = o.get(field.id[1:]) - if field_value and field.convert_value_from_anything: + if field.convert_value_from_anything: field_value = field.convert_value_from_anything(field_value) user.form_data[field.id] = field_value user.name_identifiers = [uuid] diff --git a/wcs/fields.py b/wcs/fields.py index a58a9a0f..44c33ca2 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -788,6 +788,8 @@ class FileField(WidgetField): @classmethod def convert_value_from_anything(cls, value): + if value is None: + return None if hasattr(value, 'base_filename'): upload = PicklableUpload(value.base_filename, value.content_type or 'application/octet-stream') @@ -972,6 +974,8 @@ class DateField(WidgetField): @classmethod def convert_value_from_anything(cls, value): + if value is None: + return None try: date_value = evalutils.make_date(value).timetuple() except ValueError: diff --git a/wcs/qommon/saml2.py b/wcs/qommon/saml2.py index 7c0fe696..2a70a8af 100644 --- a/wcs/qommon/saml2.py +++ b/wcs/qommon/saml2.py @@ -493,7 +493,7 @@ class Saml2Directory(Directory): continue field_value = d[key] field = dict_fields.get(field_id) - if field and field_value and field.convert_value_from_anything: + if field and field.convert_value_from_anything: field_value = field.convert_value_from_anything(field_value) if user.form_data.get(field_id) != field_value: user.form_data[field_id] = field_value diff --git a/wcs/wf/backoffice_fields.py b/wcs/wf/backoffice_fields.py index 6653edb6..4a1c87ab 100644 --- a/wcs/wf/backoffice_fields.py +++ b/wcs/wf/backoffice_fields.py @@ -100,7 +100,7 @@ class SetBackofficeFieldsWorkflowStatusItem(WorkflowStatusItem): get_publisher().notify_of_exception(sys.exc_info()) continue - if new_value and formdef_field.convert_value_from_anything: + if formdef_field.convert_value_from_anything: try: new_value = formdef_field.convert_value_from_anything(new_value) except ValueError: diff --git a/wcs/wf/profile.py b/wcs/wf/profile.py index cd6a3ee5..893c24d0 100644 --- a/wcs/wf/profile.py +++ b/wcs/wf/profile.py @@ -156,7 +156,7 @@ class UpdateUserProfileStatusItem(WorkflowStatusItem): for field in user_formdef.fields: if field.varname in new_data: field_value = new_data.get(field.varname) - if field and field_value and field.convert_value_from_anything: + if field and field.convert_value_from_anything: field_value = field.convert_value_from_anything(field_value) new_user_data[field.id] = field_value # also change initial value to the converted one, as the -- 2.16.2