solis: handle demandeUnitaire and search by index (#21476)

This commit is contained in:
Thomas NOËL 2018-01-29 16:44:42 +01:00
parent 2b21281568
commit 349ccad78c
2 changed files with 86 additions and 7 deletions

View File

@ -104,10 +104,12 @@ class Solis(BaseResource):
(response.status_code, response.content))
return response.json().get('token')
def apa_get_information(self, information, user_id=None, code=None, token=None):
def apa_get_information(self, information, user_id=None, code=None, token=None, index=None):
if token is None:
token = self.apa_token(user_id, code)
endpoint = 'asg/apa/' + information + '/' + token
if index:
endpoint += '/' + index
response = self.request(endpoint)
if response.status_code != 200:
raise APIError('error status:%s, content:%r' %
@ -183,21 +185,35 @@ class Solis(BaseResource):
'information': {
'description': _('exportDonneesIndividu, consultationDeMesDroits, '
'suiviDemandeEnInstruction, suiviDemandeHistorique, '
'propositionPlanAide'),
'propositionPlanAide, demandeUnitaire'),
'example_value': 'consultationDeMesDroits',
},
'index': {
'description': _('mandatory if information is "demandeUnitaire"'),
'example_value': '87123'
}
})
def apa_user_info(self, request, name_id, user_id, information='exportDonneesIndividu'):
def apa_user_info(self, request, name_id, user_id, information='exportDonneesIndividu',
index=None):
if information == 'demandeUnitaire' and index is None:
raise APIError('index mandatory if information=demandeUnitaire', http_status=400)
try:
link = SolisAPALink.objects.get(resource=self, name_id=name_id, user_id=user_id)
except SolisAPALink.DoesNotExist:
raise APIError('unknown link')
response = self.apa_get_information(information=information, user_id=user_id, code=link.code)
response = self.apa_get_information(information=information, user_id=user_id, code=link.code,
index=index if information == 'demandeUnitaire' else None)
if information == 'exportDonneesIndividu':
text = get_template(self.text_template_name).render(response).strip()
if text != link.text:
link.text = text
link.save()
elif index is not None and information != 'demandeUnitaire':
# search index in response
for resp in response.get('demandeAsg') or []:
if str(resp.get('demande', {}).get('indexDemande')) == index:
return {'data': {'demandeAsg': resp}}
raise APIError('cannot find indexDemande=%s in demandeAsg list' % index)
return {'data': response}
@endpoint(name='apa-users', perm='can_access',

View File

@ -17,11 +17,12 @@ APATOKEN = '''{"token":"1c2562e6-b0a9-4bcf-b669-e33a42397147","endDate":"2017-10
APATOKEN_403 = '''[{"logref":"db15cb8a-4d05-4e4f-b4e1-44ec39dc11e3","message":"Erreur d'authentification m\xc3\xa9tier ASG APA: Code confidentiel non valide pour l'individu 2823255","links":[]}]'''
APAINFOS = {
'exportDonneesIndividu': '{"individu":{"civilite":"Mme","nomUsuel":"PYPPENNE","nomNaissance":"NPYNEZ","prenom":"Pecile","dateNaissance":"1922-12-17","contact":{"telephone":"0344480774","mail":""},"adresse":{"complementDestinataire":"compl dest","numeroLieu":"38","natureLieu":null,"nomLieu":"RUE MARTIN","complementLieu":"MARPA LES NACRES - APPARTEMENT 9","finLieu":"fin adresse","codePostal":"80370","commune":"BERNAVILLE"},"tutelles":{"tutelle":[{"type":"Organisme","identite":"Association Tut\xc3\xa9laire de la Somme","mesure":null,"natureAccord":"Juridique","dateEffet":"2014-01-01","dateFin":null,"adresse":{"numeroLieu":"21","natureLieu":null,"nomLieu":"RUE SULLY","complementLieu":"BP 11660","finLieu":"","codePostal":"80016","commune":"AMIENS","cedex":"1"}},{"type":"Individu/Particulier","identite":"Ehmet TYEP","mesure":"Curatelle simple","natureAccord":"Juridique","dateEffet":"2017-01-01","dateFin":"2017-12-31","adresse":{"numeroLieu":"89","natureLieu":null,"nomLieu":"AVENUE LEON BLUM","complementLieu":"","finLieu":"","codePostal":"80100","commune":"ABBEVILLE","cedex":""}},{"type":"Individu/Particulier","identite":"Esg TYTEYP PE PEPPOXE OEX","mesure":null,"natureAccord":null,"dateEffet":null,"dateFin":null,"adresse":{"numeroLieu":"1","natureLieu":null,"nomLieu":"BOULEVARD DU PORT","complementLieu":"CAD","finLieu":"","codePostal":"80000","commune":"AMIENS","cedex":""}}]},"apa":{"classotheque":"05-2834","centreAutonomie":"Centre Autonomie nord ouest"}}}',
'consultationDeMesDroits': '{"demandeAsg":[{"demande":{"type":"Allocation Personnalis\xc3\xa9e Autonomie","nature":"Domicile"},"droit":{"dateDebut":"2017-01-05","dateFin":"2019-01-31"},"complementDossier":{"dateDepot":"2016-11-15","dateArrivee":"2016-11-16","dateDossierComplet":"2016-11-17"},"gir":{"type":"Synth\xc3\xa8se","gir":3,"dateEvaluation":"2017-02-01"},"suivi":{"instructeur":{"civilite":"madame","nom":"ZEPEQPE","prenom":"EPOZOE","telephone":"0344974383","mail":"e.zepeqpe@xoppe.pp"},"dateDecision":"2017-01-05"},"bilan":{"pourcentageTM":2.1973443031311035},"prestationAccordeeAsg":[{"prestation":"Accueil de jour GIR 1-2","periode":{"dateEffet":"2017-01-05","dateFin":"2019-01-31"},"tiers":{"type":"Ind\xc3\xa9termin\xc3\xa9","identite":null,"tarif":null,"quantitatif":null},"quantite":0,"montant":{"accorde":0,"participation":0,"verse":0},"attributaire":{"type":"Tuteur","identite":"Association Tut\xc3\xa9laire de la Somme"}},{"prestation":"Articles d\'hygi\xc3\xa8ne forfait 45\xc2\x80","periode":{"dateEffet":"2017-01-05","dateFin":null},"tiers":{"type":"Ind\xc3\xa9termin\xc3\xa9","identite":null,"tarif":45,"quantitatif":"Mois"},"quantite":1,"montant":{"accorde":45,"participation":1.68,"verse":43.32},"attributaire":{"type":"B\xc3\xa9n\xc3\xa9ficiaire","identite":"PYPPENNE Pecile"}},{"prestation":"Petite structure","periode":{"dateEffet":"2017-01-05","dateFin":"2019-01-31"},"tiers":{"type":"Etablissement","identite":"MARPA LES NACRES","tarif":null,"quantitatif":null},"quantite":0,"montant":{"accorde":440.42,"participation":7.68,"verse":432.74},"attributaire":{"type":"Etablissement","identite":"MARPA LES NACRES"}},{"prestation":"Aide humaine prestataire","periode":{"dateEffet":"2017-01-05","dateFin":"2019-01-31"},"tiers":{"type":"Prestataire","identite":"COMMUNAUTE DE COMMUNES DU TERRITOIRE NORD PICARDIE BERNAVILLE","tarif":19,"quantitatif":"Heure(s)"},"quantite":45.5,"montant":{"accorde":864.5,"participation":18.93,"verse":845.57},"attributaire":{"type":"Prestataire","identite":"COMMUNAUTE DE COMMUNES DU TERRITOIRE NORD PICARDIE BERNAVILLE"}},{"prestation":"Articles d\'hygi\xc3\xa8ne forfait 90\xc2\x80","periode":{"dateEffet":"2017-01-05","dateFin":null},"tiers":{"type":"Ind\xc3\xa9termin\xc3\xa9","identite":null,"tarif":90,"quantitatif":"Mois"},"quantite":1,"montant":{"accorde":90,"participation":3.35,"verse":86.65},"attributaire":{"type":"B\xc3\xa9n\xc3\xa9ficiaire","identite":"PYPPENNE Pecile"}}]}]}',
'consultationDeMesDroits': '{"demandeAsg":[{"demande":{"indexDemande":42,"type":"Allocation Personnalis\xc3\xa9e Autonomie","nature":"Domicile"},"droit":{"dateDebut":"2017-01-05","dateFin":"2019-01-31"},"complementDossier":{"dateDepot":"2016-11-15","dateArrivee":"2016-11-16","dateDossierComplet":"2016-11-17"},"gir":{"type":"Synth\xc3\xa8se","gir":3,"dateEvaluation":"2017-02-01"},"suivi":{"instructeur":{"civilite":"madame","nom":"ZEPEQPE","prenom":"EPOZOE","telephone":"0344974383","mail":"e.zepeqpe@xoppe.pp"},"dateDecision":"2017-01-05"},"bilan":{"pourcentageTM":2.1973443031311035},"prestationAccordeeAsg":[{"prestation":"Accueil de jour GIR 1-2","periode":{"dateEffet":"2017-01-05","dateFin":"2019-01-31"},"tiers":{"type":"Ind\xc3\xa9termin\xc3\xa9","identite":null,"tarif":null,"quantitatif":null},"quantite":0,"montant":{"accorde":0,"participation":0,"verse":0},"attributaire":{"type":"Tuteur","identite":"Association Tut\xc3\xa9laire de la Somme"}},{"prestation":"Articles d\'hygi\xc3\xa8ne forfait 45\xc2\x80","periode":{"dateEffet":"2017-01-05","dateFin":null},"tiers":{"type":"Ind\xc3\xa9termin\xc3\xa9","identite":null,"tarif":45,"quantitatif":"Mois"},"quantite":1,"montant":{"accorde":45,"participation":1.68,"verse":43.32},"attributaire":{"type":"B\xc3\xa9n\xc3\xa9ficiaire","identite":"PYPPENNE Pecile"}},{"prestation":"Petite structure","periode":{"dateEffet":"2017-01-05","dateFin":"2019-01-31"},"tiers":{"type":"Etablissement","identite":"MARPA LES NACRES","tarif":null,"quantitatif":null},"quantite":0,"montant":{"accorde":440.42,"participation":7.68,"verse":432.74},"attributaire":{"type":"Etablissement","identite":"MARPA LES NACRES"}},{"prestation":"Aide humaine prestataire","periode":{"dateEffet":"2017-01-05","dateFin":"2019-01-31"},"tiers":{"type":"Prestataire","identite":"COMMUNAUTE DE COMMUNES DU TERRITOIRE NORD PICARDIE BERNAVILLE","tarif":19,"quantitatif":"Heure(s)"},"quantite":45.5,"montant":{"accorde":864.5,"participation":18.93,"verse":845.57},"attributaire":{"type":"Prestataire","identite":"COMMUNAUTE DE COMMUNES DU TERRITOIRE NORD PICARDIE BERNAVILLE"}},{"prestation":"Articles d\'hygi\xc3\xa8ne forfait 90\xc2\x80","periode":{"dateEffet":"2017-01-05","dateFin":null},"tiers":{"type":"Ind\xc3\xa9termin\xc3\xa9","identite":null,"tarif":90,"quantitatif":"Mois"},"quantite":1,"montant":{"accorde":90,"participation":3.35,"verse":86.65},"attributaire":{"type":"B\xc3\xa9n\xc3\xa9ficiaire","identite":"PYPPENNE Pecile"}}]}]}',
'suiviDemandeEnInstruction': '{"demandeAsg":[]}',
'suiviDemandeHistorique': '{"demandeAsg":[{"demande":{"type":"Allocation Personnalis\xc3\xa9e Autonomie","nature":"Domicile"},"droit":{"dateDebut":"2013-03-01","dateFin":"2013-06-19"},"complementDossier":{"dateArrivee":null,"dateDossierComplet":"2012-10-25"},"suivi":{"decision":"Accord","dateDecision":"2013-02-12"}},{"demande":{"type":"Allocation Personnalis\xc3\xa9e Autonomie","nature":"Domicile"},"droit":{"dateDebut":"2013-06-20","dateFin":"2016-03-31"},"complementDossier":{"dateArrivee":null,"dateDossierComplet":"2012-10-25"},"suivi":{"decision":"Accord","dateDecision":"2013-06-25"}},{"demande":{"type":"Allocation Personnalis\xc3\xa9e Autonomie","nature":"Domicile"},"droit":{"dateDebut":"2016-04-01","dateFin":"2017-01-04"},"complementDossier":{"dateArrivee":"2016-06-06","dateDossierComplet":"2016-06-06"},"suivi":{"decision":"Accord","dateDecision":"2016-06-14"}}]}',
'propositionPlanAide': '{"demandeAsg":[{"demande":{"type":"Allocation Personnalis\\u00e9e Autonomie","indexDemande":111091,"nature":"Domicile"},"droit":{"dateDebut":"2016-08-23","dateFin":"2018-08-31"},"planAide":{"commentaire":"","prestationsPlanAide":{"prestationPlanAide":[{"tiers":{"tarif":12.8,"identite":"CCAS DE MERS LES BAINS","quantitatif":"Heure(s)","type":"Prestataire"},"quantite":84,"montant":1075.2,"prestation":"Aide humaine mandataire"},{"tiers":{"tarif":90,"identite":null,"quantitatif":"Mois","type":"Ind\\u00e9termin\\u00e9"},"quantite":1,"montant":90,"prestation":"Articles d\'hygi\\u00e8ne forfait 90"}]},"dateReponse":null,"datePropositionPlan":null,"avis":""},"complementDossier":{"dateDepot":"2016-06-22"},"suivi":{"dateDecision":"2016-08-23"}}]}',
'suiviDemandeHistorique': '{"demandeAsg":[{"demande":{"indexDemande":42,"type":"Allocation Personnalis\xc3\xa9e Autonomie","nature":"Domicile"},"droit":{"dateDebut":"2013-03-01","dateFin":"2013-06-19"},"complementDossier":{"dateArrivee":null,"dateDossierComplet":"2012-10-25"},"suivi":{"decision":"Accord","dateDecision":"2013-02-12"}},{"demande":{"indexDemande":43,"type":"Allocation Personnalis\xc3\xa9e Autonomie","nature":"Domicile"},"droit":{"dateDebut":"2013-06-20","dateFin":"2016-03-31"},"complementDossier":{"dateArrivee":null,"dateDossierComplet":"2012-10-25"},"suivi":{"decision":"Accord","dateDecision":"2013-06-25"}},{"demande":{"indexDemande":44,"type":"Allocation Personnalis\xc3\xa9e Autonomie","nature":"Domicile"},"droit":{"dateDebut":"2016-04-01","dateFin":"2017-01-04"},"complementDossier":{"dateArrivee":"2016-06-06","dateDossierComplet":"2016-06-06"},"suivi":{"decision":"Accord","dateDecision":"2016-06-14"}}]}',
'propositionPlanAide': '{"demandeAsg":[{"demande":{"type":"Allocation Personnalis\\u00e9e Autonomie","indexDemande":42,"nature":"Domicile"},"droit":{"dateDebut":"2016-08-23","dateFin":"2018-08-31"},"planAide":{"commentaire":"","prestationsPlanAide":{"prestationPlanAide":[{"tiers":{"tarif":12.8,"identite":"CCAS DE MERS LES BAINS","quantitatif":"Heure(s)","type":"Prestataire"},"quantite":84,"montant":1075.2,"prestation":"Aide humaine mandataire"},{"tiers":{"tarif":90,"identite":null,"quantitatif":"Mois","type":"Ind\\u00e9termin\\u00e9"},"quantite":1,"montant":90,"prestation":"Articles d\'hygi\\u00e8ne forfait 90"}]},"dateReponse":null,"datePropositionPlan":null,"avis":""},"complementDossier":{"dateDepot":"2016-06-22"},"suivi":{"dateDecision":"2016-08-23"}}]}',
}
APAREQUEST = '{"demandeAsg":{"visite":{"date":"2016-07-07","heure":"1330"},"demande":{"type":"Allocation Personnalis\\u00e9e Autonomie","indexDemande":42,"nature":"Domicile"},"droit":{"dateDebut":"2016-08-23","dateFin":"2018-08-31"},"complementDossier":{"dateArrivee":"2016-06-22","dateDossierComplet":"2016-06-22"},"suivi":{"dateDecision":"2016-08-23","decision":"Accord"}}}'
@pytest.fixture
@ -251,6 +252,7 @@ def test_solis_link_infos_unlink(app, solis):
resp = app.get(endpoint, status=200)
assert requests_post.call_count == 1 # get a token
assert requests_get.call_count == 1 # get informations
assert ('/asg/apa/%s' % apa_endpoint) in requests_get.call_args[0][0]
assert resp.json['err'] == 0
assert resp.json['data']
@ -260,6 +262,7 @@ def test_solis_link_infos_unlink(app, solis):
resp = app.get(endpoint, status=200)
assert requests_post.call_count == 2 # get a token
assert requests_get.call_count == 2 # get informations
assert ('/asg/apa/%s' % apa_endpoint) in requests_get.call_args[0][0]
assert resp.json['err'] == 1
assert not resp.json['data']
assert resp.json['err_desc'].startswith('error status:500')
@ -272,6 +275,66 @@ def test_solis_link_infos_unlink(app, solis):
assert resp.json['err_desc'] == 'unknown link'
assert resp.json['data'] is None
# get info about a specific request
with mock.patch('passerelle.utils.Request.get') as requests_get:
with mock.patch('passerelle.utils.Request.post') as requests_post:
requests_post.return_value = utils.FakedResponse(content=APATOKEN, status_code=200)
endpoint_base = utils.generic_endpoint_url('solis', 'apa-user-info', slug=solis.slug)
# via demandeUnitaire
endpoint = endpoint_base + '?name_id=%s&user_id=53&information=demandeUnitaire&index=42' % NAMEID
requests_get.return_value = utils.FakedResponse(content=APAREQUEST, status_code=200)
resp = app.get(endpoint, status=200)
assert requests_post.call_count == 1 # get a token
assert requests_get.call_count == 1 # get demandeUnitaire
url = requests_get.call_args[0][0]
assert '/asg/apa/demandeUnitaire/' in url
assert url.endswith('/42')
assert resp.json['err'] == 0
assert resp.json['data']['demandeAsg']['demande']['indexDemande'] == 42
requests_post.reset_mock()
requests_get.reset_mock()
requests_get.return_value = utils.FakedResponse(content='nothing here', status_code=404)
resp = app.get(endpoint, status=200)
assert requests_post.call_count == 1 # get a token
assert requests_get.call_count == 1 # get demandeUnitaire
url = requests_get.call_args[0][0]
assert '/asg/apa/demandeUnitaire/' in url
assert url.endswith('/42')
assert resp.json['err'] == 1
assert resp.json['err_desc'].startswith('error status:404')
# missing index
requests_post.reset_mock()
requests_get.reset_mock()
endpoint = endpoint_base + '?name_id=%s&user_id=53&information=demandeUnitaire' % NAMEID
resp = app.get(endpoint, status=400)
requests_get.assert_not_called()
requests_post.assert_not_called()
assert resp.json['err'] == 1
# get indexDemande 42 in lists
for information in ('consultationDeMesDroits', 'suiviDemandeHistorique', 'propositionPlanAide',):
requests_post.reset_mock()
requests_get.reset_mock()
endpoint = endpoint_base + '?name_id=%s&user_id=53&information=%s&index=42' % (NAMEID, information)
requests_get.return_value = utils.FakedResponse(content=APAINFOS[information], status_code=200)
resp = app.get(endpoint, status=200)
assert ('/asg/apa/%s/' % information) in requests_get.call_args[0][0]
requests_post.assert_called_once()
requests_get.assert_called_once()
assert resp.json['err'] == 0
assert resp.json['data']['demandeAsg']['demande']['indexDemande'] == 42
endpoint = endpoint_base + '?name_id=%s&user_id=53&information=%s&index=57' % (NAMEID, information)
resp = app.get(endpoint, status=200)
assert ('/asg/apa/%s/' % information) in requests_get.call_args[0][0]
assert requests_post.call_count == 2
assert requests_get.call_count == 2
assert resp.json['err'] == 1
assert resp.json['err_desc'] == 'cannot find indexDemande=57 in demandeAsg list'
# get informations for all users (exportDonneesIndividu)
change_info = APAINFOS['exportDonneesIndividu'].replace('PYPPENNE', 'PEPPYNE')
with mock.patch('passerelle.utils.Request.get') as requests_get: