diff --git a/cep/transferencia.py b/cep/transferencia.py index cd3f469..c3861e4 100644 --- a/cep/transferencia.py +++ b/cep/transferencia.py @@ -4,9 +4,16 @@ import clabe from lxml import etree +from requests import HTTPError from .client import Client from .cuenta import Cuenta +from .exc import CepError, MaxRequestError + +MAX_REQUEST_ERROR_MESSAGE = ( + b'Lo sentimos, pero ha excedido el número máximo ' + b'de consultas en este portal' +) @dataclass @@ -36,7 +43,15 @@ def validar( ) if not client: return None - xml = cls._descargar(client, 'XML') + + try: + xml = cls._descargar(client, 'XML') + except HTTPError as exc: + raise CepError from exc + + if MAX_REQUEST_ERROR_MESSAGE in xml: + raise MaxRequestError + resp = etree.fromstring(xml) ordenante = Cuenta.from_etree(resp.find('Ordenante')) diff --git a/cep/version.py b/cep/version.py index 124e462..4c9f88d 100644 --- a/cep/version.py +++ b/cep/version.py @@ -1 +1 @@ -__version__ = '0.1.7' +__version__ = '0.2.0.dev0' diff --git a/requirements-test.txt b/requirements-test.txt index 9b43c9c..d55fe2b 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,7 +1,7 @@ pytest==6.2.5 pytest-vcr==1.0.2 pytest-cov==3.0.0 -black==21.12b0 +black==22.3.0 flake8==4.0.1 isort[pipfile]==5.10.1 mypy==0.790 diff --git a/tests/test_transferencia.py b/tests/test_transferencia.py index 206e5c1..5cbea57 100644 --- a/tests/test_transferencia.py +++ b/tests/test_transferencia.py @@ -2,8 +2,10 @@ import os import pytest +from requests import HTTPError from cep import Transferencia +from cep.exc import CepError, MaxRequestError @pytest.mark.vcr @@ -54,3 +56,36 @@ def test_descagar_transferencia_con_fecha_distinta(transferencia): ) assert type(tr.to_dict()) is dict tr.descargar() + + +@pytest.mark.vcr +def test_lanza_cep_error_para_errores_500(): + try: + for i in range(10): + Transferencia.validar( + fecha=dt.date(2022, 4, 19), + clave_rastreo='CUENCA927820173168', + emisor='90646', # STP + receptor='40012', # BBVA + cuenta='012180000', + monto=0.01, + ) + except CepError as exc: + assert type(exc.__cause__) is HTTPError + assert str(exc.__cause__) == ( + '500 Server Error: Internal Server Error for url: ' + 'https://www.banxico.org.mx/cep/descarga.do?formato=XML' + ) + + +@pytest.mark.vcr +def test_maximo_numero_de_requests(): + with pytest.raises(MaxRequestError): + Transferencia.validar( + fecha=dt.date(2022, 4, 19), + clave_rastreo='CUENCA927820173168', + emisor='90646', # STP + receptor='40012', # BBVA + cuenta='012180000', + monto=0.01, + )