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 setting up the website scripts.
14 from nominatim.tools import refresh
17 def test_script(tmp_path):
18 (tmp_path / 'php').mkdir()
20 website_dir = (tmp_path / 'php' / 'website')
23 def _create_file(code):
24 outfile = website_dir / 'reverse-only-search.php'
25 outfile.write_text('<?php\n{}\n'.format(code), 'utf-8')
31 def run_website_script(tmp_path, project_env, temp_db_conn):
32 project_env.lib_dir.php = tmp_path / 'php'
35 refresh.setup_website(tmp_path, project_env, temp_db_conn)
37 proc = subprocess.run(['/usr/bin/env', 'php', '-Cq',
38 tmp_path / 'search.php'], check=False)
40 return proc.returncode
45 def test_basedir_created(tmp_path, project_env, temp_db_conn):
46 webdir = tmp_path / 'website'
48 assert not webdir.exists()
50 refresh.setup_website(webdir, project_env, temp_db_conn)
52 assert webdir.exists()
55 @pytest.mark.parametrize("setting,retval", (('yes', 10), ('no', 20)))
56 def test_setup_website_check_bool(monkeypatch, test_script, run_website_script,
58 monkeypatch.setenv('NOMINATIM_CORS_NOACCESSCONTROL', setting)
60 test_script('exit(CONST_NoAccessControl ? 10 : 20);')
62 assert run_website_script() == retval
65 @pytest.mark.parametrize("setting", (0, 10, 99067))
66 def test_setup_website_check_int(monkeypatch, test_script, run_website_script, setting):
67 monkeypatch.setenv('NOMINATIM_LOOKUP_MAX_COUNT', str(setting))
69 test_script('exit(CONST_Places_Max_ID_count == {} ? 10 : 20);'.format(setting))
71 assert run_website_script() == 10
74 def test_setup_website_check_empty_str(monkeypatch, test_script, run_website_script):
75 monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', '')
77 test_script('exit(CONST_Default_Language === false ? 10 : 20);')
79 assert run_website_script() == 10
82 def test_setup_website_check_str(monkeypatch, test_script, run_website_script):
83 monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', 'ffde 2')
85 test_script('exit(CONST_Default_Language === "ffde 2" ? 10 : 20);')
87 assert run_website_script() == 10
90 def test_relative_log_file(project_env, monkeypatch, test_script, run_website_script):
91 monkeypatch.setenv('NOMINATIM_LOG_FILE', 'access.log')
93 expected_file = str(project_env.project_dir / 'access.log')
94 test_script(f'exit(CONST_Log_File === "{expected_file}" ? 10 : 20);')
96 assert run_website_script() == 10
98 def test_variable_with_bracket(project_env, monkeypatch, test_script, run_website_script):
99 monkeypatch.setenv('NOMINATIM_DATABASE_DSN', 'pgsql:dbname=nominatim;user=foo;password=4{5')
101 test_script('exit(CONST_Database_DSN === "pgsql:dbname=nominatim;user=foo;password=4{5" ? 10 : 20);')
103 assert run_website_script() == 10