Projet

Général

Profil

0001-caluire-axel-hide-invoices-created-in-the-future-558.patch

Nicolas Roche, 04 août 2021 16:31

Télécharger (9,9 ko)

Voir les différences:

Subject: [PATCH] caluire-axel: hide invoices created in the future (#55803)

 passerelle/contrib/caluire_axel/models.py | 10 ++++++++--
 tests/data/caluire_axel/invoices.xml      |  9 +++++++++
 tests/test_caluire_axel.py                | 22 +++++++++++++++++++++-
 3 files changed, 38 insertions(+), 3 deletions(-)
passerelle/contrib/caluire_axel/models.py
990 990

  
991 991
        code = result.json_response['DATA']['PORTAIL']['GETFACTURESAPAYER']['CODE']
992 992
        if code < 0:
993 993
            raise APIError('Wrong get-invoices status', err_code='get-invoices-code-error-%s' % code)
994 994

  
995 995
        data = result.json_response['DATA']['PORTAIL']['GETFACTURESAPAYER']
996 996
        result = []
997 997

  
998
        date_now = localtime().date()
998 999
        for facture in data.get('FACTURE', []):
999
            result.append(utils.normalize_invoice(facture, family_id))
1000
            date_creation = datetime.datetime.strptime(facture['EMISSION'], axel.json_date_format).date()
1001
            if date_creation <= date_now:
1002
                result.append(utils.normalize_invoice(facture, family_id))
1000 1003
        return result
1001 1004

  
1002 1005
    def get_historical_invoices(self, regie_id, family_id, nb_mounts_limit):
1003 1006
        try:
1004 1007
            nb_mois = int(nb_mounts_limit)
1005 1008
        except ValueError:
1006 1009
            raise APIError('nb_mounts_limit must be an integer', err_code='bad-request', http_status=400)
1007 1010
        try:
......
1027 1030
        code = result.json_response['DATA']['PORTAIL']['GETLISTFACTURES']['CODE']
1028 1031
        if code < 0:
1029 1032
            raise APIError(
1030 1033
                'Wrong get-historical-invoices status',
1031 1034
                err_code='get-historical-invoices-code-error-%s' % code,
1032 1035
            )
1033 1036

  
1034 1037
        data = result.json_response['DATA']['PORTAIL']['GETLISTFACTURES']
1038
        date_now = localtime().date()
1035 1039
        result = []
1036 1040
        for facture in data.get('FACTURE', []):
1037
            result.append(utils.normalize_invoice(facture, family_id, historical=True))
1041
            date_creation = datetime.datetime.strptime(facture['EMISSION'], axel.json_date_format).date()
1042
            if date_creation <= date_now:
1043
                result.append(utils.normalize_invoice(facture, family_id, historical=True))
1038 1044
        return result
1039 1045

  
1040 1046
    def get_invoice(self, regie_id, invoice_id, family_id, historical=None, nb_mounts_limit=None):
1041 1047
        if historical:
1042 1048
            invoices_data = self.get_historical_invoices(
1043 1049
                regie_id=regie_id, family_id=family_id, nb_mounts_limit=nb_mounts_limit
1044 1050
            )
1045 1051
        else:
tests/data/caluire_axel/invoices.xml
12 12
  <IDFACTURE>43</IDFACTURE>
13 13
  <MONTANT>44.94</MONTANT>
14 14
  <ENCAISSE>0.00</ENCAISSE>
15 15
  <FACTURATION>PRESTATIONS PERISCOLAIRES NOVEMBRE 2019</FACTURATION>
16 16
  <EMISSION>12/12/2019</EMISSION>
17 17
  <ECHEANCE>04/01/2020</ECHEANCE>
18 18
  <EXISTEPDF>n</EXISTEPDF>
19 19
</FACTURE>
20
<FACTURE>
21
  <IDFACTURE>44</IDFACTURE>
22
  <MONTANT>44.44</MONTANT>
23
  <ENCAISSE>0.00</ENCAISSE>
24
  <FACTURATION>PRESTATIONS PERISCOLAIRES DECEMBRE 2019</FACTURATION>
25
  <EMISSION>12/01/2020</EMISSION>
26
  <ECHEANCE>04/02/2020</ECHEANCE>
27
  <EXISTEPDF>n</EXISTEPDF>
28
</FACTURE>
tests/test_caluire_axel.py
2815 2815
    </GETFACTURESAPAYER>
2816 2816
</PORTAIL>'''
2817 2817
    with mock_data(content, 'GetFacturesaPayer'):
2818 2818
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoices?NameID=yyy')
2819 2819
    assert resp.json['err'] == 0
2820 2820
    assert resp.json['data'] == []
2821 2821

  
2822 2822

  
2823
@freezegun.freeze_time('2019-12-05')
2823
@freezegun.freeze_time('2019-12-12')
2824 2824
def test_invoices_endpoint(app, resource):
2825 2825
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
2826 2826
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
2827 2827
    with open(filepath) as xml:
2828 2828
        content = (
2829 2829
            '''
2830 2830
<PORTAIL>
2831 2831
  <GETFACTURESAPAYER>
......
2933 2933
    </GETLISTFACTURES>
2934 2934
</PORTAIL>'''
2935 2935
    with mock_data(content, 'GetListFactures'):
2936 2936
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoices/history?NameID=yyy')
2937 2937
    assert resp.json['err'] == 0
2938 2938
    assert resp.json['data'] == []
2939 2939

  
2940 2940

  
2941
@freezegun.freeze_time('2019-12-12')
2941 2942
def test_invoices_history_endpoint(app, resource):
2942 2943
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
2943 2944
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
2944 2945
    with open(filepath) as xml:
2945 2946
        content = (
2946 2947
            '''<PORTAIL>
2947 2948
    <GETLISTFACTURES>
2948 2949
        %s
......
3038 3039
    </GETLISTFACTURES>
3039 3040
</PORTAIL>'''
3040 3041
    with mock_data(content, 'GetListFactures'):
3041 3042
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42?NameID=yyy')
3042 3043
    assert resp.json['err_desc'] == "Wrong get-historical-invoices status"
3043 3044
    assert resp.json['err'] == 'get-historical-invoices-code-error--3'
3044 3045

  
3045 3046

  
3047
@freezegun.freeze_time('2019-12-12')
3046 3048
def test_invoice_endpoint_no_result(app, resource):
3047 3049
    resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
3048 3050
    assert resp.json['err_desc'] == "Person not found"
3049 3051
    assert resp.json['err'] == 'not-found'
3050 3052

  
3051 3053
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
3052 3054
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
3053 3055
    with open(filepath) as xml:
......
3059 3061
  </GETFACTURESAPAYER>
3060 3062
</PORTAIL>'''
3061 3063
        % invoices
3062 3064
    )
3063 3065
    with mock_data(content, 'GetFacturesaPayer'):
3064 3066
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-35?NameID=yyy')
3065 3067
    assert resp.json['err_desc'] == "Invoice not found"
3066 3068
    assert resp.json['err'] == 'not-found'
3069

  
3070
    with mock_data(content, 'GetFacturesaPayer'):
3071
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-44?NameID=yyy')
3072
    assert resp.json['err_desc'] == "Invoice not found"
3073
    assert resp.json['err'] == 'not-found'
3074

  
3067 3075
    content = (
3068 3076
        '''<PORTAIL>
3069 3077
    <GETLISTFACTURES>
3070 3078
        %s
3071 3079
    </GETLISTFACTURES>
3072 3080
</PORTAIL>'''
3073 3081
        % invoices
3074 3082
    )
3083
    with mock_data(content, 'GetListFactures'):
3084
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-35?NameID=yyy')
3085
    assert resp.json['err_desc'] == "Invoice not found"
3086
    assert resp.json['err'] == 'not-found'
3087

  
3075 3088
    with mock_data(content, 'GetListFactures'):
3076 3089
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-44?NameID=yyy')
3077 3090
    assert resp.json['err_desc'] == "Invoice not found"
3078 3091
    assert resp.json['err'] == 'not-found'
3079 3092

  
3080 3093

  
3081 3094
@freezegun.freeze_time('2019-12-04')
3082 3095
def test_invoice_endpoint(app, resource):
......
3188 3201
        resp = app.get(
3189 3202
            '/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42/pdf?NameID=yyy&nb_mounts_limit=not_a_number',
3190 3203
            status=404,
3191 3204
        )
3192 3205
    assert resp.json['err_desc'] == "nb_mounts_limit must be an integer"
3193 3206
    assert resp.json['err'] == 'bad-request'
3194 3207

  
3195 3208

  
3209
@freezegun.freeze_time('2019-12-13')
3196 3210
def test_invoice_pdf_endpoint_no_result(app, resource):
3197 3211
    resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pdf?NameID=yyy', status=404)
3198 3212
    assert resp.json['err_desc'] == "Person not found"
3199 3213
    assert resp.json['err'] == 'not-found'
3200 3214

  
3201 3215
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
3202 3216
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
3203 3217
    with open(filepath) as xml:
......
3311 3325

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

  
3318 3332

  
3333
@freezegun.freeze_time('2019-12-04')
3319 3334
def test_pay_invoice_endpoint_no_result(app, resource):
3320 3335
    payload = {
3321 3336
        'transaction_date': '2021-06-15T12:00:00',
3322 3337
        'transaction_id': 'foo',
3323 3338
    }
3324 3339
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
3325 3340
    with open(filepath) as xml:
3326 3341
        content = (
......
3331 3346
</PORTAIL>'''
3332 3347
            % xml.read()
3333 3348
        )
3334 3349
    with mock_data(content, 'GetFacturesaPayer'):
3335 3350
        resp = app.post_json('/caluire-axel/test/regie/MAREGIE/invoice/XXX-35/pay?NameID=yyy', params=payload)
3336 3351
    assert resp.json['err_desc'] == "Invoice not found"
3337 3352
    assert resp.json['err'] == 'not-found'
3338 3353

  
3354
    with mock_data(content, 'GetFacturesaPayer'):
3355
        resp = app.post_json('/caluire-axel/test/regie/MAREGIE/invoice/XXX-44/pay?NameID=yyy', params=payload)
3356
    assert resp.json['err_desc'] == "Invoice not found"
3357
    assert resp.json['err'] == 'not-found'
3358

  
3339 3359

  
3340 3360
def test_pay_invoice_endpoint_wrong_response(app, resource):
3341 3361
    payload = {
3342 3362
        'transaction_date': '2021-06-15T12:00:00',
3343 3363
        'transaction_id': 'foo',
3344 3364
    }
3345 3365
    with mock.patch(
3346 3366
        'passerelle.contrib.caluire_axel.models.CaluireAxel.get_invoice',
3347
-