2024-03-11 13:06:26 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# pylint: disable=missing-module-docstring
|
|
|
|
|
2016-05-19 05:38:43 +00:00
|
|
|
from searx import settings, engines
|
2021-09-02 14:01:34 +00:00
|
|
|
from tests import SearxTestCase
|
2016-05-19 05:38:43 +00:00
|
|
|
|
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
class TestEnginesInit(SearxTestCase): # pylint: disable=missing-class-docstring
|
2016-05-19 05:38:43 +00:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
settings['outgoing']['using_tor_proxy'] = False
|
|
|
|
settings['outgoing']['extra_proxy_timeout'] = 0
|
|
|
|
|
|
|
|
def test_initialize_engines_default(self):
|
2021-12-27 08:26:22 +00:00
|
|
|
engine_list = [
|
|
|
|
{'engine': 'dummy', 'name': 'engine1', 'shortcut': 'e1'},
|
|
|
|
{'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2'},
|
|
|
|
]
|
2016-05-19 05:38:43 +00:00
|
|
|
|
2021-04-08 16:44:29 +00:00
|
|
|
engines.load_engines(engine_list)
|
2016-05-19 05:38:43 +00:00
|
|
|
self.assertEqual(len(engines.engines), 2)
|
|
|
|
self.assertIn('engine1', engines.engines)
|
|
|
|
self.assertIn('engine2', engines.engines)
|
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
def test_initialize_engines_exclude_onions(self): # pylint: disable=invalid-name
|
2016-05-19 05:38:43 +00:00
|
|
|
settings['outgoing']['using_tor_proxy'] = False
|
2021-12-27 08:26:22 +00:00
|
|
|
engine_list = [
|
|
|
|
{'engine': 'dummy', 'name': 'engine1', 'shortcut': 'e1', 'categories': 'general'},
|
|
|
|
{'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2', 'categories': 'onions'},
|
|
|
|
]
|
2016-05-19 05:38:43 +00:00
|
|
|
|
2021-05-05 11:08:54 +00:00
|
|
|
engines.load_engines(engine_list)
|
2016-05-19 05:38:43 +00:00
|
|
|
self.assertEqual(len(engines.engines), 1)
|
|
|
|
self.assertIn('engine1', engines.engines)
|
|
|
|
self.assertNotIn('onions', engines.categories)
|
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
def test_initialize_engines_include_onions(self): # pylint: disable=invalid-name
|
2016-05-19 05:38:43 +00:00
|
|
|
settings['outgoing']['using_tor_proxy'] = True
|
|
|
|
settings['outgoing']['extra_proxy_timeout'] = 100.0
|
2021-12-27 08:26:22 +00:00
|
|
|
engine_list = [
|
|
|
|
{
|
|
|
|
'engine': 'dummy',
|
|
|
|
'name': 'engine1',
|
|
|
|
'shortcut': 'e1',
|
|
|
|
'categories': 'general',
|
|
|
|
'timeout': 20.0,
|
|
|
|
'onion_url': 'http://engine1.onion',
|
|
|
|
},
|
|
|
|
{'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2', 'categories': 'onions'},
|
|
|
|
]
|
2016-05-19 05:38:43 +00:00
|
|
|
|
2021-05-05 11:08:54 +00:00
|
|
|
engines.load_engines(engine_list)
|
2016-05-19 05:38:43 +00:00
|
|
|
self.assertEqual(len(engines.engines), 2)
|
|
|
|
self.assertIn('engine1', engines.engines)
|
|
|
|
self.assertIn('engine2', engines.engines)
|
|
|
|
self.assertIn('onions', engines.categories)
|
|
|
|
self.assertIn('http://engine1.onion', engines.engines['engine1'].search_url)
|
|
|
|
self.assertEqual(engines.engines['engine1'].timeout, 120.0)
|
2022-12-04 22:43:59 +00:00
|
|
|
|
|
|
|
def test_missing_name_field(self):
|
|
|
|
settings['outgoing']['using_tor_proxy'] = False
|
|
|
|
engine_list = [
|
|
|
|
{'engine': 'dummy', 'shortcut': 'e1', 'categories': 'general'},
|
|
|
|
]
|
2024-03-11 13:06:26 +00:00
|
|
|
with self.assertLogs('searx.engines', level='ERROR') as cm: # pylint: disable=invalid-name
|
2022-12-04 22:43:59 +00:00
|
|
|
engines.load_engines(engine_list)
|
|
|
|
self.assertEqual(len(engines.engines), 0)
|
|
|
|
self.assertEqual(cm.output, ['ERROR:searx.engines:An engine does not have a "name" field'])
|
|
|
|
|
|
|
|
def test_missing_engine_field(self):
|
|
|
|
settings['outgoing']['using_tor_proxy'] = False
|
|
|
|
engine_list = [
|
|
|
|
{'name': 'engine2', 'shortcut': 'e2', 'categories': 'onions'},
|
|
|
|
]
|
2024-03-11 13:06:26 +00:00
|
|
|
with self.assertLogs('searx.engines', level='ERROR') as cm: # pylint: disable=invalid-name
|
2022-12-04 22:43:59 +00:00
|
|
|
engines.load_engines(engine_list)
|
|
|
|
self.assertEqual(len(engines.engines), 0)
|
|
|
|
self.assertEqual(
|
|
|
|
cm.output, ['ERROR:searx.engines:The "engine" field is missing for the engine named "engine2"']
|
|
|
|
)
|