test_preferences.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring, invalid-name
  3. from tests import SearxTestCase
  4. from searx import favicons
  5. from searx.locales import locales_initialize
  6. from searx.preferences import (
  7. EnumStringSetting,
  8. MapSetting,
  9. SearchLanguageSetting,
  10. MultipleChoiceSetting,
  11. PluginsSetting,
  12. ValidationException,
  13. )
  14. from searx.plugins import Plugin
  15. locales_initialize()
  16. favicons.init()
  17. class PluginStub(Plugin): # pylint: disable=missing-class-docstring, too-few-public-methods
  18. def __init__(self, plugin_id, default_on):
  19. self.id = plugin_id
  20. self.default_on = default_on
  21. class TestSettings(SearxTestCase): # pylint: disable=missing-class-docstring
  22. # map settings
  23. def test_map_setting_invalid_default_value(self):
  24. with self.assertRaises(ValidationException):
  25. MapSetting(3, map={'dog': 1, 'bat': 2})
  26. def test_map_setting_invalid_choice(self):
  27. setting = MapSetting(2, map={'dog': 1, 'bat': 2})
  28. with self.assertRaises(ValidationException):
  29. setting.parse('cat')
  30. def test_map_setting_valid_default(self):
  31. setting = MapSetting(3, map={'dog': 1, 'bat': 2, 'cat': 3})
  32. self.assertEqual(setting.get_value(), 3)
  33. def test_map_setting_valid_choice(self):
  34. setting = MapSetting(3, map={'dog': 1, 'bat': 2, 'cat': 3})
  35. self.assertEqual(setting.get_value(), 3)
  36. setting.parse('bat')
  37. self.assertEqual(setting.get_value(), 2)
  38. # enum settings
  39. def test_enum_setting_invalid_default_value(self):
  40. with self.assertRaises(ValidationException):
  41. EnumStringSetting('3', choices=['0', '1', '2'])
  42. def test_enum_setting_invalid_choice(self):
  43. setting = EnumStringSetting('0', choices=['0', '1', '2'])
  44. with self.assertRaises(ValidationException):
  45. setting.parse('3')
  46. def test_enum_setting_valid_default(self):
  47. setting = EnumStringSetting('3', choices=['1', '2', '3'])
  48. self.assertEqual(setting.get_value(), '3')
  49. def test_enum_setting_valid_choice(self):
  50. setting = EnumStringSetting('3', choices=['1', '2', '3'])
  51. self.assertEqual(setting.get_value(), '3')
  52. setting.parse('2')
  53. self.assertEqual(setting.get_value(), '2')
  54. # multiple choice settings
  55. def test_multiple_setting_invalid_default_value(self):
  56. with self.assertRaises(ValidationException):
  57. MultipleChoiceSetting(['3', '4'], choices=['0', '1', '2'])
  58. def test_multiple_setting_invalid_choice(self):
  59. setting = MultipleChoiceSetting(['1', '2'], choices=['0', '1', '2'])
  60. with self.assertRaises(ValidationException):
  61. setting.parse('4, 3')
  62. def test_multiple_setting_valid_default(self):
  63. setting = MultipleChoiceSetting(['3'], choices=['1', '2', '3'])
  64. self.assertEqual(setting.get_value(), ['3'])
  65. def test_multiple_setting_valid_choice(self):
  66. setting = MultipleChoiceSetting(['3'], choices=['1', '2', '3'])
  67. self.assertEqual(setting.get_value(), ['3'])
  68. setting.parse('2')
  69. self.assertEqual(setting.get_value(), ['2'])
  70. # search language settings
  71. def test_lang_setting_valid_choice(self):
  72. setting = SearchLanguageSetting('all', choices=['all', 'de', 'en'])
  73. setting.parse('de')
  74. self.assertEqual(setting.get_value(), 'de')
  75. def test_lang_setting_invalid_choice(self):
  76. setting = SearchLanguageSetting('all', choices=['all', 'de', 'en'])
  77. setting.parse('xx')
  78. self.assertEqual(setting.get_value(), 'all')
  79. def test_lang_setting_old_cookie_choice(self):
  80. setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES'])
  81. setting.parse('es_XA')
  82. self.assertEqual(setting.get_value(), 'es')
  83. def test_lang_setting_old_cookie_format(self):
  84. setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES'])
  85. setting.parse('es_ES')
  86. self.assertEqual(setting.get_value(), 'es-ES')
  87. # plugins settings
  88. def test_plugins_setting_all_default_enabled(self):
  89. plugin1 = PluginStub('plugin1', True)
  90. plugin2 = PluginStub('plugin2', True)
  91. setting = PluginsSetting(['3'], plugins=[plugin1, plugin2])
  92. self.assertEqual(set(setting.get_enabled()), set(['plugin1', 'plugin2']))
  93. def test_plugins_setting_few_default_enabled(self):
  94. plugin1 = PluginStub('plugin1', True)
  95. plugin2 = PluginStub('plugin2', False)
  96. plugin3 = PluginStub('plugin3', True)
  97. setting = PluginsSetting('name', plugins=[plugin1, plugin2, plugin3])
  98. self.assertEqual(set(setting.get_enabled()), set(['plugin1', 'plugin3']))
  99. class TestPreferences(SearxTestCase): # pylint: disable=missing-class-docstring
  100. def test_encode(self):
  101. from searx.preferences import Preferences # pylint: disable=import-outside-toplevel
  102. pref = Preferences(['simple'], ['general'], {}, [])
  103. url_params = (
  104. 'eJx1Vk1z4zYM_TXxRZNMd7eddg8-pe21nWnvGoiEJEQkofDDtvzrC1qSRdnbQxQTBA'
  105. 'Hw8eGRCiJ27AnDsUOHHszBgOsSdHjU-Pr7HwfDCkweHCBFVmxHgxGPB7LiU4-eL9Px'
  106. 'TzABDxZjz_r491___HsI0GJA8Ko__nSIPVo8BspLDx5DMjHU7GqH5zpCsyzXTLVMsj'
  107. 'mhPzLI8I19d5iX1SFOUkUu4QD6BE6hrpcE8_LPhH6qydWRonjORnItOYqyXHk2Zs1E'
  108. 'ARojAdB15GTrMA6VJe_Z13VLBsPL1_ccmk5YUajrBRqxNhSbpAaMdU1Rxkqp13iq6x'
  109. 'Np5LxMI15RwtgUSOWx7iqNtyqI3S4Wej6TrmsWfHx2lcD5r-PSa7NWN8glxPxf5r5c'
  110. 'ikGrPedw6wZaj1gFbuMZPFaaPKrIAtFceOvJDQSqCNBRJ7BAiGX6TtCEZt0ta2zQd8'
  111. 'uwY-4MVqOBqYJxDFvucsbyiXLVd4i6kbUuMeqh8ZA_S1yyutlgIQfFYnLykziFH9vW'
  112. 'kB8Uet5iDKQGCEWBhiSln6q80UDlBDch4psPSy1wNZMnVYR2o13m3ASwreQRnceRi2'
  113. 'AjSNqOwsqWmbAZxSp_7kcBFnJBeHez4CKpKqieDQgsQREK5fNcBB_H3HrFIUUeJo4s'
  114. 'Wx7Abekn6HnHpTM10348UMM8hEejdKbY8ncxfCaO-OgVOHn1ZJX2DRSf8px4eqj6y7'
  115. 'dvv162anXS6LYjC3h1YEt_yx-IQ2lxcMo82gw-NVOHdj28EdHH1GDBFYuaQFIMQsrz'
  116. 'GZtiyicrqlAYznyhgd2bHFeYHLvJYlHfy_svL7995bOjofp4ef_55fv36zRANbIJA2'
  117. 'FX0C_v34oE3Es9oHtQIOFFZcilS5WdV_J5YUHRoeAvdCrZ0IDTCuy4sTOvHvMe96rl'
  118. 'usfxs5rcrLuTv1lmOApYmqip6_bEz4eORSyR2xA8tmWxKnkvP3fM0Hgi4bpstFisWR'
  119. 'TWV31adSdvSkPc7SkKbtOOTxgny05ALE6pNdL5vhQ5dFQKhYxjbpJZ0ChuSWcN22nh'
  120. 'rGpPwC32HXSL7Qm8xf6Dzu6XfLfk19dFoZ4li1sRD9fJVVnWYOmiDCe97Uw0RGi4am'
  121. 'o-JJA7IMMYUO7fIvM6N6ZG4ILlotrPhyjXSbSQqQZj7i2d-2pzGntRIHefJS8viwaK'
  122. '-iW6NN9uyTSuTP88CwtKrG-GPaSz6Qn92fwEtGxVk4QMrAhMdev7m6yMBLMOF86iZN'
  123. 'JIe_xEadXAQuzW8HltyDCkJrmYVqVOI_oU7ijL64W03LLC81jcA8kFuQpDX1R90-b9'
  124. '_iZOD2J1t9xfE0BGSJ5PqHA7kUUudYuG7HFjz12C2Mz3zNhD8eQgFa_sdiy3InNWHg'
  125. 'pV9OCCkWPUZRivRfA2g3DytC3fnlajSaJs4Zihvrwto7eeQxRVR3noCSDzhbZzYKjn'
  126. 'd-DZy7PtaVp2WgvPBpzCXUL_J1OGex48RVmOXzBU8_N3kqekkefRDzxNK2_Klp9mBJ'
  127. 'wsUnXyRqq1mScHuYalUY7_AZTCR4s=&q='
  128. )
  129. pref.parse_encoded_data(url_params)
  130. self.assertEqual(
  131. vars(pref.key_value_settings['categories']),
  132. {'value': ['general'], 'locked': False, 'choices': ['general', 'none']},
  133. )