backoffice: report expected XML node in invalid XML import messages (#62109)

This commit is contained in:
Corentin Sechet 2022-02-25 16:52:39 +01:00
parent 5d88590b00
commit 51777d27ab
6 changed files with 54 additions and 3 deletions

View File

@ -0,0 +1,15 @@
import io
import pytest
from wcs.blocks import BlockDef, BlockdefImportError
def test_import_root_node_error():
export = b'<wrong_root_node><name>Name</name></wrong_root_node>'
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 <wrong_root_node> tag instead of <block>'
)

View File

@ -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'<wrong_root_node><name>Name</name></wrong_root_node>'
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 <wrong_root_node> tag instead of <formdef>'
)
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 <wrong_root_node> tag instead of <carddef>'
)

View File

@ -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'<wrong_root_node><name>Name</name></wrong_root_node>'
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 <wrong_root_node> tag instead of <workflow>'
)

View File

@ -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')

View File

@ -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')

View File

@ -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')