test_preferences.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name
  3. import flask
  4. from mock import Mock
  5. from searx import favicons
  6. from searx.locales import locales_initialize
  7. from searx.preferences import (
  8. Setting,
  9. EnumStringSetting,
  10. MapSetting,
  11. SearchLanguageSetting,
  12. MultipleChoiceSetting,
  13. PluginsSetting,
  14. ValidationException,
  15. )
  16. import searx.plugins
  17. from searx.preferences import Preferences
  18. from tests import SearxTestCase
  19. from .test_plugins import PluginMock
  20. locales_initialize()
  21. favicons.init()
  22. class TestSettings(SearxTestCase):
  23. # map settings
  24. def test_map_setting_invalid_default_value(self):
  25. with self.assertRaises(ValidationException):
  26. MapSetting(3, map={'dog': 1, 'bat': 2})
  27. def test_map_setting_invalid_choice(self):
  28. setting = MapSetting(2, map={'dog': 1, 'bat': 2})
  29. with self.assertRaises(ValidationException):
  30. setting.parse('cat')
  31. def test_map_setting_valid_default(self):
  32. setting = MapSetting(3, map={'dog': 1, 'bat': 2, 'cat': 3})
  33. self.assertEqual(setting.get_value(), 3)
  34. def test_map_setting_valid_choice(self):
  35. setting = MapSetting(3, map={'dog': 1, 'bat': 2, 'cat': 3})
  36. self.assertEqual(setting.get_value(), 3)
  37. setting.parse('bat')
  38. self.assertEqual(setting.get_value(), 2)
  39. # enum settings
  40. def test_enum_setting_invalid_default_value(self):
  41. with self.assertRaises(ValidationException):
  42. EnumStringSetting('3', choices=['0', '1', '2'])
  43. def test_enum_setting_invalid_choice(self):
  44. setting = EnumStringSetting('0', choices=['0', '1', '2'])
  45. with self.assertRaises(ValidationException):
  46. setting.parse('3')
  47. def test_enum_setting_valid_default(self):
  48. setting = EnumStringSetting('3', choices=['1', '2', '3'])
  49. self.assertEqual(setting.get_value(), '3')
  50. def test_enum_setting_valid_choice(self):
  51. setting = EnumStringSetting('3', choices=['1', '2', '3'])
  52. self.assertEqual(setting.get_value(), '3')
  53. setting.parse('2')
  54. self.assertEqual(setting.get_value(), '2')
  55. # multiple choice settings
  56. def test_multiple_setting_invalid_default_value(self):
  57. with self.assertRaises(ValidationException):
  58. MultipleChoiceSetting(['3', '4'], choices=['0', '1', '2'])
  59. def test_multiple_setting_invalid_choice(self):
  60. setting = MultipleChoiceSetting(['1', '2'], choices=['0', '1', '2'])
  61. with self.assertRaises(ValidationException):
  62. setting.parse('4, 3')
  63. def test_multiple_setting_valid_default(self):
  64. setting = MultipleChoiceSetting(['3'], choices=['1', '2', '3'])
  65. self.assertEqual(setting.get_value(), ['3'])
  66. def test_multiple_setting_valid_choice(self):
  67. setting = MultipleChoiceSetting(['3'], choices=['1', '2', '3'])
  68. self.assertEqual(setting.get_value(), ['3'])
  69. setting.parse('2')
  70. self.assertEqual(setting.get_value(), ['2'])
  71. # search language settings
  72. def test_lang_setting_valid_choice(self):
  73. setting = SearchLanguageSetting('all', choices=['all', 'de', 'en'])
  74. setting.parse('de')
  75. self.assertEqual(setting.get_value(), 'de')
  76. def test_lang_setting_invalid_choice(self):
  77. setting = SearchLanguageSetting('all', choices=['all', 'de', 'en'])
  78. setting.parse('xx')
  79. self.assertEqual(setting.get_value(), 'all')
  80. def test_lang_setting_old_cookie_choice(self):
  81. setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES'])
  82. setting.parse('es_XA')
  83. self.assertEqual(setting.get_value(), 'es')
  84. def test_lang_setting_old_cookie_format(self):
  85. setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES'])
  86. setting.parse('es_ES')
  87. self.assertEqual(setting.get_value(), 'es-ES')
  88. # plugins settings
  89. def test_plugins_setting_all_default_enabled(self):
  90. storage = searx.plugins.PluginStorage()
  91. storage.register(PluginMock("plg001", "first plugin", True))
  92. storage.register(PluginMock("plg002", "second plugin", True))
  93. plgs_settings = PluginsSetting(False, storage)
  94. self.assertEqual(set(plgs_settings.get_enabled()), {"plg001", "plg002"})
  95. def test_plugins_setting_few_default_enabled(self):
  96. storage = searx.plugins.PluginStorage()
  97. storage.register(PluginMock("plg001", "first plugin", True))
  98. storage.register(PluginMock("plg002", "second plugin", False))
  99. storage.register(PluginMock("plg003", "third plugin", True))
  100. plgs_settings = PluginsSetting(False, storage)
  101. self.assertEqual(set(plgs_settings.get_enabled()), set(['plg001', 'plg003']))
  102. class TestPreferences(SearxTestCase):
  103. def setUp(self):
  104. super().setUp()
  105. storage = searx.plugins.PluginStorage()
  106. self.preferences = Preferences(['simple'], ['general'], {}, storage)
  107. def test_encode(self):
  108. url_params = (
  109. 'eJx1Vk1z4zYM_TXxRZNMd7eddg8-pe21nWnvGoiEJEQkofDDtvzrC1qSRdnbQxQTBA'
  110. 'Hw8eGRCiJ27AnDsUOHHszBgOsSdHjU-Pr7HwfDCkweHCBFVmxHgxGPB7LiU4-eL9Px'
  111. 'TzABDxZjz_r491___HsI0GJA8Ko__nSIPVo8BspLDx5DMjHU7GqH5zpCsyzXTLVMsj'
  112. 'mhPzLI8I19d5iX1SFOUkUu4QD6BE6hrpcE8_LPhH6qydWRonjORnItOYqyXHk2Zs1E'
  113. 'ARojAdB15GTrMA6VJe_Z13VLBsPL1_ccmk5YUajrBRqxNhSbpAaMdU1Rxkqp13iq6x'
  114. 'Np5LxMI15RwtgUSOWx7iqNtyqI3S4Wej6TrmsWfHx2lcD5r-PSa7NWN8glxPxf5r5c'
  115. 'ikGrPedw6wZaj1gFbuMZPFaaPKrIAtFceOvJDQSqCNBRJ7BAiGX6TtCEZt0ta2zQd8'
  116. 'uwY-4MVqOBqYJxDFvucsbyiXLVd4i6kbUuMeqh8ZA_S1yyutlgIQfFYnLykziFH9vW'
  117. 'kB8Uet5iDKQGCEWBhiSln6q80UDlBDch4psPSy1wNZMnVYR2o13m3ASwreQRnceRi2'
  118. 'AjSNqOwsqWmbAZxSp_7kcBFnJBeHez4CKpKqieDQgsQREK5fNcBB_H3HrFIUUeJo4s'
  119. 'Wx7Abekn6HnHpTM10348UMM8hEejdKbY8ncxfCaO-OgVOHn1ZJX2DRSf8px4eqj6y7'
  120. 'dvv162anXS6LYjC3h1YEt_yx-IQ2lxcMo82gw-NVOHdj28EdHH1GDBFYuaQFIMQsrz'
  121. 'GZtiyicrqlAYznyhgd2bHFeYHLvJYlHfy_svL7995bOjofp4ef_55fv36zRANbIJA2'
  122. 'FX0C_v34oE3Es9oHtQIOFFZcilS5WdV_J5YUHRoeAvdCrZ0IDTCuy4sTOvHvMe96rl'
  123. 'usfxs5rcrLuTv1lmOApYmqip6_bEz4eORSyR2xA8tmWxKnkvP3fM0Hgi4bpstFisWR'
  124. 'TWV31adSdvSkPc7SkKbtOOTxgny05ALE6pNdL5vhQ5dFQKhYxjbpJZ0ChuSWcN22nh'
  125. 'rGpPwC32HXSL7Qm8xf6Dzu6XfLfk19dFoZ4li1sRD9fJVVnWYOmiDCe97Uw0RGi4am'
  126. 'o-JJA7IMMYUO7fIvM6N6ZG4ILlotrPhyjXSbSQqQZj7i2d-2pzGntRIHefJS8viwaK'
  127. '-iW6NN9uyTSuTP88CwtKrG-GPaSz6Qn92fwEtGxVk4QMrAhMdev7m6yMBLMOF86iZN'
  128. 'JIe_xEadXAQuzW8HltyDCkJrmYVqVOI_oU7ijL64W03LLC81jcA8kFuQpDX1R90-b9'
  129. '_iZOD2J1t9xfE0BGSJ5PqHA7kUUudYuG7HFjz12C2Mz3zNhD8eQgFa_sdiy3InNWHg'
  130. 'pV9OCCkWPUZRivRfA2g3DytC3fnlajSaJs4Zihvrwto7eeQxRVR3noCSDzhbZzYKjn'
  131. 'd-DZy7PtaVp2WgvPBpzCXUL_J1OGex48RVmOXzBU8_N3kqekkefRDzxNK2_Klp9mBJ'
  132. 'wsUnXyRqq1mScHuYalUY7_AZTCR4s=&q='
  133. )
  134. self.preferences.parse_encoded_data(url_params)
  135. self.assertEqual(
  136. vars(self.preferences.key_value_settings['categories']),
  137. {'value': ['general'], 'locked': False, 'choices': ['general', 'none']},
  138. )
  139. def test_save_key_value_setting(self):
  140. setting_key = 'foo'
  141. setting_value = 'bar'
  142. cookie_callback = {}
  143. def set_cookie_callback(name, value, max_age): # pylint: disable=unused-argument
  144. cookie_callback[name] = value
  145. response_mock = Mock(flask.Response)
  146. response_mock.set_cookie = set_cookie_callback
  147. self.preferences.key_value_settings = {
  148. setting_key: Setting(
  149. setting_value,
  150. locked=False,
  151. ),
  152. }
  153. self.preferences.save(response_mock)
  154. self.assertIn(setting_key, cookie_callback)
  155. self.assertEqual(cookie_callback[setting_key], setting_value)
  156. def test_false_key_value_setting(self):
  157. setting_key = 'foo'
  158. cookie_callback = {}
  159. def set_cookie_callback(name, value, max_age): # pylint: disable=unused-argument
  160. cookie_callback[name] = value
  161. response_mock = Mock(flask.Response)
  162. response_mock.set_cookie = set_cookie_callback
  163. self.preferences.key_value_settings = {
  164. setting_key: Setting(
  165. '',
  166. locked=True,
  167. ),
  168. }
  169. self.preferences.save(response_mock)
  170. self.assertNotIn(setting_key, cookie_callback)