From 896acbd1f7c7c4859057af5cda55c16b81736053 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:10:39 +0200
Subject: [PATCH 3/4] caluire-axel: children_info endpoint (#53825)

---
 functests/caluire_axel/test_caluire_axel.py |  9 ++++++
 passerelle/contrib/caluire_axel/models.py   | 14 +++++++++
 tests/test_caluire_axel.py                  | 34 +++++++++++++++++++++
 3 files changed, 57 insertions(+)

diff --git a/functests/caluire_axel/test_caluire_axel.py b/functests/caluire_axel/test_caluire_axel.py
index 7ef49d5f..530a17cc 100644
--- a/functests/caluire_axel/test_caluire_axel.py
+++ b/functests/caluire_axel/test_caluire_axel.py
@@ -29,6 +29,15 @@ def test_link(conn, user):
     assert data['err'] == 0
     print('\n')
 
+    print("GET children info")
+    url = conn + '/children_info?NameID=%s' % (name_id)
+    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 b51542b5..705dfb90 100644
--- a/passerelle/contrib/caluire_axel/models.py
+++ b/passerelle/contrib/caluire_axel/models.py
@@ -164,6 +164,20 @@ class CaluireAxel(BaseResource):
         family_data = self.get_family_data(link.family_id)
         return {'data': family_data}
 
+    @endpoint(
+        display_category=_('Family account'),
+        display_order=4,
+        description=_("Get information about children"),
+        perm='can_access',
+        parameters={
+            'NameID': {'description': _('Publik ID')},
+        },
+    )
+    def children_info(self, request, NameID):
+        link = self.get_link(NameID)
+        family_data = self.get_family_data(link.family_id)
+        return {'data': family_data.get('MEMBRE', [])}
+
 
 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 3e18a5ad..8305f341 100644
--- a/tests/test_caluire_axel.py
+++ b/tests/test_caluire_axel.py
@@ -400,3 +400,37 @@ def test_family_info_endpoint(app, resource):
     assert resp.json['data']['MEMBRE'][2]['text'] == 'Enfant 3 CALUIRE TEST'
     assert resp.json['data']['MEMBRE'][3]['id'] == '59509'
     assert resp.json['data']['MEMBRE'][3]['text'] == 'Enfant 5 CALUIRE TEST'
+
+
+def test_children_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/children_info?NameID=yyy')
+    assert resp.json['err_desc'] == "Axel error: FooBar"
+    assert resp.json['err'] == 'error'
+
+
+def test_children_info_endpoint_no_result(app, resource):
+    resp = app.get('/caluire-axel/test/children_info?NameID=yyy')
+    assert resp.json['err_desc'] == "Person not found"
+    assert resp.json['err'] == 'not-found'
+
+
+def test_children_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/children_info?NameID=yyy')
+    assert resp.json['err'] == 0
+    assert len(resp.json['data']) == 4
+    assert resp.json['data'][0]['id'] == '50632'
+    assert resp.json['data'][0]['text'] == 'Enfant 1 CALUIRE TEST'
+    assert resp.json['data'][1]['id'] == '50633'
+    assert resp.json['data'][1]['text'] == 'Enfant 2 CALUIRE TEST'
+    assert resp.json['data'][2]['id'] == '54621'
+    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'
-- 
2.30.2

