From 5bd4eea46cee08f95d546046e308506d4416d29c Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Sat, 16 Oct 2021 15:04:16 +0200
Subject: [PATCH 2/5] plone_restapi: add endpoint to get content types (#57904)

---
 passerelle/apps/plone_restapi/models.py       | 12 +++++
 .../data/plone_restapi/get_content_types.json | 52 +++++++++++++++++++
 tests/test_plone_restapi.py                   | 12 +++++
 3 files changed, 76 insertions(+)
 create mode 100644 tests/data/plone_restapi/get_content_types.json

diff --git a/passerelle/apps/plone_restapi/models.py b/passerelle/apps/plone_restapi/models.py
index 27f3229a..ef9aa4ad 100644
--- a/passerelle/apps/plone_restapi/models.py
+++ b/passerelle/apps/plone_restapi/models.py
@@ -196,16 +196,28 @@ class PloneRestApi(BaseResource):
             params.update(parse_qsl(filter_expression))
         params['fullobjects'] = 'y'
         response = self.request(uri=uri, uid='@search', method='GET', params=params)
 
         for record in response.get('items') or []:
             self.adapt_record(record, text_template)
         return response.get('items') or []
 
+    @endpoint(
+        perm='can_access',
+        description=_('Get content types'),
+        display_order=1,
+    )
+    def get_content_types(self, request):
+        response = self.request(uri='@types', method='GET')
+        for record in response or []:
+            self.adapt_record(record, '{{ title }}', id_key='PLONE_id')
+            record['id'] = record['id'].split('/')[-1]
+        return {'data': response or []}
+
     @endpoint(
         perm='can_access',
         description=_('Fetch'),
         parameters={
             'uri': {'description': _('Uri')},
             'uid': {'description': _('Uid')},
             'text_template': {'description': _('Text template')},
         },
diff --git a/tests/data/plone_restapi/get_content_types.json b/tests/data/plone_restapi/get_content_types.json
new file mode 100644
index 00000000..1b2cf812
--- /dev/null
+++ b/tests/data/plone_restapi/get_content_types.json
@@ -0,0 +1,52 @@
+[
+  {
+    "@id": "https://annuaire.preprod.imio.be/@types/News Item",
+    "addable": false,
+    "title": "Actualit\u00e9"
+  },
+  {
+    "@id": "https://annuaire.preprod.imio.be/@types/Collection",
+    "addable": false,
+    "title": "Collection"
+  },
+  {
+    "@id": "https://annuaire.preprod.imio.be/@types/imio.directory.Contact",
+    "addable": false,
+    "title": "Contact"
+  },
+  {
+    "@id": "https://annuaire.preprod.imio.be/@types/Folder",
+    "addable": false,
+    "title": "Dossier"
+  },
+  {
+    "@id": "https://annuaire.preprod.imio.be/@types/imio.directory.Entity",
+    "addable": true,
+    "title": "Entit\u00e9"
+  },
+  {
+    "@id": "https://annuaire.preprod.imio.be/@types/File",
+    "addable": false,
+    "title": "Fichier"
+  },
+  {
+    "@id": "https://annuaire.preprod.imio.be/@types/Image",
+    "addable": false,
+    "title": "Image"
+  },
+  {
+    "@id": "https://annuaire.preprod.imio.be/@types/Link",
+    "addable": false,
+    "title": "Lien"
+  },
+  {
+    "@id": "https://annuaire.preprod.imio.be/@types/Document",
+    "addable": false,
+    "title": "Page Web"
+  },
+  {
+    "@id": "https://annuaire.preprod.imio.be/@types/Event",
+    "addable": false,
+    "title": "\u00c9v\u00e9nement"
+  }
+]
diff --git a/tests/test_plone_restapi.py b/tests/test_plone_restapi.py
index 74f5c718..4f8cfc19 100644
--- a/tests/test_plone_restapi.py
+++ b/tests/test_plone_restapi.py
@@ -279,16 +279,28 @@ def test_request_error(app, connector, token, exception, status_code, response,
         'text_template': '{{ title }} ({{ PLONE_type }})',
     }
     with utils.mock_url(url=url, response=response, status_code=status_code, exception=exception):
         resp = app.get(endpoint, params=params)
     assert resp.json['err']
     assert err_desc in resp.json['err_desc']
 
 
+def test_get_content_types(app, connector, token):
+    endpoint = utils.generic_endpoint_url('plone-restapi', 'get_content_types', slug=connector.slug)
+    assert endpoint == '/plone-restapi/my_connector/get_content_types'
+    url = connector.service_url + '/@types'
+    with utils.mock_url(url=url, response=json_get_data('get_content_types')):
+        resp = app.get(endpoint)
+    assert not resp.json['err']
+    assert len(resp.json['data']) == 10
+    assert resp.json['data'][2]['id'] == 'imio.directory.Contact'
+    assert resp.json['data'][2]['text'] == 'Contact'
+
+
 def test_create(app, connector, token):
     endpoint = utils.generic_endpoint_url('plone-restapi', 'create', slug=connector.slug)
     assert endpoint == '/plone-restapi/my_connector/create'
     url = connector.service_url + '/braine-l-alleud'
     payload = {
         '@type': 'imio.directory.Contact',
         'title': "Test Entr'ouvert",
         'type': 'organization',
-- 
2.30.2

