searxng/tests/unit/test_plugin_self_info.py
Markus Heiser 36a1ef1239 [refactor] typification of SearXNG / EngineResults
In [1] and [2] we discussed the need of a Result.results property and how we can
avoid unclear code.  This patch implements a class for the reslut-lists of
engines::

    searx.result_types.EngineResults

A simple example for the usage in engine development::

    from searx.result_types import EngineResults
    ...
    def response(resp) -> EngineResults:
        res = EngineResults()
        ...
        res.add( res.types.Answer(answer="lorem ipsum ..", url="https://example.org") )
        ...
        return res

[1] https://github.com/searxng/searxng/pull/4183#pullrequestreview-257400034
[2] https://github.com/searxng/searxng/pull/4183#issuecomment-2614301580
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
2025-01-28 07:07:08 +01:00

69 lines
2.3 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name
from parameterized.parameterized import parameterized
from flask_babel import gettext
import searx.plugins
import searx.preferences
import searx.limiter
import searx.botdetection
from searx.extended_types import sxng_request
from searx.result_types import Answer
from tests import SearxTestCase
from .test_plugins import do_post_search
class PluginIPSelfInfo(SearxTestCase):
def setUp(self):
super().setUp()
self.storage = searx.plugins.PluginStorage()
self.storage.register_by_fqn("searx.plugins.self_info.SXNGPlugin")
self.storage.init(self.app)
self.pref = searx.preferences.Preferences(["simple"], ["general"], {}, self.storage)
self.pref.parse_dict({"locale": "en"})
cfg = searx.limiter.get_cfg()
searx.botdetection.init(cfg, None)
def test_plugin_store_init(self):
self.assertEqual(1, len(self.storage))
def test_pageno_1_2(self):
with self.app.test_request_context():
sxng_request.preferences = self.pref
sxng_request.remote_addr = "127.0.0.1"
sxng_request.headers = {"X-Forwarded-For": "1.2.3.4, 127.0.0.1", "X-Real-IP": "127.0.0.1"} # type: ignore
answer = Answer(answer=gettext("Your IP is: ") + "127.0.0.1")
search = do_post_search("ip", self.storage, pageno=1)
self.assertIn(answer, search.result_container.answers)
search = do_post_search("ip", self.storage, pageno=2)
self.assertEqual(list(search.result_container.answers), [])
@parameterized.expand(
[
"user-agent",
"USER-AgenT lorem ipsum",
]
)
def test_user_agent_in_answer(self, query: str):
query = "user-agent"
with self.app.test_request_context():
sxng_request.preferences = self.pref
sxng_request.user_agent = "Dummy agent" # type: ignore
answer = Answer(answer=gettext("Your user-agent is: ") + "Dummy agent")
search = do_post_search(query, self.storage, pageno=1)
self.assertIn(answer, search.result_container.answers)
search = do_post_search(query, self.storage, pageno=2)
self.assertEqual(list(search.result_container.answers), [])