summaryrefslogtreecommitdiff
path: root/tests/unit/test_engines_init.py
blob: e2445160ab250ad1f8fe6c22494220d5b609f163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=missing-module-docstring

from searx import settings, engines
from tests import SearxTestCase


class TestEnginesInit(SearxTestCase):  # pylint: disable=missing-class-docstring
    @classmethod
    def tearDownClass(cls):
        settings['outgoing']['using_tor_proxy'] = False
        settings['outgoing']['extra_proxy_timeout'] = 0
        engines.load_engines([])

    def test_initialize_engines_default(self):
        engine_list = [
            {'engine': 'dummy', 'name': 'engine1', 'shortcut': 'e1'},
            {'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2'},
        ]

        engines.load_engines(engine_list)
        self.assertEqual(len(engines.engines), 2)
        self.assertIn('engine1', engines.engines)
        self.assertIn('engine2', engines.engines)

    def test_initialize_engines_exclude_onions(self):  # pylint: disable=invalid-name
        settings['outgoing']['using_tor_proxy'] = False
        engine_list = [
            {'engine': 'dummy', 'name': 'engine1', 'shortcut': 'e1', 'categories': 'general'},
            {'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2', 'categories': 'onions'},
        ]

        engines.load_engines(engine_list)
        self.assertEqual(len(engines.engines), 1)
        self.assertIn('engine1', engines.engines)
        self.assertNotIn('onions', engines.categories)

    def test_initialize_engines_include_onions(self):  # pylint: disable=invalid-name
        settings['outgoing']['using_tor_proxy'] = True
        settings['outgoing']['extra_proxy_timeout'] = 100.0
        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'},
        ]

        engines.load_engines(engine_list)
        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)

    def test_missing_name_field(self):
        settings['outgoing']['using_tor_proxy'] = False
        engine_list = [
            {'engine': 'dummy', 'shortcut': 'e1', 'categories': 'general'},
        ]
        with self.assertLogs('searx.engines', level='ERROR') as cm:  # pylint: disable=invalid-name
            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'},
        ]
        with self.assertLogs('searx.engines', level='ERROR') as cm:  # pylint: disable=invalid-name
            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"']
            )