From 86d231965bed1e2a67fd667db5eb08dc468af101 Mon Sep 17 00:00:00 2001
From: Serghei Mihai <smihai@entrouvert.com>
Date: Thu, 7 Oct 2021 17:04:53 +0200
Subject: [PATCH] arcgis: remove Nancy specific 'district' endpoint (#57633)

---
 passerelle/apps/arcgis/models.py | 33 --------------
 tests/test_arcgis_nancy.py       | 76 --------------------------------
 tests/test_generic_endpoint.py   | 40 ++++++++++++-----
 3 files changed, 29 insertions(+), 120 deletions(-)
 delete mode 100644 tests/test_arcgis_nancy.py

diff --git a/passerelle/apps/arcgis/models.py b/passerelle/apps/arcgis/models.py
index 0dde6976..c82031c7 100644
--- a/passerelle/apps/arcgis/models.py
+++ b/passerelle/apps/arcgis/models.py
@@ -185,39 +185,6 @@ class ArcGIS(BaseResource, HTTPResource):
             return {'data': data, 'metadata': infos}
         return {'data': data}
 
-    @endpoint(
-        name='district',
-        description=_('Districts in Nancy Town'),
-        parameters={
-            'lat': {'description': _('Latitude')},
-            'lon': {'description': _('Longitude')},
-        },
-        show=False,
-    )
-    def district(self, request, lon=None, lat=None):
-        # deprecated endpoint
-        if 'NANCY_Grc' in self.base_url:
-            # Nancy URL used to contains folder, service and layer, remove them
-            self.base_url = 'https://geoservices.grand-nancy.org/arcgis/rest/'
-        features = self.mapservice_query(
-            request,
-            folder='public',
-            service='NANCY_Grc',
-            layer='0',
-            template='{{ attributes.NOM }}',
-            id_template='{{ attributes.NUMERO }}',
-            lon=lon,
-            lat=lat,
-        )['data']
-        if not features:
-            raise APIError('No features found.')
-        for feature in features:
-            del feature['attributes']
-        feature['id'] = int(feature['id'])
-        if len(features) == 1:
-            return {'data': features[0]}
-        return {'data': features}
-
     @endpoint(
         name='tile',
         description=_('Tiles layer'),
diff --git a/tests/test_arcgis_nancy.py b/tests/test_arcgis_nancy.py
deleted file mode 100644
index d84d55d4..00000000
--- a/tests/test_arcgis_nancy.py
+++ /dev/null
@@ -1,76 +0,0 @@
-import os
-
-import mock
-import pytest
-from django.contrib.contenttypes.models import ContentType
-from django.urls import reverse
-
-from passerelle.apps.arcgis.models import ArcGIS
-from passerelle.base.models import AccessRight, ApiUser
-from passerelle.compat import json_loads
-
-TEST_BASE_DIR = os.path.join(os.path.dirname(__file__), 'data', 'nancy_arcgis')
-
-
-def get_file_content(filename):
-    with open(os.path.join(TEST_BASE_DIR, filename), 'rb') as fd:
-        return fd.read()
-
-
-class MockedRequestsResponse(mock.Mock):
-    def json(self):
-        return json_loads(self.content)
-
-
-@pytest.fixture
-def setup(db):
-    api = ApiUser.objects.create(username='all', keytype='', key='')
-    arcgis = ArcGIS.objects.create(base_url='https://example.net/layer/0', slug='test')
-    obj_type = ContentType.objects.get_for_model(arcgis)
-    AccessRight.objects.create(
-        codename='can_access', apiuser=api, resource_type=obj_type, resource_pk=arcgis.pk
-    )
-    return arcgis
-
-
-@pytest.fixture
-def url():
-    return reverse('generic-endpoint', kwargs={'connector': 'arcgis', 'slug': 'test', 'endpoint': 'district'})
-
-
-def test_get_district_parameters_error(app, setup, url):
-    resp = app.get(url, params={'lon': 'lon', 'lat': 'lat'}, status=400)
-    assert resp.json['err_desc'] == '<lon> and <lat> must be floats'
-
-
-@mock.patch('passerelle.utils.Request.get')
-def test_get_district(mocked_get, app, setup, url):
-    mocked_get.return_value = MockedRequestsResponse(
-        content=get_file_content('sigresponse.json'), status_code=200
-    )
-
-    resp = app.get(url, params={'lon': 6.172122, 'lat': 48.673836}, status=200)
-    data = resp.json['data']
-    assert data['id'] == 4
-    assert data['text'] == 'HAUSSONVILLE / BLANDAN / MON DESERT / SAURUPT'
-
-
-@mock.patch('passerelle.utils.Request.get')
-def test_get_all_district(mocked_get, app, setup, url):
-    mocked_get.return_value = MockedRequestsResponse(
-        content=get_file_content('all_districts.json'), status_code=200
-    )
-
-    resp = app.get(url, status=200)
-    data = resp.json['data']
-    assert len(data) == 7
-
-
-@mock.patch('passerelle.utils.Request.get')
-def test_no_district(mocked_get, app, setup, url):
-    mocked_get.return_value = MockedRequestsResponse(content='{"features": []}', status_code=200)
-
-    resp = app.get(url, status=200)
-    assert resp.json['err'] == 1
-    assert resp.json['err_class'] == 'passerelle.utils.jsonresponse.APIError'
-    assert resp.json['err_desc'] == 'No features found.'
diff --git a/tests/test_generic_endpoint.py b/tests/test_generic_endpoint.py
index 60d8df61..e07a63f2 100644
--- a/tests/test_generic_endpoint.py
+++ b/tests/test_generic_endpoint.py
@@ -108,7 +108,17 @@ def test_proxy_logger(mocked_get, caplog, app, arcgis):
 
     log = ResourceLog.objects.filter(appname='arcgis', slug='test').delete()
     caplog.clear()
-    resp = app.get('/arcgis/test/district', params={'lon': 6.172122, 'lat': 48.673836}, status=200)
+    resp = app.get(
+        '/arcgis/test/mapservice-query',
+        params={
+            'lon': 6.172122,
+            'lat': 48.673836,
+            'service': 'test',
+            'template': '{{ attributes.NOM }}',
+            'id_template': '{{ attributes.NUMERO }}',
+        },
+        status=200,
+    )
 
     # Resource Custom DB Logger
     log = ResourceLog.objects.filter(appname='arcgis', slug='test').first()
@@ -117,9 +127,9 @@ def test_proxy_logger(mocked_get, caplog, app, arcgis):
     assert log.levelno == 20
     assert log.sourceip == '127.0.0.1'
     assert log.extra['connector'] == 'arcgis'
-    assert log.extra['connector_endpoint'] == 'district'
+    assert log.extra['connector_endpoint'] == 'mapservice-query'
     assert log.extra['connector_endpoint_method'] == 'GET'
-    assert '/arcgis/test/district?' in log.extra['connector_endpoint_url']
+    assert '/arcgis/test/mapservice-query?' in log.extra['connector_endpoint_url']
 
     # Resource Generic Logger
     for record in caplog.records:
@@ -128,17 +138,21 @@ def test_proxy_logger(mocked_get, caplog, app, arcgis):
         assert record.levelno == 20
         assert record.levelname == 'INFO'
         assert record.name == 'passerelle.resource.arcgis.test'
-        assert u"endpoint GET /arcgis/test/district?" in record.message
+        assert u"endpoint GET /arcgis/test/mapservice-query?" in record.message
 
     data = resp.json['data']
-    assert data['id'] == 4
-    assert data['text'] == 'HAUSSONVILLE / BLANDAN / MON DESERT / SAURUPT'
+    assert data[0]['id'] == '4'
+    assert data[0]['text'] == 'HAUSSONVILLE / BLANDAN / MON DESERT / SAURUPT'
 
     # when changing log level
     ResourceLog.objects.all().delete()
     arcgis.set_log_level('INFO')
     arcgis.save()
-    app.get('/arcgis/test/district', params={'lon': 6.172122, 'lat': 48.673836}, status=200)
+    app.get(
+        '/arcgis/test/mapservice-query',
+        params={'lon': 6.172122, 'lat': 48.673836, 'service': 'test'},
+        status=200,
+    )
     assert ResourceLog.objects.count() == 1
 
     arcgis.logger.warning('first warning')
@@ -154,7 +168,11 @@ def test_proxy_logger_transaction_id(mocked_send, app, arcgis):
     arcgis.log_evel = 'DEBUG'
     arcgis.base_url = 'https://example.net/'
     arcgis.save()
-    resp = app.get('/arcgis/test/district', params={'lon': 6.172122, 'lat': 48.673836}, status=200)
+    resp = app.get(
+        '/arcgis/test/mapservice-query',
+        params={'lon': 6.172122, 'lat': 48.673836, 'service': 'test'},
+        status=200,
+    )
 
     log1, log2 = ResourceLog.objects.filter(appname='arcgis', slug='test').all()
     assert log1.extra['transaction_id'] == log2.extra['transaction_id']
@@ -168,13 +186,13 @@ def test_proxy_logger_on_405(mocked_patch, caplog, app, arcgis):
     arcgis.log_evel = 'WARNING'
     log = ResourceLog.objects.filter(appname='arcgis', slug='test').delete()
     caplog.clear()
-    resp = app.patch('/arcgis/test/district', status=405)
+    resp = app.patch('/arcgis/test/mapservice-query', status=405)
     assert not resp.text
 
     # Resource Custom DB Logger
     log = ResourceLog.objects.filter(appname='arcgis', slug='test').first()
     assert log.levelno == 30
-    assert log.message == 'endpoint PATCH /arcgis/test/district (=> 405)'
+    assert log.message == 'endpoint PATCH /arcgis/test/mapservice-query (=> 405)'
     assert log.extra['connector_endpoint_method'] == ['GET']
 
     # Resource Generic Logger
@@ -183,7 +201,7 @@ def test_proxy_logger_on_405(mocked_patch, caplog, app, arcgis):
             continue
         assert record.levelno == 30
         assert record.levelname == 'WARNING'
-        assert record.message == 'endpoint PATCH /arcgis/test/district (=> 405)'
+        assert record.message == 'endpoint PATCH /arcgis/test/mapservice-query (=> 405)'
 
 
 class FakeConnectorBase(object):
-- 
2.33.0

