misc: clean complex variables used in ezt templates (#49679)

This commit is contained in:
Frédéric Péters 2020-12-22 17:03:59 +01:00
parent 35bdc42674
commit 9444fa214a
2 changed files with 72 additions and 1 deletions

View File

@ -2163,6 +2163,75 @@ def test_webservice_target_status(pub):
assert targets.count(status2) == 2
def test_webservice_with_complex_data(http_requests, pub):
pub.substitutions.feed(MockSubstitutionVariables())
wf = Workflow(name='wf1')
st1 = wf.add_status('Status1', 'st1')
sterr = wf.add_status('StatusErr', 'sterr')
wf.store()
datasource = {'type': 'formula',
'value': repr([{'id': 'a', 'text': 'aa', 'more': 'aaa'},
{'id': 'b', 'text': 'bb', 'more': 'bbb'},
{'id': 'c', 'text': 'cc', 'more': 'ccc'}])}
FormDef.wipe()
formdef = FormDef()
formdef.name = 'baz'
formdef.fields = [
ItemField(id='1', label='1st field',
type='item', varname='item',
data_source=datasource),
ItemsField(id='2', label='2nd field',
type='items', varname='items',
data_source=datasource),
]
formdef.workflow_id = wf.id
formdef.store()
formdata = formdef.data_class()()
formdata.data = {}
formdata.data['1'] = 'a'
formdata.data['1_display'] = 'aa'
formdata.data['1_structured'] = formdef.fields[0].store_structured_value(formdata.data, '1')
formdata.data['2'] = ['a', 'b']
formdata.data['2_display'] = 'aa, bb'
formdata.data['2_structured'] = formdef.fields[1].store_structured_value(formdata.data, '2')
formdata.just_created()
formdata.store()
item = WebserviceCallStatusItem()
item.method = 'POST'
item.url = 'http://remote.example.net'
item.post_data = {
'item': '{{ form_var_item }}',
'ezt_item': '[form_var_item]',
'items': '{{ form_var_items }}',
'ezt_items': '[form_var_items]',
'item_raw': '{{ form_var_item_raw }}',
'ezt_item_raw': '[form_var_item_raw]',
'items_raw': '{{ form_var_items_raw }}',
'ezt_items_raw': '[form_var_items_raw]',
}
pub.substitutions.feed(formdata)
with get_publisher().complex_data():
item.perform(formdata)
assert http_requests.get_last('url') == 'http://remote.example.net'
assert http_requests.get_last('method') == 'POST'
payload = json.loads(http_requests.get_last('body'))
assert payload == {
'item': 'aa',
'ezt_item': 'aa',
'items': 'aa, bb',
'ezt_items': 'aa, bb',
'item_raw': 'a',
'ezt_item_raw': 'a',
'items_raw': "['a', 'b']",
'ezt_items_raw': "['a', 'b']",
}
def test_timeout(two_pubs):
workflow = Workflow(name='timeout')
st1 = workflow.add_status('Status1', 'st1')

View File

@ -520,7 +520,9 @@ class Template(object):
ezt_raises(e)
else:
return self.value
return force_str(fd.getvalue())
if context.get('allow_complex'):
return fd.getvalue()
return re.sub(r'[\uE000-\uF8FF]', '', force_str(fd.getvalue()))
@classmethod
def is_template_string(cls, string):