From 3a363b704198aa17a0b02fbfefbaa6967d89e988 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 30 Mar 2020 14:25:39 +0200
Subject: [PATCH] toulouse-axel: upperize data sent for DUI update (#41159)

---
 functests/toulouse_axel/test_toulouse_axel.py |   2 +-
 passerelle/contrib/toulouse_axel/models.py    |   2 +
 passerelle/contrib/toulouse_axel/utils.py     |  13 ++
 .../toulouse_axel/update_family_info.json     | 192 +++++++++---------
 tests/test_toulouse_axel.py                   |  20 ++
 5 files changed, 132 insertions(+), 97 deletions(-)

diff --git a/functests/toulouse_axel/test_toulouse_axel.py b/functests/toulouse_axel/test_toulouse_axel.py
index 744ca7ad..68607da3 100644
--- a/functests/toulouse_axel/test_toulouse_axel.py
+++ b/functests/toulouse_axel/test_toulouse_axel.py
@@ -45,7 +45,7 @@ def test_link(conn, user):
     payload['DROITALIMAGE'] = 'NON'
     payload['REVENUS']['CHOIXREVENU'] = ''
     # remove non editable fields
-    for key in ['SITUATIONFAMILIALE', 'SITUATIONFAMILIALE_label', 'NBENFANTACTIF', 'NBRLACTIF', 'IDDUI', 'CODEMISEAJOUR']:
+    for key in ['SITUATIONFAMILIALE', 'SITUATIONFAMILIALE_label', 'NBENFANTACTIF', 'NBRLACTIF', 'IDDUI', 'CODEMISEAJOUR', 'management_dates']:
         payload.pop(key)
     for key in ['IDPERSONNE', 'NOM', 'PRENOM', 'NOMJEUNEFILLE', 'DATENAISSANCE', 'CIVILITE', 'INDICATEURRL', 'CSP_label']:
         if 'RL1' in payload:
diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py
index 095e2390..36e36722 100644
--- a/passerelle/contrib/toulouse_axel/models.py
+++ b/passerelle/contrib/toulouse_axel/models.py
@@ -686,6 +686,8 @@ class ToulouseAxel(BaseResource):
         if not post_data['ENFANT']:
             post_data.pop('ENFANT')
 
+        utils.upperize(post_data)
+
     UPDATE_FAMILY_SCHEMA['pre_process'] = pre_sanitize_update_family_data
 
     def sanitize_update_family_data(self, dui, post_data):
diff --git a/passerelle/contrib/toulouse_axel/utils.py b/passerelle/contrib/toulouse_axel/utils.py
index 962fc8ab..d5d56c34 100644
--- a/passerelle/contrib/toulouse_axel/utils.py
+++ b/passerelle/contrib/toulouse_axel/utils.py
@@ -19,6 +19,7 @@ from __future__ import unicode_literals
 
 from collections import OrderedDict
 import datetime
+import unicodedata
 import xml.etree.ElementTree as ET
 
 
@@ -139,6 +140,18 @@ def encode_datetime(obj):
         return obj
 
 
+def upperize(data):
+    if isinstance(data, dict):
+        for key, val in data.items():
+            data[key] = upperize(val)
+    if isinstance(data, list):
+        for i, val in enumerate(data):
+            data[i] = upperize(val)
+    if isinstance(data, str):
+        data = unicodedata.normalize('NFKD', data).encode('ascii', 'ignore').decode('ascii').upper()
+    return data
+
+
 def normalize_invoice(invoice, dui, historical=False, vendor_base=None):
     vendor = vendor_base or {}
     vendor.update(invoice)
diff --git a/tests/data/toulouse_axel/update_family_info.json b/tests/data/toulouse_axel/update_family_info.json
index 644561c1..66585ec5 100644
--- a/tests/data/toulouse_axel/update_family_info.json
+++ b/tests/data/toulouse_axel/update_family_info.json
@@ -7,11 +7,11 @@
         "COMPLEMENTVOIE": null,
         "NUMEROSTI": "1234",
         "NUMVOIE": "42",
-        "VILLE": "TOULOUSE",
-        "VOIE": "RUE bar"
+        "VILLE": "toulouse",
+        "VOIE": "rue bar"
     },
-    "DEMATFACTURES": "NON",
-    "DROITALIMAGE": "NON",
+    "DEMATFACTURES": "non",
+    "DROITALIMAGE": "non",
     "ENFANT": [
         {
             "ASSURANCE": {
@@ -19,21 +19,21 @@
                 "NOM": "foo",
                 "NUMERO": "1234"
             },
-            "AUTORISATIONURGENCEMEDICALE": "OUI",
+            "AUTORISATIONURGENCEMEDICALE": "oui",
             "CONTACT": [
                 {
-                    "CHERCHERLENFANT": "OUI",
-                    "ENCASURGENCE": "OUI",
-                    "LIENPARENTE": "VOI",
+                    "CHERCHERLENFANT": "oui",
+                    "ENCASURGENCE": "oui",
+                    "LIENPARENTE": "voi",
                     "NOM": "foo",
                     "PRENOM": "foo",
                     "TELFIXE": "0505050505",
                     "TELPORTABLE": "0606060606"
                 },
                 {
-                    "CHERCHERLENFANT": "OUI",
-                    "ENCASURGENCE": "OUI",
-                    "LIENPARENTE": "VOI",
+                    "CHERCHERLENFANT": "oui",
+                    "ENCASURGENCE": "oui",
+                    "LIENPARENTE": "voi",
                     "NOM": "foo",
                     "PRENOM": "foo",
                     "TELFIXE": "0505050505",
@@ -52,21 +52,21 @@
             "IDPERSONNE": "4242",
             "SANITAIRE": {
                 "ALLERGIE": {
-                    "ASTHME": "OUI",
-                    "MEDICAMENTEUSES": "OUI",
-                    "ALIMENTAIRES": "OUI",
+                    "ASTHME": "oui",
+                    "MEDICAMENTEUSES": "oui",
+                    "ALIMENTAIRES": "oui",
                     "AUTRES": null
                 },
                 "DATEFINVALIDITE": "2020-12-31",
                 "HANDICAP": {
                     "AUTREDIFFICULTE": null,
                     "ECOLESPECIALISEE": null,
-                    "INDICATEURAUXILIAIREVS": "NON",
-                    "INDICATEURECOLE": "NON",
-                    "INDICATEURHANDICAP": "NON",
-                    "INDICATEURNOTIFMDPH": "NON"
+                    "INDICATEURAUXILIAIREVS": "non",
+                    "INDICATEURECOLE": "non",
+                    "INDICATEURHANDICAP": "non",
+                    "INDICATEURNOTIFMDPH": "non"
                 },
-                "INDICATEURPAI": "NON",
+                "INDICATEURPAI": "non",
                 "INFOUTILES": null,
                 "MEDECIN": {
                     "NOM": "foo",
@@ -93,25 +93,25 @@
                 "NOM": "foo",
                 "NUMERO": "1234"
             },
-            "AUTORISATIONURGENCEMEDICALE": "OUI",
+            "AUTORISATIONURGENCEMEDICALE": "oui",
             "IDPERSONNE": "3535",
             "SANITAIRE": {
                 "ALLERGIE": {
-                    "ASTHME": "OUI",
-                    "MEDICAMENTEUSES": "OUI",
-                    "ALIMENTAIRES": "OUI",
+                    "ASTHME": "oui",
+                    "MEDICAMENTEUSES": "oui",
+                    "ALIMENTAIRES": "oui",
                     "AUTRES": null
                 },
                 "DATEFINVALIDITE": "2020-12-31",
                 "HANDICAP": {
                     "AUTREDIFFICULTE": null,
                     "ECOLESPECIALISEE": null,
-                    "INDICATEURAUXILIAIREVS": "NON",
-                    "INDICATEURECOLE": "NON",
-                    "INDICATEURHANDICAP": "NON",
-                    "INDICATEURNOTIFMDPH": "NON"
+                    "INDICATEURAUXILIAIREVS": "non",
+                    "INDICATEURECOLE": "non",
+                    "INDICATEURHANDICAP": "non",
+                    "INDICATEURNOTIFMDPH": "non"
                 },
-                "INDICATEURPAI": "NON",
+                "INDICATEURPAI": "non",
                 "INFOUTILES": null,
                 "MEDECIN": {
                     "NOM": "foo",
@@ -120,26 +120,26 @@
                 "VACCIN": {
                     "AUTRE": null,
                     "BCGAJOUR": null,
-                    "COQAJOUR": "OUI",
-                    "DAJOUR": "OUI",
-                    "HBAJOUR": "OUI",
-                    "HIBAJOUR": "OUI",
-                    "IIPAJOUR": "OUI",
-                    "MENINAJOUR": "OUI",
-                    "PAJOUR": "OUI",
-                    "RORAJOUR": "OUI",
-                    "TAJOUR": "OUI"
+                    "COQAJOUR": "oui",
+                    "DAJOUR": "oui",
+                    "HBAJOUR": "oui",
+                    "HIBAJOUR": "oui",
+                    "IIPAJOUR": "oui",
+                    "MENINAJOUR": "oui",
+                    "PAJOUR": "oui",
+                    "RORAJOUR": "oui",
+                    "TAJOUR": "oui"
                 }
             }
         }
     ],
     "N0TELEDOSSIER": "42",
-    "REACTUALISATIONENLIGNE": "NON",
+    "REACTUALISATIONENLIGNE": "non",
     "REVENUS": {
         "ALLOCATAIRE": "2",
         "CHOIXREVENU": "",
         "NUMEROCAF": "1234",
-        "TYPEREGIME": "GENE"
+        "TYPEREGIME": "gene"
     },
     "RL1": {
         "ADREMPLOYEUR": {
@@ -154,9 +154,9 @@
             "VOIE": null
         },
         "COURRIEL": "foo@bar.com",
-        "CSP": "ETU",
+        "CSP": "etu",
         "NOMEMPLOYEUR": "foo",
-        "PROFESSION": "ETUDIANT",
+        "PROFESSION": "etudiant",
         "TELPORTABLE": "0606060606"
     },
     "RL2": {
@@ -172,64 +172,64 @@
             "VOIE": null
         },
         "COURRIEL": "foo@bar.com",
-        "CSP": "EMP",
+        "CSP": "emp",
         "NOMEMPLOYEUR": "foo",
         "PROFESSION": "foo",
         "TELPORTABLE": "0606060606"
     },
     "TELFIXE": null,
-    "maj:adresse": "OUI",
-    "maj:enfant_0": "OUI",
-    "maj:enfant_0_assurance": "OUI",
-    "maj:enfant_0_contact": "OUI",
-    "maj:enfant_0_sanitaire": "OUI",
-    "maj:enfant_0_sanitaire_allergie": "OUI",
-    "maj:enfant_0_sanitaire_medecin": "OUI",
-    "maj:enfant_0_sanitaire_vaccin": "OUI",
-    "maj:enfant_0_sanitaire_handicap": "OUI",
-    "maj:enfant_1": "OUI",
-    "maj:enfant_1_assurance": "OUI",
-    "maj:enfant_1_contact": "OUI",
-    "maj:enfant_1_sanitaire": "OUI",
-    "maj:enfant_1_sanitaire_allergie": "OUI",
-    "maj:enfant_1_sanitaire_medecin": "OUI",
-    "maj:enfant_1_sanitaire_vaccin": "OUI",
-    "maj:enfant_1_sanitaire_handicap": "OUI",
-    "maj:enfant_2": "OUI",
-    "maj:enfant_2_assurance": "OUI",
-    "maj:enfant_2_contact": "OUI",
-    "maj:enfant_2_sanitaire": "OUI",
-    "maj:enfant_2_sanitaire_allergie": "OUI",
-    "maj:enfant_2_sanitaire_medecin": "OUI",
-    "maj:enfant_2_sanitaire_vaccin": "OUI",
-    "maj:enfant_2_sanitaire_handicap": "OUI",
-    "maj:enfant_3": "OUI",
-    "maj:enfant_3_assurance": "OUI",
-    "maj:enfant_3_contact": "OUI",
-    "maj:enfant_3_sanitaire": "OUI",
-    "maj:enfant_3_sanitaire_allergie": "OUI",
-    "maj:enfant_3_sanitaire_medecin": "OUI",
-    "maj:enfant_3_sanitaire_vaccin": "OUI",
-    "maj:enfant_3_sanitaire_handicap": "OUI",
-    "maj:enfant_4": "OUI",
-    "maj:enfant_4_assurance": "OUI",
-    "maj:enfant_4_contact": "OUI",
-    "maj:enfant_4_sanitaire": "OUI",
-    "maj:enfant_4_sanitaire_allergie": "OUI",
-    "maj:enfant_4_sanitaire_medecin": "OUI",
-    "maj:enfant_4_sanitaire_vaccin": "OUI",
-    "maj:enfant_4_sanitaire_handicap": "OUI",
-    "maj:enfant_5": "OUI",
-    "maj:enfant_5_assurance": "OUI",
-    "maj:enfant_5_contact": "OUI",
-    "maj:enfant_5_sanitaire": "OUI",
-    "maj:enfant_5_sanitaire_allergie": "OUI",
-    "maj:enfant_5_sanitaire_medecin": "OUI",
-    "maj:enfant_5_sanitaire_vaccin": "OUI",
-    "maj:enfant_5_sanitaire_handicap": "OUI",
-    "maj:revenus": "OUI",
-    "maj:rl1": "OUI",
-    "maj:rl1_adresse_employeur": "OUI",
-    "maj:rl2": "OUI",
-    "maj:rl2_adresse_employeur": "OUI"
+    "maj:adresse": "oui",
+    "maj:enfant_0": "oui",
+    "maj:enfant_0_assurance": "oui",
+    "maj:enfant_0_contact": "oui",
+    "maj:enfant_0_sanitaire": "oui",
+    "maj:enfant_0_sanitaire_allergie": "oui",
+    "maj:enfant_0_sanitaire_medecin": "oui",
+    "maj:enfant_0_sanitaire_vaccin": "oui",
+    "maj:enfant_0_sanitaire_handicap": "oui",
+    "maj:enfant_1": "oui",
+    "maj:enfant_1_assurance": "oui",
+    "maj:enfant_1_contact": "oui",
+    "maj:enfant_1_sanitaire": "oui",
+    "maj:enfant_1_sanitaire_allergie": "oui",
+    "maj:enfant_1_sanitaire_medecin": "oui",
+    "maj:enfant_1_sanitaire_vaccin": "oui",
+    "maj:enfant_1_sanitaire_handicap": "oui",
+    "maj:enfant_2": "oui",
+    "maj:enfant_2_assurance": "oui",
+    "maj:enfant_2_contact": "oui",
+    "maj:enfant_2_sanitaire": "oui",
+    "maj:enfant_2_sanitaire_allergie": "oui",
+    "maj:enfant_2_sanitaire_medecin": "oui",
+    "maj:enfant_2_sanitaire_vaccin": "oui",
+    "maj:enfant_2_sanitaire_handicap": "oui",
+    "maj:enfant_3": "oui",
+    "maj:enfant_3_assurance": "oui",
+    "maj:enfant_3_contact": "oui",
+    "maj:enfant_3_sanitaire": "oui",
+    "maj:enfant_3_sanitaire_allergie": "oui",
+    "maj:enfant_3_sanitaire_medecin": "oui",
+    "maj:enfant_3_sanitaire_vaccin": "oui",
+    "maj:enfant_3_sanitaire_handicap": "oui",
+    "maj:enfant_4": "oui",
+    "maj:enfant_4_assurance": "oui",
+    "maj:enfant_4_contact": "oui",
+    "maj:enfant_4_sanitaire": "oui",
+    "maj:enfant_4_sanitaire_allergie": "oui",
+    "maj:enfant_4_sanitaire_medecin": "oui",
+    "maj:enfant_4_sanitaire_vaccin": "oui",
+    "maj:enfant_4_sanitaire_handicap": "oui",
+    "maj:enfant_5": "oui",
+    "maj:enfant_5_assurance": "oui",
+    "maj:enfant_5_contact": "oui",
+    "maj:enfant_5_sanitaire": "oui",
+    "maj:enfant_5_sanitaire_allergie": "oui",
+    "maj:enfant_5_sanitaire_medecin": "oui",
+    "maj:enfant_5_sanitaire_vaccin": "oui",
+    "maj:enfant_5_sanitaire_handicap": "oui",
+    "maj:revenus": "oui",
+    "maj:rl1": "oui",
+    "maj:rl1_adresse_employeur": "oui",
+    "maj:rl2": "oui",
+    "maj:rl2_adresse_employeur": "oui"
 }
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index 033930e1..31dd31f4 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -53,6 +53,7 @@ from passerelle.contrib.toulouse_axel.utils import (
     lien_parente_mapping,
     type_regime_mapping,
     get_reference_year_from_date,
+    upperize,
 )
 from passerelle.utils.jsonresponse import APIError
 import utils
@@ -1070,6 +1071,25 @@ def test_sanitize_update_family_data_missing_revenus_fields(app, resource, updat
     assert full_update_params['REVENUS']['NBENFANTSACHARGE'] is None
 
 
+def test_pre_sanitize_update_upperize(app, resource, update_params):
+    Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
+
+    def check_upper(data):
+        if isinstance(data, dict):
+            for val in data.values():
+                check_upper(val)
+        if isinstance(data, list):
+            for val in data:
+                check_upper(val)
+        if isinstance(data, str):
+            assert data == data.upper()
+
+    resource.pre_sanitize_update_family_data(post_data=update_params)
+    check_upper(update_params)
+
+    assert upperize(u'something with an accent é') == u'SOMETHING WITH AN ACCENT E'
+
+
 def test_pre_sanitize_update_family_data_adresse(app, resource, update_params):
     Link.objects.create(resource=resource, name_id='yyy', dui='XXX', person_id='42')
 
-- 
2.20.1

