passerelle/tests/test_avis_imposition.py

191 lines
6.1 KiB
Python

# passerelle - uniform access to multiple data sources and services
# Copyright (C) 2016 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import re
import pytest
import requests
import responses
from passerelle.apps.avis_imposition import models
from . import utils
from .test_manager import login
# Content from the form page
with open('tests/data/avis-imposition.html') as fd:
html = fd.read()
# Contents from the submit result page
with open('tests/data/avis-imposition.json') as fd:
cases = json.load(fd)
def responses_add(*args, url=re.compile('https://cfsmsp.impots.gouv.fr/secavis/.*'), **kwargs):
responses.upsert(*args, url=url, **kwargs)
@pytest.fixture
def avis_imposition(db, settings):
from django.core.cache import close_caches
settings.CACHES['default'] = settings.CACHES['dummy']
close_caches()
with responses.mock:
responses_add(responses.GET, body=html)
yield utils.make_resource(models.AvisImposition, slug='test')
@responses.activate
@pytest.mark.parametrize(
'data',
cases,
ids=[
''.join([data['result']['declarant1']['prenom'], data['result']['declarant1']['nom']])
for data in cases
],
)
def test_ok(avis_imposition, data, app):
responses_add(responses.POST, body=data['response'])
response = utils.endpoint_get(
expected_url='/avis-imposition/test/verify',
app=app,
resource=avis_imposition,
endpoint='verify',
params={
'numero_fiscal': data['numero_fiscal'],
'reference_avis': data['reference_avis'],
},
)
assert response.json['err'] == 0
assert response.json['data'] == data['result']
@responses.activate
def test_not_found(avis_imposition, app):
responses_add(responses.GET, body=html)
responses_add(responses.POST, body=html)
response = utils.endpoint_get(
expected_url='/avis-imposition/test/verify',
app=app,
resource=avis_imposition,
endpoint='verify',
params={
'numero_fiscal': '1' * 13,
'reference_avis': '2' * 13,
},
)
assert response.json['err'] == 1
assert response.json['err_desc'] == 'These fiscal number and tax notice number are unknown.'
@responses.activate
@pytest.mark.parametrize(
'response_kwargs',
[
{'method_or_response': responses.GET, 'body': requests.RequestException('boom!')},
{'method_or_response': responses.GET, 'status': 500},
{'method_or_response': responses.POST, 'body': requests.RequestException('boom!')},
{'method_or_response': responses.POST, 'status': 500},
],
)
def test_request_error(avis_imposition, app, response_kwargs):
responses_add(**response_kwargs)
response = utils.endpoint_get(
expected_url='/avis-imposition/test/verify',
app=app,
resource=avis_imposition,
endpoint='verify',
params={
'numero_fiscal': '1234567890123',
'reference_avis': '0987654321',
},
)
assert response.json['err'] == 1
assert response.json['err_desc'] == 'service-is-down'
@responses.activate
def test_fake_data(avis_imposition, app, settings, admin_user):
settings.AVIS_IMPOSITION_FAKE_DATA = [
{
'numero_fiscal': '1' * 13,
'reference_avis': '2' * 13,
"dateEtablissement": "09/12/2019",
"dateEtablissement_iso": "2019-12-09",
"dateRecouvrement": "31/12/2019",
"dateRecouvrement_iso": "2019-12-31",
"dateEtablissement_year": 2019,
"dateRecouvrement_year": 2019,
"declarant1": {
"dateNaissance": "01/01/1970",
"dateNaissance_iso": "1970-01-01",
"nom": "DOE",
"nomNaissance": "DOE",
"prenom": "JOHN",
},
"declarant2": {
"dateNaissance": "",
"dateNaissance_iso": "",
"nom": "DOE",
"nomNaissance": "DOE",
"prenom": "JANE",
},
"foyerFiscal": {
"adresse": "R\u00c9SIDENCE DU CALVAIRE RUE VICTOR HUGO 75014 PARIS",
"adresse1": "R\u00c9SIDENCE DU CALVAIRE",
"adresse2": "RUE VICTOR HUGO",
"adresse3": "75014 PARIS",
"year": 2022,
},
"impotRevenuNetAvantCorrections": 112,
"montantImpot": "Nonimposable",
"nombreParts": 4.0,
"nombrePersonnesCharge": 4.0,
"revenuBrutGlobal": 48473,
"revenuFiscalReference": 48473,
"revenuImposable": 48473,
"situationFamille": "Pacs\u00e9(e)s",
"situationFamille_simple": "pacs/mariage",
"situationPartielle": "",
}
]
response = utils.endpoint_get(
expected_url='/avis-imposition/test/verify',
app=app,
resource=avis_imposition,
endpoint='verify',
params={
'numero_fiscal': '1' * 13,
'reference_avis': '2' * 13,
},
)
assert response.json['err'] == 0
assert response.json['data'] == settings.AVIS_IMPOSITION_FAKE_DATA[0]
# Check that test data are displayed on the backoffice page
app = login(app)
response = app.get('/avis-imposition/test/')
assert 'Test datas' in response
assert [
[td.text() for td in tr.find('td').items()]
for tr in response.pyquery('table').eq(0).find('tbody tr').items()
] == [['1111111111111', '2222222222222', '48473\xa0', 'Test']]