44from pynfe .entidades .emitente import Emitente
55from pynfe .entidades .notafiscal import NotaFiscalServico
66from pynfe .entidades .servico import Servico
7+ from pynfe .processamento .assinatura import AssinaturaA1
78from pynfe .processamento .serializacao import SerializacaoNfse
89from pynfe .utils import obter_codigo_por_municipio
910from pynfe .utils .flags import (
1213from decimal import Decimal
1314import datetime
1415import re
16+ from lxml import etree
1517
1618
1719class SerializacaoNFSeConfigTest :
@@ -22,14 +24,7 @@ def __init__(self, certificado: str, senha: str, homologacao: bool):
2224
2325
2426class SerializacaoNFSeTest :
25-
26- @staticmethod
27- def get_config () -> SerializacaoNFSeConfigTest :
28- return SerializacaoNFSeConfigTest (
29- "./tests/certificado.pfx" ,
30- bytes ("123456" , "utf-8" ),
31- True
32- )
27+ data_hora = "2025-04-10T09:45:29"
3328
3429 @staticmethod
3530 def get_notafiscal_servico () -> NotaFiscalServico :
@@ -61,7 +56,8 @@ def get_notafiscal_servico() -> NotaFiscalServico:
6156
6257 return NotaFiscalServico (
6358 identificador = '50' ,
64- data_emissao = datetime .datetime .now (),
59+ data_emissao = datetime .datetime .strptime (
60+ SerializacaoNFSeTest .data_hora , "%Y-%m-%dT%H:%M:%S" ),
6561 servico = servico ,
6662 emitente = SerializacaoNFSeTest ._get_emitente (),
6763 cliente = SerializacaoNFSeTest ._get_destinatario (),
@@ -84,16 +80,19 @@ def serializa_nfse(nfse: NotaFiscalServico, autorizador: str) -> str:
8480 xml = serializador .gerar (nfse )
8581 return xml
8682
87- # TODO: assinatura digital
88- # @staticmethod
89- # def assina_xml(config: SerializacaoNFSeConfigTest, nfse_xml: str):
90- # a1 = AssinaturaA1(config.certificado, config.senha)
91- # xml = a1.assinar(nfse_xml, True)
92- # return xml
83+ @staticmethod
84+ def assina_xml (xml : str ) -> str :
85+ config = SerializacaoNFSeTest ._get_config ()
86+ nfse = etree .fromstring (xml )
87+
88+ a1 = AssinaturaA1 (config .certificado , config .senha )
89+ xml_assinado = a1 .assinar (nfse , True )
90+
91+ return xml_assinado
9392
9493 @staticmethod
9594 def strip_xml (xml : str ) -> str :
96- return re .sub (r">\s+<" , "><" , xml .replace ( " \n " , "" ). strip ())
95+ return re .sub (r">\s+<" , "><" , xml .strip ())
9796
9897 @staticmethod
9998 def limpa_namespace () -> None :
@@ -114,6 +113,14 @@ def limpa_namespace() -> None:
114113 # xml_doc=nfse_xml_assinado, xsd_file=xsd_nfse, use_assert=True
115114 # )
116115
116+ @staticmethod
117+ def _get_config () -> SerializacaoNFSeConfigTest :
118+ return SerializacaoNFSeConfigTest (
119+ "./tests/certificado.pfx" ,
120+ bytes ("123456" , "utf-8" ),
121+ True
122+ )
123+
117124 @staticmethod
118125 def _get_emitente () -> Emitente :
119126 return Emitente (
0 commit comments