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.
 
   8 Tests for API access commands of command-line interface wrapper.
 
  12 import nominatim.clicmd.api
 
  15 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup', 'details', 'status')))
 
  16 def test_no_api_without_phpcgi(src_dir, endpoint):
 
  17     with pytest.raises(SystemExit):
 
  18         nominatim.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
 
  19                                 osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
 
  20                                 phplib_dir=str(src_dir / 'lib-php'),
 
  21                                 data_dir=str(src_dir / 'data'),
 
  23                                 sqllib_dir=str(src_dir / 'lib-sql'),
 
  24                                 config_dir=str(src_dir / 'settings'),
 
  28 @pytest.mark.parametrize("params", [('search', '--query', 'new'),
 
  29                                     ('search', '--city', 'Berlin'),
 
  30                                     ('reverse', '--lat', '0', '--lon', '0', '--zoom', '13'),
 
  31                                     ('lookup', '--id', 'N1'),
 
  32                                     ('details', '--node', '1'),
 
  33                                     ('details', '--way', '1'),
 
  34                                     ('details', '--relation', '1'),
 
  35                                     ('details', '--place_id', '10001'),
 
  39     @pytest.fixture(autouse=True)
 
  40     def setup_cli_call(self, cli_call):
 
  41         self.call_nominatim = cli_call
 
  43     def test_api_commands_simple(self, mock_func_factory, params, tmp_path):
 
  44         (tmp_path / 'website').mkdir()
 
  45         (tmp_path / 'website' / (params[0] + '.php')).write_text('')
 
  46         mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
 
  48         assert self.call_nominatim(*params, '--project-dir', str(tmp_path)) == 0
 
  50         assert mock_run_api.called == 1
 
  51         assert mock_run_api.last_args[0] == params[0]
 
  54     def test_bad_project_idr(self, mock_func_factory, params):
 
  55         mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
 
  57         assert self.call_nominatim(*params) == 1
 
  60  'search': ('--query', 'somewhere'),
 
  61  'reverse': ('--lat', '20', '--lon', '30'),
 
  62  'lookup': ('--id', 'R345345'),
 
  63  'details': ('--node', '324')
 
  66 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup')))
 
  67 class TestCliApiCommonParameters:
 
  69     @pytest.fixture(autouse=True)
 
  70     def setup_website_dir(self, cli_call, project_env, endpoint):
 
  71         self.endpoint = endpoint
 
  72         self.cli_call = cli_call
 
  73         self.project_dir = project_env.project_dir
 
  74         (self.project_dir / 'website').mkdir()
 
  77     def expect_param(self, param, expected):
 
  78         (self.project_dir / 'website' / (self.endpoint + '.php')).write_text(f"""<?php
 
  79         exit($_GET['{param}']  == '{expected}' ? 0 : 10);
 
  83     def call_nominatim(self, *params):
 
  84         return self.cli_call(self.endpoint, *QUERY_PARAMS[self.endpoint],
 
  85                              '--project-dir', str(self.project_dir), *params)
 
  88     def test_param_output(self):
 
  89         self.expect_param('format', 'xml')
 
  90         assert self.call_nominatim('--format', 'xml') == 0
 
  93     def test_param_lang(self):
 
  94         self.expect_param('accept-language', 'de')
 
  95         assert self.call_nominatim('--lang', 'de') == 0
 
  96         assert self.call_nominatim('--accept-language', 'de') == 0
 
  99     @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
 
 100     def test_param_extradata(self, param):
 
 101         self.expect_param(param, '1')
 
 103         assert self.call_nominatim('--' + param) == 0
 
 105     def test_param_polygon_output(self):
 
 106         self.expect_param('polygon_geojson', '1')
 
 108         assert self.call_nominatim('--polygon-output', 'geojson') == 0
 
 111     def test_param_polygon_threshold(self):
 
 112         self.expect_param('polygon_threshold', '0.3452')
 
 114         assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
 
 117 def test_cli_search_param_bounded(cli_call, project_env):
 
 118     webdir = project_env.project_dir / 'website'
 
 120     (webdir / 'search.php').write_text(f"""<?php
 
 121         exit($_GET['bounded']  == '1' ? 0 : 10);
 
 124     assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
 
 128 def test_cli_search_param_dedupe(cli_call, project_env):
 
 129     webdir = project_env.project_dir / 'website'
 
 131     (webdir / 'search.php').write_text(f"""<?php
 
 132         exit($_GET['dedupe']  == '0' ? 0 : 10);
 
 135     assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
 
 139 def test_cli_details_param_class(cli_call, project_env):
 
 140     webdir = project_env.project_dir / 'website'
 
 142     (webdir / 'details.php').write_text(f"""<?php
 
 143         exit($_GET['class']  == 'highway' ? 0 : 10);
 
 146     assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
 
 147                     '--class', 'highway') == 0
 
 150 @pytest.mark.parametrize('param', ('lang', 'accept-language'))
 
 151 def test_cli_details_param_lang(cli_call, project_env, param):
 
 152     webdir = project_env.project_dir / 'website'
 
 154     (webdir / 'details.php').write_text(f"""<?php
 
 155         exit($_GET['accept-language']  == 'es' ? 0 : 10);
 
 158     assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
 
 159                     '--' + param, 'es') == 0