test_search.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring, invalid-name
  3. from copy import copy
  4. import logging
  5. import searx.search
  6. from searx.search import SearchQuery, EngineRef
  7. from searx import settings
  8. from tests import SearxTestCase
  9. SAFESEARCH = 0
  10. PAGENO = 1
  11. PUBLIC_ENGINE_NAME = 'general dummy'
  12. TEST_ENGINES = [
  13. {
  14. 'name': PUBLIC_ENGINE_NAME,
  15. 'engine': 'dummy',
  16. 'categories': 'general',
  17. 'shortcut': 'gd',
  18. 'timeout': 3.0,
  19. 'tokens': [],
  20. },
  21. ]
  22. class SearchQueryTestCase(SearxTestCase): # pylint: disable=missing-class-docstring
  23. def test_repr(self):
  24. s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, '1', 5.0, 'g')
  25. self.assertEqual(
  26. repr(s), "SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, '1', 5.0, 'g', None)"
  27. ) # noqa
  28. def test_eq(self):
  29. s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, None, None, None)
  30. t = SearchQuery('test', [EngineRef('google', 'general')], 'all', 0, 1, None, None, None)
  31. self.assertEqual(s, s)
  32. self.assertNotEqual(s, t)
  33. def test_copy(self):
  34. s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, None, None, None)
  35. t = copy(s)
  36. self.assertEqual(s, t)
  37. class SearchTestCase(SearxTestCase): # pylint: disable=missing-class-docstring
  38. def setUp(self):
  39. log = logging.getLogger("searx")
  40. log_lev = log.level
  41. log.setLevel(logging.ERROR)
  42. from searx import webapp # pylint: disable=import-outside-toplevel
  43. log.setLevel(log_lev)
  44. self.app = webapp.app
  45. @classmethod
  46. def setUpClass(cls):
  47. searx.search.initialize(TEST_ENGINES)
  48. def test_timeout_simple(self):
  49. settings['outgoing']['max_request_timeout'] = None
  50. search_query = SearchQuery(
  51. 'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, None
  52. )
  53. search = searx.search.Search(search_query)
  54. with self.app.test_request_context('/search'):
  55. search.search()
  56. self.assertEqual(search.actual_timeout, 3.0)
  57. def test_timeout_query_above_default_nomax(self):
  58. settings['outgoing']['max_request_timeout'] = None
  59. search_query = SearchQuery(
  60. 'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 5.0
  61. )
  62. search = searx.search.Search(search_query)
  63. with self.app.test_request_context('/search'):
  64. search.search()
  65. self.assertEqual(search.actual_timeout, 3.0)
  66. def test_timeout_query_below_default_nomax(self):
  67. settings['outgoing']['max_request_timeout'] = None
  68. search_query = SearchQuery(
  69. 'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 1.0
  70. )
  71. search = searx.search.Search(search_query)
  72. with self.app.test_request_context('/search'):
  73. search.search()
  74. self.assertEqual(search.actual_timeout, 1.0)
  75. def test_timeout_query_below_max(self):
  76. settings['outgoing']['max_request_timeout'] = 10.0
  77. search_query = SearchQuery(
  78. 'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 5.0
  79. )
  80. search = searx.search.Search(search_query)
  81. with self.app.test_request_context('/search'):
  82. search.search()
  83. self.assertEqual(search.actual_timeout, 5.0)
  84. def test_timeout_query_above_max(self):
  85. settings['outgoing']['max_request_timeout'] = 10.0
  86. search_query = SearchQuery(
  87. 'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 15.0
  88. )
  89. search = searx.search.Search(search_query)
  90. with self.app.test_request_context('/search'):
  91. search.search()
  92. self.assertEqual(search.actual_timeout, 10.0)
  93. def test_external_bang_valid(self):
  94. search_query = SearchQuery(
  95. 'yes yes',
  96. [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  97. 'en-US',
  98. SAFESEARCH,
  99. PAGENO,
  100. None,
  101. None,
  102. external_bang="yt",
  103. )
  104. search = searx.search.Search(search_query)
  105. results = search.search()
  106. # For checking if the user redirected with the youtube external bang
  107. self.assertIsNotNone(results.redirect_url)
  108. def test_external_bang_none(self):
  109. search_query = SearchQuery(
  110. 'youtube never gonna give you up',
  111. [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  112. 'en-US',
  113. SAFESEARCH,
  114. PAGENO,
  115. None,
  116. None,
  117. )
  118. search = searx.search.Search(search_query)
  119. with self.app.test_request_context('/search'):
  120. results = search.search()
  121. # This should not redirect
  122. self.assertIsNone(results.redirect_url)