123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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)
  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=b'md5 test', pageno=1)
  71. store.call(store.plugins, 'post_search', request, search)
  72. self.assertTrue('md5 hash function: 098f6bcd4621d373cade4e832627b4f6' in search.result_container.answers)
  73. search = get_search_mock(query=b'md5 test', pageno=2)
  74. store.call(store.plugins, 'post_search', request, search)
  75. self.assertFalse('md5 hash function: 098f6bcd4621d373cade4e832627b4f6' in search.result_container.answers)
  76. # SHA1
  77. search = get_search_mock(query=b'sha1 test', pageno=1)
  78. store.call(store.plugins, 'post_search', request, search)
  79. self.assertTrue('sha1 hash function: a94a8fe5ccb19ba61c4c0873d391e9879'
  80. '82fbbd3' in search.result_container.answers)
  81. # SHA224
  82. search = get_search_mock(query=b'sha224 test', pageno=1)
  83. store.call(store.plugins, 'post_search', request, search)
  84. self.assertTrue('sha224 hash function: 90a3ed9e32b2aaf4c61c410eb9254261'
  85. '19e1a9dc53d4286ade99a809' in search.result_container.answers)
  86. # SHA256
  87. search = get_search_mock(query=b'sha256 test', pageno=1)
  88. store.call(store.plugins, 'post_search', request, search)
  89. self.assertTrue('sha256 hash function: 9f86d081884c7d659a2feaa0c55ad015a'
  90. '3bf4f1b2b0b822cd15d6c15b0f00a08' in search.result_container.answers)
  91. # SHA384
  92. search = get_search_mock(query=b'sha384 test', pageno=1)
  93. store.call(store.plugins, 'post_search', request, search)
  94. self.assertTrue('sha384 hash function: 768412320f7b0aa5812fce428dc4706b3c'
  95. 'ae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf1'
  96. '7a0a9' in search.result_container.answers)
  97. # SHA512
  98. search = get_search_mock(query=b'sha512 test', pageno=1)
  99. store.call(store.plugins, 'post_search', request, search)
  100. self.assertTrue('sha512 hash function: ee26b0dd4af7e749aa1a8ee3c10ae9923f6'
  101. '18980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5'
  102. 'fa9ad8e6f57f50028a8ff' in search.result_container.answers)