2021-12-27 08:26:22 +00:00
|
|
|
from searx.preferences import (
|
|
|
|
EnumStringSetting,
|
|
|
|
MapSetting,
|
|
|
|
SearchLanguageSetting,
|
|
|
|
MultipleChoiceSetting,
|
|
|
|
PluginsSetting,
|
|
|
|
ValidationException,
|
|
|
|
)
|
2021-09-02 14:01:34 +00:00
|
|
|
from tests import SearxTestCase
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
|
2020-08-12 07:42:27 +00:00
|
|
|
class PluginStub:
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class TestSettings(SearxTestCase):
|
|
|
|
# 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):
|
2020-11-16 08:43:23 +00:00
|
|
|
EnumStringSetting(3, choices=[0, 1, 2])
|
2016-04-08 23:09:26 +00:00
|
|
|
|
|
|
|
def test_enum_setting_invalid_choice(self):
|
|
|
|
setting = EnumStringSetting(0, choices=[0, 1, 2])
|
|
|
|
with self.assertRaises(ValidationException):
|
|
|
|
setting.parse(3)
|
|
|
|
|
|
|
|
def test_enum_setting_valid_default(self):
|
|
|
|
setting = EnumStringSetting(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_enum_setting_valid_choice(self):
|
|
|
|
setting = EnumStringSetting(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
|
|
|
|
|
|
|
# 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])
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(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])
|
2020-08-06 15:42:46 +00:00
|
|
|
self.assertEqual(setting.get_enabled(), set(['plugin1', 'plugin3']))
|
2019-07-17 07:42:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestPreferences(SearxTestCase):
|
|
|
|
def test_encode(self):
|
|
|
|
from searx.preferences import Preferences
|
2021-12-27 08:26:22 +00:00
|
|
|
|
2019-07-17 07:42:40 +00:00
|
|
|
pref = Preferences(['oscar'], ['general'], {}, [])
|
2021-12-27 08:26:22 +00:00
|
|
|
url_params = (
|
|
|
|
'eJx1VMmO2zAM_Zr6YrTocujJh6JF0QEKzKAz7VVgJNohLIseUU7ivy-VcWy5yyGOTVGP73GLKJNPYjiYgGeT4NB8BS9YOSY'
|
|
|
|
'TUdifMDYM-vmGY1d5CN0EHTYOK88W_PXNkcDBozOjnzoK0vyi4bWnHs2RU4-zvHr_-RF9a-5Cy3GARByy7X7EkKMoBeMp9CuPQ-SzYMx'
|
|
|
|
'8Vr9P1qKI-XJ_p1fOkRJWNCgVM0a-zAttmBJbHkaPSZlNts-_jiuBFgUh2mPztkpHHLBhsRArDHvm356eHh5vATS0Mqagr0ZsZO_V8hT'
|
|
|
|
'B9srt54_v6jewJugqL4Nn_hYSdhxnI-jRpi05GDQCStOT7UGVmJY8ZnltRKyF23SGiLWjqNcygKGkpyeGZIywJfD1gI5AjRTAmBM55Aw'
|
|
|
|
'Q0Tn626lj7jzWo4e5hnEsIlprX6dTgdBRpyRBFKTDgBF8AasVyT4gvSTEoXRpXWRyG3CYQYld65I_V6lboILTMAlZY65_ejRDcHgp0Tv'
|
|
|
|
'EPtGAsqTiBf3m76g7pP9B84mwjPvuUtASRDei1nDF2ix_JXW91UJkXrPh6RAhznVmKyQl7dwJdMJ6bz1QOmgzYlrEzHDMcEUuo44AgS1'
|
|
|
|
'CvkjaOb2Q2AyY5oGDTs_OLXE_c2I5cg9hk3kEJZ0fu4SuktsIA2RhuJwP86AdripThCBeO9uVUejyPGmFSxPrqEYcuWi25zOEXV9tc1m'
|
|
|
|
'_KP1nafYtdfv6Q9hKfWmGm9A_3G635UwiVndLGdFCiLWkONk0xUxGLGGweGWTa2nZYZ0fS1YKlE3Uuw8fPl52E5U8HJYbC7sbjXUsrnT'
|
2019-07-17 07:42:40 +00:00
|
|
|
'XHXRbELfO-1fGSqskiGnMK7B0dV3t8Lq08pbdtYpuVdoKWA2Yjuyah_vHp2rZWjo0zXL8Gw8DTj0='
|
2021-12-27 08:26:22 +00:00
|
|
|
)
|
2019-07-17 07:42:40 +00:00
|
|
|
pref.parse_encoded_data(url_params)
|
|
|
|
self.assertEqual(
|
|
|
|
vars(pref.key_value_settings['categories']),
|
2021-12-27 08:26:22 +00:00
|
|
|
{'value': ['general'], 'locked': False, 'choices': ['general', 'none']},
|
|
|
|
)
|