From b4c6e9b6758d52d4b27cca4e660b120729b8cf34 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 7 May 2021 14:22:21 +0200
Subject: [PATCH 4/4] caluire-axel: child_info endpoint (#53825)

---
 functests/caluire_axel/test_caluire_axel.py | 10 ++++
 passerelle/contrib/caluire_axel/models.py   | 20 ++++++++
 tests/test_caluire_axel.py                  | 56 +++++++++++++++++++++
 3 files changed, 86 insertions(+)

diff --git a/functests/caluire_axel/test_caluire_axel.py b/functests/caluire_axel/test_caluire_axel.py
index 530a17cc..20c881f8 100644
--- a/functests/caluire_axel/test_caluire_axel.py
+++ b/functests/caluire_axel/test_caluire_axel.py
@@ -38,6 +38,16 @@ def test_link(conn, user):
     assert res['err'] == 0
     print('\n')
 
+    for child in data['data']['MEMBRE']:
+        print("GET child info")
+        url = conn + '/child_info?NameID=%s&idpersonne=%s' % (name_id, child['IDENT'])
+        resp = requests.get(url)
+        resp.raise_for_status()
+        res = resp.json()
+        pprint.pprint(res)
+        assert res['err'] == 0
+        print('\n')
+
     print("Deleting link")
     url = conn + '/unlink?NameID=%s' % name_id
     resp = requests.post(url)
diff --git a/passerelle/contrib/caluire_axel/models.py b/passerelle/contrib/caluire_axel/models.py
index 705dfb90..de3b0b1a 100644
--- a/passerelle/contrib/caluire_axel/models.py
+++ b/passerelle/contrib/caluire_axel/models.py
@@ -178,6 +178,26 @@ class CaluireAxel(BaseResource):
         family_data = self.get_family_data(link.family_id)
         return {'data': family_data.get('MEMBRE', [])}
 
+    @endpoint(
+        display_category=_('Family account'),
+        display_order=5,
+        description=_("Get information about a child"),
+        perm='can_access',
+        parameters={
+            'NameID': {'description': _('Publik ID')},
+            'idpersonne': {'description': _('Child ID')},
+        },
+    )
+    def child_info(self, request, idpersonne, NameID):
+        link = self.get_link(NameID)
+        family_data = self.get_family_data(link.family_id)
+
+        for child in family_data.get('MEMBRE', []):
+            if child['IDENT'] == idpersonne:
+                return {'data': child}
+
+        raise APIError('Child not found', err_code='not-found')
+
 
 class Link(models.Model):
     resource = models.ForeignKey(CaluireAxel, on_delete=models.CASCADE)
diff --git a/tests/test_caluire_axel.py b/tests/test_caluire_axel.py
index 8305f341..637558e3 100644
--- a/tests/test_caluire_axel.py
+++ b/tests/test_caluire_axel.py
@@ -434,3 +434,59 @@ def test_children_info_endpoint(app, resource):
     assert resp.json['data'][2]['text'] == 'Enfant 3 CALUIRE TEST'
     assert resp.json['data'][3]['id'] == '59509'
     assert resp.json['data'][3]['text'] == 'Enfant 5 CALUIRE TEST'
+
+
+def test_child_info_endpoint_axel_error(app, resource):
+    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
+    with mock.patch('passerelle.contrib.caluire_axel.schemas.get_famille_individus') as operation:
+        operation.side_effect = AxelError('FooBar')
+        resp = app.get('/caluire-axel/test/child_info?NameID=yyy&idpersonne=zzz')
+    assert resp.json['err_desc'] == "Axel error: FooBar"
+    assert resp.json['err'] == 'error'
+
+
+def test_child_info_endpoint_no_result(app, resource):
+    resp = app.get('/caluire-axel/test/child_info?NameID=yyy&idpersonne=zzz')
+    assert resp.json['err_desc'] == "Person not found"
+    assert resp.json['err'] == 'not-found'
+
+    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
+    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/family_info.xml')
+    with open(filepath) as xml:
+        content = xml.read()
+    with mock_getdata(content, 'GetFamilleIndividus'):
+        resp = app.get('/caluire-axel/test/child_info?NameID=yyy&idpersonne=zzz')
+    assert resp.json['err_desc'] == "Child not found"
+    assert resp.json['err'] == 'not-found'
+
+
+def test_child_info_endpoint(app, resource):
+    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
+    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/family_info.xml')
+    with open(filepath) as xml:
+        content = xml.read()
+    with mock_getdata(content, 'GetFamilleIndividus'):
+        resp = app.get('/caluire-axel/test/child_info?NameID=yyy&idpersonne=50632')
+    assert resp.json['err'] == 0
+    assert set(resp.json['data'].keys()) == set(
+        [
+            'ADRESSE',
+            'CIVILITE',
+            'FAMILLE',
+            'GARDEALTERNEE',
+            'IDENT',
+            'MAIL',
+            'NAISSANCE',
+            'NOM',
+            'NOMJF',
+            'PAI',
+            'PRENOM',
+            'SEXE',
+            'TELFIXE',
+            'TELPORTABLE',
+            'id',
+            'text',
+        ]
+    )
+    assert resp.json['data']['id'] == '50632'
+    assert resp.json['data']['text'] == 'Enfant 1 CALUIRE TEST'
-- 
2.30.2

