test_engines_init.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. from searx import settings, engines
  4. from tests import SearxTestCase
  5. class TestEnginesInit(SearxTestCase): # pylint: disable=missing-class-docstring
  6. @classmethod
  7. def tearDownClass(cls):
  8. settings['outgoing']['using_tor_proxy'] = False
  9. settings['outgoing']['extra_proxy_timeout'] = 0
  10. engines.load_engines([])
  11. def test_initialize_engines_default(self):
  12. engine_list = [
  13. {'engine': 'dummy', 'name': 'engine1', 'shortcut': 'e1'},
  14. {'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2'},
  15. ]
  16. engines.load_engines(engine_list)
  17. self.assertEqual(len(engines.engines), 2)
  18. self.assertIn('engine1', engines.engines)
  19. self.assertIn('engine2', engines.engines)
  20. def test_initialize_engines_exclude_onions(self): # pylint: disable=invalid-name
  21. settings['outgoing']['using_tor_proxy'] = False
  22. engine_list = [
  23. {'engine': 'dummy', 'name': 'engine1', 'shortcut': 'e1', 'categories': 'general'},
  24. {'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2', 'categories': 'onions'},
  25. ]
  26. engines.load_engines(engine_list)
  27. self.assertEqual(len(engines.engines), 1)
  28. self.assertIn('engine1', engines.engines)
  29. self.assertNotIn('onions', engines.categories)
  30. def test_initialize_engines_include_onions(self): # pylint: disable=invalid-name
  31. settings['outgoing']['using_tor_proxy'] = True
  32. settings['outgoing']['extra_proxy_timeout'] = 100.0
  33. engine_list = [
  34. {
  35. 'engine': 'dummy',
  36. 'name': 'engine1',
  37. 'shortcut': 'e1',
  38. 'categories': 'general',
  39. 'timeout': 20.0,
  40. 'onion_url': 'http://engine1.onion',
  41. },
  42. {'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2', 'categories': 'onions'},
  43. ]
  44. engines.load_engines(engine_list)
  45. self.assertEqual(len(engines.engines), 2)
  46. self.assertIn('engine1', engines.engines)
  47. self.assertIn('engine2', engines.engines)
  48. self.assertIn('onions', engines.categories)
  49. self.assertIn('http://engine1.onion', engines.engines['engine1'].search_url)
  50. self.assertEqual(engines.engines['engine1'].timeout, 120.0)
  51. def test_missing_name_field(self):
  52. settings['outgoing']['using_tor_proxy'] = False
  53. engine_list = [
  54. {'engine': 'dummy', 'shortcut': 'e1', 'categories': 'general'},
  55. ]
  56. with self.assertLogs('searx.engines', level='ERROR') as cm: # pylint: disable=invalid-name
  57. engines.load_engines(engine_list)
  58. self.assertEqual(len(engines.engines), 0)
  59. self.assertEqual(cm.output, ['ERROR:searx.engines:An engine does not have a "name" field'])
  60. def test_missing_engine_field(self):
  61. settings['outgoing']['using_tor_proxy'] = False
  62. engine_list = [
  63. {'name': 'engine2', 'shortcut': 'e2', 'categories': 'onions'},
  64. ]
  65. with self.assertLogs('searx.engines', level='ERROR') as cm: # pylint: disable=invalid-name
  66. engines.load_engines(engine_list)
  67. self.assertEqual(len(engines.engines), 0)
  68. self.assertEqual(
  69. cm.output, ['ERROR:searx.engines:The "engine" field is missing for the engine named "engine2"']
  70. )