2024-03-11 13:06:26 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# pylint: disable=missing-module-docstring
|
2020-09-22 14:55:59 +00:00
|
|
|
|
|
|
|
from searx.preferences import Preferences
|
|
|
|
from searx.engines import engines
|
|
|
|
|
|
|
|
import searx.search
|
2020-11-02 10:19:53 +00:00
|
|
|
from searx.search import EngineRef
|
2020-09-22 14:55:59 +00:00
|
|
|
from searx.webadapter import validate_engineref_list
|
2021-09-02 14:01:34 +00:00
|
|
|
from tests import SearxTestCase
|
2020-09-22 14:55:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
PRIVATE_ENGINE_NAME = 'general private offline'
|
|
|
|
TEST_ENGINES = [
|
|
|
|
{
|
|
|
|
'name': PRIVATE_ENGINE_NAME,
|
|
|
|
'engine': 'dummy-offline',
|
|
|
|
'categories': 'general',
|
|
|
|
'shortcut': 'do',
|
|
|
|
'timeout': 3.0,
|
2020-12-16 12:41:32 +00:00
|
|
|
'engine_type': 'offline',
|
2020-09-22 14:55:59 +00:00
|
|
|
'tokens': ['my-token'],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
SEARCHQUERY = [EngineRef(PRIVATE_ENGINE_NAME, 'general')]
|
|
|
|
|
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
class ValidateQueryCase(SearxTestCase): # pylint: disable=missing-class-docstring
|
2020-09-22 14:55:59 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-12-16 12:41:32 +00:00
|
|
|
searx.search.initialize(TEST_ENGINES)
|
2020-09-22 14:55:59 +00:00
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
def test_query_private_engine_without_token(self): # pylint:disable=invalid-name
|
2021-12-29 15:31:05 +00:00
|
|
|
preferences = Preferences(['simple'], ['general'], engines, [])
|
2020-09-22 14:55:59 +00:00
|
|
|
valid, unknown, invalid_token = validate_engineref_list(SEARCHQUERY, preferences)
|
|
|
|
self.assertEqual(len(valid), 0)
|
|
|
|
self.assertEqual(len(unknown), 0)
|
|
|
|
self.assertEqual(len(invalid_token), 1)
|
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
def test_query_private_engine_with_incorrect_token(self): # pylint:disable=invalid-name
|
2021-12-29 15:31:05 +00:00
|
|
|
preferences_with_tokens = Preferences(['simple'], ['general'], engines, [])
|
2020-09-22 14:55:59 +00:00
|
|
|
preferences_with_tokens.parse_dict({'tokens': 'bad-token'})
|
|
|
|
valid, unknown, invalid_token = validate_engineref_list(SEARCHQUERY, preferences_with_tokens)
|
|
|
|
self.assertEqual(len(valid), 0)
|
|
|
|
self.assertEqual(len(unknown), 0)
|
|
|
|
self.assertEqual(len(invalid_token), 1)
|
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
def test_query_private_engine_with_correct_token(self): # pylint:disable=invalid-name
|
2021-12-29 15:31:05 +00:00
|
|
|
preferences_with_tokens = Preferences(['simple'], ['general'], engines, [])
|
2020-09-22 14:55:59 +00:00
|
|
|
preferences_with_tokens.parse_dict({'tokens': 'my-token'})
|
|
|
|
valid, unknown, invalid_token = validate_engineref_list(SEARCHQUERY, preferences_with_tokens)
|
|
|
|
self.assertEqual(len(valid), 1)
|
|
|
|
self.assertEqual(len(unknown), 0)
|
|
|
|
self.assertEqual(len(invalid_token), 0)
|