test_preferences.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from searx.preferences import (EnumStringSetting, MapSetting, MissingArgumentException, SearchLanguageSetting,
  2. MultipleChoiceSetting, PluginsSetting, ValidationException)
  3. from searx.testing import SearxTestCase
  4. class PluginStub:
  5. def __init__(self, plugin_id, default_on):
  6. self.id = plugin_id
  7. self.default_on = default_on
  8. class TestSettings(SearxTestCase):
  9. # map settings
  10. def test_map_setting_invalid_initialization(self):
  11. with self.assertRaises(MissingArgumentException):
  12. MapSetting(3, wrong_argument={'0': 0})
  13. def test_map_setting_invalid_default_value(self):
  14. with self.assertRaises(ValidationException):
  15. MapSetting(3, map={'dog': 1, 'bat': 2})
  16. def test_map_setting_invalid_choice(self):
  17. setting = MapSetting(2, map={'dog': 1, 'bat': 2})
  18. with self.assertRaises(ValidationException):
  19. setting.parse('cat')
  20. def test_map_setting_valid_default(self):
  21. setting = MapSetting(3, map={'dog': 1, 'bat': 2, 'cat': 3})
  22. self.assertEqual(setting.get_value(), 3)
  23. def test_map_setting_valid_choice(self):
  24. setting = MapSetting(3, map={'dog': 1, 'bat': 2, 'cat': 3})
  25. self.assertEqual(setting.get_value(), 3)
  26. setting.parse('bat')
  27. self.assertEqual(setting.get_value(), 2)
  28. # enum settings
  29. def test_enum_setting_invalid_initialization(self):
  30. with self.assertRaises(MissingArgumentException):
  31. EnumStringSetting('cat', wrong_argument=[0, 1, 2])
  32. def test_enum_setting_invalid_default_value(self):
  33. with self.assertRaises(ValidationException):
  34. EnumStringSetting(3, choices=[0, 1, 2])
  35. def test_enum_setting_invalid_choice(self):
  36. setting = EnumStringSetting(0, choices=[0, 1, 2])
  37. with self.assertRaises(ValidationException):
  38. setting.parse(3)
  39. def test_enum_setting_valid_default(self):
  40. setting = EnumStringSetting(3, choices=[1, 2, 3])
  41. self.assertEqual(setting.get_value(), 3)
  42. def test_enum_setting_valid_choice(self):
  43. setting = EnumStringSetting(3, choices=[1, 2, 3])
  44. self.assertEqual(setting.get_value(), 3)
  45. setting.parse(2)
  46. self.assertEqual(setting.get_value(), 2)
  47. # multiple choice settings
  48. def test_multiple_setting_invalid_initialization(self):
  49. with self.assertRaises(MissingArgumentException):
  50. MultipleChoiceSetting(['2'], wrong_argument=['0', '1', '2'])
  51. def test_multiple_setting_invalid_default_value(self):
  52. with self.assertRaises(ValidationException):
  53. MultipleChoiceSetting(['3', '4'], choices=['0', '1', '2'])
  54. def test_multiple_setting_invalid_choice(self):
  55. setting = MultipleChoiceSetting(['1', '2'], choices=['0', '1', '2'])
  56. with self.assertRaises(ValidationException):
  57. setting.parse('4, 3')
  58. def test_multiple_setting_valid_default(self):
  59. setting = MultipleChoiceSetting(['3'], choices=['1', '2', '3'])
  60. self.assertEqual(setting.get_value(), ['3'])
  61. def test_multiple_setting_valid_choice(self):
  62. setting = MultipleChoiceSetting(['3'], choices=['1', '2', '3'])
  63. self.assertEqual(setting.get_value(), ['3'])
  64. setting.parse('2')
  65. self.assertEqual(setting.get_value(), ['2'])
  66. # search language settings
  67. def test_lang_setting_valid_choice(self):
  68. setting = SearchLanguageSetting('all', choices=['all', 'de', 'en'])
  69. setting.parse('de')
  70. self.assertEqual(setting.get_value(), 'de')
  71. def test_lang_setting_invalid_choice(self):
  72. setting = SearchLanguageSetting('all', choices=['all', 'de', 'en'])
  73. setting.parse('xx')
  74. self.assertEqual(setting.get_value(), 'all')
  75. def test_lang_setting_old_cookie_choice(self):
  76. setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES'])
  77. setting.parse('es_XA')
  78. self.assertEqual(setting.get_value(), 'es')
  79. def test_lang_setting_old_cookie_format(self):
  80. setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES'])
  81. setting.parse('es_ES')
  82. self.assertEqual(setting.get_value(), 'es-ES')
  83. # plugins settings
  84. def test_plugins_setting_all_default_enabled(self):
  85. plugin1 = PluginStub('plugin1', True)
  86. plugin2 = PluginStub('plugin2', True)
  87. setting = PluginsSetting(['3'], choices=[plugin1, plugin2])
  88. self.assertEqual(setting.get_enabled(), set(['plugin1', 'plugin2']))
  89. def test_plugins_setting_few_default_enabled(self):
  90. plugin1 = PluginStub('plugin1', True)
  91. plugin2 = PluginStub('plugin2', False)
  92. plugin3 = PluginStub('plugin3', True)
  93. setting = PluginsSetting('name', choices=[plugin1, plugin2, plugin3])
  94. self.assertEqual(setting.get_enabled(), set(['plugin1', 'plugin3']))
  95. class TestPreferences(SearxTestCase):
  96. def test_encode(self):
  97. from searx.preferences import Preferences
  98. pref = Preferences(['oscar'], ['general'], {}, [])
  99. url_params = 'eJx1VMmO2zAM_Zr6YrTocujJh6JF0QEKzKAz7VVgJNohLIseUU7ivy-VcWy5yyGOTVGP73GLKJNPYjiYgGeT4NB8BS9YOSY' \
  100. 'TUdifMDYM-vmGY1d5CN0EHTYOK88W_PXNkcDBozOjnzoK0vyi4bWnHs2RU4-zvHr_-RF9a-5Cy3GARByy7X7EkKMoBeMp9CuPQ-SzYMx' \
  101. '8Vr9P1qKI-XJ_p1fOkRJWNCgVM0a-zAttmBJbHkaPSZlNts-_jiuBFgUh2mPztkpHHLBhsRArDHvm356eHh5vATS0Mqagr0ZsZO_V8hT' \
  102. 'B9srt54_v6jewJugqL4Nn_hYSdhxnI-jRpi05GDQCStOT7UGVmJY8ZnltRKyF23SGiLWjqNcygKGkpyeGZIywJfD1gI5AjRTAmBM55Aw' \
  103. 'Q0Tn626lj7jzWo4e5hnEsIlprX6dTgdBRpyRBFKTDgBF8AasVyT4gvSTEoXRpXWRyG3CYQYld65I_V6lboILTMAlZY65_ejRDcHgp0Tv' \
  104. 'EPtGAsqTiBf3m76g7pP9B84mwjPvuUtASRDei1nDF2ix_JXW91UJkXrPh6RAhznVmKyQl7dwJdMJ6bz1QOmgzYlrEzHDMcEUuo44AgS1' \
  105. 'CvkjaOb2Q2AyY5oGDTs_OLXE_c2I5cg9hk3kEJZ0fu4SuktsIA2RhuJwP86AdripThCBeO9uVUejyPGmFSxPrqEYcuWi25zOEXV9tc1m' \
  106. '_KP1nafYtdfv6Q9hKfWmGm9A_3G635UwiVndLGdFCiLWkONk0xUxGLGGweGWTa2nZYZ0fS1YKlE3Uuw8fPl52E5U8HJYbC7sbjXUsrnT' \
  107. 'XHXRbELfO-1fGSqskiGnMK7B0dV3t8Lq08pbdtYpuVdoKWA2Yjuyah_vHp2rZWjo0zXL8Gw8DTj0='
  108. pref.parse_encoded_data(url_params)
  109. self.assertEqual(
  110. vars(pref.key_value_settings['categories']),
  111. {'value': ['general'], 'locked': False, 'choices': ['general', 'none']})