2024-03-11 13:06:26 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# pylint: disable=missing-module-docstring, invalid-name
|
|
|
|
|
2024-10-19 22:38:04 +00:00
|
|
|
import flask
|
|
|
|
from mock import Mock
|
2024-09-24 03:37:30 +00:00
|
|
|
from tests import SearxTestCase
|
2024-08-19 15:47:54 +00:00
|
|
|
from searx import favicons
|
2022-06-10 15:01:12 +00:00
|
|
|
from searx.locales import locales_initialize
|
2021-12-27 08:26:22 +00:00
|
|
|
from searx.preferences import (
|
2024-10-19 22:38:04 +00:00
|
|
|
Setting,
|
2021-12-27 08:26:22 +00:00
|
|
|
EnumStringSetting,
|
|
|
|
MapSetting,
|
|
|
|
SearchLanguageSetting,
|
|
|
|
MultipleChoiceSetting,
|
|
|
|
PluginsSetting,
|
|
|
|
ValidationException,
|
|
|
|
)
|
2024-09-24 03:37:30 +00:00
|
|
|
from searx.plugins import Plugin
|
2024-10-19 22:38:04 +00:00
|
|
|
from searx.preferences import Preferences
|
2016-04-08 23:09:26 +00:00
|
|
|
|
2022-06-10 15:01:12 +00:00
|
|
|
locales_initialize()
|
2024-08-19 15:47:54 +00:00
|
|
|
favicons.init()
|
2022-06-10 15:01:12 +00:00
|
|
|
|
2016-04-08 23:09:26 +00:00
|
|
|
|
2024-09-24 03:37:30 +00:00
|
|
|
class PluginStub(Plugin): # pylint: disable=missing-class-docstring, too-few-public-methods
|
2020-11-16 08:43:23 +00:00
|
|
|
def __init__(self, plugin_id, default_on):
|
|
|
|
self.id = plugin_id
|
2016-04-08 23:09:26 +00:00
|
|
|
self.default_on = default_on
|
|
|
|
|
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
class TestSettings(SearxTestCase): # pylint: disable=missing-class-docstring
|
2016-04-08 23:09:26 +00:00
|
|
|
# map settings
|
2016-07-10 14:44:27 +00:00
|
|
|
|
2016-04-08 23:09:26 +00:00
|
|
|
def test_map_setting_invalid_default_value(self):
|
|
|
|
with self.assertRaises(ValidationException):
|
2020-11-16 08:43:23 +00:00
|
|
|
MapSetting(3, map={'dog': 1, 'bat': 2})
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
def test_map_setting_invalid_choice(self):
|
|
|
|
setting = MapSetting(2, map={'dog': 1, 'bat': 2})
|
|
|
|
with self.assertRaises(ValidationException):
|
|
|
|
setting.parse('cat')
|
|
|
|
|
|
|
|
def test_map_setting_valid_default(self):
|
|
|
|
setting = MapSetting(3, map={'dog': 1, 'bat': 2, 'cat': 3})
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_value(), 3)
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
def test_map_setting_valid_choice(self):
|
|
|
|
setting = MapSetting(3, map={'dog': 1, 'bat': 2, 'cat': 3})
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_value(), 3)
|
2016-04-08 23:09:26 +00:00
|
|
|
setting.parse('bat')
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_value(), 2)
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
# enum settings
|
|
|
|
|
|
|
|
def test_enum_setting_invalid_default_value(self):
|
|
|
|
with self.assertRaises(ValidationException):
|
2024-09-24 03:37:30 +00:00
|
|
|
EnumStringSetting('3', choices=['0', '1', '2'])
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
def test_enum_setting_invalid_choice(self):
|
2024-09-24 03:37:30 +00:00
|
|
|
setting = EnumStringSetting('0', choices=['0', '1', '2'])
|
2016-04-08 23:09:26 +00:00
|
|
|
with self.assertRaises(ValidationException):
|
2024-09-24 03:37:30 +00:00
|
|
|
setting.parse('3')
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
def test_enum_setting_valid_default(self):
|
2024-09-24 03:37:30 +00:00
|
|
|
setting = EnumStringSetting('3', choices=['1', '2', '3'])
|
|
|
|
self.assertEqual(setting.get_value(), '3')
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
def test_enum_setting_valid_choice(self):
|
2024-09-24 03:37:30 +00:00
|
|
|
setting = EnumStringSetting('3', choices=['1', '2', '3'])
|
|
|
|
self.assertEqual(setting.get_value(), '3')
|
|
|
|
setting.parse('2')
|
|
|
|
self.assertEqual(setting.get_value(), '2')
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
# multiple choice settings
|
|
|
|
|
|
|
|
def test_multiple_setting_invalid_default_value(self):
|
|
|
|
with self.assertRaises(ValidationException):
|
2020-11-16 08:43:23 +00:00
|
|
|
MultipleChoiceSetting(['3', '4'], choices=['0', '1', '2'])
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
def test_multiple_setting_invalid_choice(self):
|
|
|
|
setting = MultipleChoiceSetting(['1', '2'], choices=['0', '1', '2'])
|
|
|
|
with self.assertRaises(ValidationException):
|
|
|
|
setting.parse('4, 3')
|
|
|
|
|
|
|
|
def test_multiple_setting_valid_default(self):
|
|
|
|
setting = MultipleChoiceSetting(['3'], choices=['1', '2', '3'])
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_value(), ['3'])
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
def test_multiple_setting_valid_choice(self):
|
|
|
|
setting = MultipleChoiceSetting(['3'], choices=['1', '2', '3'])
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_value(), ['3'])
|
2016-04-08 23:09:26 +00:00
|
|
|
setting.parse('2')
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_value(), ['2'])
|
2016-04-08 23:09:26 +00:00
|
|
|
|
2016-12-14 05:51:15 +00:00
|
|
|
# search language settings
|
|
|
|
def test_lang_setting_valid_choice(self):
|
2019-01-06 14:27:46 +00:00
|
|
|
setting = SearchLanguageSetting('all', choices=['all', 'de', 'en'])
|
2016-12-14 05:51:15 +00:00
|
|
|
setting.parse('de')
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_value(), 'de')
|
2016-12-14 05:51:15 +00:00
|
|
|
|
|
|
|
def test_lang_setting_invalid_choice(self):
|
2019-01-06 14:27:46 +00:00
|
|
|
setting = SearchLanguageSetting('all', choices=['all', 'de', 'en'])
|
2016-12-14 05:51:15 +00:00
|
|
|
setting.parse('xx')
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_value(), 'all')
|
2016-12-14 05:51:15 +00:00
|
|
|
|
|
|
|
def test_lang_setting_old_cookie_choice(self):
|
2019-01-06 14:27:46 +00:00
|
|
|
setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES'])
|
2016-12-14 05:51:15 +00:00
|
|
|
setting.parse('es_XA')
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_value(), 'es')
|
2016-12-14 05:51:15 +00:00
|
|
|
|
|
|
|
def test_lang_setting_old_cookie_format(self):
|
2019-01-06 14:27:46 +00:00
|
|
|
setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES'])
|
2016-12-14 05:51:15 +00:00
|
|
|
setting.parse('es_ES')
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_value(), 'es-ES')
|
2016-12-14 05:51:15 +00:00
|
|
|
|
2016-04-08 23:09:26 +00:00
|
|
|
# plugins settings
|
|
|
|
def test_plugins_setting_all_default_enabled(self):
|
|
|
|
plugin1 = PluginStub('plugin1', True)
|
|
|
|
plugin2 = PluginStub('plugin2', True)
|
2022-01-04 10:53:42 +00:00
|
|
|
setting = PluginsSetting(['3'], plugins=[plugin1, plugin2])
|
2022-01-06 17:45:50 +00:00
|
|
|
self.assertEqual(set(setting.get_enabled()), set(['plugin1', 'plugin2']))
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
def test_plugins_setting_few_default_enabled(self):
|
|
|
|
plugin1 = PluginStub('plugin1', True)
|
|
|
|
plugin2 = PluginStub('plugin2', False)
|
|
|
|
plugin3 = PluginStub('plugin3', True)
|
2022-01-04 10:53:42 +00:00
|
|
|
setting = PluginsSetting('name', plugins=[plugin1, plugin2, plugin3])
|
2022-01-06 17:45:50 +00:00
|
|
|
self.assertEqual(set(setting.get_enabled()), set(['plugin1', 'plugin3']))
|
2019-07-17 07:42:40 +00:00
|
|
|
|
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
class TestPreferences(SearxTestCase): # pylint: disable=missing-class-docstring
|
2024-10-19 22:38:04 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.preferences = Preferences(['simple'], ['general'], {}, [])
|
2021-12-27 08:26:22 +00:00
|
|
|
|
2024-10-19 22:38:04 +00:00
|
|
|
def test_encode(self):
|
2021-12-27 08:26:22 +00:00
|
|
|
url_params = (
|
2022-04-30 06:01:53 +00:00
|
|
|
'eJx1Vk1z4zYM_TXxRZNMd7eddg8-pe21nWnvGoiEJEQkofDDtvzrC1qSRdnbQxQTBA'
|
|
|
|
'Hw8eGRCiJ27AnDsUOHHszBgOsSdHjU-Pr7HwfDCkweHCBFVmxHgxGPB7LiU4-eL9Px'
|
|
|
|
'TzABDxZjz_r491___HsI0GJA8Ko__nSIPVo8BspLDx5DMjHU7GqH5zpCsyzXTLVMsj'
|
|
|
|
'mhPzLI8I19d5iX1SFOUkUu4QD6BE6hrpcE8_LPhH6qydWRonjORnItOYqyXHk2Zs1E'
|
|
|
|
'ARojAdB15GTrMA6VJe_Z13VLBsPL1_ccmk5YUajrBRqxNhSbpAaMdU1Rxkqp13iq6x'
|
|
|
|
'Np5LxMI15RwtgUSOWx7iqNtyqI3S4Wej6TrmsWfHx2lcD5r-PSa7NWN8glxPxf5r5c'
|
|
|
|
'ikGrPedw6wZaj1gFbuMZPFaaPKrIAtFceOvJDQSqCNBRJ7BAiGX6TtCEZt0ta2zQd8'
|
|
|
|
'uwY-4MVqOBqYJxDFvucsbyiXLVd4i6kbUuMeqh8ZA_S1yyutlgIQfFYnLykziFH9vW'
|
|
|
|
'kB8Uet5iDKQGCEWBhiSln6q80UDlBDch4psP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XzBU8_N3kqekkefRDzxNK2_Klp9mBJ'
|
|
|
|
'wsUnXyRqq1mScHuYalUY7_AZTCR4s=&q='
|
2021-12-27 08:26:22 +00:00
|
|
|
)
|
2024-10-19 22:38:04 +00:00
|
|
|
self.preferences.parse_encoded_data(url_params)
|
2019-07-17 07:42:40 +00:00
|
|
|
self.assertEqual(
|
2024-10-19 22:38:04 +00:00
|
|
|
vars(self.preferences.key_value_settings['categories']),
|
2021-12-27 08:26:22 +00:00
|
|
|
{'value': ['general'], 'locked': False, 'choices': ['general', 'none']},
|
|
|
|
)
|
2024-10-19 22:38:04 +00:00
|
|
|
|
|
|
|
def test_save_key_value_setting(self):
|
|
|
|
setting_key = 'foo'
|
|
|
|
setting_value = 'bar'
|
|
|
|
|
|
|
|
cookie_callback = {}
|
|
|
|
|
|
|
|
def set_cookie_callback(name, value, max_age): # pylint: disable=unused-argument
|
|
|
|
cookie_callback[name] = value
|
|
|
|
|
|
|
|
response_mock = Mock(flask.Response)
|
|
|
|
response_mock.set_cookie = set_cookie_callback
|
|
|
|
self.preferences.key_value_settings = {
|
|
|
|
setting_key: Setting(
|
|
|
|
setting_value,
|
|
|
|
locked=False,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
self.preferences.save(response_mock)
|
|
|
|
self.assertIn(setting_key, cookie_callback)
|
|
|
|
self.assertEqual(cookie_callback[setting_key], setting_value)
|
|
|
|
|
|
|
|
def test_false_key_value_setting(self):
|
|
|
|
setting_key = 'foo'
|
|
|
|
|
|
|
|
cookie_callback = {}
|
|
|
|
|
|
|
|
def set_cookie_callback(name, value, max_age): # pylint: disable=unused-argument
|
|
|
|
cookie_callback[name] = value
|
|
|
|
|
|
|
|
response_mock = Mock(flask.Response)
|
|
|
|
response_mock.set_cookie = set_cookie_callback
|
|
|
|
self.preferences.key_value_settings = {
|
|
|
|
setting_key: Setting(
|
|
|
|
'',
|
|
|
|
locked=True,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
self.preferences.save(response_mock)
|
|
|
|
self.assertNotIn(setting_key, cookie_callback)
|