0001-caluire-axel-handle-wrong-messages-on-pay_invoice-55.patch
passerelle/contrib/caluire_axel/models.py | ||
---|---|---|
1194 | 1194 |
'schema': { |
1195 | 1195 |
'application/json': schemas.PAYMENT_SCHEMA, |
1196 | 1196 |
} |
1197 | 1197 |
} |
1198 | 1198 |
}, |
1199 | 1199 |
) |
1200 | 1200 |
def pay_invoice(self, request, regie_id, invoice_id, **kwargs): |
1201 | 1201 |
family_id, invoice_id = invoice_id.split('-') |
1202 | ||
1203 | 1202 |
invoice = self.get_invoice(regie_id=regie_id, family_id=family_id, invoice_id=invoice_id) |
1204 | 1203 |
if invoice is None: |
1205 | 1204 |
raise APIError('Invoice not found', err_code='not-found') |
1206 | 1205 | |
1207 | 1206 |
transaction_amount = invoice['amount'] |
1208 | 1207 |
post_data = { |
1209 | 1208 |
'IDFACTURE': int(invoice_id), |
1210 | 1209 |
'IDENTREGIEENC': regie_id, |
... | ... | |
1212 | 1211 |
'IDENTMODEREGLEMENT': 'INCB', |
1213 | 1212 |
} |
1214 | 1213 |
try: |
1215 | 1214 |
result = schemas.set_paiement(self, {'PORTAIL': {'SETPAIEMENT': post_data}}) |
1216 | 1215 |
except axel.AxelError as e: |
1217 | 1216 |
raise APIError( |
1218 | 1217 |
'Axel error: %s' % e, |
1219 | 1218 |
err_code='error', |
1220 |
data={'xml_request': e.xml_request, 'xml_response': e.xml_response}, |
|
1219 |
data={ |
|
1220 |
'xml_request': e.xml_request, |
|
1221 |
'xml_response': e.xml_response, |
|
1222 |
'regie_id': regie_id, |
|
1223 |
'family_id': family_id, |
|
1224 |
'invoice': invoice, |
|
1225 |
'post_data': post_data, |
|
1226 |
'kwargs': kwargs, |
|
1227 |
}, |
|
1228 |
) |
|
1229 |
try: |
|
1230 |
code = int(result.json_response['DATA']['PORTAIL']['SETPAIEMENT']['CODE']) |
|
1231 |
except (KeyError, ValueError): |
|
1232 |
raise APIError( |
|
1233 |
'Wrong pay-invoice response', |
|
1234 |
err_code='pay-invoice-code-response-error', |
|
1235 |
data={ |
|
1236 |
'regie_id': regie_id, |
|
1237 |
'family_id': family_id, |
|
1238 |
'invoice': invoice, |
|
1239 |
'post_data': post_data, |
|
1240 |
'kwargs': kwargs, |
|
1241 |
'result': result.json_response, |
|
1242 |
}, |
|
1243 |
http_status=500, |
|
1221 | 1244 |
) |
1222 | 1245 | |
1223 |
code = result.json_response['DATA']['PORTAIL']['SETPAIEMENT']['CODE'] |
|
1224 | 1246 |
if code < 0: |
1225 | 1247 |
raise APIError('Wrong pay-invoice status', err_code='pay-invoice-code-error-%s' % code) |
1226 | 1248 | |
1227 | 1249 |
return { |
1228 | 1250 |
'created': True, |
1229 | 1251 |
'data': { |
1230 | 1252 |
'xml_request': result.xml_request, |
1231 | 1253 |
'xml_response': result.xml_response, |
tests/test_caluire_axel.py | ||
---|---|---|
3284 | 3284 |
with mock_data(content, 'GetFacturesaPayer'): |
3285 | 3285 |
with mock.patch('passerelle.contrib.caluire_axel.schemas.set_paiement') as operation: |
3286 | 3286 |
operation.side_effect = AxelError('FooBar') |
3287 | 3287 |
resp = app.post_json( |
3288 | 3288 |
'/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', params=payload |
3289 | 3289 |
) |
3290 | 3290 |
assert resp.json['err_desc'] == "Axel error: FooBar" |
3291 | 3291 |
assert resp.json['err'] == 'error' |
3292 |
for key in ('xml_request', 'xml_response', 'regie_id', 'family_id', 'invoice', 'post_data', 'kwargs'): |
|
3293 |
assert key in resp.json['data'].keys() |
|
3292 | 3294 | |
3293 | 3295 |
content2 = ''' |
3294 | 3296 |
<PORTAIL> |
3295 | 3297 |
<SETPAIEMENT> |
3296 | 3298 |
<CODE>-3</CODE> |
3297 | 3299 |
</SETPAIEMENT> |
3298 | 3300 |
</PORTAIL>''' |
3299 | 3301 |
with mock.patch('passerelle.contrib.caluire_axel.models.CaluireAxel.soap_client') as client: |
... | ... | |
3330 | 3332 |
% xml.read() |
3331 | 3333 |
) |
3332 | 3334 |
with mock_data(content, 'GetFacturesaPayer'): |
3333 | 3335 |
resp = app.post_json('/caluire-axel/test/regie/MAREGIE/invoice/XXX-35/pay?NameID=yyy', params=payload) |
3334 | 3336 |
assert resp.json['err_desc'] == "Invoice not found" |
3335 | 3337 |
assert resp.json['err'] == 'not-found' |
3336 | 3338 | |
3337 | 3339 | |
3340 |
def test_pay_invoice_endpoint_wrong_response(app, resource): |
|
3341 |
payload = { |
|
3342 |
'transaction_date': '2021-06-15T12:00:00', |
|
3343 |
'transaction_id': 'foo', |
|
3344 |
} |
|
3345 |
with mock.patch( |
|
3346 |
'passerelle.contrib.caluire_axel.models.CaluireAxel.get_invoice', |
|
3347 |
return_value={'amount': '44.9'}, |
|
3348 |
): |
|
3349 |
with mock.patch('passerelle.contrib.caluire_axel.schemas.set_paiement') as operation: |
|
3350 |
operation.return_value = OperationResult( |
|
3351 |
json_response={'DATA': {'PORTAIL': {}}}, |
|
3352 |
xml_request='', |
|
3353 |
xml_response='', |
|
3354 |
) |
|
3355 |
resp = app.post_json( |
|
3356 |
'/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', |
|
3357 |
params=payload, |
|
3358 |
status=500, |
|
3359 |
) |
|
3360 |
assert resp.json['err'] == 'pay-invoice-code-response-error' |
|
3361 |
assert resp.json['err_desc'] == 'Wrong pay-invoice response' |
|
3362 |
for key in 'regie_id', 'family_id', 'invoice', 'post_data', 'kwargs', 'result': |
|
3363 |
assert key in resp.json['data'].keys() |
|
3364 | ||
3365 |
with mock.patch('passerelle.contrib.caluire_axel.schemas.set_paiement') as operation: |
|
3366 |
operation.return_value = OperationResult( |
|
3367 |
json_response={'DATA': {'PORTAIL': {'SETPAIEMENT': {'CODE': 'oups'}}}}, |
|
3368 |
xml_request='', |
|
3369 |
xml_response='', |
|
3370 |
) |
|
3371 |
resp = app.post_json( |
|
3372 |
'/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', |
|
3373 |
params=payload, |
|
3374 |
status=500, |
|
3375 |
) |
|
3376 |
assert resp.json['err'] == 'pay-invoice-code-response-error' |
|
3377 |
assert resp.json['err_desc'] == 'Wrong pay-invoice response' |
|
3378 |
for key in 'regie_id', 'family_id', 'invoice', 'post_data', 'kwargs', 'result': |
|
3379 |
assert key in resp.json['data'].keys() |
|
3380 | ||
3381 | ||
3338 | 3382 |
def test_pay_invoice_endpoint(app, resource): |
3339 | 3383 |
payload = { |
3340 | 3384 |
'transaction_date': '2021-06-15T12:00:00', |
3341 | 3385 |
'transaction_id': 'foo', |
3342 | 3386 |
} |
3343 | 3387 |
Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42') |
3344 | 3388 |
filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml') |
3345 | 3389 |
with open(filepath) as xml: |
3346 |
- |