From 7f84f41d23aae70b9fda69d710bdb9e0242ab65e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 2 Jul 2020 16:05:25 +0200
Subject: [PATCH] toulouse-axel: check children activities (#44724)

---
 passerelle/contrib/toulouse_axel/models.py    |  6 +-
 tests/data/toulouse_axel/child_activities.xml | 56 -------------------
 tests/test_toulouse_axel.py                   | 30 ++++++++++
 3 files changed, 34 insertions(+), 58 deletions(-)

diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index 69f5f5ca..e218aca3 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -861,9 +861,11 @@ class ToulouseAxel(BaseResource):
             # exclude child in private schools
             if child['LIBELLEECOLE'].startswith('PRIVEE'):
                 continue
-            # exclude also child with more than one registration per activity_type
+            # exclude also child with more than one registration per activity_type or missing activity
             activity_types = [a['TYPEACTIVITE'] for a in child.get('ACTIVITE', [])]
-            if len(activity_types) > len(set(activity_types)):
+            activity_types.sort()
+            if activity_types != ['MAT', 'MIDI', 'SOIR'] and activity_types != ['GARD', 'MAT', 'MIDI', 'SOIR']:
+                # GARD is optional
                 continue
             # ok, store child
             data[child['IDPERSONNE']] = child
diff --git a/tests/data/toulouse_axel/child_activities.xml b/tests/data/toulouse_axel/child_activities.xml
index e10b564b..c7142028 100644
--- a/tests/data/toulouse_axel/child_activities.xml
+++ b/tests/data/toulouse_axel/child_activities.xml
@@ -114,61 +114,5 @@
         <DATEFIN>31/07/2020</DATEFIN>
       </ACTIVITE>
     </ENFANT>
-    <ENFANT>
-      <NOM>foo</NOM>
-      <PRENOM>foo</PRENOM>
-      <DATENAISSANCE>01/01/2019</DATENAISSANCE>
-      <IDPERSONNE>1234</IDPERSONNE>
-      <REGIME>SV</REGIME>
-      <LIBELLEECOLE>MATERNELLE 42</LIBELLEECOLE>
-      <ACTIVITE>
-        <TYPEACTIVITE>MAT</TYPEACTIVITE>
-        <IDACTIVITE>A19P1M1</IDACTIVITE>
-        <LIBELLEACTIVITE>Temps du matin</LIBELLEACTIVITE>
-        <DATEENTREE>01/08/2019</DATEENTREE>
-        <DATESORTIE>31/07/2020</DATESORTIE>
-        <TARIF>0.42</TARIF>
-        <ISPAI>NON</ISPAI>
-        <COUTREVIENT>99999</COUTREVIENT>
-        <DATEDEBUT>01/08/2019</DATEDEBUT>
-        <DATEFIN>31/07/2020</DATEFIN>
-      </ACTIVITE>
-      <ACTIVITE>
-        <TYPEACTIVITE>MAT</TYPEACTIVITE>
-        <IDACTIVITE>A19P1M1bis</IDACTIVITE>
-        <LIBELLEACTIVITE>Temps du matin autre &#233;cole</LIBELLEACTIVITE>
-        <DATEENTREE>01/08/2019</DATEENTREE>
-        <DATESORTIE>31/07/2020</DATESORTIE>
-        <TARIF>0.43</TARIF>
-        <ISPAI>NON</ISPAI>
-        <COUTREVIENT>99999</COUTREVIENT>
-        <DATEDEBUT>01/08/2019</DATEDEBUT>
-        <DATEFIN>31/07/2020</DATEFIN>
-      </ACTIVITE>
-      <ACTIVITE>
-        <TYPEACTIVITE>SOIR</TYPEACTIVITE>
-        <IDACTIVITE>A19P1M3</IDACTIVITE>
-        <LIBELLEACTIVITE>Temps du soir</LIBELLEACTIVITE>
-        <DATEENTREE>01/08/2019</DATEENTREE>
-        <DATESORTIE>31/07/2020</DATESORTIE>
-        <TARIF>0.44</TARIF>
-        <ISPAI>NON</ISPAI>
-        <COUTREVIENT>99999</COUTREVIENT>
-        <DATEDEBUT>01/08/2019</DATEDEBUT>
-        <DATEFIN>31/07/2020</DATEFIN>
-      </ACTIVITE>
-      <ACTIVITE>
-        <TYPEACTIVITE>GARD</TYPEACTIVITE>
-        <IDACTIVITE>A19P1M4</IDACTIVITE>
-        <LIBELLEACTIVITE>Temps mercredi apr&#232;s midi</LIBELLEACTIVITE>
-        <DATEENTREE>01/08/2019</DATEENTREE>
-        <DATESORTIE>31/07/2020</DATESORTIE>
-        <TARIF>0.45</TARIF>
-        <ISPAI>NON</ISPAI>
-        <COUTREVIENT>99999</COUTREVIENT>
-        <DATEDEBUT>01/08/2019</DATEDEBUT>
-        <DATEFIN>31/07/2020</DATEFIN>
-      </ACTIVITE>
-    </ENFANT>
   </DUI>
 </PORTAIL>
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index 4167398d..c8ce3191 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -2424,6 +2424,36 @@ def test_clae_children_activities_info_endpoint(app, resource):
     assert resp.json['err'] == 0
 
 
+@pytest.mark.parametrize('activities, expected', [
+    # all activities except GARD - optional
+    (['MAT', 'MIDI', 'SOIR'], True),
+    # all activities
+    (['MAT', 'MIDI', 'SOIR', 'GARD'], True),
+    # duplicated activities
+    (['MAT', 'MAT', 'MIDI', 'SOIR', 'GARD'], False),
+    (['MAT', 'MIDI', 'MIDI', 'SOIR', 'GARD'], False),
+    (['MAT', 'MIDI', 'SOIR', 'SOIR', 'GARD'], False),
+    (['MAT', 'MIDI', 'SOIR', 'GARD', 'GARD'], False),
+    # missing activity
+    (['MIDI', 'SOIR'], False),
+    (['MAT', 'SOIR'], False),
+    (['MAT', 'MIDI'], False),
+])
+def test_clae_children_activities_info_check(app, resource, activities, expected):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+    result = []
+    for activity in activities:
+        result.append({'IDACTIVITE': 'foo%s' % activity, 'TYPEACTIVITE': activity})
+    result = {'DATA': {'PORTAIL': {'DUI': {'ENFANT': [
+        {'IDPERSONNE': '42', 'NOM': '', 'PRENOM': '', 'REGIME': '', 'LIBELLEECOLE': '', 'ACTIVITE': result}
+    ]}}}}
+    with mock.patch('passerelle.contrib.toulouse_axel.schemas.enfants_activites') as operation:
+        operation.return_value = schemas.OperationResult(json_response=result, xml_request='', xml_response='')
+        resp = app.get('/toulouse-axel/test/clae_children_activities_info?NameID=yyy&booking_date=2020-01-20')
+    assert resp.json['err'] == 0
+    assert len(resp.json['data']) == expected and 1 or 0
+
+
 def test_clae_booking_activities_info_endpoint_axel_error(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
     with mock.patch('passerelle.contrib.toulouse_axel.schemas.enfants_activites') as operation:
-- 
2.20.1

