2024-03-11 14:06:26 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# pylint: disable=missing-module-docstring, invalid-name
|
2015-06-16 19:56:16 +02:00
|
|
|
|
2022-12-16 21:28:57 +01:00
|
|
|
from copy import copy
|
2024-08-21 08:19:54 +02:00
|
|
|
import logging
|
2022-12-16 21:28:57 +01:00
|
|
|
|
2021-09-02 16:01:34 +02:00
|
|
|
import searx.search
|
2020-09-22 16:22:22 +02:00
|
|
|
from searx.search import SearchQuery, EngineRef
|
2021-06-01 08:07:26 +02:00
|
|
|
from searx import settings
|
2021-09-02 16:01:34 +02:00
|
|
|
from tests import SearxTestCase
|
2020-02-01 11:01:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
SAFESEARCH = 0
|
|
|
|
PAGENO = 1
|
|
|
|
PUBLIC_ENGINE_NAME = 'general dummy'
|
|
|
|
TEST_ENGINES = [
|
|
|
|
{
|
|
|
|
'name': PUBLIC_ENGINE_NAME,
|
|
|
|
'engine': 'dummy',
|
|
|
|
'categories': 'general',
|
|
|
|
'shortcut': 'gd',
|
|
|
|
'timeout': 3.0,
|
|
|
|
'tokens': [],
|
|
|
|
},
|
|
|
|
]
|
2019-08-02 13:50:51 +02:00
|
|
|
|
2015-06-16 19:56:16 +02:00
|
|
|
|
2024-03-11 14:06:26 +01:00
|
|
|
class SearchQueryTestCase(SearxTestCase): # pylint: disable=missing-class-docstring
|
2020-11-10 10:17:49 +01:00
|
|
|
def test_repr(self):
|
2020-12-18 12:19:14 +01:00
|
|
|
s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, '1', 5.0, 'g')
|
2021-12-27 09:26:22 +01:00
|
|
|
self.assertEqual(
|
2023-09-10 18:44:16 +02:00
|
|
|
repr(s), "SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, '1', 5.0, 'g', None)"
|
2021-12-27 09:26:22 +01:00
|
|
|
) # noqa
|
2020-11-10 10:17:49 +01:00
|
|
|
|
|
|
|
def test_eq(self):
|
2020-12-18 12:19:14 +01:00
|
|
|
s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, None, None, None)
|
|
|
|
t = SearchQuery('test', [EngineRef('google', 'general')], 'all', 0, 1, None, None, None)
|
2020-11-10 10:17:49 +01:00
|
|
|
self.assertEqual(s, s)
|
|
|
|
self.assertNotEqual(s, t)
|
|
|
|
|
2022-12-16 21:28:57 +01:00
|
|
|
def test_copy(self):
|
|
|
|
s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, None, None, None)
|
|
|
|
t = copy(s)
|
|
|
|
self.assertEqual(s, t)
|
|
|
|
|
2020-11-10 10:17:49 +01:00
|
|
|
|
2024-03-11 14:06:26 +01:00
|
|
|
class SearchTestCase(SearxTestCase): # pylint: disable=missing-class-docstring
|
2023-08-09 09:55:48 +02:00
|
|
|
def setUp(self):
|
|
|
|
|
2024-08-21 08:19:54 +02:00
|
|
|
log = logging.getLogger("searx")
|
|
|
|
log_lev = log.level
|
|
|
|
log.setLevel(logging.ERROR)
|
2024-03-11 14:06:26 +01:00
|
|
|
from searx import webapp # pylint: disable=import-outside-toplevel
|
2023-08-09 09:55:48 +02:00
|
|
|
|
2024-08-21 08:19:54 +02:00
|
|
|
log.setLevel(log_lev)
|
|
|
|
|
2023-08-09 09:55:48 +02:00
|
|
|
self.app = webapp.app
|
|
|
|
|
2019-08-02 13:50:51 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-12-16 13:41:32 +01:00
|
|
|
searx.search.initialize(TEST_ENGINES)
|
2019-08-02 13:50:51 +02:00
|
|
|
|
|
|
|
def test_timeout_simple(self):
|
2021-06-01 08:07:26 +02:00
|
|
|
settings['outgoing']['max_request_timeout'] = None
|
2021-12-27 09:26:22 +01:00
|
|
|
search_query = SearchQuery(
|
|
|
|
'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, None
|
|
|
|
)
|
2019-08-02 13:50:51 +02:00
|
|
|
search = searx.search.Search(search_query)
|
2023-08-09 09:55:48 +02:00
|
|
|
with self.app.test_request_context('/search'):
|
|
|
|
search.search()
|
2020-08-06 17:42:46 +02:00
|
|
|
self.assertEqual(search.actual_timeout, 3.0)
|
2019-08-02 13:50:51 +02:00
|
|
|
|
|
|
|
def test_timeout_query_above_default_nomax(self):
|
2021-06-01 08:07:26 +02:00
|
|
|
settings['outgoing']['max_request_timeout'] = None
|
2021-12-27 09:26:22 +01:00
|
|
|
search_query = SearchQuery(
|
|
|
|
'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 5.0
|
|
|
|
)
|
2019-08-02 13:50:51 +02:00
|
|
|
search = searx.search.Search(search_query)
|
2023-08-09 09:55:48 +02:00
|
|
|
with self.app.test_request_context('/search'):
|
|
|
|
search.search()
|
2020-08-06 17:42:46 +02:00
|
|
|
self.assertEqual(search.actual_timeout, 3.0)
|
2019-08-02 13:50:51 +02:00
|
|
|
|
|
|
|
def test_timeout_query_below_default_nomax(self):
|
2021-06-01 08:07:26 +02:00
|
|
|
settings['outgoing']['max_request_timeout'] = None
|
2021-12-27 09:26:22 +01:00
|
|
|
search_query = SearchQuery(
|
|
|
|
'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 1.0
|
|
|
|
)
|
2019-08-02 13:50:51 +02:00
|
|
|
search = searx.search.Search(search_query)
|
2023-08-09 09:55:48 +02:00
|
|
|
with self.app.test_request_context('/search'):
|
|
|
|
search.search()
|
2020-08-06 17:42:46 +02:00
|
|
|
self.assertEqual(search.actual_timeout, 1.0)
|
2019-08-02 13:50:51 +02:00
|
|
|
|
|
|
|
def test_timeout_query_below_max(self):
|
2021-06-01 08:07:26 +02:00
|
|
|
settings['outgoing']['max_request_timeout'] = 10.0
|
2021-12-27 09:26:22 +01:00
|
|
|
search_query = SearchQuery(
|
|
|
|
'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 5.0
|
|
|
|
)
|
2019-08-02 13:50:51 +02:00
|
|
|
search = searx.search.Search(search_query)
|
2023-08-09 09:55:48 +02:00
|
|
|
with self.app.test_request_context('/search'):
|
|
|
|
search.search()
|
2020-08-06 17:42:46 +02:00
|
|
|
self.assertEqual(search.actual_timeout, 5.0)
|
2019-08-02 13:50:51 +02:00
|
|
|
|
|
|
|
def test_timeout_query_above_max(self):
|
2021-06-01 08:07:26 +02:00
|
|
|
settings['outgoing']['max_request_timeout'] = 10.0
|
2021-12-27 09:26:22 +01:00
|
|
|
search_query = SearchQuery(
|
|
|
|
'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 15.0
|
|
|
|
)
|
2019-08-02 13:50:51 +02:00
|
|
|
search = searx.search.Search(search_query)
|
2023-08-09 09:55:48 +02:00
|
|
|
with self.app.test_request_context('/search'):
|
|
|
|
search.search()
|
2020-08-06 17:42:46 +02:00
|
|
|
self.assertEqual(search.actual_timeout, 10.0)
|
2020-02-01 11:01:17 +01:00
|
|
|
|
2024-09-24 05:37:30 +02:00
|
|
|
def test_external_bang_valid(self):
|
2021-12-27 09:26:22 +01:00
|
|
|
search_query = SearchQuery(
|
|
|
|
'yes yes',
|
|
|
|
[EngineRef(PUBLIC_ENGINE_NAME, 'general')],
|
|
|
|
'en-US',
|
|
|
|
SAFESEARCH,
|
|
|
|
PAGENO,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
external_bang="yt",
|
|
|
|
)
|
2020-07-03 15:25:04 +02:00
|
|
|
search = searx.search.Search(search_query)
|
|
|
|
results = search.search()
|
|
|
|
# For checking if the user redirected with the youtube external bang
|
2024-09-24 05:37:30 +02:00
|
|
|
self.assertIsNotNone(results.redirect_url)
|
2020-07-03 15:25:04 +02:00
|
|
|
|
2024-09-24 05:37:30 +02:00
|
|
|
def test_external_bang_none(self):
|
2021-12-27 09:26:22 +01:00
|
|
|
search_query = SearchQuery(
|
|
|
|
'youtube never gonna give you up',
|
|
|
|
[EngineRef(PUBLIC_ENGINE_NAME, 'general')],
|
|
|
|
'en-US',
|
|
|
|
SAFESEARCH,
|
|
|
|
PAGENO,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
2020-07-03 15:25:04 +02:00
|
|
|
|
|
|
|
search = searx.search.Search(search_query)
|
2023-08-09 09:55:48 +02:00
|
|
|
with self.app.test_request_context('/search'):
|
|
|
|
results = search.search()
|
2020-07-03 15:25:04 +02:00
|
|
|
# This should not redirect
|
2024-09-24 05:37:30 +02:00
|
|
|
self.assertIsNone(results.redirect_url)
|