diff --git a/tests/helpers.py b/tests/helpers.py new file mode 100644 index 0000000..55d0d26 --- /dev/null +++ b/tests/helpers.py @@ -0,0 +1,9 @@ + +def mock_urlopen(m): + import urllib.request + def mock(url): + class FakeIO: + def read(*args): + return "X".encode('utf-8') + return FakeIO() + m.setattr(urllib.request, "urlopen", mock) diff --git a/tests/test_fe.py b/tests/test_fe.py index 956afce..806663f 100644 --- a/tests/test_fe.py +++ b/tests/test_fe.py @@ -7,7 +7,10 @@ import pytest from facho import fe -def test_xmlsigned_build(): +import helpers + + +def test_xmlsigned_build(monkeypatch): #openssl req -x509 -sha256 -nodes -subj "/CN=test" -days 1 -newkey rsa:2048 -keyout example.key -out example.pem #openssl pkcs12 -export -out example.p12 -inkey example.key -in example.pem signer = fe.DianXMLExtensionSigner('./tests/example.p12') @@ -15,14 +18,17 @@ def test_xmlsigned_build(): xml = fe.FeXML('Invoice', 'http://www.dian.gov.co/contratos/facturaelectronica/v1') xml.add_extension(signer) - xml.attach_extensions() + + with monkeypatch.context() as m: + helpers.mock_urlopen(m) + xml.attach_extensions() elem = xml.find_or_create_element('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/ds:Signature') assert elem is not None #assert elem.findall('ds:SignedInfo', fe.NAMESPACES) is not None -def test_xmlsigned_with_passphrase_build(): +def test_xmlsigned_with_passphrase_build(monkeypatch): #openssl req -x509 -sha256 -nodes -subj "/CN=test" -days 1 -newkey rsa:2048 -keyout example.key -out example.pem #openssl pkcs12 -export -out example.p12 -inkey example.key -in example.pem signer = fe.DianXMLExtensionSigner('./tests/example-with-passphrase.p12', 'test') @@ -30,7 +36,10 @@ def test_xmlsigned_with_passphrase_build(): xml = fe.FeXML('Invoice', 'http://www.dian.gov.co/contratos/facturaelectronica/v1') xml.add_extension(signer) - xml.attach_extensions() + + with monkeypatch.context() as m: + helpers.mock_urlopen(m) + xml.attach_extensions() elem = xml.find_or_create_element('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/ds:Signature') assert elem is not None diff --git a/tests/test_fe_form.py b/tests/test_fe_form.py index b039b55..8e674eb 100644 --- a/tests/test_fe_form.py +++ b/tests/test_fe_form.py @@ -79,7 +79,7 @@ def test_invoicesimple_build_with_cufe(simple_invoice): assert cufe != '' -def test_invoicesimple_xml_signed(simple_invoice): +def test_invoicesimple_xml_signed(monkeypatch, simple_invoice): invoice_validator = form.DianResolucion0001Validator() simple_invoice.validate(invoice_validator) assert invoice_validator.valid() == True @@ -88,7 +88,10 @@ def test_invoicesimple_xml_signed(simple_invoice): signer = fe.DianXMLExtensionSigner('./tests/example.p12') xml.add_extension(signer) - xml.attach_extensions() + with monkeypatch.context() as m: + import helpers + helpers.mock_urlopen(m) + xml.attach_extensions() elem = xml.find_or_create_element('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/ds:Signature') assert elem.text is not None