def test_any_in_nested_sequence():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <xsd:schema
            xmlns:tns="http://tests.python-zeep.org/"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified"
            targetNamespace="http://tests.python-zeep.org/"
        >
          <xsd:element name="something" type="xsd:date"/>
          <xsd:element name="foobar" type="xsd:boolean"/>
          <xsd:element name="container">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="items" minOccurs="0">
                  <xsd:complexType>
                    <xsd:sequence>
                      <xsd:any namespace="##any" processContents="lax"/>
                    </xsd:sequence>
                  </xsd:complexType>
                </xsd:element>
                <xsd:element name="version" type="xsd:string"/>
                <xsd:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """))   # noqa

    container_elm = schema.get_element('{http://tests.python-zeep.org/}container')
    assert container_elm.signature() == (
        'items: {_value_1: ANY}, version: xsd:string, _value_1: ANY[]')

    something = schema.get_element('{http://tests.python-zeep.org/}something')
    foobar = schema.get_element('{http://tests.python-zeep.org/}foobar')

    any_1 = xsd.AnyObject(something, datetime.date(2016, 7, 4))
    any_2 = xsd.AnyObject(foobar, True)
    obj = container_elm(
        items={'_value_1': any_1}, version='str1234', _value_1=[any_1, any_2])

    node = etree.Element('document')
    container_elm.render(node, obj)
    expected = """
        <document>
          <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
            <ns0:items>
              <ns0:something>2016-07-04</ns0:something>
            </ns0:items>
            <ns0:version>str1234</ns0:version>
            <ns0:something>2016-07-04</ns0:something>
            <ns0:foobar>true</ns0:foobar>
          </ns0:container>
        </document>
    """
    assert_nodes_equal(expected, node)
    item = container_elm.parse(node.getchildren()[0], schema)
    assert item.items._value_1 == datetime.date(2016, 7, 4)
    assert item.version == 'str1234'
    assert item._value_1 == [datetime.date(2016, 7, 4), True]
def test_simple_type():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <element name="item">
            <complexType>
              <sequence>
                <element name="something" type="long"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """))

    item_cls = schema.get_element('{http://tests.python-zeep.org/}item')
    item = item_cls(something=12345678901234567890)

    node = etree.Element('document')
    item_cls.render(node, item)
    expected = """
        <document>
          <ns0:item xmlns:ns0="http://tests.python-zeep.org/">
            <ns0:something>12345678901234567890</ns0:something>
          </ns0:item>
        </document>
    """
    assert_nodes_equal(expected, node)
    item = item_cls.parse(node.getchildren()[0], schema)
    assert item.something == 12345678901234567890
def test_unqualified():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                attributeFormDefault="qualified"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <element name="Address">
            <complexType>
              <sequence>
                <element name="foo" type="xsd:string" form="unqualified" />
              </sequence>
            </complexType>
          </element>
        </schema>
    """.strip())

    schema = xsd.Schema(node)
    address_type = schema.get_element('{http://tests.python-zeep.org/}Address')
    obj = address_type(foo='bar')

    expected = """
      <document>
        <ns0:Address xmlns:ns0="http://tests.python-zeep.org/">
          <foo>bar</foo>
        </ns0:Address>
      </document>
    """

    node = etree.Element('document')
    address_type.render(node, obj)
    assert_nodes_equal(expected, node)
def test_custom_simple_type():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <element name="something">
            <simpleType>
              <restriction base="integer">
                <minInclusive value="0"/>
                <maxInclusive value="100"/>
              </restriction>
            </simpleType>
          </element>
        </schema>
    """.strip())

    schema = xsd.Schema(node)

    custom_type = schema.get_element('{http://tests.python-zeep.org/}something')
    obj = custom_type(75)

    node = etree.Element('document')
    custom_type.render(node, obj)
    expected = """
        <document>
            <ns0:something xmlns:ns0="http://tests.python-zeep.org/">75</ns0:something>
        </document>
    """
    assert_nodes_equal(expected, node)
def test_element_any_type():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <element name="container">
            <complexType>
              <sequence>
                <element name="something" type="anyType"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """.strip())
    schema = xsd.Schema(node)

    container_elm = schema.get_element('{http://tests.python-zeep.org/}container')
    obj = container_elm(something='bar')

    node = etree.Element('document')
    container_elm.render(node, obj)
    expected = """
        <document>
            <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:something>bar</ns0:something>
            </ns0:container>
        </document>
    """
    assert_nodes_equal(expected, node)
    item = container_elm.parse(node.getchildren()[0], schema)
    assert item.something == 'bar'
def test_complex_type_with_attributes():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
          <xs:complexType name="Address">
            <xs:sequence>
              <xs:element minOccurs="0" maxOccurs="1" name="NameFirst" type="xs:string"/>
              <xs:element minOccurs="0" maxOccurs="1" name="NameLast" type="xs:string"/>
              <xs:element minOccurs="0" maxOccurs="1" name="Email" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:string" use="required"/>
          </xs:complexType>
          <xs:element name="Address" type="Address"/>
        </xs:schema>
    """.strip())

    schema = xsd.Schema(node)

    address_type = schema.get_element('Address')
    obj = address_type(
        NameFirst='John', NameLast='Doe', Email='*****@*****.**', id='123')

    node = etree.Element('document')
    address_type.render(node, obj)

    expected = """
        <document>
            <Address id="123">
                <NameFirst>John</NameFirst>
                <NameLast>Doe</NameLast>
                <Email>[email protected]</Email>
            </Address>
        </document>
    """
    assert_nodes_equal(expected, node)
def test_complex_type_simple_content():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <xs:schema
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://tests.python-zeep.org/"
            targetNamespace="http://tests.python-zeep.org/"
            elementFormDefault="qualified">

          <xs:element name="ShoeSize">
            <xs:complexType>
              <xs:simpleContent>
                <xs:extension base="xs:integer">
                  <xs:attribute name="sizing" type="xs:string" />
                </xs:extension>
              </xs:simpleContent>
            </xs:complexType>
          </xs:element>
        </xs:schema>
    """.strip())
    schema = xsd.Schema(node)
    shoe_type = schema.get_element('{http://tests.python-zeep.org/}ShoeSize')

    obj = shoe_type(20, sizing='EUR')

    node = etree.Element('document')
    shoe_type.render(node, obj)
    expected = """
        <document>
            <ns0:ShoeSize xmlns:ns0="http://tests.python-zeep.org/" sizing="EUR">20</ns0:ShoeSize>
        </document>
    """
    assert_nodes_equal(expected, node)
Example #8
0
def test_ref_attribute_qualified():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                attributeFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="container">
            <xsd:complexType>
              <xsd:attribute ref="tns:attr" use="required" />
            </xsd:complexType>
          </xsd:element>
          <xsd:attribute name="attr" type="xsd:string" />
        </xsd:schema>
    """))

    elm_cls = schema.get_element('{http://tests.python-zeep.org/}container')
    instance = elm_cls(attr="hoi")

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/" ns0:attr="hoi"/>
      </document>
    """

    node = etree.Element('document')
    elm_cls.render(node, instance)
    assert_nodes_equal(expected, node)
def test_complex_type_unbounded_named():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <element name="Address" type="tns:AddressType" />
          <complexType name="AddressType">
            <sequence>
              <element minOccurs="0" maxOccurs="unbounded" name="foo" type="string" />
            </sequence>
          </complexType>
        </schema>
    """.strip())

    schema = xsd.Schema(node)
    address_type = schema.get_element('{http://tests.python-zeep.org/}Address')
    obj = address_type()
    assert obj.foo == []
    obj.foo.append('foo')
    obj.foo.append('bar')

    expected = """
        <document>
          <ns0:Address xmlns:ns0="http://tests.python-zeep.org/">
            <ns0:foo>foo</ns0:foo>
            <ns0:foo>bar</ns0:foo>
          </ns0:Address>
        </document>
    """

    node = etree.Element('document')
    address_type.render(node, obj)
    assert_nodes_equal(expected, node)
Example #10
0
def test_create_node():
    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'authentication'),
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'username'),
                    xsd.String()),
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'password'),
                    xsd.String()),
            ]),
            [
                xsd.Attribute('attr', xsd.String()),
            ]
        ))

    # sequences
    obj = custom_type(username='******', password='******', attr='x')

    expected = """
      <document>
        <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/" attr="x">
          <ns0:username>foo</ns0:username>
          <ns0:password>bar</ns0:password>
        </ns0:authentication>
      </document>
    """
    node = etree.Element('document')
    custom_type.render(node, obj)
    assert_nodes_equal(expected, node)
Example #11
0
def test_qualified_attribute():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                attributeFormDefault="qualified"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="Address">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="foo" type="xsd:string" form="unqualified" />
              </xsd:sequence>
              <xsd:attribute name="id" type="xsd:string" use="required" form="unqualified" />
              <xsd:attribute name="pos" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """))

    address_type = schema.get_element('{http://tests.python-zeep.org/}Address')
    obj = address_type(foo='bar', id="20", pos="30")

    expected = """
      <document>
        <ns0:Address xmlns:ns0="http://tests.python-zeep.org/" id="20" ns0:pos="30">
          <foo>bar</foo>
        </ns0:Address>
      </document>
    """

    node = etree.Element('document')
    address_type.render(node, obj)
    assert_nodes_equal(expected, node)
Example #12
0
def test_element_attribute_name_conflict():
    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'container'),
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'item'),
                    xsd.String()),
            ]),
            [
                xsd.Attribute('foo', xsd.String()),
                xsd.Attribute('item', xsd.String()),
            ]
        ))

    # sequences
    expected = '{http://tests.python-zeep.org/}container(item: xsd:string, foo: xsd:string, attr__item: xsd:string)'
    assert custom_type.signature() == expected
    obj = custom_type(item='foo', foo='x', attr__item='bar')

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/" foo="x" item="bar">
          <ns0:item>foo</ns0:item>
        </ns0:container>
      </document>
    """
    node = render_node(custom_type, obj)
    assert_nodes_equal(expected, node)

    obj = custom_type.parse(list(node)[0], None)
    assert obj.item == 'foo'
    assert obj.foo == 'x'
    assert obj.attr__item == 'bar'
Example #13
0
def test_duplicate_element_names():
    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'container'),
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'item'),
                    xsd.String()),
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'item'),
                    xsd.String()),
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'item'),
                    xsd.String()),
            ])
        ))

    # sequences
    expected = '{http://tests.python-zeep.org/}container(item: xsd:string, item__1: xsd:string, item__2: xsd:string)'
    assert custom_type.signature() == expected
    obj = custom_type(item='foo', item__1='bar', item__2='lala')

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item>foo</ns0:item>
          <ns0:item>bar</ns0:item>
          <ns0:item>lala</ns0:item>
        </ns0:container>
      </document>
    """
    node = render_node(custom_type, obj)
    assert_nodes_equal(expected, node)
Example #14
0
def test_mime_content_serialize_xml():
    wsdl = stub(schema=stub(_prefix_map={}))
    operation = stub(location='my-action', name='foo')

    element_1 = xsd.Element('arg1', xsd.ComplexType([
        xsd.Element('arg1_1', xsd.String())
    ]))
    element_2 = xsd.Element('arg2', xsd.String())
    abstract_message = definitions.AbstractMessage(
        etree.QName('{http://test.python-zeep.org/tests/msg}Method'))
    abstract_message.parts = OrderedDict([
        ('xarg1', definitions.MessagePart(element=element_1, type=None)),
        ('xarg2', definitions.MessagePart(element=element_2, type=None)),
    ])

    msg = messages.MimeContent(
        wsdl=wsdl, name=None, operation=operation, content_type='text/xml',
        part_name=None)

    msg._info = {
        'body': {'namespace': 'http://test.python-zeep.org/tests/rpc'},
        'header': None,
        'headerfault': None
    }
    msg.resolve(wsdl, abstract_message)

    serialized = msg.serialize(xarg1={'arg1_1': 'uh'}, xarg2='bla')
    assert serialized.headers == {
        'Content-Type': 'text/xml'
    }
    assert serialized.path == 'my-action'
    assert_nodes_equal(
        load_xml(serialized.content),
        load_xml(
            "<foo><xarg1><arg1_1>uh</arg1_1></xarg1><xarg2>bla</xarg2></foo>"))
Example #15
0
def test_any_value_element_tree():
    schema = get_any_schema()

    item = etree.Element('{http://tests.python-zeep.org}lxml')
    etree.SubElement(item, 'node').text = 'foo'

    container_elm = schema.get_element('{http://tests.python-zeep.org/}container')

    # Create via arg
    obj = container_elm(item)
    node = etree.Element('document')
    container_elm.render(node, obj)
    expected = """
        <document>
            <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:lxml xmlns:ns0="http://tests.python-zeep.org">
                    <node>foo</node>
                </ns0:lxml>
            </ns0:container>
        </document>
    """
    assert_nodes_equal(expected, node)
    item = container_elm.parse(list(node)[0], schema)
    assert isinstance(item._value_1, etree._Element)
    assert item._value_1.tag == '{http://tests.python-zeep.org}lxml'
Example #16
0
def test_rpc_message_serializer(abstract_message_input):
    wsdl = stub(schema=stub(_prefix_map={}))
    operation = stub(soapaction='my-action')

    msg = messages.RpcMessage(
        wsdl=wsdl,
        name=None,
        operation=operation,
        nsmap=soap.Soap11Binding.nsmap)

    msg._info = {
        'body': {
            'namespace': 'http://test.python-zeep.org/tests/rpc',
        },
        'header': None,
        'headerfault': None
    }
    msg.resolve(wsdl, abstract_message_input)

    serialized = msg.serialize(arg1='ah1', arg2='ah2')
    expected = """
        <?xml version="1.0"?>
        <soap-env:Envelope
            xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
          <soap-env:Body>
            <ns0:Method xmlns:ns0="http://test.python-zeep.org/tests/rpc">
              <arg1>ah1</arg1>
              <arg2>ah2</arg2>
            </ns0:Method>
          </soap-env:Body>
        </soap-env:Envelope>
    """
    assert_nodes_equal(expected, serialized.content)
def test_restriction_anon():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <element name="something">
            <simpleType>
              <restriction base="integer">
                <minInclusive value="0"/>
                <maxInclusive value="100"/>
              </restriction>
            </simpleType>
          </element>
        </schema>
    """))

    element_cls = schema.get_element('{http://tests.python-zeep.org/}something')
    assert element_cls.type.qname == etree.QName(
        '{http://tests.python-zeep.org/}something')

    obj = element_cls(75)

    node = etree.Element('document')
    element_cls.render(node, obj)
    expected = """
        <document>
            <ns0:something xmlns:ns0="http://tests.python-zeep.org/">75</ns0:something>
        </document>
    """
    assert_nodes_equal(expected, node)
def test_build_occurs_1():
    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'authentication'),
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'item_1'),
                    xsd.String()),
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'item_2'),
                    xsd.String()),
            ])
        ))
    obj = custom_type(item_1='foo', item_2='bar')
    assert obj.item_1 == 'foo'
    assert obj.item_2 == 'bar'

    result = render_node(custom_type, obj)
    expected = load_xml("""
        <document>
          <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/">
            <ns0:item_1>foo</ns0:item_1>
            <ns0:item_2>bar</ns0:item_2>
          </ns0:authentication>
        </document>
    """)
    assert_nodes_equal(result, expected)
    obj = custom_type.parse(expected[0], None)
    assert obj.item_1 == 'foo'
    assert obj.item_2 == 'bar'
Example #19
0
def test_attribute_required():
    node = load_xml("""
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://tests.python-zeep.org/">
          <element name="foo">
            <complexType>
              <xsd:attribute name="base" use="required" type="xsd:string" />
            </complexType>
          </element>
        </schema>
    """)
    schema = parse_schema_node(node)
    xsd_element = schema.get_element('{http://tests.python-zeep.org/}foo')
    value = xsd_element()

    with pytest.raises(exceptions.ValidationError):
        node = render_node(xsd_element, value)

    value.base = 'foo'
    node = render_node(xsd_element, value)

    expected = """
      <document>
        <ns0:foo xmlns:ns0="http://tests.python-zeep.org/" base="foo"/>
      </document>
    """
    assert_nodes_equal(expected, node)
def test_simple_type_list():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">

          <simpleType name="values">
            <list itemType="integer"/>
          </simpleType>
          <element name="something" type="tns:values"/>
        </schema>
    """))

    element_cls = schema.get_element('{http://tests.python-zeep.org/}something')
    obj = element_cls([1, 2, 3])
    assert obj == [1, 2, 3]

    node = etree.Element('document')
    element_cls.render(node, obj)
    expected = """
        <document>
            <ns0:something xmlns:ns0="http://tests.python-zeep.org/">1 2 3</ns0:something>
        </document>
    """
    assert_nodes_equal(expected, node)
Example #21
0
def test_complex_content_mixed(schema_visitor):
    node = load_xml("""
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="foo">
            <xsd:complexType>
              <xsd:complexContent mixed="true">
                <xsd:extension base="xsd:anyType">
                  <xsd:attribute name="bar" type="xsd:anyURI" use="required"/>
                </xsd:extension>
              </xsd:complexContent>
            </xsd:complexType>
          </xsd:element>
        </schema>
    """)
    schema_visitor.visit_schema(node)
    xsd_element = schema_visitor.schema.get_element(
        '{http://tests.python-zeep.org/}foo')
    result = xsd_element('basetype', bar='hoi')

    node = etree.Element('document')
    xsd_element.render(node, result)

    expected = """
      <document>
        <ns0:foo xmlns:ns0="http://tests.python-zeep.org/" bar="hoi">basetype</ns0:foo>
      </document>
    """
    assert_nodes_equal(expected, node)
Example #22
0
def test_list_type():
    node = load_xml("""
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/">

          <xsd:simpleType name="listOfIntegers">
            <xsd:list itemType="integer" />
          </xsd:simpleType>

          <xsd:element name="foo">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="arg" type="tns:listOfIntegers"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </schema>
    """)

    schema = xsd.Schema(node)
    xsd_element = schema.get_element(
        '{http://tests.python-zeep.org/}foo')
    value = xsd_element(arg=[1, 2, 3, 4, 5])

    node = render_node(xsd_element, value)
    expected = """
        <document>
          <ns0:foo xmlns:ns0="http://tests.python-zeep.org/">
            <arg>1 2 3 4 5</arg>
          </ns0:foo>
        </document>
    """
    assert_nodes_equal(expected, node)
Example #23
0
def test_any_without_element():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                   elementFormDefault="qualified">
          <element name="item">
            <complexType>
              <sequence>
                <any minOccurs="0" maxOccurs="1"/>
              </sequence>
              <attribute name="type" type="string"/>
              <attribute name="title" type="string"/>
            </complexType>
          </element>
        </schema>
    """))
    item_elm = schema.get_element('{http://tests.python-zeep.org/}item')
    item = item_elm(xsd.AnyObject(xsd.String(), 'foobar'), type='attr-1', title='attr-2')

    node = render_node(item_elm, item)
    expected = """
        <document>
          <ns0:item xmlns:ns0="http://tests.python-zeep.org/" type="attr-1" title="attr-2">foobar</ns0:item>
        </document>
    """
    assert_nodes_equal(expected, node)

    item = item_elm.parse(list(node)[0], schema)
    assert item.type == 'attr-1'
    assert item.title == 'attr-2'
    assert item._value_1 is None
def test_nill():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                elementFormDefault="qualified">
          <element name="container">
            <complexType>
              <sequence>
                <element name="foo" type="string" nillable="true"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """))

    address_type = schema.get_element('ns0:container')
    obj = address_type()
    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
        </ns0:container>
      </document>
    """
    node = etree.Element('document')
    address_type.render(node, obj)
    etree.cleanup_namespaces(node)

    assert_nodes_equal(expected, node)
def test_union_same_types():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <xsd:schema
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://tests.python-zeep.org/"
            targetNamespace="http://tests.python-zeep.org/"
            elementFormDefault="qualified">


          <xsd:simpleType name="MMYY">
            <xsd:restriction base="xsd:int"/>
          </xsd:simpleType>

          <xsd:simpleType name="MMYYYY">
            <xsd:restriction base="xsd:int"/>
          </xsd:simpleType>

          <xsd:simpleType name="Date">
            <xsd:union memberTypes="MMYY MMYYYY"/>
          </xsd:simpleType>
          <xsd:element name="item" type="tns:Date"/>
        </xsd:schema>
    """))

    elm = schema.get_element('ns0:item')
    node = render_node(elm, '102018')
    expected = """
        <document>
          <ns0:item xmlns:ns0="http://tests.python-zeep.org/">102018</ns0:item>
        </document>
    """
    assert_nodes_equal(expected, node)
    value = elm.parse(node.getchildren()[0], schema)
    assert value == 102018
Example #26
0
def test_serialize_any_array():
    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'authentication'),
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Any(max_occurs=2),
            ])
        ))

    any_obj = etree.Element('{http://tests.python-zeep.org}lxml')
    etree.SubElement(any_obj, 'node').text = 'foo'

    obj = custom_type(any_obj)

    expected = """
        <document>
          <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/">
            <ns0:lxml xmlns:ns0="http://tests.python-zeep.org">
              <node>foo</node>
            </ns0:lxml>
          </ns0:authentication>
        </document>
    """
    node = etree.Element('document')
    custom_type.render(node, obj)
    assert_nodes_equal(expected, node)

    schema = xsd.Schema()
    obj = custom_type.parse(node.getchildren()[0], schema=schema)
    result = serialize_object(obj)

    assert result == {
        '_value_1': [any_obj],
    }
def test_xml_complex_type_empty():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <complexType name="empty"/>
          <element name="container">
            <complexType>
              <sequence>
                <element name="something" type="tns:empty"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """))

    container_elm = schema.get_element('{http://tests.python-zeep.org/}container')
    obj = container_elm(something={})

    node = etree.Element('document')
    container_elm.render(node, obj)
    expected = """
        <document>
            <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:something/>
            </ns0:container>
        </document>
    """
    assert_nodes_equal(expected, node)
    item = container_elm.parse(list(node)[0], schema)
    assert item.something is None
def test_xml_defaults_parse_boolean():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="container">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="foo" type="xsd:boolean" default="false"/>
              </xsd:sequence>
              <xsd:attribute name="bar" type="xsd:boolean" default="0"/>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """))

    container_type = schema.get_element(
        '{http://tests.python-zeep.org/}container')
    obj = container_type()
    assert obj.foo == "false"
    assert obj.bar == "0"

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/" bar="false">
          <ns0:foo>false</ns0:foo>
        </ns0:container>
      </document>
    """
    node = etree.Element('document')
    container_type.render(node, obj)
    assert_nodes_equal(expected, node)
def test_xml_element_ref():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                   elementFormDefault="qualified">
          <element name="foo" type="string"/>
          <element name="bar">
            <complexType>
              <sequence>
                <element ref="tns:foo"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """))

    foo_type = schema.get_element('{http://tests.python-zeep.org/}foo')
    assert isinstance(foo_type.type, xsd.String)

    custom_type = schema.get_element('{http://tests.python-zeep.org/}bar')
    custom_type.signature()
    obj = custom_type(foo='bar')

    node = etree.Element('document')
    custom_type.render(node, obj)
    expected = """
        <document>
            <ns0:bar xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:foo>bar</ns0:foo>
            </ns0:bar>
        </document>
    """
    assert_nodes_equal(expected, node)
def test_xml_complex_type_unbounded_one():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <element name="Address">
            <complexType>
              <sequence>
                <element minOccurs="0" maxOccurs="unbounded" name="foo" type="string" />
              </sequence>
            </complexType>
          </element>
        </schema>
    """))
    address_type = schema.get_element('{http://tests.python-zeep.org/}Address')
    obj = address_type(foo=['foo'])

    expected = """
        <document>
          <ns0:Address xmlns:ns0="http://tests.python-zeep.org/">
            <ns0:foo>foo</ns0:foo>
          </ns0:Address>
        </document>
    """

    node = etree.Element('document')
    address_type.render(node, obj)
    assert_nodes_equal(expected, node)
def test_union_same_types():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <xsd:schema
            xmlns="http://tests.python-zeep.org/"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://tests.python-zeep.org/"
            targetNamespace="http://tests.python-zeep.org/"
            elementFormDefault="qualified">


          <xsd:simpleType name="MMYY">
            <xsd:restriction base="xsd:int"/>
          </xsd:simpleType>

          <xsd:simpleType name="MMYYYY">
            <xsd:restriction base="xsd:int"/>
          </xsd:simpleType>

          <xsd:simpleType name="Date">
            <xsd:union memberTypes="tns:MMYY MMYYYY"/>
          </xsd:simpleType>
          <xsd:element name="item" type="tns:Date"/>
        </xsd:schema>
    """))

    elm = schema.get_element('ns0:item')
    node = render_node(elm, '102018')
    expected = """
        <document>
          <ns0:item xmlns:ns0="http://tests.python-zeep.org/">102018</ns0:item>
        </document>
    """
    assert_nodes_equal(expected, node)
    value = elm.parse(list(node)[0], schema)
    assert value == 102018
def test_build_objects():
    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'authentication'),
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'username'),
                    xsd.String()),
                xsd.Group(
                    etree.QName('http://tests.python-zeep.org/', 'groupie'),
                    xsd.Sequence([
                        xsd.Element(
                            etree.QName('http://tests.python-zeep.org/',
                                        'password'),
                            xsd.String(),
                        )
                    ]))
            ])))
    assert custom_type.signature()
    obj = custom_type(username='******', password='******')

    expected = """
      <document>
        <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:username>foo</ns0:username>
          <ns0:password>bar</ns0:password>
        </ns0:authentication>
      </document>
    """
    node = etree.Element('document')
    custom_type.render(node, obj)
    assert_nodes_equal(expected, node)

    obj = custom_type.parse(node[0], None)
    assert obj.username == 'foo'
    assert obj.password == 'bar'
Example #33
0
def test_xml_element_ref_occurs():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                   elementFormDefault="qualified">
          <element name="foo" type="string"/>
          <element name="bar">
            <complexType>
              <sequence>
                <element ref="tns:foo" minOccurs="0"/>
                <element name="bar" type="string"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """))

    foo_type = schema.get_element('{http://tests.python-zeep.org/}foo')
    assert isinstance(foo_type.type, xsd.String)

    custom_type = schema.get_element('{http://tests.python-zeep.org/}bar')
    custom_type.signature()
    obj = custom_type(bar='foo')

    node = etree.Element('document')
    custom_type.render(node, obj)
    expected = """
        <document>
            <ns0:bar xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:bar>foo</ns0:bar>
            </ns0:bar>
        </document>
    """
    assert_nodes_equal(expected, node)
Example #34
0
def test_xml_array():
    schema = xsd.Schema(load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <element name="Address">
            <complexType>
              <sequence>
                <element minOccurs="0" maxOccurs="unbounded" name="foo" type="string" />
              </sequence>
            </complexType>
          </element>
        </schema>
    """))
    schema.set_ns_prefix('tns', 'http://tests.python-zeep.org/')

    address_type = schema.get_element('tns:Address')

    obj = address_type()
    assert obj.foo == []
    obj.foo.append('foo')
    obj.foo.append('bar')

    expected = """
        <document  xmlns:tns="http://tests.python-zeep.org/">
          <tns:Address>
            <tns:foo>foo</tns:foo>
            <tns:foo>bar</tns:foo>
          </tns:Address>
        </document>
    """
    node = etree.Element('document', nsmap=schema._prefix_map_custom)
    address_type.render(node, obj)
    assert_nodes_equal(expected, node)
Example #35
0
def test_attribute_any_type(schema_visitor):
    node = load_xml("""
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://tests.python-zeep.org/">
          <element name="foo">
            <complexType>
              <xsd:attribute name="base" type="xsd:anyURI" />
            </complexType>
          </element>
        </schema>
    """)
    schema_visitor.visit_schema(node)
    xsd_element = schema_visitor.schema.get_element(
        '{http://tests.python-zeep.org/}foo')
    value = xsd_element(base='hoi')

    node = render_node(xsd_element, value)
    expected = """
      <document>
        <ns0:foo xmlns:ns0="http://tests.python-zeep.org/" base="hoi"/>
      </document>
    """
    assert_nodes_equal(expected, node)
Example #36
0
def test_create_node():
    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'authentication'),
        xsd.ComplexType(children=[
            xsd.Element(
                etree.QName('http://tests.python-zeep.org/', 'username'),
                xsd.String()),
            xsd.Element(
                etree.QName('http://tests.python-zeep.org/', 'password'),
                xsd.String()),
        ]))
    obj = custom_type(username='******', password='******')

    expected = """
      <document>
        <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:username>foo</ns0:username>
          <ns0:password>bar</ns0:password>
        </ns0:authentication>
      </document>
    """
    node = etree.Element('document')
    custom_type.render(node, obj)
    assert_nodes_equal(expected, node)
def test_qualified_attribute():
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                attributeFormDefault="qualified"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="Address">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="foo" type="xsd:string" form="unqualified" />
              </xsd:sequence>
              <xsd:attribute name="id" type="xsd:string" use="required" form="unqualified" />
              <xsd:attribute name="pos" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """))

    address_type = schema.get_element("{http://tests.python-zeep.org/}Address")
    obj = address_type(foo="bar", id="20", pos="30")

    expected = """
      <document>
        <ns0:Address xmlns:ns0="http://tests.python-zeep.org/" id="20" ns0:pos="30">
          <foo>bar</foo>
        </ns0:Address>
      </document>
    """

    node = etree.Element("document")
    address_type.render(node, obj)
    assert_nodes_equal(expected, node)
Example #38
0
def test_any_without_element():
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                   elementFormDefault="qualified">
          <element name="item">
            <complexType>
              <sequence>
                <any minOccurs="0" maxOccurs="1"/>
              </sequence>
              <attribute name="type" type="string"/>
              <attribute name="title" type="string"/>
            </complexType>
          </element>
        </schema>
    """))
    item_elm = schema.get_element('{http://tests.python-zeep.org/}item')
    item = item_elm(xsd.AnyObject(xsd.String(), 'foobar'),
                    type='attr-1',
                    title='attr-2')

    node = render_node(item_elm, item)
    expected = """
        <document>
          <ns0:item xmlns:ns0="http://tests.python-zeep.org/" type="attr-1" title="attr-2">foobar</ns0:item>
        </document>
    """
    assert_nodes_equal(expected, node)

    item = item_elm.parse(node.getchildren()[0], schema)
    assert item.type == 'attr-1'
    assert item.title == 'attr-2'
    assert item._value_1 is None
Example #39
0
def test_mime_content_serialize_xml():
    wsdl = stub(schema=stub(_prefix_map={}))
    operation = stub(location='my-action', name='foo')

    element_1 = xsd.Element(
        'arg1', xsd.ComplexType([xsd.Element('arg1_1', xsd.String())]))
    element_2 = xsd.Element('arg2', xsd.String())
    abstract_message = definitions.AbstractMessage(
        etree.QName('{http://test.python-zeep.org/tests/msg}Method'))
    abstract_message.parts = OrderedDict([
        ('xarg1', definitions.MessagePart(element=element_1, type=None)),
        ('xarg2', definitions.MessagePart(element=element_2, type=None)),
    ])

    msg = messages.MimeContent(wsdl=wsdl,
                               name=None,
                               operation=operation,
                               content_type='text/xml',
                               part_name=None)

    msg._info = {
        'body': {
            'namespace': 'http://test.python-zeep.org/tests/rpc'
        },
        'header': None,
        'headerfault': None
    }
    msg.resolve(wsdl, abstract_message)

    serialized = msg.serialize(xarg1={'arg1_1': 'uh'}, xarg2='bla')
    assert serialized.headers == {'Content-Type': 'text/xml'}
    assert serialized.path == 'my-action'
    assert_nodes_equal(
        load_xml(serialized.content),
        load_xml(
            "<foo><xarg1><arg1_1>uh</arg1_1></xarg1><xarg2>bla</xarg2></foo>"))
def test_complex_type_with_attributes():
    schema = xsd.Schema(
        load_xml("""
        <?xml version="1.0"?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
          <xs:complexType name="Address">
            <xs:sequence>
              <xs:element minOccurs="0" maxOccurs="1" name="NameFirst" type="xs:string"/>
              <xs:element minOccurs="0" maxOccurs="1" name="NameLast" type="xs:string"/>
              <xs:element minOccurs="0" maxOccurs="1" name="Email" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:string" use="required"/>
          </xs:complexType>
          <xs:element name="Address" type="Address"/>
        </xs:schema>
    """))

    address_type = schema.get_element("Address")
    obj = address_type(NameFirst="John",
                       NameLast="Doe",
                       Email="*****@*****.**",
                       id="123")

    node = etree.Element("document")
    address_type.render(node, obj)

    expected = """
        <document>
            <Address id="123">
                <NameFirst>John</NameFirst>
                <NameLast>Doe</NameLast>
                <Email>[email protected]</Email>
            </Address>
        </document>
    """
    assert_nodes_equal(expected, node)
Example #41
0
def test_defaults():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="container">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="foo" type="xsd:string" default="hoi"/>
              </xsd:sequence>
              <xsd:attribute name="bar" type="xsd:string" default="hoi"/>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """.strip())

    schema = xsd.Schema(node)
    container_type = schema.get_element(
        '{http://tests.python-zeep.org/}container')
    obj = container_type()
    assert obj.foo == "hoi"
    assert obj.bar == "hoi"

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/" bar="hoi">
          <ns0:foo>hoi</ns0:foo>
        </ns0:container>
      </document>
    """
    node = etree.Element('document')
    container_type.render(node, obj)
    assert_nodes_equal(expected, node)
def test_xml_nill():
    schema = xsd.Schema(
        load_xml(
            """
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                elementFormDefault="qualified">
          <element name="container">
            <complexType>
              <sequence>
                <element name="foo" type="string" nillable="true"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """
        )
    )

    address_type = schema.get_element("ns0:container")
    obj = address_type()
    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
        </ns0:container>
      </document>
    """
    node = etree.Element("document")
    address_type.render(node, obj)
    etree.cleanup_namespaces(node)

    assert_nodes_equal(expected, node)
Example #43
0
def test_complex_type_empty():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <complexType name="empty"/>
          <element name="container">
            <complexType>
              <sequence>
                <element name="something" type="tns:empty"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """.strip())

    schema = xsd.Schema(node)

    container_elm = schema.get_element(
        '{http://tests.python-zeep.org/}container')
    obj = container_elm()

    node = etree.Element('document')
    container_elm.render(node, obj)
    expected = """
        <document>
            <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:something/>
            </ns0:container>
        </document>
    """
    assert_nodes_equal(expected, node)
    item = container_elm.parse(node.getchildren()[0], schema)
    assert item.something is None
Example #44
0
def test_array():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
          <element name="Address">
            <complexType>
              <sequence>
                <element minOccurs="0" maxOccurs="unbounded" name="foo" type="string" />
              </sequence>
            </complexType>
          </element>
        </schema>
    """.strip())

    schema = xsd.Schema(node)
    address_type = schema.get_element('{http://tests.python-zeep.org/}Address')
    obj = address_type()
    assert obj.foo == []
    obj.foo.append('foo')
    obj.foo.append('bar')

    expected = """
        <document>
          <ns0:Address xmlns:ns0="http://tests.python-zeep.org/">
            <ns0:foo>foo</ns0:foo>
            <ns0:foo>bar</ns0:foo>
          </ns0:Address>
        </document>
    """

    node = etree.Element('document')
    address_type.render(node, obj)
    assert_nodes_equal(expected, node)
Example #45
0
def test_choice_element_optional():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="container">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:choice minOccurs="0">
                  <xsd:element name="item_1" type="xsd:string" />
                  <xsd:element name="item_2" type="xsd:string" />
                  <xsd:element name="item_3" type="xsd:string" />
                </xsd:choice>
                <xsd:element name="item_4" type="xsd:string" />
             </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """.strip())
    schema = xsd.Schema(node)
    element = schema.get_element('ns0:container')
    value = element(item_4="foo")

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item_4>foo</ns0:item_4>
        </ns0:container>
      </document>
    """
    node = etree.Element('document')
    element.render(node, value)
    assert_nodes_equal(expected, node)
Example #46
0
def test_element_ref():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/"
                   elementFormDefault="qualified">
          <element name="foo" type="string"/>
          <element name="bar">
            <complexType>
              <sequence>
                <element ref="tns:foo"/>
              </sequence>
            </complexType>
          </element>
        </schema>
    """.strip())

    schema = xsd.Schema(node)

    foo_type = schema.get_element('{http://tests.python-zeep.org/}foo')
    assert isinstance(foo_type.type, xsd.String)

    custom_type = schema.get_element('{http://tests.python-zeep.org/}bar')
    obj = custom_type(foo='bar')

    node = etree.Element('document')
    custom_type.render(node, obj)
    expected = """
        <document>
            <ns0:bar xmlns:ns0="http://tests.python-zeep.org/">
                <ns0:foo>bar</ns0:foo>
            </ns0:bar>
        </document>
    """
    assert_nodes_equal(expected, node)
def test_list_type():
    node = load_xml(
        """
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                targetNamespace="http://tests.python-zeep.org/">

          <xsd:simpleType name="listOfIntegers">
            <xsd:list itemType="integer" />
          </xsd:simpleType>

          <xsd:element name="foo">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="arg" type="tns:listOfIntegers"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </schema>
    """
    )

    schema = parse_schema_node(node)
    xsd_element = schema.get_element("{http://tests.python-zeep.org/}foo")
    value = xsd_element(arg=[1, 2, 3, 4, 5])

    node = render_node(xsd_element, value)
    expected = """
        <document>
          <ns0:foo xmlns:ns0="http://tests.python-zeep.org/">
            <arg>1 2 3 4 5</arg>
          </ns0:foo>
        </document>
    """
    assert_nodes_equal(expected, node)
def test_wsdl_no_schema_namespace():
    wsdl_main = StringIO(
        """
        <wsdl:definitions
            xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
            xmlns:tns="http://Example.org"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://Example.org"
            xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
          <wsdl:types>
            <xsd:schema elementFormDefault="qualified" >
              <xsd:element name="AddResponse">
                <xsd:complexType>
                    <xsd:sequence>
                    <xsd:element minOccurs="0" maxOccurs="1" ref="demo" />
                    </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="Add">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element minOccurs="1" name="a" type="xsd:int" />
                    <xsd:element minOccurs="1" name="b" type="xsd:int" />
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="AddResponse">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element minOccurs="0" name="result" type="xsd:int" />
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:schema>
            <xsd:schema elementFormDefault="qualified" >
              <xsd:element name="demo">
              </xsd:element>
            </xsd:schema>
          </wsdl:types>
          <wsdl:message name="ICalculator_Add_InputMessage">
            <wsdl:part name="parameters" element="Add" />
          </wsdl:message>
          <wsdl:message name="ICalculator_Add_OutputMessage">
            <wsdl:part name="parameters" element="AddResponse" />
          </wsdl:message>
          <wsdl:portType name="ICalculator">
            <wsdl:operation name="Add">
              <wsdl:input message="tns:ICalculator_Add_InputMessage" />
              <wsdl:output message="tns:ICalculator_Add_OutputMessage" />
            </wsdl:operation>
          </wsdl:portType>
          <wsdl:binding name="DefaultBinding_ICalculator" type="tns:ICalculator">
            <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
            <wsdl:operation name="Add">
              <soap:operation soapAction="http://Example.org/ICalculator/Add" style="document" />
              <wsdl:input>
                <soap:body use="literal" />
              </wsdl:input>
              <wsdl:output>
                <soap:body use="literal" />
              </wsdl:output>
            </wsdl:operation>
          </wsdl:binding>
          <wsdl:service name="CalculatorService">
            <wsdl:port name="ICalculator" binding="tns:DefaultBinding_ICalculator">
              <soap:address location="http://Example.org/ICalculator" />
            </wsdl:port>
          </wsdl:service>
        </wsdl:definitions>
    """
    )
    client = stub(settings=Settings(), plugins=[], wsse=None)

    transport = DummyTransport()
    document = wsdl.Document(wsdl_main, transport)
    binding = (
        document.services.get("CalculatorService").ports.get("ICalculator").binding
    )

    envelope, headers = binding._create(
        "Add",
        args=[3, 4],
        kwargs={},
        client=client,
        options={"address": "http://tests.python-zeep.org/test"},
    )

    expected = """
        <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
          <soap-env:Body>
            <Add>
                <a>3</a>
                <b>4</b>
            </Add>
          </soap-env:Body>
        </soap-env:Envelope>
    """
    assert_nodes_equal(expected, envelope)
def test_extra_http_headers(recwarn, monkeypatch):

    wsdl_main = StringIO(
        """
        <?xml version="1.0"?>
        <wsdl:definitions
          xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:tns="http://tests.python-zeep.org/xsd-main"
          xmlns:sec="http://tests.python-zeep.org/wsdl-secondary"
          xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap12/"
          xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap12/"
          targetNamespace="http://tests.python-zeep.org/xsd-main">
          <wsdl:types>
            <xsd:schema
                targetNamespace="http://tests.python-zeep.org/xsd-main"
                xmlns:tns="http://tests.python-zeep.org/xsd-main">
              <xsd:element name="input" type="xsd:string"/>
              <xsd:element name="input2" type="xsd:string"/>
            </xsd:schema>
          </wsdl:types>

          <wsdl:message name="dummyRequest">
            <wsdl:part name="response" element="tns:input"/>
          </wsdl:message>
          <wsdl:message name="dummyResponse">
            <wsdl:part name="response" element="tns:input2"/>
          </wsdl:message>

          <wsdl:portType name="TestPortType">
            <wsdl:operation name="TestOperation1">
              <wsdl:input message="dummyRequest"/>
              <wsdl:output message="dummyResponse"/>
            </wsdl:operation>
          </wsdl:portType>

          <wsdl:binding name="TestBinding" type="tns:TestPortType">
            <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
            <wsdl:operation name="TestOperation1">
              <soap:operation soapAction="urn:dummyRequest"/>
              <wsdl:input>
                <soap:body use="literal"/>
              </wsdl:input>
              <wsdl:output>
                <soap:body use="literal"/>
              </wsdl:output>
            </wsdl:operation>
          </wsdl:binding>
          <wsdl:service name="TestService">
            <wsdl:documentation>Test service</wsdl:documentation>
            <wsdl:port name="TestPortType" binding="tns:TestBinding">
              <soap:address location="http://tests.python-zeep.org/test"/>
            </wsdl:port>
          </wsdl:service>
        </wsdl:definitions>
    """.strip()
    )

    client = stub(settings=Settings(), plugins=[], wsse=None)

    transport = DummyTransport()
    doc = wsdl.Document(wsdl_main, transport, settings=client.settings)
    binding = doc.services.get("TestService").ports.get("TestPortType").binding

    headers = {"Authorization": "Bearer 1234"}
    with client.settings(extra_http_headers=headers):
        envelope, headers = binding._create(
            "TestOperation1",
            args=["foo"],
            kwargs={},
            client=client,
            options={"address": "http://tests.python-zeep.org/test"},
        )

    expected = """
        <soap-env:Envelope xmlns:soap-env="http://www.w3.org/2003/05/soap-envelope">
          <soap-env:Body>
            <ns0:input xmlns:ns0="http://tests.python-zeep.org/xsd-main">foo</ns0:input>
          </soap-env:Body>
        </soap-env:Envelope>
    """
    assert_nodes_equal(expected, envelope)

    assert headers["Authorization"] == "Bearer 1234"
Example #50
0
def test_choice_element_multiple():
    node = etree.fromstring("""
        <?xml version="1.0"?>
        <xsd:schema
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified"
                targetNamespace="http://tests.python-zeep.org/">
          <xsd:element name="container">
            <xsd:complexType>
              <xsd:choice maxOccurs="3">
                <xsd:element name="item_1" type="xsd:string" />
                <xsd:element name="item_2" type="xsd:string" />
                <xsd:element name="item_3" type="xsd:string" />
              </xsd:choice>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
    """.strip())
    schema = xsd.Schema(node)
    element = schema.get_element('ns0:container')

    value = element(_value_1=[
        {
            'item_1': 'foo'
        },
        {
            'item_2': 'bar'
        },
        {
            'item_1': 'three'
        },
    ])
    assert value._value_1 == [
        {
            'item_1': 'foo'
        },
        {
            'item_2': 'bar'
        },
        {
            'item_1': 'three'
        },
    ]

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item_1>foo</ns0:item_1>
          <ns0:item_2>bar</ns0:item_2>
          <ns0:item_1>three</ns0:item_1>
        </ns0:container>
      </document>
    """
    node = etree.Element('document')
    element.render(node, value)
    assert_nodes_equal(expected, node)

    value = element.parse(node.getchildren()[0], schema)
    assert value._value_1 == [
        {
            'item_1': 'foo'
        },
        {
            'item_2': 'bar'
        },
        {
            'item_1': 'three'
        },
    ]
Example #51
0
def test_empty_input_parse():
    wsdl_content = StringIO("""
    <wsdl:definitions
        xmlns:tns="http://tests.python-zeep.org/"
        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://tests.python-zeep.org/">
      <wsdl:types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified"
            targetNamespace="http://tests.python-zeep.org/">
        <element name="Result">
            <complexType>
            <sequence>
                <element name="item" type="xsd:string"/>
            </sequence>
            </complexType>
        </element>
        </schema>
      </wsdl:types>
      <wsdl:message name="Request"></wsdl:message>
      <wsdl:message name="Response">
        <wsdl:part element="tns:Result" name="Result"/>
      </wsdl:message>
      <wsdl:portType name="PortType">
        <wsdl:operation name="getResult">
          <wsdl:input message="tns:Request" name="getResultRequest"/>
          <wsdl:output message="tns:Response" name="getResultResponse"/>
        </wsdl:operation>
      </wsdl:portType>
      <wsdl:binding name="Binding" type="tns:PortType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="getResult">
          <soap:operation soapAction=""/>
          <wsdl:input name="Result">
            <soap:body use="literal"/>
          </wsdl:input>
          </wsdl:operation>
      </wsdl:binding>
      <wsdl:service name="Service">
        <wsdl:port binding="tns:Binding" name="ActiveStations">
        <soap:address location="https://opendap.co-ops.nos.noaa.gov/axis/services/ActiveStations"/>
        </wsdl:port>
      </wsdl:service>
    </wsdl:definitions>
    """.strip())

    root = wsdl.Document(wsdl_content, None)

    binding = root.bindings["{http://tests.python-zeep.org/}Binding"]
    operation = binding.get("getResult")
    assert operation.input.signature() == ""

    serialized = operation.input.serialize()
    expected = """
        <?xml version="1.0"?>
        <soap-env:Envelope
            xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
          <soap-env:Body/>
        </soap-env:Envelope>
    """
    assert_nodes_equal(expected, serialized.content)
Example #52
0
def test_serialize_multiple_parts():
    wsdl_content = StringIO("""
    <definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
                 xmlns:tns="http://tests.python-zeep.org/tns"
                 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 targetNamespace="http://tests.python-zeep.org/tns">
      <types>
        <xsd:schema targetNamespace="http://tests.python-zeep.org/tns"
                    elementFormDefault="qualified">
          <xsd:element name="Request">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="arg1" type="xsd:string"/>
                <xsd:element name="arg2" type="xsd:string"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
      </types>

      <message name="Input">
        <part name="request1" element="tns:Request"/>
        <part name="request2" element="tns:Request"/>
      </message>

      <portType name="TestPortType">
        <operation name="TestOperation">
          <input message="Input"/>
        </operation>
      </portType>

      <binding name="TestBinding" type="tns:TestPortType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="TestOperation">
          <soap:operation soapAction=""/>
          <input>
            <soap:body use="literal"/>
          </input>
        </operation>
      </binding>
    </definitions>
    """.strip())

    root = wsdl.Document(wsdl_content, None)

    binding = root.bindings["{http://tests.python-zeep.org/tns}TestBinding"]
    operation = binding.get("TestOperation")

    serialized = operation.input.serialize(request1={
        "arg1": "ah1",
        "arg2": "ah2"
    },
                                           request2={
                                               "arg1": "ah1",
                                               "arg2": "ah2"
                                           })
    expected = """
        <?xml version="1.0"?>
        <soap-env:Envelope
            xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
          <soap-env:Body>
            <ns0:Request xmlns:ns0="http://tests.python-zeep.org/tns">
            <ns0:arg1>ah1</ns0:arg1>
            <ns0:arg2>ah2</ns0:arg2>
            </ns0:Request>
            <ns1:Request xmlns:ns1="http://tests.python-zeep.org/tns">
            <ns1:arg1>ah1</ns1:arg1>
            <ns1:arg2>ah2</ns1:arg2>
            </ns1:Request>
          </soap-env:Body>
        </soap-env:Envelope>
    """
    assert_nodes_equal(expected, serialized.content)
Example #53
0
def test_parse_with_header_other_message():
    wsdl_content = StringIO("""
    <definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
                 xmlns:tns="http://tests.python-zeep.org/tns"
                 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 targetNamespace="http://tests.python-zeep.org/tns">
      <types>
        <xsd:schema targetNamespace="http://tests.python-zeep.org/tns">
          <xsd:element name="Request" type="xsd:string"/>
          <xsd:element name="RequestHeader" type="xsd:string"/>
        </xsd:schema>
      </types>

      <message name="InputHeader">
        <part name="header" element="tns:RequestHeader"/>
      </message>
      <message name="Input">
        <part element="tns:Request"/>
      </message>

      <portType name="TestPortType">
        <operation name="TestOperation">
          <input message="Input"/>
        </operation>
      </portType>

      <binding name="TestBinding" type="tns:TestPortType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="TestOperation">
          <soap:operation soapAction=""/>
          <input>
            <soap:header message="tns:InputHeader" part="header" use="literal" />
            <soap:body use="literal"/>
          </input>
        </operation>
      </binding>
    </definitions>
    """.strip())

    root = wsdl.Document(wsdl_content, None)
    root.types.set_ns_prefix("soap-env",
                             "http://schemas.xmlsoap.org/soap/envelope/")

    binding = root.bindings["{http://tests.python-zeep.org/tns}TestBinding"]
    operation = binding.get("TestOperation")

    assert (operation.input.header.signature(
        schema=root.types) == "soap-env:Header(header: xsd:string)")
    assert (operation.input.body.signature(
        schema=root.types) == "ns0:Request(xsd:string)")

    header = root.types.get_element(
        "{http://tests.python-zeep.org/tns}RequestHeader")("foo")
    serialized = operation.input.serialize("ah1",
                                           _soapheaders={"header": header})
    expected = """
        <?xml version="1.0"?>
        <soap-env:Envelope
            xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
          <soap-env:Header>
            <ns0:RequestHeader xmlns:ns0="http://tests.python-zeep.org/tns">foo</ns0:RequestHeader>
          </soap-env:Header>
          <soap-env:Body>
            <ns0:Request xmlns:ns0="http://tests.python-zeep.org/tns">ah1</ns0:Request>
          </soap-env:Body>
        </soap-env:Envelope>
    """
    assert_nodes_equal(expected, serialized.content)
Example #54
0
def test_serializer_with_header_custom_xml():
    wsdl_content = StringIO("""
    <definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
                 xmlns:tns="http://tests.python-zeep.org/tns"
                 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 targetNamespace="http://tests.python-zeep.org/tns">
      <types>
        <xsd:schema targetNamespace="http://tests.python-zeep.org/tns"
                    elementFormDefault="qualified">
          <xsd:element name="Request">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="arg1" type="xsd:string"/>
                <xsd:element name="arg2" type="xsd:string"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
      </types>

      <message name="Input">
        <part element="tns:Request"/>
      </message>

      <portType name="TestPortType">
        <operation name="TestOperation">
          <input message="Input"/>
        </operation>
      </portType>

      <binding name="TestBinding" type="tns:TestPortType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="TestOperation">
          <soap:operation soapAction=""/>
          <input>
            <soap:body use="literal"/>
          </input>
        </operation>
      </binding>
    </definitions>
    """.strip())

    root = wsdl.Document(wsdl_content, None)
    binding = root.bindings["{http://tests.python-zeep.org/tns}TestBinding"]
    operation = binding.get("TestOperation")

    header_value = etree.Element("{http://test.python-zeep.org/custom}auth")
    etree.SubElement(
        header_value,
        "{http://test.python-zeep.org/custom}username").text = "mvantellingen"

    serialized = operation.input.serialize(arg1="ah1",
                                           arg2="ah2",
                                           _soapheaders=[header_value])

    expected = """
        <?xml version="1.0"?>
        <soap-env:Envelope
            xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
          <soap-env:Header>
            <ns0:auth xmlns:ns0="http://test.python-zeep.org/custom">
              <ns0:username>mvantellingen</ns0:username>
            </ns0:auth>
          </soap-env:Header>
          <soap-env:Body>
            <ns0:Request xmlns:ns0="http://tests.python-zeep.org/tns">
              <ns0:arg1>ah1</ns0:arg1>
              <ns0:arg2>ah2</ns0:arg2>
            </ns0:Request>
          </soap-env:Body>
        </soap-env:Envelope>
    """
    assert_nodes_equal(expected, serialized.content)
Example #55
0
def test_serialize_with_header_and_custom_mixed():
    wsdl_content = StringIO("""
    <definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
                 xmlns:tns="http://tests.python-zeep.org/tns"
                 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 targetNamespace="http://tests.python-zeep.org/tns">
      <types>
        <xsd:schema targetNamespace="http://tests.python-zeep.org/tns"
                    elementFormDefault="qualified">
          <xsd:element name="Request">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="arg1" type="xsd:string"/>
                <xsd:element name="arg2" type="xsd:string"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="Authentication">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="username" type="xsd:string"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
        </xsd:schema>
      </types>

      <message name="Input">
        <part element="tns:Request"/>
        <part element="tns:Authentication" name="Header"/>
      </message>

      <portType name="TestPortType">
        <operation name="TestOperation">
          <input message="Input"/>
        </operation>
      </portType>

      <binding name="TestBinding" type="tns:TestPortType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="TestOperation">
          <soap:operation soapAction=""/>
          <input>
            <soap:body use="literal"/>
            <soap:header message="tns:Input" part="Header" use="literal"/>
          </input>
        </operation>
      </binding>
    </definitions>
    """.strip())

    root = wsdl.Document(wsdl_content, None)

    binding = root.bindings["{http://tests.python-zeep.org/tns}TestBinding"]
    operation = binding.get("TestOperation")

    header = root.types.get_element(
        "{http://tests.python-zeep.org/tns}Authentication")
    header_1 = header(username="******")

    header = xsd.Element(
        "{http://test.python-zeep.org/custom}custom",
        xsd.ComplexType([
            xsd.Element("{http://test.python-zeep.org/custom}foo",
                        xsd.String())
        ]),
    )
    header_2 = header(foo="bar")

    serialized = operation.input.serialize(arg1="ah1",
                                           arg2="ah2",
                                           _soapheaders=[header_1, header_2])
    expected = """
        <?xml version="1.0"?>
        <soap-env:Envelope
            xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
          <soap-env:Header>
            <ns0:Authentication xmlns:ns0="http://tests.python-zeep.org/tns">
              <ns0:username>mvantellingen</ns0:username>
            </ns0:Authentication>
            <ns1:custom xmlns:ns1="http://test.python-zeep.org/custom">
              <ns1:foo>bar</ns1:foo>
            </ns1:custom>
          </soap-env:Header>
          <soap-env:Body>
            <ns0:Request xmlns:ns0="http://tests.python-zeep.org/tns">
              <ns0:arg1>ah1</ns0:arg1>
              <ns0:arg2>ah2</ns0:arg2>
            </ns0:Request>
          </soap-env:Body>
        </soap-env:Envelope>
    """
    assert_nodes_equal(expected, serialized.content)
Example #56
0
def test_any():
    some_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'doei'),
        xsd.String())

    complex_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'complex'),
        xsd.ComplexType(
            children=[
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'item_1'),
                    xsd.String()),
                xsd.Element(
                    etree.QName('http://tests.python-zeep.org/', 'item_2'),
                    xsd.String()),
            ]
        ))

    custom_type = xsd.Element(
        etree.QName('http://tests.python-zeep.org/', 'hoi'),
        xsd.ComplexType(
            children=[
                xsd.Any(),
                xsd.Any(),
                xsd.Any(),
            ]
        ))

    any_1 = xsd.AnyObject(some_type, "DOEI!")
    any_2 = xsd.AnyObject(
        complex_type, complex_type(item_1='val_1', item_2='val_2'))
    any_3 = xsd.AnyObject(
        complex_type, [
            complex_type(item_1='val_1_1', item_2='val_1_2'),
            complex_type(item_1='val_2_1', item_2='val_2_2'),
        ])

    obj = custom_type(_any_1=any_1, _any_2=any_2, _any_3=any_3)

    expected = """
      <document>
        <ns0:hoi xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:doei>DOEI!</ns0:doei>
          <ns0:complex>
            <ns0:item_1>val_1</ns0:item_1>
            <ns0:item_2>val_2</ns0:item_2>
          </ns0:complex>
          <ns0:complex>
            <ns0:item_1>val_1_1</ns0:item_1>
            <ns0:item_2>val_1_2</ns0:item_2>
          </ns0:complex>
          <ns0:complex>
            <ns0:item_1>val_2_1</ns0:item_1>
            <ns0:item_2>val_2_2</ns0:item_2>
          </ns0:complex>
        </ns0:hoi>
      </document>
    """
    node = etree.Element('document')
    custom_type.render(node, obj)
    assert_nodes_equal(expected, node)
Example #57
0
def test_nil_elements():
    custom_type = xsd.Element(
        "{http://tests.python-zeep.org/}container",
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    "{http://tests.python-zeep.org/}item_1",
                    xsd.ComplexType(
                        xsd.Sequence([
                            xsd.Element(
                                "{http://tests.python-zeep.org/}item_1_1",
                                xsd.String(),
                            )
                        ])),
                    nillable=True,
                ),
                xsd.Element(
                    "{http://tests.python-zeep.org/}item_2",
                    xsd.DateTime(),
                    nillable=True,
                ),
                xsd.Element(
                    "{http://tests.python-zeep.org/}item_3",
                    xsd.String(),
                    min_occurs=0,
                    nillable=False,
                ),
                xsd.Element(
                    "{http://tests.python-zeep.org/}item_4",
                    xsd.ComplexType(
                        xsd.Sequence([
                            xsd.Element(
                                "{http://tests.python-zeep.org/}item_4_1",
                                xsd.String(),
                                nillable=True,
                            )
                        ])),
                ),
                xsd.Element(
                    "{http://tests.python-zeep.org/}item_5",
                    xsd.ComplexType(
                        xsd.Sequence([
                            xsd.Element(
                                "{http://tests.python-zeep.org/}item_5_1",
                                xsd.String(),
                                min_occurs=1,
                                nillable=False,
                            )
                        ],
                                     min_occurs=0)),
                ),
            ])),
    )
    obj = custom_type(item_1=None,
                      item_2=None,
                      item_3=None,
                      item_4={},
                      item_5=xsd.Nil)

    expected = """
      <document>
        <ns0:container xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:item_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
          <ns0:item_2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
          <ns0:item_4>
            <ns0:item_4_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
          </ns0:item_4>
          <ns0:item_5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
        </ns0:container>
      </document>
    """
    node = render_node(custom_type, obj)
    etree.cleanup_namespaces(node)
    assert_nodes_equal(expected, node)
Example #58
0
def test_choice_init():
    root = xsd.Element(
        etree.QName("http://tests.python-zeep.org/", "kies"),
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    etree.QName("http://tests.python-zeep.org/", "pre"),
                    xsd.String(),
                ),
                xsd.Choice(
                    [
                        xsd.Element(
                            etree.QName("http://tests.python-zeep.org/",
                                        "item_1"),
                            xsd.String(),
                        ),
                        xsd.Element(
                            etree.QName("http://tests.python-zeep.org/",
                                        "item_2"),
                            xsd.String(),
                        ),
                        xsd.Element(
                            etree.QName("http://tests.python-zeep.org/",
                                        "item_3"),
                            xsd.String(),
                        ),
                        xsd.Sequence([
                            xsd.Element(
                                etree.QName("http://tests.python-zeep.org/",
                                            "item_4_1"),
                                xsd.String(),
                            ),
                            xsd.Element(
                                etree.QName("http://tests.python-zeep.org/",
                                            "item_4_2"),
                                xsd.String(),
                            ),
                        ]),
                    ],
                    max_occurs=4,
                ),
                xsd.Element(
                    etree.QName("http://tests.python-zeep.org/", "post"),
                    xsd.String(),
                ),
            ])),
    )

    obj = root(
        pre="foo",
        _value_1=[
            {
                "item_1": "value-1"
            },
            {
                "item_2": "value-2"
            },
            {
                "item_1": "value-3"
            },
            {
                "item_4_1": "value-4-1",
                "item_4_2": "value-4-2"
            },
        ],
        post="bar",
    )

    assert obj._value_1 == [
        {
            "item_1": "value-1"
        },
        {
            "item_2": "value-2"
        },
        {
            "item_1": "value-3"
        },
        {
            "item_4_1": "value-4-1",
            "item_4_2": "value-4-2"
        },
    ]

    node = etree.Element("document")
    root.render(node, obj)
    assert etree.tostring(node)

    expected = """
    <document>
      <ns0:kies xmlns:ns0="http://tests.python-zeep.org/">
        <ns0:pre>foo</ns0:pre>
        <ns0:item_1>value-1</ns0:item_1>
        <ns0:item_2>value-2</ns0:item_2>
        <ns0:item_1>value-3</ns0:item_1>
        <ns0:item_4_1>value-4-1</ns0:item_4_1>
        <ns0:item_4_2>value-4-2</ns0:item_4_2>
        <ns0:post>bar</ns0:post>
      </ns0:kies>
    </document>
    """.strip()
    assert_nodes_equal(expected, node)
Example #59
0
def test_any():
    some_type = xsd.Element(
        etree.QName("http://tests.python-zeep.org/", "doei"), xsd.String())

    complex_type = xsd.Element(
        etree.QName("http://tests.python-zeep.org/", "complex"),
        xsd.ComplexType(
            xsd.Sequence([
                xsd.Element(
                    etree.QName("http://tests.python-zeep.org/", "item_1"),
                    xsd.String(),
                ),
                xsd.Element(
                    etree.QName("http://tests.python-zeep.org/", "item_2"),
                    xsd.String(),
                ),
            ])),
    )

    custom_type = xsd.Element(
        etree.QName("http://tests.python-zeep.org/", "hoi"),
        xsd.ComplexType(xsd.Sequence([xsd.Any(),
                                      xsd.Any(),
                                      xsd.Any()])),
    )

    any_1 = xsd.AnyObject(some_type, "DOEI!")
    any_2 = xsd.AnyObject(complex_type,
                          complex_type(item_1="val_1", item_2="val_2"))
    any_3 = xsd.AnyObject(
        complex_type,
        [
            complex_type(item_1="val_1_1", item_2="val_1_2"),
            complex_type(item_1="val_2_1", item_2="val_2_2"),
        ],
    )

    obj = custom_type(_value_1=any_1, _value_2=any_2, _value_3=any_3)

    expected = """
      <document>
        <ns0:hoi xmlns:ns0="http://tests.python-zeep.org/">
          <ns0:doei>DOEI!</ns0:doei>
          <ns0:complex>
            <ns0:item_1>val_1</ns0:item_1>
            <ns0:item_2>val_2</ns0:item_2>
          </ns0:complex>
          <ns0:complex>
            <ns0:item_1>val_1_1</ns0:item_1>
            <ns0:item_2>val_1_2</ns0:item_2>
          </ns0:complex>
          <ns0:complex>
            <ns0:item_1>val_2_1</ns0:item_1>
            <ns0:item_2>val_2_2</ns0:item_2>
          </ns0:complex>
        </ns0:hoi>
      </document>
    """
    node = etree.Element("document")
    custom_type.render(node, obj)
    assert_nodes_equal(expected, node)
Example #60
0
def test_timestamp_token():
    envelope = load_xml("""
            <soap-env:Envelope
                xmlns:ns0="http://example.com/stockquote.xsd"
                xmlns:soap="https://schemas.xmlsoap.org/wsdl/soap/"
                xmlns:soap-env="https://schemas.xmlsoap.org/soap/envelope/"
                xmlns:wsdl="https://schemas.xmlsoap.org/wsdl/"
                xmlns:wsu="https://schemas.xmlsoap.org/ws/2003/06/utility"
                xmlns:xsd="https://www.w3.org/2001/XMLSchema"
            >
              <soap-env:Header xmlns:ns0="https://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                <ns0:Security>
                  <ns0:UsernameToken/>
                </ns0:Security>
              </soap-env:Header>
              <soap-env:Body>
                <ns0:TradePriceRequest>
                  <tickerSymbol>foobar</tickerSymbol>
                  <ns0:country/>
                </ns0:TradePriceRequest>
              </soap-env:Body>
            </soap-env:Envelope>
        """)  # noqa

    timestamp_token = WSU.Timestamp()
    timestamp_token.attrib["Id"] = "id-21a27a50-9ebf-49cc-96bf-fcf7131e7858"
    some_date_obj = datetime.datetime(2018, 11, 18, 15, 44, 27, 440252)
    timestamp_elements = [
        WSU.Created(some_date_obj.strftime("%Y-%m-%dT%H:%M:%SZ")),
        WSU.Expires(
            (some_date_obj +
             datetime.timedelta(minutes=10)).strftime("%Y-%m-%dT%H:%M:%SZ")),
    ]
    timestamp_token.extend(timestamp_elements)

    token = UsernameToken("Vishu",
                          "Guntupalli",
                          timestamp_token=timestamp_token)
    envelope, headers = token.apply(envelope, {})
    expected = """
            <soap-env:Envelope
                xmlns:ns0="http://example.com/stockquote.xsd"
                xmlns:soap="https://schemas.xmlsoap.org/wsdl/soap/"
                xmlns:soap-env="https://schemas.xmlsoap.org/soap/envelope/"
                xmlns:wsdl="https://schemas.xmlsoap.org/wsdl/"
                xmlns:wsu="https://schemas.xmlsoap.org/ws/2003/06/utility"
                xmlns:xsd="https://www.w3.org/2001/XMLSchema">
              <soap-env:Header xmlns:ns0="https://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                <ns0:Security>
                  <ns0:UsernameToken>
                    <ns0:Username>Vishu</ns0:Username>
                    <ns0:Password Type="https://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Guntupalli</ns0:Password>
                  </ns0:UsernameToken>
                  <wsu:Timestamp xmlns:wsu="https://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" Id="id-21a27a50-9ebf-49cc-96bf-fcf7131e7858">
                       <wsu:Created>2018-11-18T15:44:27Z</wsu:Created>
                       <wsu:Expires>2018-11-18T15:54:27Z</wsu:Expires>
                  </wsu:Timestamp>
                </ns0:Security>
              </soap-env:Header>
              <soap-env:Body>
                <ns0:TradePriceRequest>
                  <tickerSymbol>foobar</tickerSymbol>
                  <ns0:country/>
                </ns0:TradePriceRequest>
              </soap-env:Body>
            </soap-env:Envelope>
        """  # noqa
    assert_nodes_equal(envelope, expected)