# Path: pytrustnfe/nfe/assinatura.py
# class Assinatura(object):
#     def __init__(self, arquivo, senha):
#         self.arquivo = arquivo
#         self.senha = senha
# 
#     def assina_xml(self, xml_element, reference, getchildren=False):
#         cert, key = extract_cert_and_key_from_pfx(self.arquivo, self.senha)
# 
#         for element in xml_element.iter("*"):
#             if element.text is not None and not element.text.strip():
#                 element.text = None
# 
#         signer = XMLSigner(
#             method=signxml.methods.enveloped,
#             signature_algorithm="rsa-sha1",
#             digest_algorithm="sha1",
#             c14n_algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315",
#         )
# 
#         ns = {}
#         ns[None] = signer.namespaces["ds"]
#         signer.namespaces = ns
# 
#         ref_uri = ("#%s" % reference) if reference else None
#         signed_root = signer.sign(
#             xml_element, key=key.encode(), cert=cert.encode(), reference_uri=ref_uri
#         )
#         if reference:
#             element_signed = signed_root.find(".//*[@Id='%s']" % reference)
#             signature = signed_root.find(
#                 ".//{http://www.w3.org/2000/09/xmldsig#}Signature"
#             )
# 
#             if getchildren and element_signed is not None and signature is not None:
#                 child = element_signed.getchildren()
#                 child.append(signature)
#             elif element_signed is not None and signature is not None:
#                 parent = element_signed.getparent()
#                 parent.append(signature)
#         return etree.tostring(signed_root, encoding=str)


import os
import os.path
import unittest
from lxml import etree
from pytrustnfe.nfe.assinatura import Assinatura
Created on Jun 14, 2015

@author: danimar
"""


XML_ASSINAR = (
    '<Envelope xmlns="urn:envelope">'
    '   <Data Id="NFe43150602261542000143550010000000761792265342">'
    "     Hello, World!"
    "   </Data>"
    "</Envelope>"
)


XML_ERRADO = (
    '<Envelope xmlns="urn:envelope">'
    ' <Data Id="NFe">'
    "     Hello, World!"
    "   </Data>"
    "</Envelope>"
)


class test_assinatura(unittest.TestCase):

    caminho = os.path.dirname(__file__)

    def test_assinar_xml_senha_invalida(self):
        pfx = open(os.path.join(self.caminho, "teste.pfx"), "rb").read()

# Path: pytrustnfe/nfe/assinatura.py
# class Assinatura(object):
#     def __init__(self, arquivo, senha):
#         self.arquivo = arquivo
#         self.senha = senha
# 
#     def assina_xml(self, xml_element, reference, getchildren=False):
#         cert, key = extract_cert_and_key_from_pfx(self.arquivo, self.senha)
# 
#         for element in xml_element.iter("*"):
#             if element.text is not None and not element.text.strip():
#                 element.text = None
# 
#         signer = XMLSigner(
#             method=signxml.methods.enveloped,
#             signature_algorithm="rsa-sha1",
#             digest_algorithm="sha1",
#             c14n_algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315",
#         )
# 
#         ns = {}
#         ns[None] = signer.namespaces["ds"]
#         signer.namespaces = ns
# 
#         ref_uri = ("#%s" % reference) if reference else None
#         signed_root = signer.sign(
#             xml_element, key=key.encode(), cert=cert.encode(), reference_uri=ref_uri
#         )
#         if reference:
#             element_signed = signed_root.find(".//*[@Id='%s']" % reference)
#             signature = signed_root.find(
#                 ".//{http://www.w3.org/2000/09/xmldsig#}Signature"
#             )
# 
#             if getchildren and element_signed is not None and signature is not None:
#                 child = element_signed.getchildren()
#                 child.append(signature)
#             elif element_signed is not None and signature is not None:
#                 parent = element_signed.getparent()
#                 parent.append(signature)
#         return etree.tostring(signed_root, encoding=str)


# Path: tests/test_assinatura.py
import os
import os.path
import unittest
from lxml import etree
from pytrustnfe.nfe.assinatura import Assinatura

Created on Jun 14, 2015

@author: danimar
"""


XML_ASSINAR = (
    '<Envelope xmlns="urn:envelope">'
    '   <Data Id="NFe43150602261542000143550010000000761792265342">'
    "     Hello, World!"
    "   </Data>"
    "</Envelope>"
)


XML_ERRADO = (
    '<Envelope xmlns="urn:envelope">'
    ' <Data Id="NFe">'
    "     Hello, World!"
    "   </Data>"
    "</Envelope>"
)


class test_assinatura(unittest.TestCase):

    caminho = os.path.dirname(__file__)

    def test_assinar_xml_senha_invalida(self):
        pfx = open(os.path.join(self.caminho, "teste.pfx"), "rb").read()

        signer = Assinatura(pfx, "123")