1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 {'search': Mock(query=query,
  7. result_container=Mock(answers=set()),
  8. **kwargs)}
  9. class PluginStoreTest(SearxTestCase):
  10. def test_PluginStore_init(self):
  11. store = plugins.PluginStore()
  12. self.assertTrue(isinstance(store.plugins, list) and len(store.plugins) == 0)
  13. def test_PluginStore_register(self):
  14. store = plugins.PluginStore()
  15. testplugin = plugins.Plugin()
  16. store.register(testplugin)
  17. self.assertTrue(len(store.plugins) == 1)
  18. def test_PluginStore_call(self):
  19. store = plugins.PluginStore()
  20. testplugin = plugins.Plugin()
  21. store.register(testplugin)
  22. setattr(testplugin, 'asdf', Mock())
  23. request = Mock(user_plugins=[])
  24. store.call('asdf', request, Mock())
  25. self.assertFalse(testplugin.asdf.called)
  26. request.user_plugins.append(testplugin)
  27. store.call('asdf', request, Mock())
  28. self.assertTrue(testplugin.asdf.called)
  29. class SelfIPTest(SearxTestCase):
  30. def test_PluginStore_init(self):
  31. store = plugins.PluginStore()
  32. store.register(plugins.self_info)
  33. self.assertTrue(len(store.plugins) == 1)
  34. # IP test
  35. request = Mock(user_plugins=store.plugins,
  36. remote_addr='127.0.0.1')
  37. request.headers.getlist.return_value = []
  38. ctx = get_search_mock(query='ip')
  39. store.call('post_search', request, ctx)
  40. self.assertTrue('127.0.0.1' in ctx['search'].result_container.answers)
  41. # User agent test
  42. request = Mock(user_plugins=store.plugins,
  43. user_agent='Mock')
  44. request.headers.getlist.return_value = []
  45. ctx = get_search_mock(query='user-agent')
  46. store.call('post_search', request, ctx)
  47. self.assertTrue('Mock' in ctx['search'].result_container.answers)
  48. ctx = get_search_mock(query='user-agent')
  49. store.call('post_search', request, ctx)
  50. self.assertTrue('Mock' in ctx['search'].result_container.answers)
  51. ctx = get_search_mock(query='What is my User-Agent?')
  52. store.call('post_search', request, ctx)
  53. self.assertTrue('Mock' in ctx['search'].result_container.answers)