From 4b5a08d76c5118a8e8d83a38faaa31d8ded8dadd Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Wed, 27 Jul 2022 16:13:59 +0200
Subject: [PATCH] toulouse_maelis: adapt updateFamily error format (#67791)

---
 passerelle/contrib/toulouse_maelis/models.py  |  4 +-
 tests/data/toulouse_maelis/R_read_family.xml  |  1 +
 .../toulouse_maelis/R_update_family_error.xml | 42 ++++++++++++++++++-
 tests/test_toulouse_maelis.py                 | 11 ++---
 4 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/passerelle/contrib/toulouse_maelis/models.py b/passerelle/contrib/toulouse_maelis/models.py
index 14f23a23..68787b34 100644
--- a/passerelle/contrib/toulouse_maelis/models.py
+++ b/passerelle/contrib/toulouse_maelis/models.py
@@ -450,18 +450,18 @@ class ToulouseMaelis(BaseResource, HTTPResource):
     )
     def update_family(self, request, NameID, post_data):
         family_id = self.get_link(NameID).family_id
         self.replace_null_values(post_data)
 
         response = self.call('Family', 'updateFamily', dossierNumber=family_id, **post_data)
         data = serialize_object(response)
         family_id = data.get('number')
-        if not family_id:
-            errors = data.get('rl1ErrorList') + data.get('childErrorList')
+        errors = data.get('childErrorList')
+        if errors:
             err_codes = [x.split(':')[0][:4] for x in errors]
             raise APIError(' ; '.join(errors), err_code=', '.join(err_codes))
         return {'data': data}
 
     @endpoint(
         display_category='Famille',
         description="Mise à jour des coordonnées d'une personne",
         name='update-coordinate',
diff --git a/tests/data/toulouse_maelis/R_read_family.xml b/tests/data/toulouse_maelis/R_read_family.xml
index d3e0c575..b08aa52a 100644
--- a/tests/data/toulouse_maelis/R_read_family.xml
+++ b/tests/data/toulouse_maelis/R_read_family.xml
@@ -60,16 +60,17 @@
             <number>51</number>
           </CAFInfo>
         </RL2>
         <emergencyPersonList>
           <num>614059</num>
           <id>S10032140599</id>
           <firstname>KENY</firstname>
           <lastname>ARKANA</lastname>
+          <dateBirth>1982-12-20T00:00:00+01:00</dateBirth>
           <quality>MERE</quality>
           <contact>
             <phone>0123456789</phone>
           </contact>
         </emergencyPersonList>
         <authorizedPersonList>
           <numPerson>614058</numPerson>
           <firstname>CATHY</firstname>
diff --git a/tests/data/toulouse_maelis/R_update_family_error.xml b/tests/data/toulouse_maelis/R_update_family_error.xml
index 23d4b59b..5c5b02e1 100644
--- a/tests/data/toulouse_maelis/R_update_family_error.xml
+++ b/tests/data/toulouse_maelis/R_update_family_error.xml
@@ -1,16 +1,54 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf8"?>
 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
     <ns2:updateFamilyResponse xmlns:ns2="family.ws.maelis.sigec.com">
       <familyResult>
-        <number>0</number>
+        <number>196569</number>
+        <category>BI</category>
+        <situation>C</situation>
+        <flagCom>true</flagCom>
         <RL1>
+          <num>614061</num>
+          <lastname>DOEE</lastname>
+          <firstname>JHON</firstname>
+          <quality>AU</quality>
+          <civility>MR</civility>
+          <dateBirth>1938-07-26T00:00:00+01:00</dateBirth>
           <adresse>
             <idStreet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
+            <num>0</num>
+            <street1>Chateau</street1>
+            <town>Paris</town>
+            <zipcode>75014</zipcode>
           </adresse>
+          <contact>
+            <isContactMail>false</isContactMail>
+            <isContactSms>false</isContactSms>
+            <isInvoicePdf>false</isInvoicePdf>
+          </contact>
         </RL1>
+        <RL2>
+          <num>614062</num>
+          <lastname>DOE</lastname>
+          <firstname>JANNETTE</firstname>
+          <quality>MERE</quality>
+          <civility>MR</civility>
+          <dateBirth>1940-06-22T00:00:00+02:00</dateBirth>
+          <adresse>
+            <idStreet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
+            <num>0</num>
+            <street1>Chateau</street1>
+            <town>Paris</town>
+            <zipcode>75014</zipcode>
+          </adresse>
+          <contact>
+            <isContactMail>false</isContactMail>
+            <isContactSms>false</isContactSms>
+            <isInvoicePdf>false</isInvoicePdf>
+          </contact>
+        </RL2>
         <childErrorList>E65a : Il existe déjà un enfant correspondant au nom [ZIMMERMAN], prénom [ROBERT], date de naissance [24/05/1941] - Personne n°[614051] - Famille n°[196544]</childErrorList>
       </familyResult>
     </ns2:updateFamilyResponse>
   </soap:Body>
 </soap:Envelope>
diff --git a/tests/test_toulouse_maelis.py b/tests/test_toulouse_maelis.py
index 9da268a3..9942a9c2 100644
--- a/tests/test_toulouse_maelis.py
+++ b/tests/test_toulouse_maelis.py
@@ -956,24 +956,19 @@ def test_update_family_maelis_error(mocked_post, mocked_get, con, app):
         'childList/0/lastname': 'Zimmerman',
         'childList/0/firstname': 'Robert',
         'childList/0/sexe': 'M',
         'childList/0/birth/dateBirth': '1941-05-24',
         'childList/0/birth/place': 'Duluth',
     }
 
     Link.objects.create(resource=con, family_id='1312', name_id='local')
-    # get 500 because error repsonse is wrongly formatted
-    resp = app.post_json(url + '?NameID=local', params=params, status=500)
-    assert resp.json == {
-        'err': 1,
-        'err_class': 'zeep.exceptions.XMLParseError',
-        'err_desc': "Unexpected element 'adresse', expected 'lastname'",
-        'data': None,
-    }
+    resp = app.post_json(url + '?NameID=local', params=params)
+    assert resp.json['err'] == 'E65a'
+    assert "E65a : Il existe déjà un enfant" in resp.json['err_desc']
 
 
 @mock.patch('passerelle.utils.Request.get')
 @mock.patch('passerelle.utils.Request.post')
 def test_update_family_soap_error(mocked_post, mocked_get, con, app):
     mocked_get.return_value = FAMILY_SERVICE_WSDL
     mocked_post.return_value = UPDATE_FAMILY_500
     url = get_endpoint('update-family')
-- 
2.30.2

