fix(WIP): Reemplazando OpenSsl.crypto,pkcs12
This commit is contained in:
parent
a3d2176068
commit
d061077b30
@ -8,13 +8,15 @@ import xades
|
||||
from datetime import datetime
|
||||
import OpenSSL
|
||||
import zipfile
|
||||
import warnings
|
||||
# import warnings
|
||||
import hashlib
|
||||
from contextlib import contextmanager
|
||||
from .data.dian import codelist
|
||||
from . import form
|
||||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
# from pathlib import Path
|
||||
|
||||
from cryptography.hazmat.primitives.serialization import pkcs12
|
||||
|
||||
AMBIENTE_PRUEBAS = codelist.TipoAmbiente.by_name('Pruebas')['code']
|
||||
AMBIENTE_PRODUCCION = codelist.TipoAmbiente.by_name('Producción')['code']
|
||||
@ -68,11 +70,11 @@ NAMESPACES = {
|
||||
}
|
||||
|
||||
|
||||
|
||||
def fe_from_string(document: str) -> FachoXML:
|
||||
return FeXML.from_string(document)
|
||||
|
||||
from contextlib import contextmanager
|
||||
|
||||
# from contextlib import contextmanager
|
||||
@contextmanager
|
||||
def mock_xades_policy():
|
||||
from mock import patch
|
||||
@ -90,7 +92,7 @@ def mock_xades_policy():
|
||||
mock.return_value = UrllibPolicyMock()
|
||||
yield
|
||||
|
||||
|
||||
|
||||
class FeXML(FachoXML):
|
||||
|
||||
def __init__(self, root, namespace):
|
||||
@ -118,8 +120,7 @@ class FeXML(FachoXML):
|
||||
|
||||
|
||||
class DianXMLExtensionCUDFE(FachoXMLExtension):
|
||||
|
||||
def __init__(self, invoice, tipo_ambiente = AMBIENTE_PRUEBAS):
|
||||
def __init__(self, invoice, tipo_ambiente=AMBIENTE_PRUEBAS):
|
||||
self.tipo_ambiente = tipo_ambiente
|
||||
self.invoice = invoice
|
||||
|
||||
@ -351,7 +352,6 @@ class DianXMLExtensionSoftwareSecurityCode(FachoXMLExtension):
|
||||
|
||||
|
||||
class DianXMLExtensionSigner:
|
||||
|
||||
def __init__(self, pkcs12_path, passphrase=None, localpolicy=True):
|
||||
self._pkcs12_data = open(pkcs12_path, 'rb').read()
|
||||
self._passphrase = None
|
||||
@ -362,7 +362,6 @@ class DianXMLExtensionSigner:
|
||||
@classmethod
|
||||
def from_bytes(cls, data, passphrase=None, localpolicy=True):
|
||||
self = cls.__new__(cls)
|
||||
|
||||
self._pkcs12_data = data
|
||||
self._passphrase = None
|
||||
self._localpolicy = localpolicy
|
||||
@ -394,7 +393,6 @@ class DianXMLExtensionSigner:
|
||||
)
|
||||
xml.append(signature)
|
||||
|
||||
|
||||
ref = xmlsig.template.add_reference(
|
||||
signature, xmlsig.constants.TransformSha256, uri="", name="xmldsig-%s-ref0" % (id_uuid)
|
||||
)
|
||||
@ -428,9 +426,13 @@ class DianXMLExtensionSigner:
|
||||
POLICY_NAME,
|
||||
xmlsig.constants.TransformSha256)
|
||||
ctx = xades.XAdESContext(policy)
|
||||
ctx.load_pkcs12(OpenSSL.crypto.load_pkcs12(self._pkcs12_data,
|
||||
self._passphrase))
|
||||
ctx.load_pkcs12(pkcs12.load_key_and_certificates(
|
||||
self._pkcs12_data,
|
||||
self._passphrase))
|
||||
|
||||
# ctx.load_pkcs12(OpenSSL.crypto.load_pkcs12(
|
||||
# self._pkcs12_data,
|
||||
# self._passphrase))
|
||||
if self._localpolicy:
|
||||
with mock_xades_policy():
|
||||
ctx.sign(signature)
|
||||
@ -438,7 +440,7 @@ class DianXMLExtensionSigner:
|
||||
else:
|
||||
ctx.sign(signature)
|
||||
ctx.verify(signature)
|
||||
#xmlsig take parent root
|
||||
# xmlsig take parent root
|
||||
xml.remove(signature)
|
||||
return signature
|
||||
|
||||
|
@ -127,39 +127,50 @@ def test_invoice_totals(simple_invoice_without_lines):
|
||||
simple_invoice.invoice_supplier.ident = '700085371'
|
||||
simple_invoice.invoice_customer.ident = '800199436'
|
||||
simple_invoice.add_invoice_line(form.InvoiceLine(
|
||||
quantity = form.Quantity(1, '94'),
|
||||
description = 'producto',
|
||||
item = form.StandardItem(9999),
|
||||
price = form.Price(form.Amount(1_500_000), '01', ''),
|
||||
tax = form.TaxTotal(
|
||||
subtotals = [
|
||||
quantity=form.Quantity(1, '94'),
|
||||
description='producto',
|
||||
item=form.StandardItem(9999),
|
||||
price=form.Price(form.Amount(1_500_000), '01', ''),
|
||||
tax=form.TaxTotal(
|
||||
subtotals=[
|
||||
form.TaxSubTotal(
|
||||
scheme = form.TaxScheme('01'),
|
||||
percent = 19.0
|
||||
)])
|
||||
scheme=form.TaxScheme('01'),
|
||||
percent=19.0
|
||||
)]),
|
||||
withholding=form.WithholdingTaxTotal(
|
||||
subtotals=[])
|
||||
))
|
||||
simple_invoice.calculate()
|
||||
assert 1 == len(simple_invoice.invoice_lines)
|
||||
assert form.Amount(1_500_000) == simple_invoice.invoice_legal_monetary_total.line_extension_amount
|
||||
assert form.Amount(1_785_000) == simple_invoice.invoice_legal_monetary_total.payable_amount
|
||||
assert form.Amount(1_500_000) == (
|
||||
simple_invoice.invoice_legal_monetary_total.line_extension_amount)
|
||||
assert form.Amount(1_785_000) == (
|
||||
simple_invoice.invoice_legal_monetary_total.payable_amount)
|
||||
|
||||
|
||||
def test_invoice_cufe(simple_invoice_without_lines):
|
||||
simple_invoice = simple_invoice_without_lines
|
||||
simple_invoice.invoice_ident = '323200000129'
|
||||
simple_invoice.invoice_issue = datetime.strptime('2019-01-16 10:53:10-05:00', '%Y-%m-%d %H:%M:%S%z')
|
||||
simple_invoice.invoice_supplier.ident = form.PartyIdentification('700085371', '5', '31')
|
||||
simple_invoice.invoice_customer.ident = form.PartyIdentification('800199436', '5', '31')
|
||||
simple_invoice.invoice_issue = datetime.strptime(
|
||||
'2019-01-16 10:53:10-05:00', '%Y-%m-%d %H:%M:%S%z')
|
||||
simple_invoice.invoice_supplier.ident = form.PartyIdentification(
|
||||
'700085371', '5', '31')
|
||||
simple_invoice.invoice_customer.ident = form.PartyIdentification(
|
||||
'800199436', '5', '31')
|
||||
simple_invoice.add_invoice_line(form.InvoiceLine(
|
||||
quantity = form.Quantity(1.00, '94'),
|
||||
description = 'producto',
|
||||
item = form.StandardItem(111),
|
||||
price = form.Price(form.Amount(1_500_000), '01', ''),
|
||||
tax = form.TaxTotal(
|
||||
subtotals = [
|
||||
quantity=form.Quantity(
|
||||
1.00, '94'),
|
||||
description='producto',
|
||||
item=form.StandardItem(111),
|
||||
price=form.Price(form.Amount(1_500_000), '01', ''),
|
||||
tax=form.TaxTotal(
|
||||
subtotals=[
|
||||
form.TaxSubTotal(
|
||||
scheme = form.TaxScheme('01'),
|
||||
percent = 19.0
|
||||
)])
|
||||
scheme=form.TaxScheme('01'),
|
||||
percent=19.0
|
||||
)]),
|
||||
withholding=form.WithholdingTaxTotal(
|
||||
subtotals=[])
|
||||
))
|
||||
|
||||
simple_invoice.calculate()
|
||||
@ -167,39 +178,54 @@ def test_invoice_cufe(simple_invoice_without_lines):
|
||||
|
||||
cufe_extension = fe.DianXMLExtensionCUFE(
|
||||
simple_invoice,
|
||||
tipo_ambiente = fe.AMBIENTE_PRODUCCION,
|
||||
clave_tecnica = '693ff6f2a553c3646a063436fd4dd9ded0311471'
|
||||
tipo_ambiente=fe.AMBIENTE_PRODUCCION,
|
||||
clave_tecnica='693ff6f2a553c3646a063436fd4dd9ded0311471'
|
||||
)
|
||||
formatVars = cufe_extension.formatVars()
|
||||
#NumFac
|
||||
|
||||
# NumFac
|
||||
assert formatVars[0] == '323200000129', "NumFac"
|
||||
#FecFac
|
||||
|
||||
# FecFac
|
||||
assert formatVars[1] == '2019-01-16', "FecFac"
|
||||
#HoraFac
|
||||
|
||||
# HoraFac
|
||||
assert formatVars[2] == '10:53:10-05:00', "HoraFac"
|
||||
#ValorBruto
|
||||
|
||||
# ValorBruto
|
||||
assert formatVars[3] == '1500000.00', "ValorBruto"
|
||||
#CodImpuesto1
|
||||
|
||||
# CodImpuesto1
|
||||
assert formatVars[4] == '01', "CodImpuesto1"
|
||||
#ValorImpuesto1
|
||||
|
||||
# ValorImpuesto1
|
||||
assert formatVars[5] == '285000.00', "ValorImpuesto1"
|
||||
#CodImpuesto2
|
||||
|
||||
# CodImpuesto2
|
||||
assert formatVars[6] == '04', "CodImpuesto2"
|
||||
#ValorImpuesto2
|
||||
|
||||
# ValorImpuesto2
|
||||
assert formatVars[7] == '0.00', "ValorImpuesto2"
|
||||
#CodImpuesto3
|
||||
|
||||
# CodImpuesto3
|
||||
assert formatVars[8] == '03', "CodImpuesto3"
|
||||
#ValorImpuesto3
|
||||
|
||||
# ValorImpuesto3
|
||||
assert formatVars[9] == '0.00', "ValorImpuesto3"
|
||||
#ValTotFac
|
||||
|
||||
# ValTotFac
|
||||
assert formatVars[10] == '1785000.00', "ValTotFac"
|
||||
#NitOFE
|
||||
|
||||
# NitOFE
|
||||
assert formatVars[11] == '700085371', "NitOFE"
|
||||
#NumAdq
|
||||
|
||||
# NumAdq
|
||||
assert formatVars[12] == '800199436', "NumAdq"
|
||||
#ClTec
|
||||
|
||||
# ClTec
|
||||
assert formatVars[13] == '693ff6f2a553c3646a063436fd4dd9ded0311471', "ClTec"
|
||||
#TipoAmbiente
|
||||
|
||||
# TipoAmbiente
|
||||
assert formatVars[14] == '1', "TipoAmbiente"
|
||||
|
||||
xml_invoice.add_extension(cufe_extension)
|
||||
@ -229,7 +255,9 @@ def test_credit_note_cude(simple_credit_note_without_lines):
|
||||
form.TaxSubTotal(
|
||||
scheme=form.TaxScheme('01'),
|
||||
percent=19.0
|
||||
)])
|
||||
)]),
|
||||
withholding=form.WithholdingTaxTotal(
|
||||
subtotals=[])
|
||||
))
|
||||
|
||||
simple_invoice.calculate()
|
||||
|
@ -62,6 +62,8 @@ def test_allowance_charge_in_invoice(simple_invoice_without_lines):
|
||||
form.TaxSubTotal(
|
||||
percent=19.0,
|
||||
)]),
|
||||
withholding=form.WithholdingTaxTotal(
|
||||
subtotals=[])
|
||||
))
|
||||
|
||||
inv.add_allowance_charge(form.AllowanceCharge(amount=form.Amount(19.0)))
|
||||
@ -92,8 +94,9 @@ def test_allowance_charge_in_invoice_line(simple_invoice_without_lines):
|
||||
subtotals=[
|
||||
form.TaxSubTotal(
|
||||
percent=19.0,
|
||||
)]
|
||||
),
|
||||
)]),
|
||||
withholding=form.WithholdingTaxTotal(
|
||||
subtotals=[]),
|
||||
allowance_charge=[
|
||||
form.AllowanceChargeAsDiscount(amount=form.Amount(10.0))
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user