1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """ Steps that run queries against the API.
9 Queries may either be run directly via PHP using the query script
10 or via the HTTP interface using php-cgi.
12 from pathlib import Path
18 from urllib.parse import urlencode
20 from utils import run_script
21 from http_responses import GenericResponse, SearchResponse, ReverseResponse, StatusResponse
22 from check_functions import Bbox, check_for_attributes
23 from table_compare import NominatimID
25 LOG = logging.getLogger(__name__)
28 'HTTP_HOST' : 'localhost',
29 'HTTP_USER_AGENT' : 'Mozilla/5.0 (X11; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0',
30 'HTTP_ACCEPT' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
31 'HTTP_ACCEPT_ENCODING' : 'gzip, deflate',
32 'HTTP_CONNECTION' : 'keep-alive',
33 'SERVER_SIGNATURE' : '<address>Nominatim BDD Tests</address>',
34 'SERVER_SOFTWARE' : 'Nominatim test',
35 'SERVER_NAME' : 'localhost',
36 'SERVER_ADDR' : '127.0.1.1',
38 'REMOTE_ADDR' : '127.0.0.1',
39 'DOCUMENT_ROOT' : '/var/www',
40 'REQUEST_SCHEME' : 'http',
41 'CONTEXT_PREFIX' : '/',
42 'SERVER_ADMIN' : 'webmaster@localhost',
43 'REMOTE_PORT' : '49319',
44 'GATEWAY_INTERFACE' : 'CGI/1.1',
45 'SERVER_PROTOCOL' : 'HTTP/1.1',
46 'REQUEST_METHOD' : 'GET',
47 'REDIRECT_STATUS' : 'CGI'
51 def make_todo_list(context, result_id):
53 context.execute_steps("then at least 1 result is returned")
54 return range(len(context.response.result))
56 context.execute_steps(f"then more than {result_id}results are returned")
57 return (int(result_id.strip()), )
60 def compare(operator, op1, op2):
61 if operator == 'less than':
63 elif operator == 'more than':
65 elif operator == 'exactly':
67 elif operator == 'at least':
69 elif operator == 'at most':
72 raise Exception("unknown operator '%s'" % operator)
75 def send_api_query(endpoint, params, fmt, context):
76 if fmt is not None and fmt.strip() != 'debug':
77 params['format'] = fmt.strip()
79 if context.table.headings[0] == 'param':
80 for line in context.table:
81 params[line['param']] = line['value']
83 for h in context.table.headings:
84 params[h] = context.table[0][h]
86 if context.nominatim.api_engine is None:
87 return send_api_query_php(endpoint, params, context)
89 return asyncio.run(context.nominatim.api_engine(endpoint, params,
90 Path(context.nominatim.website_dir.name),
91 context.nominatim.test_env,
92 getattr(context, 'http_headers', {})))
96 def send_api_query_php(endpoint, params, context):
97 env = dict(BASE_SERVER_ENV)
98 env['QUERY_STRING'] = urlencode(params)
100 env['SCRIPT_NAME'] = '/%s.php' % endpoint
101 env['REQUEST_URI'] = '%s?%s' % (env['SCRIPT_NAME'], env['QUERY_STRING'])
102 env['CONTEXT_DOCUMENT_ROOT'] = os.path.join(context.nominatim.website_dir.name, 'website')
103 env['SCRIPT_FILENAME'] = os.path.join(env['CONTEXT_DOCUMENT_ROOT'],
106 LOG.debug("Environment:" + json.dumps(env, sort_keys=True, indent=2))
108 if hasattr(context, 'http_headers'):
109 env.update(context.http_headers)
111 cmd = ['/usr/bin/env', 'php-cgi', '-f']
112 if context.nominatim.code_coverage_path:
113 env['XDEBUG_MODE'] = 'coverage'
114 env['COV_SCRIPT_FILENAME'] = env['SCRIPT_FILENAME']
115 env['COV_PHP_DIR'] = context.nominatim.src_dir
116 env['COV_TEST_NAME'] = '%s:%s' % (context.scenario.filename, context.scenario.line)
117 env['SCRIPT_FILENAME'] = \
118 os.path.join(os.path.split(__file__)[0], 'cgi-with-coverage.php')
119 cmd.append(env['SCRIPT_FILENAME'])
120 env['PHP_CODE_COVERAGE_FILE'] = context.nominatim.next_code_coverage_file()
122 cmd.append(env['SCRIPT_FILENAME'])
124 for k,v in params.items():
125 cmd.append("%s=%s" % (k, v))
127 outp, err = run_script(cmd, cwd=context.nominatim.website_dir.name, env=env)
129 assert len(err) == 0, "Unexpected PHP error: %s" % (err)
131 if outp.startswith('Status: '):
132 status = int(outp[8:11])
136 content_start = outp.find('\r\n\r\n')
138 return outp[content_start + 4:], status
140 @given(u'the HTTP header')
141 def add_http_header(context):
142 if not hasattr(context, 'http_headers'):
143 context.http_headers = {}
145 for h in context.table.headings:
146 envvar = 'HTTP_' + h.upper().replace('-', '_')
147 context.http_headers[envvar] = context.table[0][h]
150 @when(u'sending (?P<fmt>\S+ )?search query "(?P<query>.*)"(?P<addr> with address)?')
151 def website_search_request(context, fmt, query, addr):
156 params['addressdetails'] = '1'
157 if fmt and fmt.strip() == 'debug':
158 params['debug'] = '1'
160 outp, status = send_api_query('search', params, fmt, context)
162 context.response = SearchResponse(outp, fmt or 'json', status)
164 @when(u'sending (?P<fmt>\S+ )?reverse coordinates (?P<lat>.+)?,(?P<lon>.+)?')
165 def website_reverse_request(context, fmt, lat, lon):
171 if fmt and fmt.strip() == 'debug':
172 params['debug'] = '1'
174 outp, status = send_api_query('reverse', params, fmt, context)
176 context.response = ReverseResponse(outp, fmt or 'xml', status)
178 @when(u'sending (?P<fmt>\S+ )?reverse point (?P<nodeid>.+)')
179 def website_reverse_request(context, fmt, nodeid):
181 if fmt and fmt.strip() == 'debug':
182 params['debug'] = '1'
183 params['lon'], params['lat'] = (f'{c:f}' for c in context.osm.grid_node(int(nodeid)))
186 outp, status = send_api_query('reverse', params, fmt, context)
188 context.response = ReverseResponse(outp, fmt or 'xml', status)
192 @when(u'sending (?P<fmt>\S+ )?details query for (?P<query>.*)')
193 def website_details_request(context, fmt, query):
195 if query[0] in 'NWR':
196 nid = NominatimID(query)
197 params['osmtype'] = nid.typ
198 params['osmid'] = nid.oid
200 params['class'] = nid.cls
202 params['place_id'] = query
203 outp, status = send_api_query('details', params, fmt, context)
205 context.response = GenericResponse(outp, fmt or 'json', status)
207 @when(u'sending (?P<fmt>\S+ )?lookup query for (?P<query>.*)')
208 def website_lookup_request(context, fmt, query):
209 params = { 'osm_ids' : query }
210 outp, status = send_api_query('lookup', params, fmt, context)
212 context.response = SearchResponse(outp, fmt or 'xml', status)
214 @when(u'sending (?P<fmt>\S+ )?status query')
215 def website_status_request(context, fmt):
217 outp, status = send_api_query('status', params, fmt, context)
219 context.response = StatusResponse(outp, fmt or 'text', status)
221 @step(u'(?P<operator>less than|more than|exactly|at least|at most) (?P<number>\d+) results? (?:is|are) returned')
222 def validate_result_number(context, operator, number):
223 assert context.response.errorcode == 200
224 numres = len(context.response.result)
225 assert compare(operator, numres, int(number)), \
226 "Bad number of results: expected {} {}, got {}.".format(operator, number, numres)
228 @then(u'a HTTP (?P<status>\d+) is returned')
229 def check_http_return_status(context, status):
230 assert context.response.errorcode == int(status), \
231 "Return HTTP status is {}.".format(context.response.errorcode)
233 @then(u'the page contents equals "(?P<text>.+)"')
234 def check_page_content_equals(context, text):
235 assert context.response.page == text
237 @then(u'the result is valid (?P<fmt>\w+)')
238 def step_impl(context, fmt):
239 context.execute_steps("Then a HTTP 200 is returned")
240 assert context.response.format == fmt
242 @then(u'a (?P<fmt>\w+) user error is returned')
243 def check_page_error(context, fmt):
244 context.execute_steps("Then a HTTP 400 is returned")
245 assert context.response.format == fmt
248 assert re.search(r'<error>.+</error>', context.response.page, re.DOTALL) is not None
250 assert re.search(r'({"error":)', context.response.page, re.DOTALL) is not None
252 @then(u'result header contains')
253 def check_header_attr(context):
254 for line in context.table:
255 assert re.fullmatch(line['value'], context.response.header[line['attr']]) is not None, \
256 "attribute '%s': expected: '%s', got '%s'" % (
257 line['attr'], line['value'],
258 context.response.header[line['attr']])
261 @then(u'result header has (?P<neg>not )?attributes (?P<attrs>.*)')
262 def check_header_no_attr(context, neg, attrs):
263 check_for_attributes(context.response.header, attrs,
264 'absent' if neg else 'present')
267 @then(u'results contain')
268 def step_impl(context):
269 context.execute_steps("then at least 1 result is returned")
271 for line in context.table:
272 context.response.match_row(line, context=context)
275 @then(u'result (?P<lid>\d+ )?has (?P<neg>not )?attributes (?P<attrs>.*)')
276 def validate_attributes(context, lid, neg, attrs):
277 for i in make_todo_list(context, lid):
278 check_for_attributes(context.response.result[i], attrs,
279 'absent' if neg else 'present')
282 @then(u'result addresses contain')
283 def step_impl(context):
284 context.execute_steps("then at least 1 result is returned")
286 for line in context.table:
287 idx = int(line['ID']) if 'ID' in line.headings else None
289 for name, value in zip(line.headings, line.cells):
291 context.response.assert_address_field(idx, name, value)
293 @then(u'address of result (?P<lid>\d+) has(?P<neg> no)? types (?P<attrs>.*)')
294 def check_address(context, lid, neg, attrs):
295 context.execute_steps("then more than %s results are returned" % lid)
297 addr_parts = context.response.result[int(lid)]['address']
299 for attr in attrs.split(','):
301 assert attr not in addr_parts
303 assert attr in addr_parts
305 @then(u'address of result (?P<lid>\d+) (?P<complete>is|contains)')
306 def check_address(context, lid, complete):
307 context.execute_steps("then more than %s results are returned" % lid)
310 addr_parts = dict(context.response.result[lid]['address'])
312 for line in context.table:
313 context.response.assert_address_field(lid, line['type'], line['value'])
314 del addr_parts[line['type']]
317 assert len(addr_parts) == 0, "Additional address parts found: %s" % str(addr_parts)
320 @then(u'result (?P<lid>\d+ )?has bounding box in (?P<coords>[\d,.-]+)')
321 def check_bounding_box_in_area(context, lid, coords):
322 expected = Bbox(coords)
324 for idx in make_todo_list(context, lid):
325 res = context.response.result[idx]
326 check_for_attributes(res, 'boundingbox')
327 context.response.check_row(idx, res['boundingbox'] in expected,
328 f"Bbox is not contained in {expected}")
331 @then(u'result (?P<lid>\d+ )?has centroid in (?P<coords>[\d,.-]+)')
332 def check_centroid_in_area(context, lid, coords):
333 expected = Bbox(coords)
335 for idx in make_todo_list(context, lid):
336 res = context.response.result[idx]
337 check_for_attributes(res, 'lat,lon')
338 context.response.check_row(idx, (res['lon'], res['lat']) in expected,
339 f"Centroid is not inside {expected}")
342 @then(u'there are(?P<neg> no)? duplicates')
343 def check_for_duplicates(context, neg):
344 context.execute_steps("then at least 1 result is returned")
349 for res in context.response.result:
350 dup = (res['osm_type'], res['class'], res['type'], res['display_name'])
357 assert not has_dupe, "Found duplicate for %s" % (dup, )
359 assert has_dupe, "No duplicates found"