payfip_ws: encapsulate requests exceptions in PayFiPError (#57838)
This commit is contained in:
parent
6284e0fbb3
commit
cc54798189
|
@ -23,6 +23,7 @@ import functools
|
|||
import os
|
||||
import random
|
||||
import re
|
||||
import requests
|
||||
import unicodedata
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
|
@ -132,7 +133,11 @@ class PayFiP(object):
|
|||
except zeep.exceptions.Fault as fault:
|
||||
raise self.fault_to_exception(fault) or PayFiPError('unknown', fault.message, fault)
|
||||
except zeep.exceptions.Error as zeep_error:
|
||||
raise PayFiPError('erreur-soap', str(zeep_error), zeep_error)
|
||||
raise PayFiPError('SOAP error', str(zeep_error), zeep_error)
|
||||
except requests.RequestException as e:
|
||||
raise PayFiPError('HTTP error', e)
|
||||
except Exception as e:
|
||||
raise PayFiPError('Unexpected error', e)
|
||||
|
||||
def get_info_client(self, numcli):
|
||||
return self._perform(
|
||||
|
|
|
@ -77,13 +77,22 @@ def history_name(request):
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def history(history_name, request):
|
||||
def history(history_name, request, zeep_history_plugin):
|
||||
if 'update_data' not in request.keywords:
|
||||
history_mock = PayFiPHTTMock(history_name)
|
||||
with httmock.HTTMock(history_mock.mock):
|
||||
yield history_mock
|
||||
else:
|
||||
yield None
|
||||
history_path = 'tests/data/payfip-%s.json' % history_name
|
||||
d = [
|
||||
(xmlindent(exchange['sent']['envelope']),
|
||||
xmlindent(exchange['received']['envelope']))
|
||||
for exchange in zeep_history_plugin._buffer
|
||||
]
|
||||
content = json.dumps(d)
|
||||
with open(history_path, 'wb') as fd:
|
||||
fd.write(content)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -104,32 +113,29 @@ def backend(request):
|
|||
})
|
||||
|
||||
|
||||
@httmock.urlmatch()
|
||||
def raise_on_request(url, request):
|
||||
# ensure we do not access network
|
||||
from requests.exceptions import RequestException
|
||||
raise RequestException('huhu')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def payfip(history, history_name, request):
|
||||
history = HistoryPlugin()
|
||||
def zeep_history_plugin():
|
||||
return HistoryPlugin()
|
||||
|
||||
@httmock.urlmatch()
|
||||
def raise_on_request(url, request):
|
||||
# ensure we do not access network
|
||||
from requests.exceptions import RequestException
|
||||
raise RequestException('huhu')
|
||||
|
||||
@pytest.fixture
|
||||
def payfip(zeep_history_plugin):
|
||||
with httmock.HTTMock(raise_on_request):
|
||||
payfip = PayFiP(wsdl_url='./eopayment/resource/PaiementSecuriseService.wsdl',
|
||||
zeep_client_kwargs={'plugins': [history]})
|
||||
zeep_client_kwargs={'plugins': [zeep_history_plugin]})
|
||||
yield payfip
|
||||
|
||||
# add @pytest.mark.update_data to test to update fixtures data
|
||||
if 'update_data' in request.keywords:
|
||||
history_path = 'tests/data/payfip-%s.json' % history_name
|
||||
d = [
|
||||
(xmlindent(exchange['sent']['envelope']),
|
||||
xmlindent(exchange['received']['envelope']))
|
||||
for exchange in history._buffer
|
||||
]
|
||||
content = json.dumps(d)
|
||||
with open(history_path, 'wb') as fd:
|
||||
fd.write(content)
|
||||
|
||||
@pytest.fixture
|
||||
def payfip_history(history, payfip, zeep_history_plugin, request):
|
||||
yield
|
||||
|
||||
|
||||
def set_history_name(name):
|
||||
|
@ -142,13 +148,13 @@ def set_history_name(name):
|
|||
# pytestmark = pytest.mark.update_data
|
||||
|
||||
|
||||
def test_get_client_info(payfip):
|
||||
def test_get_client_info(history, payfip):
|
||||
result = payfip.get_info_client(NUMCLI)
|
||||
assert result.numcli == NUMCLI
|
||||
assert result.libelleN2 == 'POUETPOUET'
|
||||
|
||||
|
||||
def test_get_idop_ok(payfip):
|
||||
def test_get_idop_ok(history, payfip):
|
||||
result = payfip.get_idop(
|
||||
numcli=NUMCLI,
|
||||
exer='2019',
|
||||
|
@ -162,7 +168,7 @@ def test_get_idop_ok(payfip):
|
|||
assert result == 'cc0cb210-1cd4-11ea-8cca-0213ad91a103'
|
||||
|
||||
|
||||
def test_get_idop_refdet_error(payfip):
|
||||
def test_get_idop_refdet_error(history, payfip):
|
||||
with pytest.raises(PayFiPError, match='.*R3.*Le format.*REFDET.*conforme'):
|
||||
payfip.get_idop(
|
||||
numcli=NUMCLI,
|
||||
|
@ -176,7 +182,7 @@ def test_get_idop_refdet_error(payfip):
|
|||
saisie='T')
|
||||
|
||||
|
||||
def test_get_idop_adresse_mel_incorrect(payfip):
|
||||
def test_get_idop_adresse_mel_incorrect(payfip, payfip_history):
|
||||
with pytest.raises(PayFiPError, match='.*A2.*Adresse.*incorrecte'):
|
||||
payfip.get_idop(
|
||||
numcli=NUMCLI,
|
||||
|
@ -190,7 +196,7 @@ def test_get_idop_adresse_mel_incorrect(payfip):
|
|||
saisie='T')
|
||||
|
||||
|
||||
def test_get_info_paiement_ok(payfip):
|
||||
def test_get_info_paiement_ok(history, payfip):
|
||||
result = payfip.get_info_paiement('cc0cb210-1cd4-11ea-8cca-0213ad91a103')
|
||||
assert {k: result[k] for k in result} == {
|
||||
'dattrans': '12122019',
|
||||
|
@ -208,7 +214,7 @@ def test_get_info_paiement_ok(payfip):
|
|||
}
|
||||
|
||||
|
||||
def test_get_info_paiement_P1(payfip, freezer):
|
||||
def test_get_info_paiement_P1(history, payfip, freezer):
|
||||
# idop par pas encore reçu par la plate-forme ou déjà nettoyé (la nuit)
|
||||
with pytest.raises(PayFiPError, match='.*P1.*IdOp incorrect.*'):
|
||||
payfip.get_info_paiement('cc0cb210-1cd4-11ea-8cca-0213ad91a103')
|
||||
|
@ -244,41 +250,41 @@ def test_P1_and_payment_status_utc_aware_now_later(history, backend, freezer):
|
|||
|
||||
|
||||
@set_history_name('test_get_info_paiement_P1')
|
||||
def test_P1_and_payment_status_utc_naive_now_later(payfip, backend, freezer):
|
||||
def test_P1_and_payment_status_utc_naive_now_later(history, payfip, backend, freezer):
|
||||
now = datetime.datetime.now()
|
||||
freezer.move_to(datetime.timedelta(minutes=25))
|
||||
response = backend.payment_status(transaction_id='cc0cb210-1cd4-11ea-8cca-0213ad91a103', transaction_date=now)
|
||||
assert response.result == eopayment.EXPIRED
|
||||
|
||||
|
||||
def test_get_info_paiement_P5(payfip):
|
||||
def test_get_info_paiement_P5(history, payfip):
|
||||
# idop reçu par la plate-forme mais transaction en cours
|
||||
with pytest.raises(PayFiPError, match='.*P5.*sultat de la transaction non connu.*'):
|
||||
payfip.get_info_paiement('cc0cb210-1cd4-11ea-8cca-0213ad91a103')
|
||||
|
||||
|
||||
@set_history_name('test_get_info_paiement_P5')
|
||||
def test_P5_and_payment_status(payfip, backend, freezer):
|
||||
def test_P5_and_payment_status(history, payfip, backend, freezer):
|
||||
response = backend.payment_status(transaction_id='cc0cb210-1cd4-11ea-8cca-0213ad91a103')
|
||||
assert response.result == eopayment.WAITING
|
||||
|
||||
|
||||
@set_history_name('test_get_info_paiement_P5')
|
||||
def test_P5_and_payment_status_utc_aware_now(payfip, backend, freezer):
|
||||
def test_P5_and_payment_status_utc_aware_now(history, payfip, backend, freezer):
|
||||
utc_now = datetime.datetime.now(pytz.utc)
|
||||
response = backend.payment_status(transaction_id='cc0cb210-1cd4-11ea-8cca-0213ad91a103', transaction_date=utc_now)
|
||||
assert response.result == eopayment.WAITING
|
||||
|
||||
|
||||
@set_history_name('test_get_info_paiement_P5')
|
||||
def test_P5_and_payment_status_utc_naive_now(payfip, backend, freezer):
|
||||
def test_P5_and_payment_status_utc_naive_now(history, payfip, backend, freezer):
|
||||
now = datetime.datetime.now()
|
||||
response = backend.payment_status(transaction_id='cc0cb210-1cd4-11ea-8cca-0213ad91a103', transaction_date=now)
|
||||
assert response.result == eopayment.WAITING
|
||||
|
||||
|
||||
@set_history_name('test_get_info_paiement_P5')
|
||||
def test_P5_and_payment_status_utc_aware_now_later(payfip, backend, freezer):
|
||||
def test_P5_and_payment_status_utc_aware_now_later(history, payfip, backend, freezer):
|
||||
utc_now = datetime.datetime.now(pytz.utc)
|
||||
freezer.move_to(datetime.timedelta(minutes=25))
|
||||
response = backend.payment_status(transaction_id='cc0cb210-1cd4-11ea-8cca-0213ad91a103', transaction_date=utc_now)
|
||||
|
@ -286,14 +292,14 @@ def test_P5_and_payment_status_utc_aware_now_later(payfip, backend, freezer):
|
|||
|
||||
|
||||
@set_history_name('test_get_info_paiement_P5')
|
||||
def test_P5_and_payment_status_utc_naive_now_later(payfip, backend, freezer):
|
||||
def test_P5_and_payment_status_utc_naive_now_later(history, payfip, backend, freezer):
|
||||
now = datetime.datetime.now()
|
||||
freezer.move_to(datetime.timedelta(minutes=25))
|
||||
response = backend.payment_status(transaction_id='cc0cb210-1cd4-11ea-8cca-0213ad91a103', transaction_date=now)
|
||||
assert response.result == eopayment.EXPIRED
|
||||
|
||||
|
||||
def test_payment_ok(payfip, backend):
|
||||
def test_payment_ok(history, payfip, backend):
|
||||
payment_id, kind, url = backend.request(
|
||||
amount='10.00',
|
||||
email=MEL,
|
||||
|
@ -492,3 +498,13 @@ def test_get_minimal_amount(backend):
|
|||
|
||||
def test_get_maximal_amount(backend):
|
||||
assert backend.get_maximal_amount() is not None
|
||||
|
||||
|
||||
def test_request_error(payfip, backend):
|
||||
with pytest.raises(PayFiPError):
|
||||
with httmock.HTTMock(raise_on_request):
|
||||
payment_id, kind, url = backend.request(
|
||||
amount='10.00',
|
||||
email=MEL,
|
||||
# make test deterministic
|
||||
refdet='201912261758460053903194')
|
||||
|
|
Loading…
Reference in New Issue