From 721f00b520f389413aaff3398eeffeeb017e765d Mon Sep 17 00:00:00 2001
From: Serghei Mihai <smihai@entrouvert.com>
Date: Wed, 28 Aug 2019 11:29:19 +0200
Subject: [PATCH] astregs; add endpoints to update and delection indana
 indicator (#35627)

---
 passerelle/apps/astregs/models.py             | 54 +++++++++++++++++++
 .../DossierIndicateurModificationResponse.xml |  1 +
 .../DossierIndicateurSuppressionResponse.xml  |  1 +
 tests/test_astregs.py                         | 47 ++++++++++++++++
 4 files changed, 103 insertions(+)
 create mode 100644 tests/data/astregs/DossierIndicateurModificationResponse.xml
 create mode 100644 tests/data/astregs/DossierIndicateurSuppressionResponse.xml

diff --git a/passerelle/apps/astregs/models.py b/passerelle/apps/astregs/models.py
index 552a2edd..a56abc41 100644
--- a/passerelle/apps/astregs/models.py
+++ b/passerelle/apps/astregs/models.py
@@ -260,6 +260,28 @@ INDANA_SCHEMA = {
     }
 }
 
+INDANA_DELETION_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
+        }
+    }
+}
+
+
 
 class AstreGS(BaseResource):
     wsdl_base_url = models.URLField(_('Webservices base URL'))
@@ -585,6 +607,38 @@ class AstreGS(BaseResource):
         return {'data': serialize_object(r)}
 
 
+    @endpoint(name='update-indana-indicator', perm='can_access',
+              post={'description': _('Update indana indicator'),
+                    'request_body': {
+                        'schema': {
+                            'application/json': INDANA_SCHEMA
+                        }
+                    }
+              }
+    )
+    def update_indana_indicator(self, request, post_data):
+        r = self.call('DossierIndicateur', 'Modification',
+                      DossierIndicateur=post_data
+        )
+        return {'data': serialize_object(r)}
+
+
+    @endpoint(name='delete-indana-indicator', perm='can_access',
+              post={'description': _('Delete indana indicator'),
+                    'request_body': {
+                        'schema': {
+                            'application/json': INDANA_DELETION_SCHEMA
+                        }
+                    }
+              }
+    )
+    def delete_indana_indicator(self, request, post_data):
+        r = self.call('DossierIndicateur', 'Suppression',
+                      DossierIndicateurCle=post_data
+        )
+        return {'data': serialize_object(r)}
+
+
 class Link(models.Model):
     resource = models.ForeignKey(AstreGS)
     name_id = models.CharField(max_length=32)
diff --git a/tests/data/astregs/DossierIndicateurModificationResponse.xml b/tests/data/astregs/DossierIndicateurModificationResponse.xml
new file mode 100644
index 00000000..5efee0c4
--- /dev/null
+++ b/tests/data/astregs/DossierIndicateurModificationResponse.xml
@@ -0,0 +1 @@
+<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns1:modificationResponse 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>N</ns1:ValInd_1><ns1:IndAide>Non</ns1:IndAide></ns1:DossierIndicateurReturn></ns1:response></ns1:modificationResponse></soapenv:Body></soapenv:Envelope>
diff --git a/tests/data/astregs/DossierIndicateurSuppressionResponse.xml b/tests/data/astregs/DossierIndicateurSuppressionResponse.xml
new file mode 100644
index 00000000..0021ff00
--- /dev/null
+++ b/tests/data/astregs/DossierIndicateurSuppressionResponse.xml
@@ -0,0 +1 @@
+<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns1:suppressionResponse 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>N</ns1:ValInd_1><ns1:IndAide>Non</ns1:IndAide></ns1:DossierIndicateurReturn></ns1:response></ns1:suppressionResponse></soapenv:Body></soapenv:Envelope>
diff --git a/tests/test_astregs.py b/tests/test_astregs.py
index b3bc9f03..90546a27 100644
--- a/tests/test_astregs.py
+++ b/tests/test_astregs.py
@@ -452,3 +452,50 @@ def test_create_indana_indicator(mocked_post, mocked_get, connector, app):
     assert data['AnneeInd_1'] == '2019'
     assert data['ValInd_1'] == 'O'
     assert data['IndAide'] == 'Non'
+
+
+@mock.patch('passerelle.utils.Request.get')
+@mock.patch('passerelle.utils.Request.post')
+def test_update_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('DossierIndicateurModificationResponse.xml'),
+                                         headers={'Content-Type': 'text/xml'},
+                                         status_code=200)
+    payload = {
+        'CodeDossier': '2019_06407',
+        'CodeInd_1': '501',
+        'AnneeInd_1': '2019',
+        'ValInd_1': 'N'
+    }
+    resp = app.post_json('/astregs/test/update-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'] == 'N'
+    assert data['IndAide'] == 'Non'
+
+
+@mock.patch('passerelle.utils.Request.get')
+@mock.patch('passerelle.utils.Request.post')
+def test_delete_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('DossierIndicateurSuppressionResponse.xml'),
+                                         headers={'Content-Type': 'text/xml'},
+                                         status_code=200)
+    payload = {
+        'CodeDossier': '2019_06407',
+        'CodeInd_1': '501',
+        'AnneeInd_1': '2019'
+    }
+    resp = app.post_json('/astregs/test/delete-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'] == 'N'
+    assert data['IndAide'] == 'Non'
-- 
2.23.0

