0001-caluire-axel-handle-wrong-messages-on-pay_invoice-55.patch
passerelle/contrib/caluire_axel/models.py | ||
---|---|---|
1191 | 1191 |
'schema': { |
1192 | 1192 |
'application/json': schemas.PAYMENT_SCHEMA, |
1193 | 1193 |
} |
1194 | 1194 |
} |
1195 | 1195 |
}, |
1196 | 1196 |
) |
1197 | 1197 |
def pay_invoice(self, request, regie_id, invoice_id, **kwargs): |
1198 | 1198 |
family_id, invoice_id = invoice_id.split('-') |
1199 | ||
1200 | 1199 |
invoice = self.get_invoice(regie_id=regie_id, family_id=family_id, invoice_id=invoice_id) |
1201 | 1200 |
if invoice is None: |
1202 | 1201 |
raise APIError('Invoice not found', err_code='not-found') |
1203 | 1202 | |
1204 | 1203 |
transaction_amount = invoice['amount'] |
1205 | 1204 |
post_data = { |
1206 | 1205 |
'IDFACTURE': int(invoice_id), |
1207 | 1206 |
'IDENTREGIEENC': regie_id, |
... | ... | |
1209 | 1208 |
'IDENTMODEREGLEMENT': 'INCB', |
1210 | 1209 |
} |
1211 | 1210 |
try: |
1212 | 1211 |
result = schemas.set_paiement(self, {'PORTAIL': {'SETPAIEMENT': post_data}}) |
1213 | 1212 |
except axel.AxelError as e: |
1214 | 1213 |
raise APIError( |
1215 | 1214 |
'Axel error: %s' % e, |
1216 | 1215 |
err_code='error', |
1217 |
data={'xml_request': e.xml_request, 'xml_response': e.xml_response}, |
|
1216 |
data={ |
|
1217 |
'xml_request': e.xml_request, |
|
1218 |
'xml_response': e.xml_response, |
|
1219 |
'regie_id': regie_id, |
|
1220 |
'family_id': family_id, |
|
1221 |
'invoice': invoice, |
|
1222 |
'payload': kwargs, |
|
1223 |
}, |
|
1224 |
) |
|
1225 |
try: |
|
1226 |
code = int(result.json_response['DATA']['PORTAIL']['SETPAIEMENT']['CODE']) |
|
1227 |
except (KeyError, ValueError): |
|
1228 |
raise APIError( |
|
1229 |
'Wrong pay-invoice response', |
|
1230 |
err_code='pay-invoice-code-response-error', |
|
1231 |
data={ |
|
1232 |
'regie_id': regie_id, |
|
1233 |
'family_id': family_id, |
|
1234 |
'invoice': invoice, |
|
1235 |
'payload': kwargs, |
|
1236 |
}, |
|
1218 | 1237 |
) |
1219 | 1238 | |
1220 |
code = result.json_response['DATA']['PORTAIL']['SETPAIEMENT']['CODE'] |
|
1221 | 1239 |
if code < 0: |
1222 | 1240 |
raise APIError('Wrong pay-invoice status', err_code='pay-invoice-code-error-%s' % code) |
1223 | 1241 | |
1224 | 1242 |
return { |
1225 | 1243 |
'created': True, |
1226 | 1244 |
'data': { |
1227 | 1245 |
'xml_request': result.xml_request, |
1228 | 1246 |
'xml_response': result.xml_response, |
tests/test_caluire_axel.py | ||
---|---|---|
3240 | 3240 |
with mock_data(content, 'GetFacturesaPayer'): |
3241 | 3241 |
with mock.patch('passerelle.contrib.caluire_axel.schemas.set_paiement') as operation: |
3242 | 3242 |
operation.side_effect = AxelError('FooBar') |
3243 | 3243 |
resp = app.post_json( |
3244 | 3244 |
'/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', params=payload |
3245 | 3245 |
) |
3246 | 3246 |
assert resp.json['err_desc'] == "Axel error: FooBar" |
3247 | 3247 |
assert resp.json['err'] == 'error' |
3248 |
for key in 'regie_id', 'family_id', 'invoice', 'payload': |
|
3249 |
assert key in resp.json['data'].keys() |
|
3248 | 3250 | |
3249 | 3251 |
content2 = ''' |
3250 | 3252 |
<PORTAIL> |
3251 | 3253 |
<SETPAIEMENT> |
3252 | 3254 |
<CODE>-3</CODE> |
3253 | 3255 |
</SETPAIEMENT> |
3254 | 3256 |
</PORTAIL>''' |
3255 | 3257 |
with mock.patch('passerelle.contrib.caluire_axel.models.CaluireAxel.soap_client') as client: |
... | ... | |
3286 | 3288 |
% xml.read() |
3287 | 3289 |
) |
3288 | 3290 |
with mock_data(content, 'GetFacturesaPayer'): |
3289 | 3291 |
resp = app.post_json('/caluire-axel/test/regie/MAREGIE/invoice/XXX-35/pay?NameID=yyy', params=payload) |
3290 | 3292 |
assert resp.json['err_desc'] == "Invoice not found" |
3291 | 3293 |
assert resp.json['err'] == 'not-found' |
3292 | 3294 | |
3293 | 3295 | |
3296 |
def test_pay_invoice_endpoint_wrong_response(app, resource): |
|
3297 |
payload = { |
|
3298 |
'transaction_date': '2021-06-15T12:00:00', |
|
3299 |
'transaction_id': 'foo', |
|
3300 |
} |
|
3301 |
with mock.patch( |
|
3302 |
'passerelle.contrib.caluire_axel.models.CaluireAxel.get_invoice', |
|
3303 |
return_value={'amount': '44.9'}, |
|
3304 |
): |
|
3305 |
with mock.patch('passerelle.contrib.caluire_axel.schemas.set_paiement') as operation: |
|
3306 |
operation.return_value = OperationResult( |
|
3307 |
json_response={'DATA': {'PORTAIL': {}}}, |
|
3308 |
xml_request='', |
|
3309 |
xml_response='', |
|
3310 |
) |
|
3311 |
resp = app.post_json( |
|
3312 |
'/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', params=payload |
|
3313 |
) |
|
3314 |
assert resp.json['err'] == 'pay-invoice-code-response-error' |
|
3315 |
assert resp.json['err_desc'] == 'Wrong pay-invoice response' |
|
3316 |
for key in 'regie_id', 'family_id', 'invoice', 'payload': |
|
3317 |
assert key in resp.json['data'].keys() |
|
3318 | ||
3319 |
with mock.patch('passerelle.contrib.caluire_axel.schemas.set_paiement') as operation: |
|
3320 |
operation.return_value = OperationResult( |
|
3321 |
json_response={'DATA': {'PORTAIL': {'SETPAIEMENT': {'CODE': 'oups'}}}}, |
|
3322 |
xml_request='', |
|
3323 |
xml_response='', |
|
3324 |
) |
|
3325 |
resp = app.post_json( |
|
3326 |
'/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', params=payload |
|
3327 |
) |
|
3328 |
assert resp.json['err'] == 'pay-invoice-code-response-error' |
|
3329 |
assert resp.json['err_desc'] == 'Wrong pay-invoice response' |
|
3330 |
for key in 'regie_id', 'family_id', 'invoice', 'payload': |
|
3331 |
assert key in resp.json['data'].keys() |
|
3332 | ||
3333 | ||
3294 | 3334 |
def test_pay_invoice_endpoint(app, resource): |
3295 | 3335 |
payload = { |
3296 | 3336 |
'transaction_date': '2021-06-15T12:00:00', |
3297 | 3337 |
'transaction_id': 'foo', |
3298 | 3338 |
} |
3299 | 3339 |
Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42') |
3300 | 3340 |
filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml') |
3301 | 3341 |
with open(filepath) as xml: |
3302 |
- |