summaryrefslogtreecommitdiff
path: root/tests/unit/test_webadapter.py
blob: 9d8ff5f2888e6c796779c118ebf29f9ab34ec123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-

from searx.preferences import Preferences
from searx.engines import engines

import searx.search
from searx.search import EngineRef
from searx.webadapter import validate_engineref_list
from tests import SearxTestCase


PRIVATE_ENGINE_NAME = 'general private offline'
TEST_ENGINES = [
    {
        'name': PRIVATE_ENGINE_NAME,
        'engine': 'dummy-offline',
        'categories': 'general',
        'shortcut': 'do',
        'timeout': 3.0,
        'engine_type': 'offline',
        'tokens': ['my-token'],
    },
]
SEARCHQUERY = [EngineRef(PRIVATE_ENGINE_NAME, 'general')]


class ValidateQueryCase(SearxTestCase):

    @classmethod
    def setUpClass(cls):
        searx.search.initialize(TEST_ENGINES)

    def test_query_private_engine_without_token(self):
        preferences = Preferences(['oscar'], ['general'], engines, [])
        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)

    def test_query_private_engine_with_incorrect_token(self):
        preferences_with_tokens = Preferences(['oscar'], ['general'], engines, [])
        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)

    def test_query_private_engine_with_correct_token(self):
        preferences_with_tokens = Preferences(['oscar'], ['general'], engines, [])
        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)