Projet

Général

Profil

0001-caluire-axel-handle-wrong-messages-on-pay_invoice-55.patch

Nicolas Roche, 27 juillet 2021 20:07

Télécharger (6,05 ko)

Voir les différences:

Subject: [PATCH] caluire-axel: handle wrong messages on pay_invoice (#55793)

 passerelle/contrib/caluire_axel/models.py | 27 ++++++++++++--
 tests/test_caluire_axel.py                | 44 +++++++++++++++++++++++
 2 files changed, 69 insertions(+), 2 deletions(-)
passerelle/contrib/caluire_axel/models.py
1212 1212
            'IDENTMODEREGLEMENT': 'INCB',
1213 1213
        }
1214 1214
        try:
1215 1215
            result = schemas.set_paiement(self, {'PORTAIL': {'SETPAIEMENT': post_data}})
1216 1216
        except axel.AxelError as e:
1217 1217
            raise APIError(
1218 1218
                'Axel error: %s' % e,
1219 1219
                err_code='error',
1220
                data={'xml_request': e.xml_request, 'xml_response': e.xml_response},
1220
                data={
1221
                    'xml_request': e.xml_request,
1222
                    'xml_response': e.xml_response,
1223
                    'regie_id': regie_id,
1224
                    'family_id': family_id,
1225
                    'invoice': invoice,
1226
                    'post_data': post_data,
1227
                    'kwargs': kwargs,
1228
                },
1229
            )
1230
        try:
1231
            code = int(result.json_response['DATA']['PORTAIL']['SETPAIEMENT']['CODE'])
1232
        except (KeyError, ValueError):
1233
            raise APIError(
1234
                'Wrong pay-invoice response',
1235
                err_code='pay-invoice-code-response-error',
1236
                data={
1237
                    'regie_id': regie_id,
1238
                    'family_id': family_id,
1239
                    'invoice': invoice,
1240
                    'post_data': post_data,
1241
                    'kwargs': kwargs,
1242
                    'result': result.json_response,
1243
                },
1244
                http_status=500,
1221 1245
            )
1222 1246

  
1223
        code = result.json_response['DATA']['PORTAIL']['SETPAIEMENT']['CODE']
1224 1247
        if code < 0:
1225 1248
            raise APIError('Wrong pay-invoice status', err_code='pay-invoice-code-error-%s' % code)
1226 1249

  
1227 1250
        return {
1228 1251
            'created': True,
1229 1252
            'data': {
1230 1253
                'xml_request': result.xml_request,
1231 1254
                '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
-