test_plugins.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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=dict()))
  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) # pylint: disable=E1101
  25. store.call([testplugin], 'asdf', request, Mock())
  26. self.assertTrue(testplugin.asdf.called) # pylint: disable=E1101
  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='ip', pageno=1)
  36. store.call(store.plugins, 'post_search', request, search)
  37. self.assertTrue('127.0.0.1' in search.result_container.answers["ip"]["answer"])
  38. search = get_search_mock(query='ip', pageno=2)
  39. store.call(store.plugins, 'post_search', request, search)
  40. self.assertFalse('ip' 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='user-agent', pageno=1)
  45. store.call(store.plugins, 'post_search', request, search)
  46. self.assertTrue('Mock' in search.result_container.answers["user-agent"]["answer"])
  47. search = get_search_mock(query='user-agent', pageno=2)
  48. store.call(store.plugins, 'post_search', request, search)
  49. self.assertFalse('user-agent' in search.result_container.answers)
  50. search = get_search_mock(query='user-agent', pageno=1)
  51. store.call(store.plugins, 'post_search', request, search)
  52. self.assertTrue('Mock' in search.result_container.answers["user-agent"]["answer"])
  53. search = get_search_mock(query='user-agent', pageno=2)
  54. store.call(store.plugins, 'post_search', request, search)
  55. self.assertFalse('user-agent' in search.result_container.answers)
  56. search = get_search_mock(query='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["user-agent"]["answer"])
  59. search = get_search_mock(query='What is my User-Agent?', pageno=2)
  60. store.call(store.plugins, 'post_search', request, search)
  61. self.assertFalse('user-agent' in search.result_container.answers)
  62. class HashPluginTest(SearxTestCase):
  63. def test_PluginStore_init(self):
  64. store = plugins.PluginStore()
  65. store.register(plugins.hash_plugin)
  66. self.assertTrue(len(store.plugins) == 1)
  67. request = Mock(remote_addr='127.0.0.1')
  68. request.headers.getlist.return_value = []
  69. # MD5
  70. search = get_search_mock(query='md5 test', pageno=1)
  71. store.call(store.plugins, 'post_search', request, search)
  72. self.assertTrue('md5 hash digest: 098f6bcd4621d373cade4e832627b4f6'
  73. in search.result_container.answers['hash']['answer'])
  74. search = get_search_mock(query=b'md5 test', pageno=2)
  75. store.call(store.plugins, 'post_search', request, search)
  76. self.assertFalse('hash' in search.result_container.answers)
  77. # SHA1
  78. search = get_search_mock(query='sha1 test', pageno=1)
  79. store.call(store.plugins, 'post_search', request, search)
  80. self.assertTrue('sha1 hash digest: a94a8fe5ccb19ba61c4c0873d391e9879'
  81. '82fbbd3' in search.result_container.answers['hash']['answer'])
  82. # SHA224
  83. search = get_search_mock(query='sha224 test', pageno=1)
  84. store.call(store.plugins, 'post_search', request, search)
  85. self.assertTrue('sha224 hash digest: 90a3ed9e32b2aaf4c61c410eb9254261'
  86. '19e1a9dc53d4286ade99a809' in search.result_container.answers['hash']['answer'])
  87. # SHA256
  88. search = get_search_mock(query='sha256 test', pageno=1)
  89. store.call(store.plugins, 'post_search', request, search)
  90. self.assertTrue('sha256 hash digest: 9f86d081884c7d659a2feaa0c55ad015a'
  91. '3bf4f1b2b0b822cd15d6c15b0f00a08' in search.result_container.answers['hash']['answer'])
  92. # SHA384
  93. search = get_search_mock(query='sha384 test', pageno=1)
  94. store.call(store.plugins, 'post_search', request, search)
  95. self.assertTrue('sha384 hash digest: 768412320f7b0aa5812fce428dc4706b3c'
  96. 'ae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf1'
  97. '7a0a9' in search.result_container.answers['hash']['answer'])
  98. # SHA512
  99. search = get_search_mock(query='sha512 test', pageno=1)
  100. store.call(store.plugins, 'post_search', request, search)
  101. self.assertTrue('sha512 hash digest: ee26b0dd4af7e749aa1a8ee3c10ae9923f6'
  102. '18980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5'
  103. 'fa9ad8e6f57f50028a8ff' in search.result_container.answers['hash']['answer'])