From 32f27333fde031928b8985d42b42ed75bc2da122 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Fri, 11 Feb 2022 10:08:16 +0100
Subject: [PATCH 2/2] wcs: allow "in your care" cell to be used in frontoffice
 (#58851)

---
 combo/apps/wcs/models.py                      |  2 ++
 .../wcs/templates/combo/wcs/care_forms.html   |  6 ++++
 tests/test_wcs.py                             | 30 +++++++++++++++----
 3 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index 02b523ef..e61f13e2 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -34,6 +34,7 @@ from requests.exceptions import RequestException
 from combo.data.library import register_cell_class
 from combo.data.models import CellBase, Page
 from combo.utils import requests
+from combo.utils.misc import is_portal_agent
 
 from .utils import get_wcs_json, get_wcs_services, is_wcs_enabled
 
@@ -736,6 +737,7 @@ class WcsCareFormsCell(CategoriesAndWcsSiteValidityMixin, CategoriesFilteringMix
 
     def get_cell_extra_context(self, context):
         context = super().get_cell_extra_context(context)
+        context['is_portal_agent'] = is_portal_agent()
 
         categories_filter = []
         if self.categories:
diff --git a/combo/apps/wcs/templates/combo/wcs/care_forms.html b/combo/apps/wcs/templates/combo/wcs/care_forms.html
index 6cc853b9..00150214 100644
--- a/combo/apps/wcs/templates/combo/wcs/care_forms.html
+++ b/combo/apps/wcs/templates/combo/wcs/care_forms.html
@@ -17,7 +17,11 @@
     {% for data in forms.data|dictsortreversed:"form_receipt_datetime" %}
     <tr>
       <td>{{ data.name }} {% if data.form_digest %}<br><small>{{ data.form_digest }}</small>{% endif %}</td>
+      {% if is_portal_agent %}
       <td><a href="{{ data.form_url_backoffice }}">{{ data.form_number }}</a></td>
+      {% else %}
+      <td><a href="{{ data.form_url }}">{{ data.form_number }}</a></td>
+      {% endif %}
       <td>{{ data.datetime|strptime:"%Y-%m-%d %H:%M:%S" }}</td>
       <td>{{ data.last_update_time|strptime:"%Y-%m-%d %H:%M:%S" }}</td>
       <td>{{ data.status }}</td>
@@ -26,6 +30,8 @@
     </tbody>
   </table>
   {% endif %}
+  {% if is_portal_agent %}
   <p><a class="pk-button" href="{{ forms.url }}backoffice/management/listing{% if forms.categories %}?category_slugs={{ forms.categories|join:"," }}{% endif %}">{% trans "See all forms" %}</a></p>
+  {% endif %}
 {% endfor %}
 {% endblock %}
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index 0404cee4..9f50c9d4 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -86,10 +86,12 @@ WCS_USER_FORMS_DATA = [
 WCS_FORMS_DATA = [
     {
         'form_receipt_datetime': '2019-10-17T16:46:03',
+        'form_url': '/foobar/1',
         'form_url_backoffice': '/backoffice/management/foobar/1/',
     },
     {
         'form_receipt_datetime': '2019-10-17T16:46:04',
+        'form_url': '/foobar/2',
         'form_url_backoffice': '/backoffice/management/foobar/2/',
     },
 ]
@@ -433,7 +435,7 @@ def mocked_requests_send(request, **kwargs):
         return MockedRequestResponse(content=json.dumps(data))
 
     for elem in data:
-        for key in ['url', 'form_url_backoffice']:
+        for key in ['url', 'form_url', 'form_url_backoffice']:
             if key not in elem:
                 continue
             elem_url = elem[key]
@@ -998,7 +1000,9 @@ def test_care_forms_cell_render(mock_send, context):
 
     context['synchronous'] = True  # to get fresh content
 
-    result = cell.render(context)
+    with mock.patch('combo.apps.wcs.models.is_portal_agent') as is_portal_agent:
+        is_portal_agent.return_value = True
+        result = cell.render(context)
 
     assert 'http://127.0.0.1:8999/backoffice/management/foobar/1' in result
     assert 'http://127.0.0.1:8999/backoffice/management/foobar/2' in result
@@ -1007,6 +1011,16 @@ def test_care_forms_cell_render(mock_send, context):
     assert 'http://127.0.0.2:8999/backoffice/management/foobar/2' in result
     assert '"http://127.0.0.2:8999/backoffice/management/listing"' in result
 
+    with mock.patch('combo.apps.wcs.models.is_portal_agent') as is_portal_agent:
+        is_portal_agent.return_value = False
+        result = cell.render(context)
+
+    assert 'http://127.0.0.1:8999/foobar/1' in result
+    assert 'http://127.0.0.1:8999/foobar/2' in result
+    assert 'http://127.0.0.2:8999/foobar/1' in result
+    assert 'http://127.0.0.2:8999/foobar/2' in result
+    assert '/listing' not in result
+
     data = cell.get_data(context)
     assert 'default' in data
     assert 'other' in data
@@ -1021,7 +1035,9 @@ def test_care_forms_cell_render(mock_send, context):
     # limit to a list of categories
     cell.categories = {'data': ['default:test-3', 'other:test-4']}
 
-    result = cell.render(context)
+    with mock.patch('combo.apps.wcs.models.is_portal_agent') as is_portal_agent:
+        is_portal_agent.return_value = True
+        result = cell.render(context)
     assert '"http://127.0.0.1:8999/backoffice/management/listing?category_slugs=test-3"' in result
     assert '"http://127.0.0.2:8999/backoffice/management/listing?category_slugs=test-4"' in result
 
@@ -1038,7 +1054,9 @@ def test_care_forms_cell_render(mock_send, context):
     # limit to a single category
     cell.categories = {'data': ['default:test-3']}
 
-    result = cell.render(context)
+    with mock.patch('combo.apps.wcs.models.is_portal_agent') as is_portal_agent:
+        is_portal_agent.return_value = True
+        result = cell.render(context)
     assert '"http://127.0.0.1:8999/backoffice/management/listing?category_slugs=test-3"' in result
     assert '"http://127.0.0.2:8999/backoffice/management/listing' not in result
 
@@ -1173,7 +1191,9 @@ def test_care_forms_cell_render_single_site(mock_send, context):
 
     context['synchronous'] = True  # to get fresh content
 
-    result = cell.render(context)
+    with mock.patch('combo.apps.wcs.models.is_portal_agent') as is_portal_agent:
+        is_portal_agent.return_value = True
+        result = cell.render(context)
     assert '"http://127.0.0.1:8999/backoffice/management/listing"' in result
     assert '"http://127.0.0.2:8999/backoffice/management/listing"' not in result
 
-- 
2.34.1

