mirror of
https://github.com/searxng/searxng.git
synced 2024-11-15 01:00:12 +01:00
2039060b64
The intention of this PR is to modernize the settings_loader implementations. The concept is old (remember, this is partly from 2014), back then we only had one config file, meanwhile we have had a folder with config files for a very long time. Callers can now load a YAML configuration from this folder as follows :: settings_loader.get_yaml_cfg('my-config.yml') - BTW this is a fix of #3557. - Further the `existing_filename_or_none` construct dates back to times when there was not yet a `pathlib.Path` in all Python versions we supported in the past. - Typehints have been added wherever appropriate At the same time, this patch should also be downward compatible and not introduce a new environment variable. The localization of the folder with the configurations is further based on: SEARXNG_SETTINGS_PATH (wich defaults to /etc/searxng/settings.yml) Which means, the default config folder is `/etc/searxng/`. ATTENTION: intended functional changes! If SEARXNG_SETTINGS_PATH was set and pointed to a not existing file, the previous implementation silently loaded the default configuration. This behavior has been changed: if the file or folder does not exist, an EnvironmentError exception will be thrown in future. Closes: https://github.com/searxng/searxng/issues/3557 Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
112 lines
6.1 KiB
Python
112 lines
6.1 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
# pylint: disable=missing-module-docstring
|
|
|
|
from pathlib import Path
|
|
|
|
import os
|
|
from unittest.mock import patch
|
|
|
|
from searx.exceptions import SearxSettingsException
|
|
from searx import settings_loader
|
|
from tests import SearxTestCase
|
|
|
|
|
|
def _settings(f_name):
|
|
return str(Path(__file__).parent.absolute() / "settings" / f_name)
|
|
|
|
|
|
class TestLoad(SearxTestCase): # pylint: disable=missing-class-docstring
|
|
def test_load_zero(self):
|
|
with self.assertRaises(SearxSettingsException):
|
|
settings_loader.load_yaml('/dev/zero')
|
|
|
|
with self.assertRaises(SearxSettingsException):
|
|
settings_loader.load_yaml(_settings("syntaxerror_settings.yml"))
|
|
|
|
self.assertEqual(settings_loader.load_yaml(_settings("empty_settings.yml")), {})
|
|
|
|
|
|
class TestDefaultSettings(SearxTestCase): # pylint: disable=missing-class-docstring
|
|
def test_load(self):
|
|
settings, msg = settings_loader.load_settings(load_user_settings=False)
|
|
self.assertTrue(msg.startswith('load the default settings from'))
|
|
self.assertFalse(settings['general']['debug'])
|
|
self.assertTrue(isinstance(settings['general']['instance_name'], str))
|
|
self.assertEqual(settings['server']['secret_key'], "ultrasecretkey")
|
|
self.assertTrue(isinstance(settings['server']['port'], int))
|
|
self.assertTrue(isinstance(settings['server']['bind_address'], str))
|
|
self.assertTrue(isinstance(settings['engines'], list))
|
|
self.assertTrue(isinstance(settings['doi_resolvers'], dict))
|
|
self.assertTrue(isinstance(settings['default_doi_resolver'], str))
|
|
|
|
|
|
class TestUserSettings(SearxTestCase): # pylint: disable=missing-class-docstring
|
|
def test_is_use_default_settings(self):
|
|
self.assertFalse(settings_loader.is_use_default_settings({}))
|
|
self.assertTrue(settings_loader.is_use_default_settings({'use_default_settings': True}))
|
|
self.assertTrue(settings_loader.is_use_default_settings({'use_default_settings': {}}))
|
|
with self.assertRaises(ValueError):
|
|
self.assertFalse(settings_loader.is_use_default_settings({'use_default_settings': 1}))
|
|
with self.assertRaises(ValueError):
|
|
self.assertFalse(settings_loader.is_use_default_settings({'use_default_settings': 0}))
|
|
|
|
def test_user_settings_not_found(self):
|
|
with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("not_exists.yml")}):
|
|
with self.assertRaises(EnvironmentError):
|
|
_s, _m = settings_loader.load_settings()
|
|
with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': "/folder/not/exists"}):
|
|
with self.assertRaises(EnvironmentError):
|
|
_s, _m = settings_loader.load_settings()
|
|
|
|
def test_user_settings(self):
|
|
with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("user_settings_simple.yml")}):
|
|
settings, msg = settings_loader.load_settings()
|
|
self.assertTrue(msg.startswith('merge the default settings'))
|
|
self.assertEqual(settings['server']['secret_key'], "user_secret_key")
|
|
self.assertEqual(settings['server']['default_http_headers']['Custom-Header'], "Custom-Value")
|
|
|
|
def test_user_settings_remove(self):
|
|
with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("user_settings_remove.yml")}):
|
|
settings, msg = settings_loader.load_settings()
|
|
self.assertTrue(msg.startswith('merge the default settings'))
|
|
self.assertEqual(settings['server']['secret_key'], "user_secret_key")
|
|
self.assertEqual(settings['server']['default_http_headers']['Custom-Header'], "Custom-Value")
|
|
engine_names = [engine['name'] for engine in settings['engines']]
|
|
self.assertNotIn('wikinews', engine_names)
|
|
self.assertNotIn('wikibooks', engine_names)
|
|
self.assertIn('wikipedia', engine_names)
|
|
|
|
def test_user_settings_remove2(self):
|
|
with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("user_settings_remove2.yml")}):
|
|
settings, msg = settings_loader.load_settings()
|
|
self.assertTrue(msg.startswith('merge the default settings'))
|
|
self.assertEqual(settings['server']['secret_key'], "user_secret_key")
|
|
self.assertEqual(settings['server']['default_http_headers']['Custom-Header'], "Custom-Value")
|
|
engine_names = [engine['name'] for engine in settings['engines']]
|
|
self.assertNotIn('wikinews', engine_names)
|
|
self.assertNotIn('wikibooks', engine_names)
|
|
self.assertIn('wikipedia', engine_names)
|
|
wikipedia = list(filter(lambda engine: (engine.get('name')) == 'wikipedia', settings['engines']))
|
|
self.assertEqual(wikipedia[0]['engine'], 'wikipedia')
|
|
self.assertEqual(wikipedia[0]['tokens'], ['secret_token'])
|
|
newengine = list(filter(lambda engine: (engine.get('name')) == 'newengine', settings['engines']))
|
|
self.assertEqual(newengine[0]['engine'], 'dummy')
|
|
|
|
def test_user_settings_keep_only(self):
|
|
with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("user_settings_keep_only.yml")}):
|
|
settings, msg = settings_loader.load_settings()
|
|
self.assertTrue(msg.startswith('merge the default settings'))
|
|
engine_names = [engine['name'] for engine in settings['engines']]
|
|
self.assertEqual(engine_names, ['wikibooks', 'wikinews', 'wikipedia', 'newengine'])
|
|
# wikipedia has been removed, then added again with the "engine" section of user_settings_keep_only.yml
|
|
self.assertEqual(len(settings['engines'][2]), 1)
|
|
|
|
def test_custom_settings(self):
|
|
with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("user_settings.yml")}):
|
|
settings, msg = settings_loader.load_settings()
|
|
self.assertTrue(msg.startswith('load the user settings from'))
|
|
self.assertEqual(settings['server']['port'], 9000)
|
|
self.assertEqual(settings['server']['secret_key'], "user_settings_secret")
|
|
engine_names = [engine['name'] for engine in settings['engines']]
|
|
self.assertEqual(engine_names, ['wikidata', 'wikibooks', 'wikinews', 'wikiquote'])
|