2017-03-24 16:28:39 +01:00
|
|
|
# tests/test_api_particulier.py
|
|
|
|
# Copyright (C) 2017 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/>.
|
|
|
|
|
2020-06-03 10:59:13 +02:00
|
|
|
import logging
|
2017-03-24 16:28:39 +01:00
|
|
|
|
|
|
|
import pytest
|
2020-05-11 09:21:48 +02:00
|
|
|
import requests
|
2020-12-10 15:03:57 +01:00
|
|
|
from django.urls import reverse
|
2017-03-24 16:28:39 +01:00
|
|
|
from httmock import HTTMock, response, urlmatch
|
2020-05-11 09:21:48 +02:00
|
|
|
|
2017-03-24 16:28:39 +01:00
|
|
|
from passerelle.apps.api_particulier.models import APIParticulier
|
2020-06-03 10:59:13 +02:00
|
|
|
from passerelle.base.models import ResourceLog
|
2022-03-18 09:46:37 +01:00
|
|
|
from tests.test_manager import login
|
|
|
|
from tests.utils import endpoint_get, make_resource
|
2017-03-24 16:28:39 +01:00
|
|
|
|
|
|
|
SVAIR_RESPONSE = {
|
|
|
|
"declarant1": {
|
|
|
|
"nom": "Martin",
|
|
|
|
"nomNaissance": "Martin",
|
|
|
|
"prenoms": "Pierre",
|
|
|
|
"dateNaissance": "22/03/1985",
|
|
|
|
},
|
|
|
|
"declarant2": {
|
|
|
|
"nom": "Martin",
|
|
|
|
"nomNaissance": "Honore",
|
|
|
|
"prenoms": "Marie",
|
|
|
|
"dateNaissance": "03/04/1986",
|
|
|
|
},
|
|
|
|
"foyerFiscal": {"annee": 2015, "adresse": "12 rue Balzac 75008 Paris"},
|
|
|
|
"dateRecouvrement": "10/10/2015",
|
|
|
|
"dateEtablissement": "08/07/2015",
|
|
|
|
"nombreParts": 2,
|
|
|
|
"situationFamille": "Marié(e)s",
|
|
|
|
"nombrePersonnesCharge": 2,
|
|
|
|
"revenuBrutGlobal": 29880,
|
|
|
|
"revenuImposable": 29880,
|
|
|
|
"impotRevenuNetAvantCorrections": 2165,
|
|
|
|
"montantImpot": 2165,
|
|
|
|
"revenuFiscalReference": 29880,
|
|
|
|
"anneeImpots": "2015",
|
|
|
|
"anneeRevenus": "2014",
|
|
|
|
}
|
|
|
|
|
|
|
|
CAF_FAMILLE = {
|
|
|
|
"adresse": {
|
|
|
|
"codePostalVille": "12345 CONDAT",
|
|
|
|
"complementIdentiteGeo": "ESCALIER B",
|
|
|
|
"identite": "Madame MARIE DUPONT",
|
|
|
|
"numeroRue": "123 RUE BIDON",
|
|
|
|
"pays": "FRANCE",
|
|
|
|
},
|
|
|
|
"allocataires": [
|
|
|
|
{"dateDeNaissance": "12111971", "nomPrenom": "MARIE DUPONT", "sexe": "F"},
|
|
|
|
{"dateDeNaissance": "18101969", "nomPrenom": "JEAN DUPONT", "sexe": "M"},
|
|
|
|
],
|
|
|
|
"annee": 2017,
|
|
|
|
"enfants": [{"dateDeNaissance": "11122016", "nomPrenom": "LUCIE DUPONT", "sexe": "F"}],
|
|
|
|
"mois": 4,
|
|
|
|
"quotientFamilial": 1754,
|
|
|
|
}
|
|
|
|
|
2021-05-19 17:14:40 +02:00
|
|
|
INTROSPECT = {
|
|
|
|
"_id": "1d99db5a-a099-4314-ad2f-2707c6b505a6",
|
|
|
|
"name": "Application de sandbox",
|
|
|
|
"scopes": [
|
|
|
|
"dgfip_avis_imposition",
|
|
|
|
"dgfip_adresse",
|
|
|
|
"cnaf_allocataires",
|
|
|
|
"cnaf_enfants",
|
|
|
|
"cnaf_adresse",
|
|
|
|
"cnaf_quotient_familial",
|
|
|
|
"mesri_statut_etudiant",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2017-03-24 16:28:39 +01:00
|
|
|
|
2020-01-08 17:27:58 +01:00
|
|
|
@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$', path=r'^/api/v2/avis-imposition$')
|
2020-05-25 17:08:47 +02:00
|
|
|
def api_particulier_v2_avis_imposition(url, request):
|
2017-03-24 16:28:39 +01:00
|
|
|
return response(200, SVAIR_RESPONSE, request=request)
|
|
|
|
|
2021-02-20 16:26:01 +01:00
|
|
|
|
2020-01-08 17:27:58 +01:00
|
|
|
@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$', path=r'^/api/v2/composition-familiale$')
|
2020-05-25 17:08:47 +02:00
|
|
|
def api_particulier_v2_situation_familiale(url, request):
|
2017-03-24 16:28:39 +01:00
|
|
|
return response(200, CAF_FAMILLE, request=request)
|
|
|
|
|
|
|
|
|
2021-05-19 17:14:40 +02:00
|
|
|
@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$', path=r'^/api/introspect$')
|
|
|
|
def api_particulier_introspect(url, request):
|
|
|
|
return response(200, INTROSPECT, request=request)
|
|
|
|
|
|
|
|
|
2020-01-08 17:27:58 +01:00
|
|
|
@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$')
|
2017-03-24 16:28:39 +01:00
|
|
|
def api_particulier_error_500(url, request):
|
2020-06-03 16:21:08 +02:00
|
|
|
return response(500, {'error': 500}, request=request)
|
2017-03-24 16:28:39 +01:00
|
|
|
|
|
|
|
|
2020-05-11 09:21:48 +02:00
|
|
|
@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$')
|
|
|
|
def api_particulier_connection_error(url, request):
|
|
|
|
raise requests.RequestException('connection timed-out')
|
|
|
|
|
|
|
|
|
2020-01-08 17:27:58 +01:00
|
|
|
@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$')
|
2017-03-24 16:28:39 +01:00
|
|
|
def api_particulier_error_not_json(url, request):
|
|
|
|
return response(200, 'something bad happened', request=request)
|
|
|
|
|
|
|
|
|
2020-01-08 17:27:58 +01:00
|
|
|
@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$')
|
2017-03-24 16:28:39 +01:00
|
|
|
def api_particulier_error_not_found(url, request):
|
|
|
|
return response(
|
|
|
|
404,
|
|
|
|
{
|
|
|
|
'error': 'not_found',
|
2022-03-17 16:54:24 +01:00
|
|
|
'message': 'Les paramètres fournis sont incorrects ou ne correspondent pas à un avis',
|
2017-03-24 16:28:39 +01:00
|
|
|
},
|
|
|
|
request=request,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-06-16 10:53:26 +02:00
|
|
|
@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$')
|
|
|
|
def api_particulier_error_not_found_caf(url, request):
|
|
|
|
return response(
|
|
|
|
404,
|
|
|
|
{'error': 'not_found', 'message': 'Dossier allocataire inexistant. Le document ne peut être édité.'},
|
|
|
|
request=request,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-07-28 12:03:05 +02:00
|
|
|
@urlmatch(netloc=r'^particulier.*\.api\.gouv\.fr$')
|
|
|
|
def api_particulier_error_not_found_deregistrated(url, request):
|
|
|
|
return response(
|
|
|
|
404,
|
|
|
|
{'error': 'not_found', 'message': 'Dossier radié. Le document ne peut être édité.'},
|
|
|
|
request=request,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-05-07 11:09:30 +02:00
|
|
|
@pytest.fixture
|
2017-03-24 16:28:39 +01:00
|
|
|
def mock_api_particulier():
|
2021-05-19 17:14:40 +02:00
|
|
|
with HTTMock(
|
|
|
|
api_particulier_v2_avis_imposition, api_particulier_v2_situation_familiale, api_particulier_introspect
|
|
|
|
):
|
2017-03-24 16:28:39 +01:00
|
|
|
yield None
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def resource(db):
|
|
|
|
return make_resource(
|
|
|
|
APIParticulier,
|
|
|
|
slug='test',
|
|
|
|
title='API Particulier Prod',
|
|
|
|
description='API Particulier Prod',
|
2019-02-15 15:11:59 +01:00
|
|
|
platform='test',
|
|
|
|
api_key='83c68bf0b6013c4daf3f8213f7212aa5',
|
|
|
|
)
|
2017-03-24 16:28:39 +01:00
|
|
|
|
|
|
|
|
2021-08-17 15:24:29 +02:00
|
|
|
vector = [
|
|
|
|
(
|
|
|
|
['impots_svair', 'avis-imposition'],
|
|
|
|
{
|
|
|
|
'numero_fiscal': '1234567890123',
|
|
|
|
'reference_avis': '3210987654321',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(['caf_famille', 'situation-familiale'], {'code_postal': 12, 'numero_allocataire': '0000015'}),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_error_500(app, resource, mock_api_particulier):
|
2017-03-24 16:28:39 +01:00
|
|
|
with HTTMock(api_particulier_error_500):
|
2021-02-20 16:26:01 +01:00
|
|
|
|
2017-03-24 16:28:39 +01:00
|
|
|
def do(endpoint, params):
|
|
|
|
resp = endpoint_get('/api-particulier/test/%s' % endpoint, app, resource, endpoint, params=params)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.json['err'] == 1
|
|
|
|
assert resp.json['data']['status_code'] == 500
|
2020-06-03 16:21:08 +02:00
|
|
|
assert resp.json['data']['code'] == 'non-200'
|
2021-02-20 16:26:01 +01:00
|
|
|
|
2017-03-24 16:28:39 +01:00
|
|
|
for endpoints, params in vector:
|
|
|
|
for endpoint in endpoints:
|
|
|
|
do(endpoint, params)
|
2021-08-17 15:24:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_not_json(app, resource, mock_api_particulier):
|
2017-03-24 16:28:39 +01:00
|
|
|
with HTTMock(api_particulier_error_not_json):
|
2021-02-20 16:26:01 +01:00
|
|
|
|
2017-03-24 16:28:39 +01:00
|
|
|
def do(endpoint, params):
|
|
|
|
resp = endpoint_get('/api-particulier/test/%s' % endpoint, app, resource, endpoint, params=params)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.json['err'] == 1
|
2020-01-14 19:18:28 +01:00
|
|
|
assert 'returned non-JSON content' in resp.json['err_desc']
|
2020-06-03 16:21:08 +02:00
|
|
|
assert resp.json['data']['code'] == 'non-json'
|
2021-02-20 16:26:01 +01:00
|
|
|
|
2017-03-24 16:28:39 +01:00
|
|
|
for endpoints, params in vector:
|
|
|
|
for endpoint in endpoints:
|
|
|
|
do(endpoint, params)
|
2021-08-17 15:24:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_not_found(app, resource, mock_api_particulier):
|
2017-03-24 16:28:39 +01:00
|
|
|
with HTTMock(api_particulier_error_not_found):
|
2021-02-20 16:26:01 +01:00
|
|
|
|
2017-03-24 16:28:39 +01:00
|
|
|
def do(endpoint, params):
|
|
|
|
resp = endpoint_get('/api-particulier/test/%s' % endpoint, app, resource, endpoint, params=params)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.json['err'] == 1
|
2020-07-28 12:03:05 +02:00
|
|
|
assert 'incorrects ou ne correspondent pas' in resp.json['err_desc']
|
2020-06-03 16:21:08 +02:00
|
|
|
assert resp.json['data']['code'] == 'not-found'
|
2021-02-20 16:26:01 +01:00
|
|
|
|
2020-05-11 09:21:48 +02:00
|
|
|
for endpoints, params in vector:
|
|
|
|
for endpoint in endpoints:
|
|
|
|
do(endpoint, params)
|
2021-08-17 15:24:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_connection_error(app, resource, mock_api_particulier):
|
2020-05-11 09:21:48 +02:00
|
|
|
with HTTMock(api_particulier_connection_error):
|
2021-02-20 16:26:01 +01:00
|
|
|
|
2020-05-11 09:21:48 +02:00
|
|
|
def do(endpoint, params):
|
|
|
|
resp = endpoint_get('/api-particulier/test/%s' % endpoint, app, resource, endpoint, params=params)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.json['err'] == 1
|
|
|
|
assert (
|
|
|
|
resp.json['err_desc']
|
|
|
|
== 'API-particulier platform "test" connection error: connection timed-out'
|
2021-02-20 16:26:01 +01:00
|
|
|
)
|
|
|
|
|
2017-03-24 16:28:39 +01:00
|
|
|
for endpoints, params in vector:
|
|
|
|
for endpoint in endpoints:
|
|
|
|
do(endpoint, params)
|
|
|
|
|
2021-08-17 15:24:29 +02:00
|
|
|
|
|
|
|
def test_numero_fiscal_too_short(app, resource, mock_api_particulier):
|
2020-07-01 16:57:45 +02:00
|
|
|
resp = endpoint_get(
|
|
|
|
'/api-particulier/test/avis-imposition',
|
|
|
|
app,
|
|
|
|
resource,
|
|
|
|
'avis-imposition',
|
|
|
|
params={
|
|
|
|
'numero_fiscal': ' 1234567890', # too short
|
|
|
|
'reference_avis': '3210987654321',
|
|
|
|
'user': 'John Doe',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.json['err'] == 1
|
|
|
|
assert resp.json['data'] is None
|
2021-08-28 13:18:42 +02:00
|
|
|
assert 'bad numero_fiscal' in resp.json['err_desc']
|
2021-08-17 15:24:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_reference_avis_too_short(app, resource, mock_api_particulier):
|
2020-07-01 16:57:45 +02:00
|
|
|
resp = endpoint_get(
|
|
|
|
'/api-particulier/test/avis-imposition',
|
|
|
|
app,
|
|
|
|
resource,
|
|
|
|
'avis-imposition',
|
|
|
|
params={
|
|
|
|
'numero_fiscal': '1234567890123',
|
|
|
|
'reference_avis': '32109876543 ', # too short
|
|
|
|
'user': 'John Doe',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.json['err'] == 1
|
|
|
|
assert resp.json['data'] is None
|
2021-08-28 13:18:42 +02:00
|
|
|
assert 'bad reference_avis' in resp.json['err_desc']
|
2020-07-01 16:57:45 +02:00
|
|
|
|
2017-03-24 16:28:39 +01:00
|
|
|
|
2020-05-25 17:08:47 +02:00
|
|
|
def test_avis_imposition(app, resource, mock_api_particulier):
|
2017-03-24 16:28:39 +01:00
|
|
|
resp = endpoint_get(
|
2020-05-25 17:08:47 +02:00
|
|
|
'/api-particulier/test/avis-imposition',
|
2017-03-24 16:28:39 +01:00
|
|
|
app,
|
|
|
|
resource,
|
2020-05-25 17:08:47 +02:00
|
|
|
'avis-imposition',
|
2017-03-24 16:28:39 +01:00
|
|
|
params={
|
2020-07-01 16:57:45 +02:00
|
|
|
'numero_fiscal': '1234567890123',
|
|
|
|
'reference_avis': '3210987654321',
|
|
|
|
'user': 'John Doe',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.json['data']['montantImpot'] == 2165
|
|
|
|
assert resp.json['err'] == 0
|
|
|
|
|
|
|
|
resp = endpoint_get(
|
|
|
|
'/api-particulier/test/avis-imposition',
|
|
|
|
app,
|
|
|
|
resource,
|
|
|
|
'avis-imposition',
|
|
|
|
params={
|
|
|
|
'numero_fiscal': '1234567890123X', # 14 chars : will be cutted
|
|
|
|
'reference_avis': '3210987654321X', # idem
|
2017-03-24 16:28:39 +01:00
|
|
|
'user': 'John Doe',
|
|
|
|
},
|
|
|
|
)
|
2020-07-01 16:57:45 +02:00
|
|
|
assert resp.status_code == 200
|
2017-03-24 16:28:39 +01:00
|
|
|
assert resp.json['data']['montantImpot'] == 2165
|
2020-07-01 16:57:45 +02:00
|
|
|
assert resp.json['err'] == 0
|
2017-03-24 16:28:39 +01:00
|
|
|
|
|
|
|
|
2020-05-25 17:08:47 +02:00
|
|
|
def test_situation_familiale(app, resource, mock_api_particulier):
|
2021-06-08 10:59:12 +02:00
|
|
|
params = {
|
|
|
|
'code_postal': '99148',
|
|
|
|
'numero_allocataire': '0000354',
|
|
|
|
'user': 'John Doe',
|
|
|
|
}
|
2017-03-24 16:28:39 +01:00
|
|
|
resp = endpoint_get(
|
2021-06-08 10:59:12 +02:00
|
|
|
'/api-particulier/test/situation-familiale', app, resource, 'situation-familiale', params=params
|
2017-03-24 16:28:39 +01:00
|
|
|
)
|
|
|
|
assert resp.json['data']['adresse']['codePostalVille'] == '12345 CONDAT'
|
|
|
|
|
2021-06-08 10:59:12 +02:00
|
|
|
params['numero_allocataire'] = '11'
|
|
|
|
resp = endpoint_get(
|
|
|
|
'/api-particulier/test/situation-familiale', app, resource, 'situation-familiale', params=params
|
|
|
|
)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.json['err'] == 1
|
|
|
|
assert '7 digits' in resp.json['err_desc']
|
|
|
|
|
|
|
|
params['numero_allocataire'] = '123456a'
|
|
|
|
resp = endpoint_get(
|
|
|
|
'/api-particulier/test/situation-familiale', app, resource, 'situation-familiale', params=params
|
|
|
|
)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.json['err'] == 1
|
|
|
|
assert '7 digits' in resp.json['err_desc']
|
|
|
|
|
2022-07-18 09:33:30 +02:00
|
|
|
# last letter truncated automatically
|
|
|
|
params['numero_allocataire'] = '1234567a'
|
|
|
|
resp = endpoint_get(
|
|
|
|
'/api-particulier/test/situation-familiale', app, resource, 'situation-familiale', params=params
|
|
|
|
)
|
|
|
|
assert resp.json['data']['adresse']['codePostalVille'] == '12345 CONDAT'
|
|
|
|
# cleaned data is also inlcuded in the response
|
|
|
|
assert resp.json['data']['numero_allocataire'] == '1234567'
|
|
|
|
assert resp.json['data']['code_postal'] == params['code_postal']
|
|
|
|
|
2021-06-08 10:59:12 +02:00
|
|
|
params['code_postal'] = ' '
|
|
|
|
resp = endpoint_get(
|
|
|
|
'/api-particulier/test/situation-familiale', app, resource, 'situation-familiale', params=params
|
|
|
|
)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
assert resp.json['err'] == 1
|
|
|
|
assert 'missing' in resp.json['err_desc']
|
|
|
|
|
2017-03-24 16:28:39 +01:00
|
|
|
|
2020-11-20 20:03:43 +01:00
|
|
|
def test_detail_page(app, resource, admin_user):
|
|
|
|
login(app)
|
2017-03-24 16:28:39 +01:00
|
|
|
response = app.get(
|
|
|
|
reverse(
|
|
|
|
'view-connector',
|
|
|
|
kwargs={
|
|
|
|
'connector': 'api-particulier',
|
|
|
|
'slug': 'test',
|
|
|
|
},
|
|
|
|
)
|
2021-02-20 16:26:01 +01:00
|
|
|
)
|
2020-01-14 19:18:28 +01:00
|
|
|
assert 'API Particulier Prod' in response.text
|
|
|
|
assert 'family allowance' in response.text
|
|
|
|
assert 'fiscal information' in response.text
|
2020-06-03 10:59:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'mock,should_log',
|
|
|
|
[
|
|
|
|
(api_particulier_error_not_found, False),
|
|
|
|
(api_particulier_error_500, True),
|
2020-07-28 12:03:05 +02:00
|
|
|
(api_particulier_error_not_json, True),
|
|
|
|
(api_particulier_error_not_found_caf, False),
|
|
|
|
(api_particulier_error_not_found_deregistrated, False),
|
2020-06-03 10:59:13 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_api_particulier_dont_log_not_found(app, resource, mock, should_log):
|
|
|
|
with HTTMock(mock):
|
2022-03-17 16:47:33 +01:00
|
|
|
endpoint_get(
|
2020-06-03 10:59:13 +02:00
|
|
|
'/api-particulier/test/avis-imposition',
|
|
|
|
app,
|
|
|
|
resource,
|
|
|
|
'avis-imposition',
|
|
|
|
params={
|
2020-07-01 16:57:45 +02:00
|
|
|
'numero_fiscal': '1234567890123',
|
|
|
|
'reference_avis': '3210987654321',
|
2020-06-03 10:59:13 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
logs = ResourceLog.objects.all()
|
|
|
|
if should_log:
|
2021-08-17 15:24:29 +02:00
|
|
|
assert logs.count() == 3
|
2020-06-03 10:59:13 +02:00
|
|
|
assert logs.filter(levelno=logging.ERROR).count() == 1
|
|
|
|
else:
|
2021-08-17 15:24:29 +02:00
|
|
|
assert logs.count() == 2
|
2020-06-03 10:59:13 +02:00
|
|
|
assert not logs.filter(levelno=logging.ERROR).exists()
|
2021-05-19 17:14:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_scopes(app, resource, mock_api_particulier):
|
2021-06-10 15:09:13 +02:00
|
|
|
assert not resource.accessible_scopes
|
2021-05-19 17:14:40 +02:00
|
|
|
resp = endpoint_get('/api-particulier/test/scopes', app, resource, 'scopes')
|
|
|
|
assert resp.json['data'] == [
|
|
|
|
"cnaf_adresse",
|
|
|
|
"cnaf_allocataires",
|
|
|
|
"cnaf_enfants",
|
|
|
|
"cnaf_quotient_familial",
|
|
|
|
"dgfip_adresse",
|
|
|
|
"dgfip_avis_imposition",
|
|
|
|
"mesri_statut_etudiant",
|
|
|
|
]
|
2021-06-10 15:09:13 +02:00
|
|
|
assert len(APIParticulier.objects.get(slug=resource.slug).accessible_scopes) == 7
|
|
|
|
|
|
|
|
|
|
|
|
def test_scopes_error(app, resource):
|
|
|
|
resource.accessible_scopes = ['some', 'scopes']
|
|
|
|
resource.save()
|
|
|
|
assert len(APIParticulier.objects.get(slug=resource.slug).accessible_scopes) == 2
|
|
|
|
with HTTMock(api_particulier_error_500):
|
|
|
|
resp = endpoint_get('/api-particulier/test/scopes', app, resource, 'scopes')
|
|
|
|
assert resp.json['err']
|
|
|
|
assert len(APIParticulier.objects.get(slug=resource.slug).accessible_scopes) == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_cron(resource):
|
|
|
|
assert not resource.accessible_scopes
|
|
|
|
with HTTMock(api_particulier_introspect):
|
|
|
|
resource.daily()
|
|
|
|
assert len(resource.accessible_scopes) == 7
|
|
|
|
|
|
|
|
with HTTMock(api_particulier_error_500):
|
|
|
|
resource.daily()
|
|
|
|
assert len(resource.accessible_scopes) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_manager_creation(db, app, admin_user, resource):
|
|
|
|
app = login(app)
|
|
|
|
path = '/manage/%s/add' % resource.get_connector_slug()
|
|
|
|
|
|
|
|
resp = app.get(path)
|
|
|
|
resp.form['slug'] = 'test2'
|
|
|
|
resp.form['title'] = 'API Particulier Test2'
|
|
|
|
resp.form['description'] = 'API Particulier Test2'
|
|
|
|
resp.form['platform'] = 'test'
|
|
|
|
resp.form['api_key'] = '83c68bf0b6013c4daf3f8213f7212aa5'
|
|
|
|
with HTTMock(api_particulier_introspect):
|
|
|
|
resp = resp.form.submit()
|
|
|
|
assert len(APIParticulier.objects.get(slug='test2').accessible_scopes) == 7
|
|
|
|
resp = resp.follow()
|
|
|
|
assert len(resp.html.find('ul', {'class': 'accessible-scopes'}).find_all('li')) == 7
|
|
|
|
|
|
|
|
resp = app.get(path)
|
|
|
|
resp.form['slug'] = 'test3'
|
|
|
|
resp.form['title'] = 'API Particulier Test3'
|
|
|
|
resp.form['description'] = 'API Particulier Test3'
|
|
|
|
resp.form['platform'] = 'test'
|
|
|
|
resp.form['api_key'] = '83c68bf0b6013c4daf3f8213f7212aa5'
|
|
|
|
with HTTMock(api_particulier_error_500):
|
|
|
|
resp = resp.form.submit()
|
|
|
|
assert not APIParticulier.objects.get(slug='test3').accessible_scopes
|
|
|
|
resp = resp.follow()
|
|
|
|
assert len(resp.html.find('ul', {'class': 'accessible-scopes'}).find_all('li')) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_manager_edition(db, app, admin_user, resource):
|
|
|
|
app = login(app)
|
|
|
|
path = '/%s/%s/' % (resource.get_connector_slug(), resource.slug)
|
|
|
|
|
|
|
|
assert not APIParticulier.objects.get(slug=resource.slug).accessible_scopes
|
|
|
|
with HTTMock(api_particulier_introspect):
|
|
|
|
resp = app.get(path)
|
|
|
|
assert len(resp.html.find('ul', {'class': 'accessible-scopes'}).find_all('li')) == 0
|
|
|
|
|
|
|
|
path = '/manage/%s/%s/edit' % (resource.get_connector_slug(), resource.slug)
|
|
|
|
resp = app.get(path)
|
|
|
|
with HTTMock(api_particulier_introspect):
|
|
|
|
resp = resp.form.submit()
|
|
|
|
assert len(APIParticulier.objects.get(slug=resource.slug).accessible_scopes) == 7
|
|
|
|
resp = resp.follow()
|
|
|
|
assert len(resp.html.find('ul', {'class': 'accessible-scopes'}).find_all('li')) == 7
|
|
|
|
|
|
|
|
path = '/manage/%s/%s/edit' % (resource.get_connector_slug(), resource.slug)
|
|
|
|
resp = app.get(path)
|
|
|
|
with HTTMock(api_particulier_error_500):
|
|
|
|
resp = resp.form.submit()
|
|
|
|
assert not APIParticulier.objects.get(slug=resource.slug).accessible_scopes
|
|
|
|
resp = resp.follow()
|
|
|
|
assert len(resp.html.find('ul', {'class': 'accessible-scopes'}).find_all('li')) == 0
|