test_plugins.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. from searx.testing import SearxTestCase
  3. from searx import plugins
  4. from mock import Mock
  5. def get_search_mock(query, **kwargs):
  6. return Mock(search_query=Mock(query=query, **kwargs),
  7. result_container=Mock(answers=set()))
  8. class PluginStoreTest(SearxTestCase):
  9. def test_PluginStore_init(self):
  10. store = plugins.PluginStore()
  11. self.assertTrue(isinstance(store.plugins, list) and len(store.plugins) == 0)
  12. def test_PluginStore_register(self):
  13. store = plugins.PluginStore()
  14. testplugin = plugins.Plugin()
  15. store.register(testplugin)
  16. self.assertTrue(len(store.plugins) == 1)
  17. def test_PluginStore_call(self):
  18. store = plugins.PluginStore()
  19. testplugin = plugins.Plugin()
  20. store.register(testplugin)
  21. setattr(testplugin, 'asdf', Mock())
  22. request = Mock()
  23. store.call([], 'asdf', request, Mock())
  24. self.assertFalse(testplugin.asdf.called)
  25. store.call([testplugin], 'asdf', request, Mock())
  26. self.assertTrue(testplugin.asdf.called)
  27. class SelfIPTest(SearxTestCase):
  28. def test_PluginStore_init(self):
  29. store = plugins.PluginStore()
  30. store.register(plugins.self_info)
  31. self.assertTrue(len(store.plugins) == 1)
  32. # IP test
  33. request = Mock(remote_addr='127.0.0.1')
  34. request.headers.getlist.return_value = []
  35. search = get_search_mock(query=b'ip', pageno=1)
  36. store.call(store.plugins, 'post_search', request, search)
  37. self.assertTrue('127.0.0.1' in search.result_container.answers)
  38. search = get_search_mock(query=b'ip', pageno=2)
  39. store.call(store.plugins, 'post_search', request, search)
  40. self.assertFalse('127.0.0.1' in search.result_container.answers)
  41. # User agent test
  42. request = Mock(user_agent='Mock')
  43. request.headers.getlist.return_value = []
  44. search = get_search_mock(query=b'user-agent', pageno=1)
  45. store.call(store.plugins, 'post_search', request, search)
  46. self.assertTrue('Mock' in search.result_container.answers)
  47. search = get_search_mock(query=b'user-agent', pageno=2)
  48. store.call(store.plugins, 'post_search', request, search)
  49. self.assertFalse('Mock' in search.result_container.answers)
  50. search = get_search_mock(query=b'user-agent', pageno=1)
  51. store.call(store.plugins, 'post_search', request, search)
  52. self.assertTrue('Mock' in search.result_container.answers)
  53. search = get_search_mock(query=b'user-agent', pageno=2)
  54. store.call(store.plugins, 'post_search', request, search)
  55. self.assertFalse('Mock' in search.result_container.answers)
  56. search = get_search_mock(query=b'What is my User-Agent?', pageno=1)
  57. store.call(store.plugins, 'post_search', request, search)
  58. self.assertTrue('Mock' in search.result_container.answers)
  59. search = get_search_mock(query=b'What is my User-Agent?', pageno=2)
  60. store.call(store.plugins, 'post_search', request, search)
  61. self.assertFalse('Mock' in search.result_container.answers)