From 837a40e3b7358b8ebf6a27de1bd9d904d98bb451 Mon Sep 17 00:00:00 2001 From: mareklycka Date: Wed, 8 Aug 2018 08:47:41 +0200 Subject: [PATCH] Enforces unicode decoding of data The underlying APIs changed between Python2 and Python3, where once they returned a string, they now return bytes. This change enforces the decoding of the returned value as a UTF-8. Failure to decode is handled by the existing exception handling. Tests were updated to mimic the new API. Change-Id: I98b474840a163f60e98aed0f75bc6b9df2a9aa00 Story: 2003343 Task: 24381 --- heat_dashboard/content/stacks/forms.py | 18 +++++++++--------- .../test/tests/content/test_stacks.py | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/heat_dashboard/content/stacks/forms.py b/heat_dashboard/content/stacks/forms.py index 869e2aa..d9def18 100644 --- a/heat_dashboard/content/stacks/forms.py +++ b/heat_dashboard/content/stacks/forms.py @@ -203,16 +203,16 @@ class TemplateForm(forms.SelfHandlingForm): log_template_name = files[upload_str].name LOG.info('got upload %s', log_template_name) - tpl = files[upload_str].read() - if tpl.startswith('{'): - try: + try: + tpl = files[upload_str].read().decode('utf-8') + if tpl.startswith('{'): json.loads(tpl) - except Exception as e: - msg = _('There was a problem parsing the' - ' %(prefix)s: %(error)s') - msg = msg % {'prefix': prefix, 'error': six.text_type(e)} - raise forms.ValidationError(msg) - cleaned[data_str] = tpl + cleaned[data_str] = tpl + except Exception as e: + msg = _('There was a problem parsing the' + ' %(prefix)s: %(error)s') + msg = msg % {'prefix': prefix, 'error': six.text_type(e)} + raise forms.ValidationError(msg) # URL handler elif url and (has_upload or data): diff --git a/heat_dashboard/test/tests/content/test_stacks.py b/heat_dashboard/test/tests/content/test_stacks.py index 7a8ccfa..c93c2c9 100644 --- a/heat_dashboard/test/tests/content/test_stacks.py +++ b/heat_dashboard/test/tests/content/test_stacks.py @@ -918,7 +918,7 @@ class TemplateFormTests(test.TestCase): } json_str = '{notvalidjson::::::json/////json' files = {'template_upload': - self.SimpleFile('template_name', json_str)} + self.SimpleFile('template_name', json_str.encode('utf-8'))} self.assertRaises( exceptions.ValidationError, @@ -937,7 +937,7 @@ class TemplateFormTests(test.TestCase): json_str = '{"isvalid":"json"}' files = {'template_upload': - self.SimpleFile('template_name', json_str)} + self.SimpleFile('template_name', json_str.encode('utf-8'))} t.clean_uploaded_files('template', 'template', precleaned, files) self.assertEqual(