From 5be2064c0993b57b6e64e47c3d33cf7094a4049c Mon Sep 17 00:00:00 2001 From: Michel Sabchuk Date: Fri, 20 Jan 2017 13:31:20 -0200 Subject: [PATCH 1/3] =?UTF-8?q?Valida=C3=A7=C3=A3o=20XML=20contra=20o=20ar?= =?UTF-8?q?quivo=20XSD=20ao=20fechar=20PLP.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Valida XML contra o arquivo de especificação do XML provido pelos Correios. Escreve testes superficiais. --- pysigep/correios/data/layout.xsd | 581 +++++++++++++++++++++ pysigep/sigep/__init__.py | 16 +- pysigep/utils/__init__.py | 14 +- test/sigep_test/fecha_plp_servicos_test.py | 44 +- 4 files changed, 630 insertions(+), 25 deletions(-) create mode 100644 pysigep/correios/data/layout.xsd diff --git a/pysigep/correios/data/layout.xsd b/pysigep/correios/data/layout.xsd new file mode 100644 index 0000000..89ea921 --- /dev/null +++ b/pysigep/correios/data/layout.xsd @@ -0,0 +1,581 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pysigep/sigep/__init__.py b/pysigep/sigep/__init__.py index bebb8ee..de8552e 100644 --- a/pysigep/sigep/__init__.py +++ b/pysigep/sigep/__init__.py @@ -27,6 +27,7 @@ import os +from lxml import etree from pysigep import send, _url from pysigep.utils import render_xml, _valida @@ -127,13 +128,23 @@ def cep_consulta(**kwargs): return send(path, 'consultaCEPResponse', API, url, **kwargs) +def fecha_plp_servicos_validation_schema(): + BASE_DIR = os.path.dirname(os.path.dirname(__file__)) + validation_file = open(os.path.join(BASE_DIR, 'correios/data/layout.xsd')) + schema_root = etree.XML(validation_file.read()) + return etree.XMLSchema(schema_root) + + def fecha_plp_servicos(**kwargs): _valida('fecha_plp_servicos', API, kwargs) url = _url(kwargs['ambiente'], API) path = 'FechaPlpVariosServicos.xml' path = os.path.dirname(os.path.dirname(__file__)) path = os.path.join(path, 'templates') - xml = render_xml(path, "PLP.xml", kwargs) + xml = render_xml( + path, "PLP.xml", kwargs, + validation_schema=fecha_plp_servicos_validation_schema() + ) kwargs["xml"] = '' + xml return send("FechaPlpVariosServicos.xml", 'fechaPlpVariosServicosResponse', API, url, encoding="ISO-8859-1", **kwargs) @@ -143,5 +154,4 @@ def solicita_xml_plp(**kwargs): _valida('solicita_xml_plp', API, kwargs) url = _url(kwargs['ambiente'], API) path = 'SolicitaXmlPlp.xml' - return send(path, 'solicitaXmlPlpResponse', - API, url, **kwargs) + return send(path, 'solicitaXmlPlpResponse', API, url, **kwargs) diff --git a/pysigep/utils/__init__.py b/pysigep/utils/__init__.py index d70c024..046705c 100644 --- a/pysigep/utils/__init__.py +++ b/pysigep/utils/__init__.py @@ -25,13 +25,17 @@ } -def render_xml(path, template_name, usuario): - env = Environment( - loader=FileSystemLoader(path), extensions=['jinja2.ext.with_']) +def render_xml(path, template_name, usuario, validation_schema=None): + env = Environment(loader=FileSystemLoader(path), + extensions=['jinja2.ext.with_']) template = env.get_template(template_name) xml = template.render(usuario) - parser = etree.XMLParser(remove_blank_text=True, remove_comments=True, - strip_cdata=False) + parser = etree.XMLParser( + remove_blank_text=True, + remove_comments=True, + strip_cdata=False, + schema=validation_schema + ) root = etree.fromstring(xml, parser=parser) return etree.tostring(root) diff --git a/test/sigep_test/fecha_plp_servicos_test.py b/test/sigep_test/fecha_plp_servicos_test.py index 7d3a30e..e92171b 100644 --- a/test/sigep_test/fecha_plp_servicos_test.py +++ b/test/sigep_test/fecha_plp_servicos_test.py @@ -2,19 +2,21 @@ from unittest import TestCase +from lxml import etree from pysigep.sigep import fecha_plp_servicos class TestFechaPlpServicos(TestCase): - def test_send_plp_servicos(self): - - data = { + def setUp(self): + self.etiqueta_sem_dv = 'DL76023727BR' + self.etiqueta_com_dv = 'DL760237272BR' + self.data = { 'idPlpCliente': '123', 'usuario': 'sigep', 'senha': 'n5f9t8', 'listaEtiquetas': [ - 'PH18556091BR', + self.etiqueta_sem_dv, ], 'cartaoPostagem': '0123456789', 'numero_contrato': '0123456789', @@ -31,10 +33,10 @@ def test_send_plp_servicos(self): 'telefone_remetente': '6112345008', 'email_remetente': u'cli@mail.com.br', 'objetos': [{ - 'numero_etiqueta': 'PH18556091BR', + 'numero_etiqueta': self.etiqueta_com_dv, 'codigo_servico_postagem': '', 'cubagem': '', - 'peso': '', + 'peso': '100', 'nome_destinatario': '', 'telefone_destinatario': '', 'celular_destinatario': '', @@ -44,28 +46,36 @@ def test_send_plp_servicos(self): 'numero_end_destinatario': '', 'bairro_destinatario': '', 'cidade_destinatario': '', - 'uf_destinatario': '', - 'cep_destinatario': '', + 'uf_destinatario': 'PR', + 'cep_destinatario': '81130000', 'numero_nota_fiscal': '', 'serie_nota_fiscal': '', 'descricao_objeto': '', 'valor_a_cobrar': '', 'valor_declarado': '', - 'tipo_objeto': '', - 'dimensao_altura': '', - 'dimensao_largura': '', - 'dimensao_comprimento': '', - 'dimensao_diametro': '', + 'tipo_objeto': '002', + 'dimensao_altura': '28', + 'dimensao_largura': '11', + 'dimensao_comprimento': '16', + 'dimensao_diametro': '0', 'servicos_adicionais': [ '019', '001' ] }], 'cartaoPostagem': '0123456789', } - with self.assertRaises(Exception): - fecha_plp_servicos(**data) - data['ambiente'] = 1 - retorno = fecha_plp_servicos(**data) + def test_send_plp_servicos_precisa_definir_ambiente(self): + with self.assertRaises(Exception): + fecha_plp_servicos(**self.data) + def test_send_plp_servicos(self): + self.data['ambiente'] = 1 + retorno = fecha_plp_servicos(**self.data) self.assertTrue(retorno != u'') + + def test_send_plp_servicos_valida_xml_contra_xsd(self): + self.data['ambiente'] = 1 + self.data['objetos'][0]['numero_etiqueta'] = self.etiqueta_sem_dv + with self.assertRaises(etree.XMLSyntaxError): + retorno = fecha_plp_servicos(**self.data) From 77da01d1a020737e777b49b3d0a7a60537de3015 Mon Sep 17 00:00:00 2001 From: Michel Sabchuk Date: Fri, 20 Jan 2017 14:22:24 -0200 Subject: [PATCH 2/3] =?UTF-8?q?Adiciona=20arquivo=20de=20=5F=5Finit=5F=5F?= =?UTF-8?q?=20para=20ser=20instal=C3=A1vel.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pysigep/correios/data/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 pysigep/correios/data/__init__.py diff --git a/pysigep/correios/data/__init__.py b/pysigep/correios/data/__init__.py new file mode 100644 index 0000000..e69de29 From 06774c4bdadd0bc14e5d2c29ba799158201adde2 Mon Sep 17 00:00:00 2001 From: Michel Sabchuk Date: Fri, 20 Jan 2017 14:25:26 -0200 Subject: [PATCH 3/3] Adiciona manifest para que arquivo xsd seja incluso. --- MANIFEST.in | 1 + 1 file changed, 1 insertion(+) diff --git a/MANIFEST.in b/MANIFEST.in index 4757f3c..1a068eb 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -2,3 +2,4 @@ include MANIFEST.in include LICENSE include README.md include pysigep/templates/*.xml +include pysigep/correios/data/layout.xsd