test_plugins.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(user_plugins=[])
  23. store.call('asdf', request, Mock())
  24. self.assertFalse(testplugin.asdf.called)
  25. request.user_plugins.append(testplugin)
  26. store.call('asdf', request, Mock())
  27. self.assertTrue(testplugin.asdf.called)
  28. class SelfIPTest(SearxTestCase):
  29. def test_PluginStore_init(self):
  30. store = plugins.PluginStore()
  31. store.register(plugins.self_info)
  32. self.assertTrue(len(store.plugins) == 1)
  33. # IP test
  34. request = Mock(user_plugins=store.plugins,
  35. remote_addr='127.0.0.1')
  36. request.headers.getlist.return_value = []
  37. search = get_search_mock(query='ip', pageno=1)
  38. store.call('post_search', request, search)
  39. self.assertTrue('127.0.0.1' in search.result_container.answers)
  40. search = get_search_mock(query='ip', pageno=2)
  41. store.call('post_search', request, search)
  42. self.assertFalse('127.0.0.1' in search.result_container.answers)
  43. # User agent test
  44. request = Mock(user_plugins=store.plugins,
  45. user_agent='Mock')
  46. request.headers.getlist.return_value = []
  47. search = get_search_mock(query='user-agent', pageno=1)
  48. store.call('post_search', request, search)
  49. self.assertTrue('Mock' in search.result_container.answers)
  50. search = get_search_mock(query='user-agent', pageno=2)
  51. store.call('post_search', request, search)
  52. self.assertFalse('Mock' in search.result_container.answers)
  53. search = get_search_mock(query='user-agent', pageno=1)
  54. store.call('post_search', request, search)
  55. self.assertTrue('Mock' in search.result_container.answers)
  56. search = get_search_mock(query='user-agent', pageno=2)
  57. store.call('post_search', request, search)
  58. self.assertFalse('Mock' in search.result_container.answers)
  59. search = get_search_mock(query='What is my User-Agent?', pageno=1)
  60. store.call('post_search', request, search)
  61. self.assertTrue('Mock' in search.result_container.answers)
  62. search = get_search_mock(query='What is my User-Agent?', pageno=2)
  63. store.call('post_search', request, search)
  64. self.assertFalse('Mock' in search.result_container.answers)