Projet

Général

Profil

0001-backoffice-report-expected-XML-node-in-invalid-XML-i.patch

Corentin Séchet, 28 février 2022 15:45

Télécharger (4,87 ko)

Voir les différences:

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
tests/test_blockdef_import.py
1
import io
2

  
3
import pytest
4

  
5
from wcs.blocks import BlockDef, BlockdefImportError
6

  
7

  
8
def test_import_root_node_error():
9
    export = b'<wrong_root_node><name>Name</name></wrong_root_node>'
10
    with pytest.raises(BlockdefImportError) as excinfo:
11
        BlockDef.import_from_xml(io.BytesIO(export))
12
    assert (
13
        excinfo.value.msg
14
        == 'Provided XML file is invalid, it starts with a <wrong_root_node> tag instead of <block>'
15
    )
tests/test_formdef_import.py
867 867
        'Unknown field types: foobaz; '
868 868
        'Unknown fields blocks: foobar, foobaz'
869 869
    )
870

  
871

  
872
def test_import_formdef_root_node_error():
873
    export = b'<wrong_root_node><name>Name</name></wrong_root_node>'
874
    with pytest.raises(FormdefImportError) as excinfo:
875
        FormDef.import_from_xml(io.BytesIO(export))
876
    assert (
877
        excinfo.value.msg
878
        == 'Provided XML file is invalid, it starts with a <wrong_root_node> tag instead of <formdef>'
879
    )
880

  
881
    with pytest.raises(FormdefImportError) as excinfo:
882
        CardDef.import_from_xml(io.BytesIO(export))
883
    assert (
884
        excinfo.value.msg
885
        == 'Provided XML file is invalid, it starts with a <wrong_root_node> tag instead of <carddef>'
886
    )
tests/test_workflow_import.py
1135 1135
        'Unknown mail templates: unknown-mt-1, unknown-mt-2; '
1136 1136
        'Unknown roles: unknown-role1, unknown-role2'
1137 1137
    )
1138

  
1139

  
1140
def test_import_root_node_error():
1141
    export = b'<wrong_root_node><name>Name</name></wrong_root_node>'
1142
    with pytest.raises(WorkflowImportError) as excinfo:
1143
        Workflow.import_from_xml(io.BytesIO(export))
1144
    assert (
1145
        excinfo.value.msg
1146
        == 'Provided XML file is invalid, it starts with a <wrong_root_node> tag instead of <workflow>'
1147
    )
wcs/blocks.py
186 186
            tree = tree.getroot()
187 187

  
188 188
        if tree.tag != cls.xml_root_node:
189
            raise BlockdefImportError(_('Unexpected root node'))
189
            raise BlockdefImportError(
190
                _('Provided XML file is invalid, it starts with a <%(seen)s> tag instead of <%(expected)s>')
191
                % {'seen': tree.tag, 'expected': cls.xml_root_node}
192
            )
190 193

  
191 194
        if include_id and tree.attrib.get('id'):
192 195
            blockdef.id = tree.attrib.get('id')
wcs/formdef.py
1351 1351
            tree = tree.getroot()
1352 1352

  
1353 1353
        if tree.tag != cls.xml_root_node:
1354
            raise FormdefImportError(_('Unexpected root node'))
1354
            raise FormdefImportError(
1355
                _('Provided XML file is invalid, it starts with a <%(seen)s> tag instead of <%(expected)s>')
1356
                % {'seen': tree.tag, 'expected': cls.xml_root_node}
1357
            )
1355 1358

  
1356 1359
        if include_id and tree.attrib.get('id'):
1357 1360
            formdef.id = tree.attrib.get('id')
wcs/workflows.py
852 852
            tree = tree.getroot()
853 853

  
854 854
        if tree.tag != 'workflow':
855
            raise WorkflowImportError(_('Not a workflow'))
855
            raise WorkflowImportError(
856
                _('Provided XML file is invalid, it starts with a <%(seen)s> tag instead of <%(expected)s>')
857
                % {'seen': tree.tag, 'expected': 'workflow'}
858
            )
856 859

  
857 860
        if include_id and tree.attrib.get('id'):
858 861
            workflow.id = tree.attrib.get('id')
859
-