test_plugin_self_info.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring, invalid-name
  3. from mock import Mock
  4. from parameterized.parameterized import parameterized
  5. from searx import (
  6. plugins,
  7. limiter,
  8. botdetection,
  9. )
  10. from tests import SearxTestCase
  11. from .test_plugins import get_search_mock
  12. class PluginIPSelfInfo(SearxTestCase): # pylint: disable=missing-class-docstring
  13. def setUp(self):
  14. plugin = plugins.load_and_initialize_plugin('searx.plugins.self_info', False, (None, {}))
  15. self.store = plugins.PluginStore()
  16. self.store.register(plugin)
  17. cfg = limiter.get_cfg()
  18. botdetection.init(cfg, None)
  19. def test_plugin_store_init(self):
  20. self.assertEqual(1, len(self.store.plugins))
  21. def test_ip_in_answer(self):
  22. request = Mock()
  23. request.remote_addr = '127.0.0.1'
  24. request.headers = {'X-Forwarded-For': '1.2.3.4, 127.0.0.1', 'X-Real-IP': '127.0.0.1'}
  25. search = get_search_mock(query='ip', pageno=1)
  26. self.store.call(self.store.plugins, 'post_search', request, search)
  27. self.assertIn('127.0.0.1', search.result_container.answers["ip"]["answer"])
  28. def test_ip_not_in_answer(self):
  29. request = Mock()
  30. request.remote_addr = '127.0.0.1'
  31. request.headers = {'X-Forwarded-For': '1.2.3.4, 127.0.0.1', 'X-Real-IP': '127.0.0.1'}
  32. search = get_search_mock(query='ip', pageno=2)
  33. self.store.call(self.store.plugins, 'post_search', request, search)
  34. self.assertNotIn('ip', search.result_container.answers)
  35. @parameterized.expand(
  36. [
  37. 'user-agent',
  38. 'What is my User-Agent?',
  39. ]
  40. )
  41. def test_user_agent_in_answer(self, query: str):
  42. request = Mock(user_agent=Mock(string='Mock'))
  43. search = get_search_mock(query=query, pageno=1)
  44. self.store.call(self.store.plugins, 'post_search', request, search)
  45. self.assertIn('Mock', search.result_container.answers["user-agent"]["answer"])
  46. @parameterized.expand(
  47. [
  48. 'user-agent',
  49. 'What is my User-Agent?',
  50. ]
  51. )
  52. def test_user_agent_not_in_answer(self, query: str):
  53. request = Mock(user_agent=Mock(string='Mock'))
  54. search = get_search_mock(query=query, pageno=2)
  55. self.store.call(self.store.plugins, 'post_search', request, search)
  56. self.assertNotIn('user-agent', search.result_container.answers)