From 6e37ada7e5699d399d8ae0abf3a964343fc09b46 Mon Sep 17 00:00:00 2001
From: Serghei Mihai <smihai@entrouvert.com>
Date: Tue, 30 Jul 2019 11:48:04 +0200
Subject: [PATCH] astregs: add endpoint to create grant demands indicators
 (#34868)

---
 passerelle/apps/astregs/models.py             |  44 ++++
 tests/data/astregs/DossierIndicateur.wsdl     | 228 ++++++++++++++++++
 .../DossierIndicateurCreationResponse.xml     |   1 +
 tests/test_astregs.py                         |  24 ++
 4 files changed, 297 insertions(+)
 create mode 100644 tests/data/astregs/DossierIndicateur.wsdl
 create mode 100644 tests/data/astregs/DossierIndicateurCreationResponse.xml

diff --git a/passerelle/apps/astregs/models.py b/passerelle/apps/astregs/models.py
index a0c08d09..8f85fe4f 100644
--- a/passerelle/apps/astregs/models.py
+++ b/passerelle/apps/astregs/models.py
@@ -231,6 +231,35 @@ GRANT_SCHEMA = {
     }
 }
 
+INDANA_SCHEMA = {
+    "$schema": "http://json-schema.org/draft-03/schema#",
+    "title": "AstreGS INDANA indicator",
+    "description": "",
+    "type": "object",
+    "properties": {
+        "CodeDossier": {
+            "type": "string",
+            "required": True
+        },
+        "CodeInd_1": {
+            "type": "string",
+            "required": True
+        },
+        "AnneeInd_1": {
+            "type": "string",
+            "required": True
+        },
+        "ValInd_1": {
+            "type": "string",
+            "required": True
+        },
+        "IndAide": {
+            "type": "string",
+            "required": False
+        }
+    }
+}
+
 
 class AstreGS(BaseResource):
     wsdl_base_url = models.URLField(_('Webservices base URL'))
@@ -523,6 +552,21 @@ class AstreGS(BaseResource):
         )
         return {'data': serialize_object(r)}
 
+    @endpoint(name='create-indana-indicator', perm='can_access',
+              post={'description': _('Create indana indicator'),
+                    'request_body': {
+                        'schema': {
+                            'application/json': INDANA_SCHEMA
+                        }
+                    }
+              }
+    )
+    def create_indana_indicator(self, request, post_data):
+        r = self.call('DossierIndicateur', 'Creation',
+                      DossierIndicateur=post_data
+        )
+        return {'data': serialize_object(r)}
+
 
 class Link(models.Model):
     resource = models.ForeignKey(AstreGS)
diff --git a/tests/data/astregs/DossierIndicateur.wsdl b/tests/data/astregs/DossierIndicateur.wsdl
new file mode 100644
index 00000000..9ea2527c
--- /dev/null
+++ b/tests/data/astregs/DossierIndicateur.wsdl
@@ -0,0 +1,228 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<wsdl:definitions name="DossierIndicateur" targetNamespace="http://gfi.astre.webservices/gad/dossierindicateur" xmlns:impl="http://gfi.astre.webservices/gad/dossierindicateur" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:intf="http://gfi.astre.webservices/gad/dossierindicateur">
+  <wsdl:types>
+    <schema elementFormDefault="qualified" targetNamespace="http://gfi.astre.webservices/gad/dossierindicateur" xmlns="http://www.w3.org/2001/XMLSchema">
+				<!--  request et response -->
+			<complexType name="Authentification">
+				<sequence>
+					<element name="USERNOM" type="string"/>
+					<element name="USERPWD" type="string"/>
+				</sequence>
+			</complexType>
+			<complexType name="Contexte">
+				<sequence>
+					<element name="Organisme" type="string"/>
+					<element name="Budget" type="string"/>
+					<element name="Exercice" type="string"/>
+				</sequence>
+			</complexType>
+			<complexType name="DossierIndicateurRequest">
+				<sequence>
+					<element name="Authentification" type="impl:Authentification"/>
+					<element name="Contexte" type="impl:Contexte"/>
+					<element name="DossierIndicateur" type="impl:DossierIndicateur"/>
+				</sequence>
+			</complexType>
+			<complexType name="DossierIndicateurResponse">
+				<sequence>
+					<element name="DossierIndicateurReturn" type="impl:DossierIndicateur"/>
+				</sequence>
+			</complexType>
+			<complexType name="DossierIndicateur">
+				<sequence>
+					<element name="CodeDossier" nillable="true" type="xsd:string"/>
+					<element name="CodeInd_1" nillable="true" type="xsd:string"/>
+					<element name="AnneeInd_1" nillable="true" type="xsd:string"/>
+					<element name="ValInd_1" nillable="true" type="xsd:string"/>
+					<element name="IndAide" nillable="true" type="xsd:string"/>
+				</sequence>
+			</complexType>
+			<complexType name="DossierIndicateur_Cle">
+				<sequence>
+					<element name="CodeDossier" nillable="true" type="xsd:string"/>
+					<element name="CodeInd_1" nillable="true" type="xsd:string"/>
+					<element name="AnneeInd_1" nillable="true" type="xsd:string"/>
+				</sequence>
+			</complexType>
+			<complexType name="DossierIndicateurRequest_Cles">
+				<sequence>
+					<element name="Authentification" type="impl:Authentification"/>
+					<element name="Contexte" type="impl:Contexte"/>
+					<element name="DossierIndicateurCle" type="impl:DossierIndicateur_Cle"/>
+				</sequence>
+			</complexType>
+			<element name="creation">
+				<complexType>
+					<sequence>
+						<element name="request" type="impl:DossierIndicateurRequest"/>
+					</sequence>
+				</complexType>
+			</element>
+			<element name="creationResponse">
+				<complexType>
+					<sequence>
+						<element name="response" type="impl:DossierIndicateurResponse"/>
+					</sequence>
+				</complexType>
+			</element>
+			<element name="modification">
+				<complexType>
+					<sequence>
+						<element name="request" type="impl:DossierIndicateurRequest"/>
+					</sequence>
+				</complexType>
+			</element>
+			<element name="modificationResponse">
+				<complexType>
+					<sequence>
+						<element name="response" type="impl:DossierIndicateurResponse"/>
+					</sequence>
+				</complexType>
+			</element>
+			<element name="suppression">
+				<complexType>
+					<sequence>
+						<element name="request" type="impl:DossierIndicateurRequest_Cles"/>
+					</sequence>
+				</complexType>
+			</element>
+			<element name="suppressionResponse">
+				<complexType>
+					<sequence>
+						<element name="response" type="impl:DossierIndicateurResponse"/>
+					</sequence>
+				</complexType>
+			</element>
+			<element name="chargement">
+				<complexType>
+					<sequence>
+						<element name="request" type="impl:DossierIndicateurRequest_Cles"/>
+					</sequence>
+				</complexType>
+			</element>
+			<element name="chargementResponse">
+				<complexType>
+					<sequence>
+						<element name="response" type="impl:DossierIndicateurResponse"/>
+					</sequence>
+				</complexType>
+			</element>
+		</schema>
+  </wsdl:types>
+  <wsdl:message name="chargementResponse">
+    <wsdl:part name="response" element="impl:chargementResponse">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="creationResponse">
+    <wsdl:part name="response" element="impl:creationResponse">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="modificationRequest">
+    <wsdl:part name="request" element="impl:modification">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="suppressionResponse">
+    <wsdl:part name="response" element="impl:suppressionResponse">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="creationRequest">
+    <wsdl:part name="request" element="impl:creation">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="chargementRequest">
+    <wsdl:part name="request" element="impl:chargement">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="suppressionRequest">
+    <wsdl:part name="request" element="impl:suppression">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="modificationResponse">
+    <wsdl:part name="response" element="impl:modificationResponse">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:portType name="DossierIndicateur">
+<wsdl:documentation>Web service de gestion des indicateurs rattachés à un dossier Astre GS</wsdl:documentation>
+    <wsdl:operation name="Modification">
+<wsdl:documentation>
+				Les champs CodeDossier, CodeInd_1 et AnneeInd_1 sont obligatoires.
+			</wsdl:documentation>
+      <wsdl:input name="modificationRequest" message="impl:modificationRequest">
+    </wsdl:input>
+      <wsdl:output name="modificationResponse" message="impl:modificationResponse">
+    </wsdl:output>
+    </wsdl:operation>
+    <wsdl:operation name="Chargement">
+<wsdl:documentation>
+                Les champs CodeDossier, CodeInd_1 et AnneeInd_1 sont obligatoires.
+                Lorsque le champ IndAide vaut Oui, l'indicateur ne peut pas être supprimé.
+			</wsdl:documentation>
+      <wsdl:input name="chargementRequest" message="impl:chargementRequest">
+    </wsdl:input>
+      <wsdl:output name="chargementResponse" message="impl:chargementResponse">
+    </wsdl:output>
+    </wsdl:operation>
+    <wsdl:operation name="Creation">
+<wsdl:documentation>
+                Les champs CodeDossier, CodeInd_1 et AnneeInd_1 sont obligatoires.
+			</wsdl:documentation>
+      <wsdl:input name="creationRequest" message="impl:creationRequest">
+    </wsdl:input>
+      <wsdl:output name="creationResponse" message="impl:creationResponse">
+    </wsdl:output>
+    </wsdl:operation>
+    <wsdl:operation name="Suppression">
+<wsdl:documentation>
+                Les champs CodeDossier, CodeInd_1 et AnneeInd_1 sont obligatoires.
+			</wsdl:documentation>
+      <wsdl:input name="suppressionRequest" message="impl:suppressionRequest">
+    </wsdl:input>
+      <wsdl:output name="suppressionResponse" message="impl:suppressionResponse">
+    </wsdl:output>
+    </wsdl:operation>
+  </wsdl:portType>
+  <wsdl:binding name="DossierIndicateurSoapBinding" type="impl:DossierIndicateur">
+    <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
+    <wsdl:operation name="Modification">
+      <wsdlsoap:operation soapAction=""/>
+      <wsdl:input name="modificationRequest">
+        <wsdlsoap:body use="literal"/>
+      </wsdl:input>
+      <wsdl:output name="modificationResponse">
+        <wsdlsoap:body use="literal"/>
+      </wsdl:output>
+    </wsdl:operation>
+    <wsdl:operation name="Suppression">
+      <wsdlsoap:operation soapAction=""/>
+      <wsdl:input name="suppressionRequest">
+        <wsdlsoap:body use="literal"/>
+      </wsdl:input>
+      <wsdl:output name="suppressionResponse">
+        <wsdlsoap:body use="literal"/>
+      </wsdl:output>
+    </wsdl:operation>
+    <wsdl:operation name="Creation">
+      <wsdlsoap:operation soapAction=""/>
+      <wsdl:input name="creationRequest">
+        <wsdlsoap:body use="literal"/>
+      </wsdl:input>
+      <wsdl:output name="creationResponse">
+        <wsdlsoap:body use="literal"/>
+      </wsdl:output>
+    </wsdl:operation>
+    <wsdl:operation name="Chargement">
+      <wsdlsoap:operation soapAction=""/>
+      <wsdl:input name="chargementRequest">
+        <wsdlsoap:body use="literal"/>
+      </wsdl:input>
+      <wsdl:output name="chargementResponse">
+        <wsdlsoap:body use="literal"/>
+      </wsdl:output>
+    </wsdl:operation>
+  </wsdl:binding>
+  <wsdl:service name="DossierIndicateur">
+    <wsdl:port name="DossierIndicateur" binding="impl:DossierIndicateurSoapBinding">
+      <wsdlsoap:address location="http://10.1.20.153:28001/axis2/services/DossierIndicateur/"/>
+    </wsdl:port>
+  </wsdl:service>
+</wsdl:definitions>
\ No newline at end of file
diff --git a/tests/data/astregs/DossierIndicateurCreationResponse.xml b/tests/data/astregs/DossierIndicateurCreationResponse.xml
new file mode 100644
index 00000000..00d5d437
--- /dev/null
+++ b/tests/data/astregs/DossierIndicateurCreationResponse.xml
@@ -0,0 +1 @@
+<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns1:creationResponse xmlns:ns1="http://gfi.astre.webservices/gad/dossierindicateur"><ns1:response><ns1:DossierIndicateurReturn><ns1:CodeDossier>2019_06407</ns1:CodeDossier><ns1:CodeInd_1>501</ns1:CodeInd_1><ns1:AnneeInd_1>2019</ns1:AnneeInd_1><ns1:ValInd_1>O</ns1:ValInd_1><ns1:IndAide>Non</ns1:IndAide></ns1:DossierIndicateurReturn></ns1:response></ns1:creationResponse></soapenv:Body></soapenv:Envelope>
diff --git a/tests/test_astregs.py b/tests/test_astregs.py
index 0a256233..925b3437 100644
--- a/tests/test_astregs.py
+++ b/tests/test_astregs.py
@@ -386,3 +386,27 @@ def test_create_grant_demand(mocked_post, mocked_get, connector, app):
     assert data['NomTiersDem'] == 'My Tiers'
     assert data['CodeStatut'] == 'ENR'
     assert data['DateCreation'] == '11/07/2019'
+
+
+@mock.patch('passerelle.utils.Request.get')
+@mock.patch('passerelle.utils.Request.post')
+def test_create_indana_indicator(mocked_post, mocked_get, connector, app):
+    mocked_get.return_value = mock.Mock(content=get_xml_file('DossierIndicateur.wsdl'))
+    mocked_post.return_value = mock.Mock(content=get_xml_file('DossierIndicateurCreationResponse.xml'),
+                                         headers={'Content-Type': 'text/xml'},
+                                         status_code=200)
+    payload = {
+        'CodeDossier': '2019_06407',
+        'CodeInd_1': '501',
+        'AnneeInd_1': '2019',
+        'ValInd_1': 'O'
+    }
+    resp = app.post_json('/astregs/test/create-indana-indicator', params=payload)
+    assert resp.json['err'] == 0
+    assert resp.json['data']
+    data = resp.json['data']
+    assert data['CodeDossier'] == '2019_06407'
+    assert data['CodeInd_1'] == '501'
+    assert data['AnneeInd_1'] == '2019'
+    assert data['ValInd_1'] == 'O'
+    assert data['IndAide'] == 'Non'
-- 
2.23.0.rc1

