test_plugins.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name
  3. import babel
  4. from mock import Mock
  5. import searx.plugins
  6. import searx.preferences
  7. import searx.results
  8. from searx.result_types import Result
  9. from searx.extended_types import sxng_request
  10. from tests import SearxTestCase
  11. plg_store = searx.plugins.PluginStorage()
  12. plg_store.load_builtins()
  13. def get_search_mock(query, **kwargs):
  14. lang = kwargs.get("lang", "en-US")
  15. kwargs["pageno"] = kwargs.get("pageno", 1)
  16. kwargs["locale"] = babel.Locale.parse(lang, sep="-")
  17. user_plugins = kwargs.pop("user_plugins", [x.id for x in plg_store])
  18. return Mock(
  19. search_query=Mock(query=query, **kwargs),
  20. user_plugins=user_plugins,
  21. result_container=searx.results.ResultContainer(),
  22. )
  23. def do_pre_search(query, storage, **kwargs) -> bool:
  24. search = get_search_mock(query, **kwargs)
  25. ret = storage.pre_search(sxng_request, search)
  26. return ret
  27. def do_post_search(query, storage, **kwargs) -> Mock:
  28. search = get_search_mock(query, **kwargs)
  29. storage.post_search(sxng_request, search)
  30. return search
  31. class PluginMock(searx.plugins.Plugin):
  32. def __init__(self, _id: str, name: str, default_on: bool):
  33. self.id = _id
  34. self.default_on = default_on
  35. self._name = name
  36. super().__init__()
  37. # pylint: disable= unused-argument
  38. def pre_search(self, request, search) -> bool:
  39. return True
  40. def post_search(self, request, search) -> None:
  41. return None
  42. def on_result(self, request, search, result) -> bool:
  43. return False
  44. def info(self):
  45. return searx.plugins.PluginInfo(
  46. id=self.id,
  47. name=self._name,
  48. description=f"Dummy plugin: {self.id}",
  49. preference_section="general",
  50. )
  51. class PluginStorage(SearxTestCase):
  52. def setUp(self):
  53. super().setUp()
  54. engines = {}
  55. self.storage = searx.plugins.PluginStorage()
  56. self.storage.register(PluginMock("plg001", "first plugin", True))
  57. self.storage.register(PluginMock("plg002", "second plugin", True))
  58. self.storage.init(self.app)
  59. self.pref = searx.preferences.Preferences(["simple"], ["general"], engines, self.storage)
  60. self.pref.parse_dict({"locale": "en"})
  61. def test_init(self):
  62. self.assertEqual(2, len(self.storage))
  63. def test_hooks(self):
  64. with self.app.test_request_context():
  65. sxng_request.preferences = self.pref
  66. query = ""
  67. ret = do_pre_search(query, self.storage, pageno=1)
  68. self.assertTrue(ret is True)
  69. ret = self.storage.on_result(
  70. sxng_request,
  71. get_search_mock("lorem ipsum", user_plugins=["plg001", "plg002"]),
  72. Result(),
  73. )
  74. self.assertFalse(ret)