summaryrefslogtreecommitdiff
path: root/tests/unit/test_plugins.py
blob: 33df0f320dd1d4219462e8af2e3ffd715f639597 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name

import babel
from mock import Mock

import searx.plugins
import searx.preferences
import searx.results

from searx.result_types import Result
from searx.extended_types import sxng_request

from tests import SearxTestCase

plg_store = searx.plugins.PluginStorage()
plg_store.load_builtins()


def get_search_mock(query, **kwargs):

    lang = kwargs.get("lang", "en-US")
    kwargs["pageno"] = kwargs.get("pageno", 1)
    kwargs["locale"] = babel.Locale.parse(lang, sep="-")
    user_plugins = kwargs.pop("user_plugins", [x.id for x in plg_store])

    return Mock(
        search_query=Mock(query=query, **kwargs),
        user_plugins=user_plugins,
        result_container=searx.results.ResultContainer(),
    )


def do_pre_search(query, storage, **kwargs) -> bool:

    search = get_search_mock(query, **kwargs)
    ret = storage.pre_search(sxng_request, search)
    return ret


def do_post_search(query, storage, **kwargs) -> Mock:

    search = get_search_mock(query, **kwargs)
    storage.post_search(sxng_request, search)
    return search


class PluginMock(searx.plugins.Plugin):

    def __init__(self, _id: str, name: str, default_on: bool):
        self.id = _id
        self.default_on = default_on
        self._name = name
        super().__init__()

    # pylint: disable= unused-argument
    def pre_search(self, request, search) -> bool:
        return True

    def post_search(self, request, search) -> None:
        return None

    def on_result(self, request, search, result) -> bool:
        return False

    def info(self):
        return searx.plugins.PluginInfo(
            id=self.id,
            name=self._name,
            description=f"Dummy plugin: {self.id}",
            preference_section="general",
        )


class PluginStorage(SearxTestCase):

    def setUp(self):
        super().setUp()
        engines = {}

        self.storage = searx.plugins.PluginStorage()
        self.storage.register(PluginMock("plg001", "first plugin", True))
        self.storage.register(PluginMock("plg002", "second plugin", True))
        self.storage.init(self.app)
        self.pref = searx.preferences.Preferences(["simple"], ["general"], engines, self.storage)
        self.pref.parse_dict({"locale": "en"})

    def test_init(self):

        self.assertEqual(2, len(self.storage))

    def test_hooks(self):

        with self.app.test_request_context():
            sxng_request.preferences = self.pref
            query = ""

            ret = do_pre_search(query, self.storage, pageno=1)
            self.assertTrue(ret is True)

            ret = self.storage.on_result(
                sxng_request,
                get_search_mock("lorem ipsum", user_plugins=["plg001", "plg002"]),
                Result(),
            )
            self.assertFalse(ret)