From 51777d27abaddca53da37b9e7096f9051353e9a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20S=C3=A9chet?= Date: Fri, 25 Feb 2022 16:52:39 +0100 Subject: [PATCH] backoffice: report expected XML node in invalid XML import messages (#62109) --- tests/test_blockdef_import.py | 15 +++++++++++++++ tests/test_formdef_import.py | 17 +++++++++++++++++ tests/test_workflow_import.py | 10 ++++++++++ wcs/blocks.py | 5 ++++- wcs/formdef.py | 5 ++++- wcs/workflows.py | 5 ++++- 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 tests/test_blockdef_import.py diff --git a/tests/test_blockdef_import.py b/tests/test_blockdef_import.py new file mode 100644 index 00000000..03586ab2 --- /dev/null +++ b/tests/test_blockdef_import.py @@ -0,0 +1,15 @@ +import io + +import pytest + +from wcs.blocks import BlockDef, BlockdefImportError + + +def test_import_root_node_error(): + export = b'Name' + with pytest.raises(BlockdefImportError) as excinfo: + BlockDef.import_from_xml(io.BytesIO(export)) + assert ( + excinfo.value.msg + == 'Provided XML file is invalid, it starts with a tag instead of ' + ) diff --git a/tests/test_formdef_import.py b/tests/test_formdef_import.py index 833c3933..4ad399e1 100644 --- a/tests/test_formdef_import.py +++ b/tests/test_formdef_import.py @@ -867,3 +867,20 @@ def test_import_formdef_multiple_errors(pub): 'Unknown field types: foobaz; ' 'Unknown fields blocks: foobar, foobaz' ) + + +def test_import_formdef_root_node_error(): + export = b'Name' + with pytest.raises(FormdefImportError) as excinfo: + FormDef.import_from_xml(io.BytesIO(export)) + assert ( + excinfo.value.msg + == 'Provided XML file is invalid, it starts with a tag instead of ' + ) + + with pytest.raises(FormdefImportError) as excinfo: + CardDef.import_from_xml(io.BytesIO(export)) + assert ( + excinfo.value.msg + == 'Provided XML file is invalid, it starts with a tag instead of ' + ) diff --git a/tests/test_workflow_import.py b/tests/test_workflow_import.py index 6f4ef2f4..e6997bed 100644 --- a/tests/test_workflow_import.py +++ b/tests/test_workflow_import.py @@ -1135,3 +1135,13 @@ def test_import_workflow_multiple_errors(pub): 'Unknown mail templates: unknown-mt-1, unknown-mt-2; ' 'Unknown roles: unknown-role1, unknown-role2' ) + + +def test_import_root_node_error(): + export = b'Name' + with pytest.raises(WorkflowImportError) as excinfo: + Workflow.import_from_xml(io.BytesIO(export)) + assert ( + excinfo.value.msg + == 'Provided XML file is invalid, it starts with a tag instead of ' + ) diff --git a/wcs/blocks.py b/wcs/blocks.py index cd9e9ba8..0cb182b6 100644 --- a/wcs/blocks.py +++ b/wcs/blocks.py @@ -186,7 +186,10 @@ class BlockDef(StorableObject): tree = tree.getroot() if tree.tag != cls.xml_root_node: - raise BlockdefImportError(_('Unexpected root node')) + raise BlockdefImportError( + _('Provided XML file is invalid, it starts with a <%(seen)s> tag instead of <%(expected)s>') + % {'seen': tree.tag, 'expected': cls.xml_root_node} + ) if include_id and tree.attrib.get('id'): blockdef.id = tree.attrib.get('id') diff --git a/wcs/formdef.py b/wcs/formdef.py index a8eb2893..e18a14d6 100644 --- a/wcs/formdef.py +++ b/wcs/formdef.py @@ -1351,7 +1351,10 @@ class FormDef(StorableObject): tree = tree.getroot() if tree.tag != cls.xml_root_node: - raise FormdefImportError(_('Unexpected root node')) + raise FormdefImportError( + _('Provided XML file is invalid, it starts with a <%(seen)s> tag instead of <%(expected)s>') + % {'seen': tree.tag, 'expected': cls.xml_root_node} + ) if include_id and tree.attrib.get('id'): formdef.id = tree.attrib.get('id') diff --git a/wcs/workflows.py b/wcs/workflows.py index 3e9b64e3..97b3cc3a 100644 --- a/wcs/workflows.py +++ b/wcs/workflows.py @@ -852,7 +852,10 @@ class Workflow(StorableObject): tree = tree.getroot() if tree.tag != 'workflow': - raise WorkflowImportError(_('Not a workflow')) + raise WorkflowImportError( + _('Provided XML file is invalid, it starts with a <%(seen)s> tag instead of <%(expected)s>') + % {'seen': tree.tag, 'expected': 'workflow'} + ) if include_id and tree.attrib.get('id'): workflow.id = tree.attrib.get('id') -- 2.34.1