def test_gen1(self):
        orig = """
        <EncryptedData xmlns='http://www.w3.org/2001/04/xmlenc#' Type='http://www.w3.org/2001/04/xmlenc#Element'>
            <EncryptionMethod Algorithm='http://www.w3.org/2001/04/xmlenc#tripledes-cbc'/>
            <ds:KeyInfo xmlns:ds='http://www.w3.org/2000/09/xmldsig#'>
                <ds:KeyName>John Smith</ds:KeyName>
            </ds:KeyInfo>
            <CipherData>
                <CipherValue>ABCDEF</CipherValue>
            </CipherData>
        </EncryptedData>
        """

        # Main structure + NS
        doc = xml()
        doc.ns = default_ns.xenc
        doc.ns_map += default_ns.dsig

        # Top-level
        data = doc.EncryptedData
        data._Type = 'http://www.w3.org/2001/04/xmlenc#Element'

        # Children
        data.EncryptionMethod._Algorithm = 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc'
        data.dsig_KeyInfo.dsig_KeyName = 'John Smith'
        data.CipherData.CipherValue = 'ABCDEF'

        # Compare
        compare_xml(orig, doc.to_xml())
    def test_gen5(self):
        orig = """
        <ReferenceList xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
            <DataReference URI="#invoice34">
                <ds:Transforms>
                    <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
                        <ds:XPath xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                            self::xenc:EncryptedData[@Id="example1"]
                        </ds:XPath>
                    </ds:Transform>
                </ds:Transforms>
            </DataReference>
        </ReferenceList>
        """

        doc = xml()
        doc.ns_map += default_ns.dsig, default_ns.xenc

        doc.ReferenceList.DataReference._URI = '#invoice34'
        transforms = doc.ReferenceList.DataReference.dsig_Transforms
        transforms.dsig_Transform._Algorithm = 'http://www.w3.org/TR/1999/REC-xpath-19991116'
        transforms.dsig_Transform.dsig_XPath = 'self::xenc:EncryptedData[@Id="example1"]'

        # Compare
        compare_xml(orig, doc.to_xml())
    def test_to_xml_to_lxml_cleanup_ns_false(self):
        orig = """
              <root>
              <a xmlns:x="bar/x">
                <x:b foo="bar/foo1">
                    <x:ccc xmlns:rep="bar/rep">111</x:ccc>
                </x:b>
                <x:b foo="bar/foo2"/>
              </a>
            </root>
      """
        doc = xml()
        doc.ns_map += {'x':'bar/x', 'rep':'bar/rep'}

        root = doc.root
        b = root.a.x_b[0]
        b._foo = 'bar/foo1'
        b.x_ccc = '111'
        root.a.x_b[1]._foo = 'bar/foo2'

        expected = b'<root><a><ns0:b xmlns:ns0="bar/x" foo="bar/foo1"><ns0:ccc>111</ns0:ccc></ns0:b><ns1:b xmlns:ns1="bar/x"\
 foo="bar/foo2"/></a></root>'
        result = doc.to_xml(cleanup_ns=False)

        # Compare
        compare_xml(orig, result)

        # Here lxml assigned its own namespace prefixes
        self.assertEquals(expected, result)
    def test_gen2(self):
        orig = """
        <?xml version='1.0'?>
         <PaymentInfo xmlns='http://example.org/paymentv2'>
           <Name>John Smith</Name>
           <CreditCard Limit='5,000' Currency='USD'>
             <Number>4019 2445 0277 5567</Number>
             <Issuer>Example Bank</Issuer>
             <Expiration>04/02</Expiration>
           </CreditCard>
         </PaymentInfo>
        """
        doc = xml()
        doc.ns = 'http://example.org/paymentv2'

        info = doc.PaymentInfo
        info.Name = 'John Smith'
        info.CreditCard._Limit = '5,000'
        info.CreditCard._Currency = 'USD'
        info.CreditCard.Number = '4019 2445 0277 5567'
        info.CreditCard.Issuer = 'Example Bank'
        info.CreditCard.Expiration = '04/02'

        # Compare
        compare_xml(orig, doc.to_xml())
    def test_to_xml_to_lxml_cleanup_ns_true(self):
        orig = """
              <root>
              <a xmlns:x="bar/x">
                <x:b foo="bar/foo1">
                    <x:ccc xmlns:rep="bar/rep">111</x:ccc>
                </x:b>
                <x:b foo="bar/foo2"/>
              </a>
            </root>
      """
        doc = xml()
        doc.ns_map += {'x':'bar/x', 'rep':'bar/rep'}

        root = doc.root
        b = root.a.x_b[0]
        b._foo = 'bar/foo1'
        b.x_ccc = '111'
        root.a.x_b[1]._foo = 'bar/foo2'

        expected = b'<root xmlns:x="bar/x"><a><x:b foo="bar/foo1"><x:ccc>111</x:ccc></x:b><x:b foo="bar/foo2"/></a></root>'
        result = doc.to_xml()

        # Compare
        compare_xml(orig, result)

        # Now, we expect for our namespace to be defined as in 'result', i.e. each namespace is declared once
        self.assertEquals(expected, result)
    def test_diff_noparse_markup(self):
        orig = """
        <root>
          <a>
            <b>ccc</b>
          </a>
        </root>
        """
        doc = xml()
        doc.root.a.b.ddd = 'eee'

        expected = """
Expected:

            <root>
              <a>
                <b>ccc</b>
              </a>
            </root>
            
Got:
    <root><a><b><ddd>eee</ddd></b></a></root>
        """

        try:
            compare_xml(orig, doc.to_xml(), NOPARSE_MARKUP)
        except AssertionError as e:
            self.assertEquals(e.args[0].strip(), expected.strip())
    def test_gen_attr2(self):
        orig = """
        <a aa="1" xmlns="example.com">
          <b bb="2" bbb="22" xmlns="example.com/2">
            <c cc="3" ccc="33">ddd</c>
          </b>
        </a>
        """
        doc1 = xml()
        doc1.ns = 'example.com'
        doc1.a._aa = '1'
        doc1.a.b.ns = 'example.com/2'
        doc1.a.b._bb = '2'
        doc1.a.b._bbb = '22'
        doc1.a.b.c._cc = '3'
        doc1.a.b.c._ccc = '33'
        doc1.a.b.c = 'ddd'

        doc2 = xml()
        doc2.a.ns = 'example.com'
        doc2.a._aa = '1'
        doc2.a.b.ns = 'example.com/2'
        doc2.a.b._bb = '2'
        doc2.a.b._bbb = '22'
        doc2.a.b.c._cc = '3'
        doc2.a.b.c._ccc = '33'
        doc2.a.b.c = 'ddd'

        compare_xml(orig, doc1.to_xml())
        compare_xml(orig, doc2.to_xml())
    def test_gen4(self):
        orig = """
          <CipherReference URI="http://www.example.com/CipherValues.xml">
          <Transforms xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
            <ds:Transform 
             Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
                <ds:XPath xmlns:rep="http://www.example.org/repository">
                  self::text()[parent::rep:CipherValue[@Id="example1"]]
                </ds:XPath>
            </ds:Transform>
            <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#base64"/>
          </Transforms>
        </CipherReference>
  """

        doc = xml()
        doc.ns_map += default_ns.dsig
        doc.ns_map += {'rep':'http://www.example.org/repository'}

        cipher_ref = doc.CipherReference
        cipher_ref._URI = 'http://www.example.com/CipherValues.xml'

        transform = cipher_ref.Transforms.dsig_Transform[0]
        transform._Algorithm = 'http://www.w3.org/TR/1999/REC-xpath-19991116'
        transform.dsig_XPath = 'self::text()[parent::rep:CipherValue[@Id="example1"]]'

        cipher_ref.Transforms.dsig_Transform[1]._Algorithm = 'http://www.w3.org/2000/09/xmldsig#base64'

        # Compare
        compare_xml(orig, doc.to_xml())
    def test_gen3(self):
        orig = """
          <?xml version='1.0'?> 
          <PaymentInfo xmlns='http://example.org/paymentv2'>
            <Name>John Smith</Name>
            <CreditCard Limit='5,000' Currency='USD'>
              <EncryptedData xmlns='http://www.w3.org/2001/04/xmlenc#'
               Type='http://www.w3.org/2001/04/xmlenc#Content'>
                <CipherData>
                  <CipherValue>A23B45C56</CipherValue>
                </CipherData>
              </EncryptedData>
            </CreditCard>
          </PaymentInfo>
        """

        doc = xml()
        doc.ns = 'http://example.org/paymentv2'

        info = doc.PaymentInfo
        info.Name = 'John Smith'
        info.CreditCard._Limit = '5,000'
        info.CreditCard._Currency = 'USD'

        enc_data = info.CreditCard.EncryptedData
        enc_data.ns = default_ns.xenc
        enc_data._Type = 'http://www.w3.org/2001/04/xmlenc#Content'
        enc_data.CipherData.CipherValue = 'A23B45C56'

        # Compare
        compare_xml(orig, doc.to_xml())
    def test_gen1(self):

        orig = """
        <ns0:Envelope xmlns:ns0="http://www.w3.org/2003/05/soap-envelope"
        xmlns:rem="http://remoting.example.com" xmlns:wsa="http://www.w3.org/2005/08/addressing">
           <ns0:Header>
              <wsa:Action ns0:mustUnderstand="1" ns0:type="2">urn:hl7-org:v3:MCCI_IN000002UV01</wsa:Action>
              <wsa:MessageID>uuid:123</wsa:MessageID>
              <wsa:ReplyTo>
                 <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
              </wsa:ReplyTo>
           </ns0:Header>
           <ns0:Body>
              <rem:usrOrgRoleLogin>
                 <arg0 rem:keep_alive="true">
                    <user>my-user</user>
                    <pwd>my-password</pwd>
                    <role>my-role</role>
                    <org>my-org</org>
                    <rem:access rem:type="0">
                       no
                       <access>000</access>
                    </rem:access>
                    <rem:access rem:type="1">
                       yes
                       <access>111</access>
                    </rem:access>
                 </arg0>
              </rem:usrOrgRoleLogin>
           </ns0:Body>
        </ns0:Envelope>
        """
        doc = xml()
        doc.ns_map += default_ns.s12, default_ns.wsa, {'rem':'http://remoting.example.com'}

        header = doc.s12_Envelope.s12_Header
        header.wsa_Action = 'urn:hl7-org:v3:MCCI_IN000002UV01'
        header.wsa_Action._s12_mustUnderstand = '1'
        header.wsa_Action._s12_type = '2'
        header.wsa_MessageID = 'uuid:123'
        header.wsa_ReplyTo.wsa_Address = 'http://www.w3.org/2005/08/addressing/anonymous'

        arg0 = doc.s12_Envelope.s12_Body.rem_usrOrgRoleLogin.arg0
        arg0.user = '******'
        arg0._rem_keep_alive = 'true'
        arg0.pwd = 'my-password'
        arg0.role = 'my-role'
        arg0.org = 'my-org'

        arg0.rem_access[0] = 'no'
        arg0.rem_access[0]._rem_type = '0'
        arg0.rem_access[0].access = '000'

        arg0.rem_access[1] = 'yes'
        arg0.rem_access[1]._rem_type = '1'
        arg0.rem_access[1].access = '111'

        compare_xml(orig, doc.to_xml())
    def test_gen1(self):
        orig = """
        <greeting>Hello, world!</greeting> 
        """

        doc = xml()
        doc.greeting = 'Hello, world!'

        compare_xml(orig, doc.to_xml())
Beispiel #12
0
 def test_to_xml_defaults(self):
     orig = """
     <root xmlns="example.com">
       <aaa>111</aaa>
     </root>
     """
     doc = xml()
     doc.root.ns = 'example.com'
     doc.root.aaa = '111'
     compare_xml(orig, doc.to_xml())
Beispiel #13
0
    def test_gen2(self):
        orig = """
        <edi:price xmlns:edi='http://ecommerce.example.org/schema' units='Euro'>32.18</edi:price>
        """
        doc = xml()
        doc.ns_map += {'edi':'http://ecommerce.example.org/schema'}
        doc.edi_price = '32.18'
        doc.edi_price._units = 'Euro'

        compare_xml(orig, doc.to_xml())
Beispiel #14
0
    def test_gen1(self):
        orig = """
        <x xmlns:edi='http://ecommerce.example.org/schema'>
        </x>
        """
        doc = xml()
        doc.ns_map += {'edi':'http://ecommerce.example.org/schema'}
        doc.x = ''

        compare_xml(orig, doc.to_xml())
Beispiel #15
0
 def test_to_xml_serialize_non_root(self):
     expected = """
     <d>
       <e>
         <f>111</f>
       </e>
     </d>
     """
     doc = xml()
     doc.a.b.c.d.e.f = '111'
     compare_xml(expected, doc.a.b.c.d.to_xml())
 def test_ok(self):
     orig = """
     <root>
       <a>
         <b>ccc</b>
       </a>
     </root>
     """
     doc = xml()
     doc.root.a.b = 'ccc'
     compare_xml(orig, doc.to_xml())
 def test_ok_ns_default(self):
     orig = """
     <root xmlns="example.com">
       <a>
         <b>ccc</b>
       </a>
     </root>
     """
     doc = xml()
     doc.ns = 'example.com'
     doc.root.a.b = 'ccc'
     compare_xml(orig, doc.to_xml())
 def test_ok_ns_custom(self):
     orig = """
     <root xmlns:x="example.com" xmlns:q="example.com/2">
       <x:a>
         <q:b>ccc</q:b>
       </x:a>
     </root>
     """
     doc = xml()
     doc.ns_map += {'x':'example.com', 'q':'example.com/2'}
     doc.root.x_a.q_b = 'ccc'
     compare_xml(orig, doc.to_xml())
Beispiel #19
0
    def test_gen_default_ns_elem(self):
        orig = """
        <root xmlns="example.com">
          <x:a xmlns:x="example.com/x">
            <b>123</b>
          </x:a>
        </root>
        """
        doc = xml()
        doc.root.ns = 'example.com'
        doc.root.ns_map += {'x':'example.com/x'}
        doc.root.x_a.b = '123'

        compare_xml(orig, doc.to_xml())
Beispiel #20
0
    def test_to_xml_to_lxml(self):
        orig = """
            <root xmlns="example.com">
              <aaa>111</aaa>
            </root>
            """
        doc = xml()
        doc.root.ns = 'example.com'
        doc.root.aaa = '111'

        result = doc.to_xml(False)
        self.assertIsInstance(result, _Element)

        compare_xml(orig, tostring(result))
Beispiel #21
0
    def test_gen_set_already_existing_ns(self):
        orig = """
            <root xmlns="example.com">
              <x:a xmlns:x="example.com/x">
                <b>123</b>
              </x:a>
            </root>
            """
        doc = xml()
        doc.root.ns = 'example.com'
        doc.root.ns_map += {'x':'example.com/x'}
        doc.root.x_a.b = '123'
        doc.root.x_a.ns = 'example.com/x' # Set explicitely and expected to reuse prefix defined in doc.root.ns_map

        compare_xml(orig, doc.to_xml())
Beispiel #22
0
    def test_gen4(self):
        orig = """
        <root>
          <bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'>
            <bk:title>Cheaper by the Dozen</bk:title>
            <isbn:number>1568491379</isbn:number>
          </bk:book>
        </root>
        """
        doc = xml()
        doc.ns_map += {'bk':'urn:loc.gov:books', 'isbn':'urn:ISBN:0-395-36341-6'}

        doc.root.bk_book.bk_title = 'Cheaper by the Dozen'
        doc.root.bk_book.isbn_number = '1568491379'

        compare_xml(orig, doc.to_xml())
    def test_ok(self):
        orig = """
        <aaa>
            <bbb>0</bbb>
            <bbb>1</bbb>
            <bbb>2</bbb>
            <bbb>3</bbb>
        </aaa>
        """
        doc = xml()
        doc.aaa.bbb[0] = '0'
        doc.aaa.bbb[1] = '1'
        doc.aaa.bbb[2] = '2'
        doc.aaa.bbb[3] = '3'

        compare_xml(orig, doc.to_xml())
    def test_gen_attr1(self):
        orig = """
        <a aa="1">
          <b bb="2" bbb="22">
            <c cc="3" ccc="33">ddd</c>
          </b>
        </a>
        """
        doc = xml()
        doc.a._aa = '1'
        doc.a.b._bb = '2'
        doc.a.b._bbb = '22'
        doc.a.b.c._cc = '3'
        doc.a.b.c._ccc = '33'
        doc.a.b.c = 'ddd'

        compare_xml(orig, doc.to_xml())
Beispiel #25
0
    def test_gen7(self):
        orig = """
        <x xmlns:n1="example.com" xmlns="example.com">
          <good a="1" b="2" />
          <good a="1" n1:a="2" />
        </x>
        """
        doc = xml()
        doc.ns = 'example.com'
        doc.ns_map += {'n1':'example.com'}

        doc.x.good[0]._a = '1'
        doc.x.good[0]._b = '2'

        doc.x.good[1]._a = '1'
        doc.x.good[1]._n1_a = '2'

        compare_xml(orig, doc.to_xml())
    def test_diff_parse_xml(self):
        orig = """
        <root>
          <a>
            <b>ccc</b>
          </a>
        </root>
        """
        doc = xml()
        doc.root.a.b.ddd = 'eee'

        expected = """
Expected:
  <root>
    <a>
      <b>ccc</b>
    </a>
  </root>

Got:
  <root>
    <a>
      <b>
        <ddd>eee</ddd>
      </b>
    </a>
  </root>

Diff:
  <root>
    <a>
      <b>
      ccc (got: None)
        +<ddd>eee</ddd>
      </b>
    </a>
  </root>
        """

        try:
            compare_xml(orig, doc.to_xml())
        except AssertionError as e:
            self.assertEquals(e.args[0].strip(), expected.strip())
    def test_gen1(self):
        orig = """
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
           <fo:layout-master-set>
              <fo:simple-page-master master-name="all-pages">
                 <fo:region-body region-name="xsl-region-body" margin="0.75in" writing-mode="tb-rl"/>
                 <fo:region-before region-name="xsl-region-before" extent="0.75in"/>
              </fo:simple-page-master>
              <fo:page-sequence-master master-name="default-sequence">
                 <fo:repeatable-page-master-reference master-reference="all-pages"/>
              </fo:page-sequence-master>
           </fo:layout-master-set>
           <fo:page-sequence master-name="default-sequence">
              <fo:flow flow-name="xsl-region-body">
                 <fo:block>[Content in a language which allows either horizontal or vertical formatting]</fo:block>
              </fo:flow>
           </fo:page-sequence>
        </fo:root>
        """
        doc = xml()
        doc.ns = default_ns.fo

        simple_page_master = doc.root['layout-master-set']['simple-page-master']
        simple_page_master['#master-name'] = 'all-pages'

        simple_page_master['region-body']['#region-name'] = 'xsl-region-body'
        simple_page_master['region-body']['#margin'] = '0.75in'
        simple_page_master['region-body']['#writing-mode'] = 'tb-rl'

        simple_page_master['region-before']['#region-name'] = 'xsl-region-before'
        simple_page_master['region-before']['#extent'] = '0.75in'

        page_sequence_master = doc.root['layout-master-set']['page-sequence-master']
        page_sequence_master['#master-name'] = 'default-sequence'
        page_sequence_master['repeatable-page-master-reference']['#master-reference'] = 'all-pages'

        page_sequence = doc.root['page-sequence']
        page_sequence['#master-name'] = 'default-sequence'
        page_sequence.flow['#flow-name'] = 'xsl-region-body'
        page_sequence.flow.block = '[Content in a language which allows either horizontal or vertical formatting]'

        compare_xml(orig, doc.to_xml())
Beispiel #28
0
    def test_gen5(self):
        orig = """
        <book xmlns='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'>
            <title>Cheaper by the Dozen</title>
            <isbn:number>1568491379</isbn:number>
            <notes>
              <p xmlns='http://www.w3.org/1999/xhtml'>
              </p>
            </notes>
        </book>
        """
        doc = xml()
        doc.ns = 'urn:loc.gov:books'
        doc.ns_map += {'isbn':'urn:ISBN:0-395-36341-6'}, default_ns.html

        doc.book.title = 'Cheaper by the Dozen'
        doc.book.isbn_number = '1568491379'
        doc.book.notes.html_p = ''

        compare_xml(orig, doc.to_xml())
    def test_gen2(self):
        orig = """
        <root>
          <p xml:lang="en">The quick brown fox jumps over the lazy dog.</p>
          <p xml:lang="en-GB">What colour is it?</p>
          <p xml:lang="en-US">What color is it?</p>
          <sp who="Faust" desc='leise' xml:lang="de">
            <l>Habe nun, ach! Philosophie,</l>
            <l>Juristerei, und Medizin</l>
            <l>und leider auch Theologie</l>
            <l>durchaus studiert mit heißem Bemüh'n.</l>
            </sp>
        </root>
        """

        doc = xml()
        doc.ns_map += default_ns.xml

        root = doc.root

        root.p[0]._xml_lang = 'en'
        root.p[0] = 'The quick brown fox jumps over the lazy dog.'

        root.p[1]._xml_lang = 'en-GB'
        root.p[1] = 'What colour is it?'

        root.p[2]._xml_lang = 'en-US'
        root.p[2] = 'What color is it?'

        sp = root.sp
        sp._who = 'Faust'
        sp._desc = 'leise'
        sp._xml_lang = 'de'

        sp.l[0] = 'Habe nun, ach! Philosophie,'
        sp.l[1] = 'Juristerei, und Medizin'
        sp.l[2] = 'und leider auch Theologie'
        sp.l[3] = 'durchaus studiert mit heißem Bemüh\'n.'

        compare_xml(orig, doc.to_xml())
    def test_gen_getitem2(self):
        orig = """
        <a xmlns="example.com">
          <b xmlns="example.com/2">
            <c>ddd</c>
          </b>
        </a>
        """
        doc1 = xml()
        doc1.a.ns = 'example.com'
        doc1.a.b.ns = 'example.com/2'
        doc1.a.b.c = 'ddd'

        doc2 = xml()
        doc2['a'].ns = 'example.com'
        doc2.a['b'].ns = 'example.com/2'
        doc2.a.b['c'] = 'ddd'

        doc3 = xml()
        doc3['a'].ns = 'example.com'
        doc3['a']['b'].ns = 'example.com/2'
        doc3['a'].b['c'] = 'ddd'

        compare_xml(orig, doc1.to_xml())
        compare_xml(orig, doc2.to_xml())
        compare_xml(orig, doc3.to_xml())