Projet

Général

Profil

0001-caluire-axel-allow-to-call-invoice-endpoint-anonymou.patch

Nicolas Roche, 26 août 2021 12:49

Télécharger (6,62 ko)

Voir les différences:

Subject: [PATCH] caluire-axel: allow to call invoice endpoint anonymously
 (#56006)

 passerelle/contrib/caluire_axel/models.py | 14 +++-
 tests/test_caluire_axel.py                | 82 +++++++++++++++++++++++
 2 files changed, 93 insertions(+), 3 deletions(-)
passerelle/contrib/caluire_axel/models.py
1103 1103
        description=_('Get invoice details'),
1104 1104
        parameters={
1105 1105
            'NameID': {'description': _('Publik ID')},
1106 1106
            'regie_id': {'description': _('Regie identifier'), 'example_value': 'ENF'},
1107 1107
            'invoice_id': {'description': _('Invoice identifier'), 'example_value': 'IDFAM-42'},
1108 1108
            'nb_mounts_limit': {'description': _('Number of months of history'), 'example_value': '12'},
1109 1109
        },
1110 1110
    )
1111
    def invoice(self, request, regie_id, invoice_id, NameID, nb_mounts_limit='12'):
1112
        link = self.get_link(NameID)
1111
    def invoice(self, request, regie_id, invoice_id, NameID=None, nb_mounts_limit='12'):
1113 1112
        real_invoice_id = invoice_id.split('-')[-1]
1114 1113
        historical = invoice_id.startswith('historical-')
1114
        if historical:
1115
            invoice_id = invoice_id[len('historical-') :]
1116
        family_id, real_invoice_id = invoice_id.split('-')
1117
        if NameID:
1118
            link = self.get_link(NameID)
1119
            if link.family_id != family_id:
1120
                raise APIError(
1121
                    'Invoice does not belongs to linked family', err_code='bad-request', http_status=400
1122
                )
1115 1123
        invoice = self.get_invoice(
1116 1124
            regie_id=regie_id,
1117 1125
            invoice_id=real_invoice_id,
1118
            family_id=link.family_id,
1126
            family_id=family_id,
1119 1127
            historical=historical,
1120 1128
            nb_mounts_limit=nb_mounts_limit,
1121 1129
        )
1122 1130
        if invoice is None:
1123 1131
            raise APIError('Invoice not found', err_code='not-found')
1124 1132

  
1125 1133
        return {'data': invoice}
1126 1134

  
tests/test_caluire_axel.py
3010 3010
        operation.side_effect = AxelError('FooBar')
3011 3011
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
3012 3012
    assert resp.json['err_desc'] == "Axel error: FooBar"
3013 3013
    assert resp.json['err'] == 'error'
3014 3014

  
3015 3015

  
3016 3016
def test_invoice_endpoint_bad_request(app, resource):
3017 3017
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
3018
    resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-ZZZ-42?NameID=yyy', status=400)
3019
    assert resp.json['err_desc'] == "Invoice does not belongs to linked family"
3020
    assert resp.json['err'] == 'bad-request'
3021

  
3018 3022
    with mock_data(None, 'GetListFactures'):
3019 3023
        resp = app.get(
3020 3024
            '/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42?NameID=yyy&nb_mounts_limit=not_a_number',
3021 3025
            status=400,
3022 3026
        )
3023 3027
    assert resp.json['err_desc'] == "nb_mounts_limit must be an integer"
3024 3028
    assert resp.json['err'] == 'bad-request'
3025 3029

  
......
3164 3168
                'ECHEANCE': '2019-12-04',
3165 3169
                'EMISSION': '2019-11-12',
3166 3170
                'EXISTEPDF': True,
3167 3171
            },
3168 3172
        },
3169 3173
    }
3170 3174

  
3171 3175

  
3176
@freezegun.freeze_time('2019-12-04')
3177
def test_invoice_endpoint_anonymously(app, resource):
3178
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
3179
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
3180
    with open(filepath) as xml:
3181
        invoices = xml.read()
3182
    content = (
3183
        '''<PORTAIL>
3184
  <GETFACTURESAPAYER>
3185
    %s
3186
  </GETFACTURESAPAYER>
3187
</PORTAIL>'''
3188
        % invoices
3189
    )
3190
    with mock_data(content, 'GetFacturesaPayer'):
3191
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42')
3192
    assert resp.json['err'] == 0
3193
    assert resp.json['data'] == {
3194
        'id': 'XXX-42',
3195
        'display_id': '42',
3196
        'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
3197
        'amount': '4.94',
3198
        'total_amount': '44.94',
3199
        'amount_paid': '40.00',
3200
        'online_payment': True,
3201
        'created': '2019-11-12',
3202
        'pay_limit_date': '2019-12-04',
3203
        'has_pdf': True,
3204
        'paid': False,
3205
        'vendor': {
3206
            'caluire-axel': {
3207
                'IDFACTURE': 42,
3208
                'MONTANT': '44.94',
3209
                'ENCAISSE': '40.00',
3210
                'FACTURATION': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
3211
                'ECHEANCE': '2019-12-04',
3212
                'EMISSION': '2019-11-12',
3213
                'EXISTEPDF': True,
3214
            }
3215
        },
3216
    }
3217

  
3218
    content = (
3219
        '''<PORTAIL>
3220
    <GETLISTFACTURES>
3221
        %s
3222
    </GETLISTFACTURES>
3223
</PORTAIL>'''
3224
        % invoices
3225
    )
3226
    with mock_data(content, 'GetListFactures'):
3227
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-42')
3228
    assert resp.json['err'] == 0
3229
    assert resp.json['data'] == {
3230
        'id': 'historical-XXX-42',
3231
        'display_id': '42',
3232
        'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
3233
        'amount': 0,
3234
        'total_amount': '44.94',
3235
        'online_payment': False,
3236
        'created': '2019-11-12',
3237
        'pay_limit_date': '',
3238
        'has_pdf': True,
3239
        'paid': False,
3240
        'vendor': {
3241
            'caluire-axel': {
3242
                'IDFACTURE': 42,
3243
                'MONTANT': '44.94',
3244
                'ENCAISSE': '40.00',
3245
                'FACTURATION': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
3246
                'ECHEANCE': '2019-12-04',
3247
                'EMISSION': '2019-11-12',
3248
                'EXISTEPDF': True,
3249
            },
3250
        },
3251
    }
3252

  
3253

  
3172 3254
def test_invoice_pdf_endpoint_axel_error(app, resource):
3173 3255
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
3174 3256
    with mock.patch('passerelle.contrib.caluire_axel.schemas.get_factures_a_payer') as operation:
3175 3257
        operation.side_effect = AxelError('FooBar')
3176 3258
        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42/pdf?NameID=yyy', status=404)
3177 3259
    assert resp.json['err_desc'] == "Axel error: FooBar"
3178 3260
    assert resp.json['err'] == 'error'
3179 3261

  
3180
-