Projet

Général

Profil

0001-caluire-axel-check-DUI-on-pay_invoice-endpoint-56006.patch

Nicolas Roche, 06 août 2021 14:59

Télécharger (9,6 ko)

Voir les différences:

Subject: [PATCH] caluire-axel: check DUI on pay_invoice endpoint (#56006)

 passerelle/contrib/caluire_axel/models.py | 28 +++++++++++--------
 tests/test_caluire_axel.py                | 34 +++++++++++++++++++++--
 2 files changed, 47 insertions(+), 15 deletions(-)
passerelle/contrib/caluire_axel/models.py
1198 1198
        post={
1199 1199
            'request_body': {
1200 1200
                'schema': {
1201 1201
                    'application/json': schemas.PAYMENT_SCHEMA,
1202 1202
                }
1203 1203
            }
1204 1204
        },
1205 1205
    )
1206
    def pay_invoice(self, request, regie_id, invoice_id, **kwargs):
1207
        family_id, invoice_id = invoice_id.split('-')
1206
    def pay_invoice(self, request, regie_id, invoice_id, NameID, post_data):
1207
        family_id, real_invoice_id = invoice_id.split('-')
1208
        link = self.get_link(NameID)
1209
        assert link.family_id == family_id, 'invoice does not belongs to linked family'
1208 1210

  
1209
        invoice = self.get_invoice(regie_id=regie_id, family_id=family_id, invoice_id=invoice_id)
1211
        invoice = self.get_invoice(regie_id=regie_id, family_id=family_id, invoice_id=real_invoice_id)
1210 1212
        if invoice is None:
1211 1213
            raise APIError('Invoice not found', err_code='not-found')
1212 1214

  
1213 1215
        transaction_amount = invoice['amount']
1214
        post_data = {
1215
            'IDFACTURE': int(invoice_id),
1216
        payload = {
1217
            'IDFACTURE': int(real_invoice_id),
1216 1218
            'IDENTREGIEENC': regie_id,
1217 1219
            'MONTANT': transaction_amount,
1218 1220
            'IDENTMODEREGLEMENT': 'INCB',
1219 1221
        }
1220 1222
        try:
1221
            result = schemas.set_paiement(self, {'PORTAIL': {'SETPAIEMENT': post_data}})
1223
            result = schemas.set_paiement(self, {'PORTAIL': {'SETPAIEMENT': payload}})
1222 1224
        except axel.AxelError as e:
1223 1225
            raise APIError(
1224 1226
                'Axel error: %s' % e,
1225 1227
                err_code='error',
1226 1228
                data={
1227 1229
                    'xml_request': e.xml_request,
1228 1230
                    'xml_response': e.xml_response,
1229 1231
                    'regie_id': regie_id,
1230
                    'family_id': family_id,
1231
                    'invoice': invoice,
1232
                    'invoice_id': invoice_id,
1233
                    'NameID': NameID,
1232 1234
                    'post_data': post_data,
1233
                    'kwargs': kwargs,
1235
                    'invoice': invoice,
1236
                    'payload': payload,
1234 1237
                },
1235 1238
            )
1236 1239
        try:
1237 1240
            code = int(result.json_response['DATA']['PORTAIL']['SETPAIEMENT']['CODE'])
1238 1241
        except (KeyError, ValueError):
1239 1242
            raise APIError(
1240 1243
                'Wrong pay-invoice response',
1241 1244
                err_code='pay-invoice-code-response-error',
1242 1245
                data={
1243 1246
                    'regie_id': regie_id,
1244
                    'family_id': family_id,
1245
                    'invoice': invoice,
1247
                    'invoice_id': invoice_id,
1248
                    'NameID': NameID,
1246 1249
                    'post_data': post_data,
1247
                    'kwargs': kwargs,
1250
                    'invoice': invoice,
1251
                    'payload': payload,
1248 1252
                    'result': result.json_response,
1249 1253
                },
1250 1254
                http_status=500,
1251 1255
            )
1252 1256

  
1253 1257
        if code < 0:
1254 1258
            raise APIError('Wrong pay-invoice status', err_code='pay-invoice-code-error-%s' % code)
1255 1259

  
tests/test_caluire_axel.py
3298 3298
    with mock_data(content, 'GetFacturesaPayer'):
3299 3299
        with mock.patch('passerelle.contrib.caluire_axel.schemas.set_paiement') as operation:
3300 3300
            operation.side_effect = AxelError('FooBar')
3301 3301
            resp = app.post_json(
3302 3302
                '/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', params=payload
3303 3303
            )
3304 3304
    assert resp.json['err_desc'] == "Axel error: FooBar"
3305 3305
    assert resp.json['err'] == 'error'
3306
    for key in ('xml_request', 'xml_response', 'regie_id', 'family_id', 'invoice', 'post_data', 'kwargs'):
3306
    for key in (
3307
        'xml_request',
3308
        'xml_response',
3309
        'regie_id',
3310
        'invoice_id',
3311
        'NameID',
3312
        'post_data',
3313
        'invoice',
3314
        'payload',
3315
    ):
3307 3316
        assert key in resp.json['data'].keys()
3308 3317

  
3309 3318
    content2 = '''
3310 3319
<PORTAIL>
3311 3320
  <SETPAIEMENT>
3312 3321
    <CODE>-3</CODE>
3313 3322
  </SETPAIEMENT>
3314 3323
</PORTAIL>'''
......
3324 3333

  
3325 3334

  
3326 3335
def test_pay_invoice_endpoint_bad_request(app, resource):
3327 3336
    resp = app.post_json(
3328 3337
        '/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', params={}, status=400
3329 3338
    )
3330 3339
    assert resp.json['err_desc'] == "'transaction_date' is a required property"
3331 3340

  
3341
    payload = {
3342
        'transaction_date': '2021-06-15T12:00:00',
3343
        'transaction_id': 'foo',
3344
    }
3345
    resp = app.post_json('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy', params=payload)
3346
    assert resp.json['err_desc'] == "Person not found"
3347
    assert resp.json['err'] == 'not-found'
3348

  
3349
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
3350
    resp = app.post_json(
3351
        '/caluire-axel/test/regie/MAREGIE/invoice/ZZZ-42/pay?NameID=yyy', params=payload, status=500
3352
    )
3353
    assert resp.json['err'] == 1
3354
    assert resp.json['err_class'] == 'AssertionError'
3355
    assert resp.json['err_desc'] == 'invoice does not belongs to linked family'
3356

  
3332 3357

  
3333 3358
@freezegun.freeze_time('2019-12-04')
3334 3359
def test_pay_invoice_endpoint_no_result(app, resource):
3335 3360
    payload = {
3336 3361
        'transaction_date': '2021-06-15T12:00:00',
3337 3362
        'transaction_id': 'foo',
3338 3363
    }
3364
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
3339 3365
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
3340 3366
    with open(filepath) as xml:
3341 3367
        content = (
3342 3368
            '''<PORTAIL>
3343 3369
  <GETFACTURESAPAYER>
3344 3370
    %s
3345 3371
  </GETFACTURESAPAYER>
3346 3372
</PORTAIL>'''
......
3357 3383
    assert resp.json['err'] == 'not-found'
3358 3384

  
3359 3385

  
3360 3386
def test_pay_invoice_endpoint_wrong_response(app, resource):
3361 3387
    payload = {
3362 3388
        'transaction_date': '2021-06-15T12:00:00',
3363 3389
        'transaction_id': 'foo',
3364 3390
    }
3391
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
3365 3392
    with mock.patch(
3366 3393
        'passerelle.contrib.caluire_axel.models.CaluireAxel.get_invoice',
3367 3394
        return_value={'amount': '44.9'},
3368 3395
    ):
3369 3396
        with mock.patch('passerelle.contrib.caluire_axel.schemas.set_paiement') as operation:
3370 3397
            operation.return_value = OperationResult(
3371 3398
                json_response={'DATA': {'PORTAIL': {}}},
3372 3399
                xml_request='',
......
3374 3401
            )
3375 3402
            resp = app.post_json(
3376 3403
                '/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy',
3377 3404
                params=payload,
3378 3405
                status=500,
3379 3406
            )
3380 3407
        assert resp.json['err'] == 'pay-invoice-code-response-error'
3381 3408
        assert resp.json['err_desc'] == 'Wrong pay-invoice response'
3382
        for key in 'regie_id', 'family_id', 'invoice', 'post_data', 'kwargs', 'result':
3409
        for key in 'regie_id', 'invoice_id', 'NameID', 'post_data', 'invoice', 'payload', 'result':
3383 3410
            assert key in resp.json['data'].keys()
3384 3411

  
3385 3412
        with mock.patch('passerelle.contrib.caluire_axel.schemas.set_paiement') as operation:
3386 3413
            operation.return_value = OperationResult(
3387 3414
                json_response={'DATA': {'PORTAIL': {'SETPAIEMENT': {'CODE': 'oups'}}}},
3388 3415
                xml_request='',
3389 3416
                xml_response='',
3390 3417
            )
3391 3418
            resp = app.post_json(
3392 3419
                '/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pay?NameID=yyy',
3393 3420
                params=payload,
3394 3421
                status=500,
3395 3422
            )
3396 3423
        assert resp.json['err'] == 'pay-invoice-code-response-error'
3397 3424
        assert resp.json['err_desc'] == 'Wrong pay-invoice response'
3398
        for key in 'regie_id', 'family_id', 'invoice', 'post_data', 'kwargs', 'result':
3425
        for key in 'regie_id', 'invoice_id', 'NameID', 'post_data', 'invoice', 'payload', 'result':
3399 3426
            assert key in resp.json['data'].keys()
3400 3427

  
3401 3428

  
3402 3429
def test_pay_invoice_endpoint(app, resource):
3403 3430
    payload = {
3404 3431
        'transaction_date': '2021-06-15T12:00:00',
3405 3432
        'transaction_id': 'foo',
3406 3433
    }
......
3433 3460
    assert resp.json['created'] is True
3434 3461

  
3435 3462

  
3436 3463
def test_pay_invoice_providing_mode_reglement(app, resource):
3437 3464
    payload = {
3438 3465
        'transaction_date': '2021-06-15T12:00:00',
3439 3466
        'transaction_id': 'foo',
3440 3467
    }
3468
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
3441 3469
    with mock.patch(
3442 3470
        'passerelle.contrib.caluire_axel.models.CaluireAxel.get_invoice',
3443 3471
        return_value={'amount': '44.9'},
3444 3472
    ):
3445 3473
        with mock.patch('passerelle.contrib.caluire_axel.schemas.set_paiement') as operation:
3446 3474
            operation.return_value = OperationResult(
3447 3475
                json_response={'DATA': {'PORTAIL': {'SETPAIEMENT': {'CODE': 0}}}},
3448 3476
                xml_request='',
3449
-