0001-arcgis-remove-Nancy-specific-district-endpoint-57633.patch
passerelle/apps/arcgis/models.py | ||
---|---|---|
185 | 185 |
return {'data': data, 'metadata': infos} |
186 | 186 |
return {'data': data} |
187 | 187 | |
188 |
@endpoint( |
|
189 |
name='district', |
|
190 |
description=_('Districts in Nancy Town'), |
|
191 |
parameters={ |
|
192 |
'lat': {'description': _('Latitude')}, |
|
193 |
'lon': {'description': _('Longitude')}, |
|
194 |
}, |
|
195 |
show=False, |
|
196 |
) |
|
197 |
def district(self, request, lon=None, lat=None): |
|
198 |
# deprecated endpoint |
|
199 |
if 'NANCY_Grc' in self.base_url: |
|
200 |
# Nancy URL used to contains folder, service and layer, remove them |
|
201 |
self.base_url = 'https://geoservices.grand-nancy.org/arcgis/rest/' |
|
202 |
features = self.mapservice_query( |
|
203 |
request, |
|
204 |
folder='public', |
|
205 |
service='NANCY_Grc', |
|
206 |
layer='0', |
|
207 |
template='{{ attributes.NOM }}', |
|
208 |
id_template='{{ attributes.NUMERO }}', |
|
209 |
lon=lon, |
|
210 |
lat=lat, |
|
211 |
)['data'] |
|
212 |
if not features: |
|
213 |
raise APIError('No features found.') |
|
214 |
for feature in features: |
|
215 |
del feature['attributes'] |
|
216 |
feature['id'] = int(feature['id']) |
|
217 |
if len(features) == 1: |
|
218 |
return {'data': features[0]} |
|
219 |
return {'data': features} |
|
220 | ||
221 | 188 |
@endpoint( |
222 | 189 |
name='tile', |
223 | 190 |
description=_('Tiles layer'), |
tests/test_arcgis_nancy.py | ||
---|---|---|
1 |
import os |
|
2 | ||
3 |
import mock |
|
4 |
import pytest |
|
5 |
from django.contrib.contenttypes.models import ContentType |
|
6 |
from django.urls import reverse |
|
7 | ||
8 |
from passerelle.apps.arcgis.models import ArcGIS |
|
9 |
from passerelle.base.models import AccessRight, ApiUser |
|
10 |
from passerelle.compat import json_loads |
|
11 | ||
12 |
TEST_BASE_DIR = os.path.join(os.path.dirname(__file__), 'data', 'nancy_arcgis') |
|
13 | ||
14 | ||
15 |
def get_file_content(filename): |
|
16 |
with open(os.path.join(TEST_BASE_DIR, filename), 'rb') as fd: |
|
17 |
return fd.read() |
|
18 | ||
19 | ||
20 |
class MockedRequestsResponse(mock.Mock): |
|
21 |
def json(self): |
|
22 |
return json_loads(self.content) |
|
23 | ||
24 | ||
25 |
@pytest.fixture |
|
26 |
def setup(db): |
|
27 |
api = ApiUser.objects.create(username='all', keytype='', key='') |
|
28 |
arcgis = ArcGIS.objects.create(base_url='https://example.net/layer/0', slug='test') |
|
29 |
obj_type = ContentType.objects.get_for_model(arcgis) |
|
30 |
AccessRight.objects.create( |
|
31 |
codename='can_access', apiuser=api, resource_type=obj_type, resource_pk=arcgis.pk |
|
32 |
) |
|
33 |
return arcgis |
|
34 | ||
35 | ||
36 |
@pytest.fixture |
|
37 |
def url(): |
|
38 |
return reverse('generic-endpoint', kwargs={'connector': 'arcgis', 'slug': 'test', 'endpoint': 'district'}) |
|
39 | ||
40 | ||
41 |
def test_get_district_parameters_error(app, setup, url): |
|
42 |
resp = app.get(url, params={'lon': 'lon', 'lat': 'lat'}, status=400) |
|
43 |
assert resp.json['err_desc'] == '<lon> and <lat> must be floats' |
|
44 | ||
45 | ||
46 |
@mock.patch('passerelle.utils.Request.get') |
|
47 |
def test_get_district(mocked_get, app, setup, url): |
|
48 |
mocked_get.return_value = MockedRequestsResponse( |
|
49 |
content=get_file_content('sigresponse.json'), status_code=200 |
|
50 |
) |
|
51 | ||
52 |
resp = app.get(url, params={'lon': 6.172122, 'lat': 48.673836}, status=200) |
|
53 |
data = resp.json['data'] |
|
54 |
assert data['id'] == 4 |
|
55 |
assert data['text'] == 'HAUSSONVILLE / BLANDAN / MON DESERT / SAURUPT' |
|
56 | ||
57 | ||
58 |
@mock.patch('passerelle.utils.Request.get') |
|
59 |
def test_get_all_district(mocked_get, app, setup, url): |
|
60 |
mocked_get.return_value = MockedRequestsResponse( |
|
61 |
content=get_file_content('all_districts.json'), status_code=200 |
|
62 |
) |
|
63 | ||
64 |
resp = app.get(url, status=200) |
|
65 |
data = resp.json['data'] |
|
66 |
assert len(data) == 7 |
|
67 | ||
68 | ||
69 |
@mock.patch('passerelle.utils.Request.get') |
|
70 |
def test_no_district(mocked_get, app, setup, url): |
|
71 |
mocked_get.return_value = MockedRequestsResponse(content='{"features": []}', status_code=200) |
|
72 | ||
73 |
resp = app.get(url, status=200) |
|
74 |
assert resp.json['err'] == 1 |
|
75 |
assert resp.json['err_class'] == 'passerelle.utils.jsonresponse.APIError' |
|
76 |
assert resp.json['err_desc'] == 'No features found.' |
tests/test_generic_endpoint.py | ||
---|---|---|
108 | 108 | |
109 | 109 |
log = ResourceLog.objects.filter(appname='arcgis', slug='test').delete() |
110 | 110 |
caplog.clear() |
111 |
resp = app.get('/arcgis/test/district', params={'lon': 6.172122, 'lat': 48.673836}, status=200) |
|
111 |
resp = app.get( |
|
112 |
'/arcgis/test/mapservice-query', |
|
113 |
params={ |
|
114 |
'lon': 6.172122, |
|
115 |
'lat': 48.673836, |
|
116 |
'service': 'test', |
|
117 |
'template': '{{ attributes.NOM }}', |
|
118 |
'id_template': '{{ attributes.NUMERO }}', |
|
119 |
}, |
|
120 |
status=200, |
|
121 |
) |
|
112 | 122 | |
113 | 123 |
# Resource Custom DB Logger |
114 | 124 |
log = ResourceLog.objects.filter(appname='arcgis', slug='test').first() |
... | ... | |
117 | 127 |
assert log.levelno == 20 |
118 | 128 |
assert log.sourceip == '127.0.0.1' |
119 | 129 |
assert log.extra['connector'] == 'arcgis' |
120 |
assert log.extra['connector_endpoint'] == 'district'
|
|
130 |
assert log.extra['connector_endpoint'] == 'mapservice-query'
|
|
121 | 131 |
assert log.extra['connector_endpoint_method'] == 'GET' |
122 |
assert '/arcgis/test/district?' in log.extra['connector_endpoint_url']
|
|
132 |
assert '/arcgis/test/mapservice-query?' in log.extra['connector_endpoint_url']
|
|
123 | 133 | |
124 | 134 |
# Resource Generic Logger |
125 | 135 |
for record in caplog.records: |
... | ... | |
128 | 138 |
assert record.levelno == 20 |
129 | 139 |
assert record.levelname == 'INFO' |
130 | 140 |
assert record.name == 'passerelle.resource.arcgis.test' |
131 |
assert u"endpoint GET /arcgis/test/district?" in record.message
|
|
141 |
assert u"endpoint GET /arcgis/test/mapservice-query?" in record.message
|
|
132 | 142 | |
133 | 143 |
data = resp.json['data'] |
134 |
assert data['id'] == 4
|
|
135 |
assert data['text'] == 'HAUSSONVILLE / BLANDAN / MON DESERT / SAURUPT' |
|
144 |
assert data[0]['id'] == '4'
|
|
145 |
assert data[0]['text'] == 'HAUSSONVILLE / BLANDAN / MON DESERT / SAURUPT'
|
|
136 | 146 | |
137 | 147 |
# when changing log level |
138 | 148 |
ResourceLog.objects.all().delete() |
139 | 149 |
arcgis.set_log_level('INFO') |
140 | 150 |
arcgis.save() |
141 |
app.get('/arcgis/test/district', params={'lon': 6.172122, 'lat': 48.673836}, status=200) |
|
151 |
app.get( |
|
152 |
'/arcgis/test/mapservice-query', |
|
153 |
params={'lon': 6.172122, 'lat': 48.673836, 'service': 'test'}, |
|
154 |
status=200, |
|
155 |
) |
|
142 | 156 |
assert ResourceLog.objects.count() == 1 |
143 | 157 | |
144 | 158 |
arcgis.logger.warning('first warning') |
... | ... | |
154 | 168 |
arcgis.log_evel = 'DEBUG' |
155 | 169 |
arcgis.base_url = 'https://example.net/' |
156 | 170 |
arcgis.save() |
157 |
resp = app.get('/arcgis/test/district', params={'lon': 6.172122, 'lat': 48.673836}, status=200) |
|
171 |
resp = app.get( |
|
172 |
'/arcgis/test/mapservice-query', |
|
173 |
params={'lon': 6.172122, 'lat': 48.673836, 'service': 'test'}, |
|
174 |
status=200, |
|
175 |
) |
|
158 | 176 | |
159 | 177 |
log1, log2 = ResourceLog.objects.filter(appname='arcgis', slug='test').all() |
160 | 178 |
assert log1.extra['transaction_id'] == log2.extra['transaction_id'] |
... | ... | |
168 | 186 |
arcgis.log_evel = 'WARNING' |
169 | 187 |
log = ResourceLog.objects.filter(appname='arcgis', slug='test').delete() |
170 | 188 |
caplog.clear() |
171 |
resp = app.patch('/arcgis/test/district', status=405)
|
|
189 |
resp = app.patch('/arcgis/test/mapservice-query', status=405)
|
|
172 | 190 |
assert not resp.text |
173 | 191 | |
174 | 192 |
# Resource Custom DB Logger |
175 | 193 |
log = ResourceLog.objects.filter(appname='arcgis', slug='test').first() |
176 | 194 |
assert log.levelno == 30 |
177 |
assert log.message == 'endpoint PATCH /arcgis/test/district (=> 405)'
|
|
195 |
assert log.message == 'endpoint PATCH /arcgis/test/mapservice-query (=> 405)'
|
|
178 | 196 |
assert log.extra['connector_endpoint_method'] == ['GET'] |
179 | 197 | |
180 | 198 |
# Resource Generic Logger |
... | ... | |
183 | 201 |
continue |
184 | 202 |
assert record.levelno == 30 |
185 | 203 |
assert record.levelname == 'WARNING' |
186 |
assert record.message == 'endpoint PATCH /arcgis/test/district (=> 405)'
|
|
204 |
assert record.message == 'endpoint PATCH /arcgis/test/mapservice-query (=> 405)'
|
|
187 | 205 | |
188 | 206 | |
189 | 207 |
class FakeConnectorBase(object): |
190 |
- |