From 9c68cf8d4a6cd609682f3ad7ff577fd783947214 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Thu, 14 Nov 2019 19:39:53 +0100
Subject: [PATCH] atal: return works comment and hide raw data (#37669)

---
 passerelle/apps/atal/models.py | 35 +++++++++++++++++++++++-----------
 tests/test_atal.py             | 21 ++++++++++++++++----
 2 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/passerelle/apps/atal/models.py b/passerelle/apps/atal/models.py
index a6ad709f..dcbf9a5f 100644
--- a/passerelle/apps/atal/models.py
+++ b/passerelle/apps/atal/models.py
@@ -201,10 +201,15 @@ class ATALConnector(BaseResource):
         parameters={
             'demand_number': {
                 'description': _('Demande number'), 'example_value': 'DIT18050001'
+            },
+            'full': {
+                'description': _('Full'),
+                'example_value': 'true',
+                'type': 'bool',
             }
         }
     )
-    def infos(self, request, demand_number):
+    def infos(self, request, demand_number, full=False):
         demand_details = helpers.serialize_object(
             self._soap_call(
                 wsdl='DemandeService', method='retrieveDetailsDemande',
@@ -218,7 +223,7 @@ class ATALConnector(BaseResource):
             raise APIError('Could not get a status')
 
         responses = (demand_details.get('reponses') or {}).get('Reponse') or []
-        comments = []
+        demand_comments = []
         if responses:
             for response in responses:
                 comment = {
@@ -227,22 +232,25 @@ class ATALConnector(BaseResource):
                 }
                 if 'dateReponse' in response:
                     comment['date'] = dateformat.format(response['dateReponse'], DATE_FORMAT)
-                comments.append(comment)
+                demand_comments.append(comment)
 
-        last_comment = {
+        demand_comment = {
             'text': None,
             'date': None
         }
-        if comments:
-            last_comment = comments[-1]
+        if demand_comments:
+            demand_comment = demand_comments[-1]
 
         data = {
             'status': status,
-            'demand_details': demand_details,
-            'comments': comments,
-            'last_comment': last_comment,
-
+            'demand_comment': demand_comment,
+            'demand_details': None,
+            'demand_comments': []
         }
+        if full:
+            data['demand_details'] = demand_details
+            data['demand_comments'] = demand_comments
+
         if status not in ('PRISE EN COMPTE', 'ARCHIVEE'):
             return {
                 'data': data
@@ -258,8 +266,13 @@ class ATALConnector(BaseResource):
         if not status:
             raise APIError('Could not get a status')
 
-        data['works_status'] = works_status
         data['status'] = status
+        data['works_comment'] = works_status.get('commentaires', '')
+        data['works_status'] = None
+
+        if full:
+            data['works_status'] = works_status
+
         return {
             'data': data
         }
diff --git a/tests/test_atal.py b/tests/test_atal.py
index c198b932..699896db 100644
--- a/tests/test_atal.py
+++ b/tests/test_atal.py
@@ -298,9 +298,22 @@ def test_infos(app, connector, monkeypatch):
     assert response.json['err'] == 0
     data = response.json['data']
     assert data['status'] == u'travaux pas commencés'
+    assert data['demand_comment'] == {
+        'text': 'bonjour atal',
+        'date': 'Thursday 24 October 2019, 16:51'
+    }
+    assert data['demand_comments'] == []
+    assert data['works_comment'] is None
+    assert data['works_status'] is None
 
-    assert len(data['comments']) == 2
-    assert data['comments'][0] == {
+    # full in query string
+    monkeypatch.setattr(
+        passerelle.utils.Request, 'post', mock.Mock(side_effect=[api_response1, api_response2])
+    )
+    response = app.get('/atal/slug-atal/infos/DIT18050001/?full=true')
+    data = response.json['data']
+    assert len(data['demand_comments']) == 2
+    assert data['demand_comments'][0] == {
         'text': 'OK',
         'date': 'Thursday 24 October 2019, 16:48'
     }
@@ -308,5 +321,5 @@ def test_infos(app, connector, monkeypatch):
         'text': 'bonjour atal',
         'date': 'Thursday 24 October 2019, 16:51'
     }
-    assert data['comments'][1] == last_comment
-    assert data['last_comment'] == last_comment
+    assert data['demand_comments'][1] == last_comment
+    assert data['demand_comment'] == last_comment
-- 
2.20.1

