2024-03-11 14:06:26 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# pylint: disable=missing-module-docstring
|
|
|
|
|
2020-09-19 18:25:24 +02:00
|
|
|
import mock
|
2024-09-24 05:37:30 +02:00
|
|
|
from parameterized.parameterized import parameterized
|
2020-09-19 18:25:24 +02:00
|
|
|
from searx import webutils
|
2021-09-02 16:01:34 +02:00
|
|
|
from tests import SearxTestCase
|
2020-09-19 18:25:24 +02:00
|
|
|
|
|
|
|
|
2024-03-11 14:06:26 +01:00
|
|
|
class TestWebUtils(SearxTestCase): # pylint: disable=missing-class-docstring
|
2024-09-24 05:37:30 +02:00
|
|
|
|
|
|
|
@parameterized.expand(
|
|
|
|
[
|
2021-12-27 09:26:22 +01:00
|
|
|
('https://searx.me/', 'https://searx.me/'),
|
|
|
|
('https://searx.me/ű', 'https://searx.me/ű'),
|
|
|
|
('https://searx.me/' + (100 * 'a'), 'https://searx.me/[...]aaaaaaaaaaaaaaaaa'),
|
|
|
|
('https://searx.me/' + (100 * 'ű'), 'https://searx.me/[...]űűűűűűűűűűűűűűűűű'),
|
2024-09-24 05:37:30 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_prettify_url(self, test_url: str, expected: str):
|
|
|
|
self.assertEqual(webutils.prettify_url(test_url, max_length=32), expected)
|
|
|
|
|
|
|
|
@parameterized.expand(
|
|
|
|
[
|
|
|
|
(0, None, None),
|
|
|
|
(None, None, None),
|
|
|
|
('', None, None),
|
|
|
|
(False, None, None),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_highlight_content_none(self, content, query, expected):
|
|
|
|
self.assertEqual(webutils.highlight_content(content, query), expected)
|
|
|
|
|
|
|
|
def test_highlight_content_same(self):
|
|
|
|
content = '<html></html>not<'
|
|
|
|
self.assertEqual(webutils.highlight_content(content, None), content)
|
|
|
|
|
|
|
|
@parameterized.expand(
|
|
|
|
[
|
|
|
|
('test', 'a', 'a'),
|
|
|
|
('a test', 'a', '<span class="highlight">a</span>'),
|
2021-12-27 09:26:22 +01:00
|
|
|
('" test "', 'a test string', 'a <span class="highlight">test</span> string'),
|
2023-01-15 16:08:11 +01:00
|
|
|
('"a"', 'this is a test string', 'this is <span class="highlight">a</span> test string'),
|
2021-12-27 09:26:22 +01:00
|
|
|
(
|
|
|
|
'a test',
|
|
|
|
'this is a test string that matches entire query',
|
2024-09-24 05:37:30 +02:00
|
|
|
'this is <span class="highlight">a</span>'
|
|
|
|
' <span class="highlight">test</span>'
|
|
|
|
' string that matches entire query',
|
2021-12-27 09:26:22 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
'this a test',
|
|
|
|
'this is a string to test.',
|
|
|
|
(
|
2024-09-24 05:37:30 +02:00
|
|
|
'<span class="highlight">this</span>'
|
|
|
|
' is <span class="highlight">a</span>'
|
|
|
|
' string to <span class="highlight">test</span>.'
|
2021-12-27 09:26:22 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'match this "exact phrase"',
|
|
|
|
'this string contains the exact phrase we want to match',
|
2023-01-15 16:08:11 +01:00
|
|
|
''.join(
|
|
|
|
[
|
|
|
|
'<span class="highlight">this</span> string contains the <span class="highlight">exact</span> ',
|
|
|
|
'<span class="highlight">phrase</span> we want to <span class="highlight">match</span>',
|
|
|
|
]
|
2021-12-27 09:26:22 +01:00
|
|
|
),
|
|
|
|
),
|
2023-12-19 09:21:54 +01:00
|
|
|
(
|
|
|
|
'a class',
|
|
|
|
'a string with class.',
|
|
|
|
'<span class="highlight">a</span> string with <span class="highlight">class</span>.',
|
|
|
|
),
|
2024-09-24 05:37:30 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_highlight_content_equal(self, query: str, content: str, expected: str):
|
|
|
|
self.assertEqual(webutils.highlight_content(content, query), expected)
|
2021-02-09 05:58:54 +01:00
|
|
|
|
2020-09-19 18:25:24 +02:00
|
|
|
|
2024-03-11 14:06:26 +01:00
|
|
|
class TestUnicodeWriter(SearxTestCase): # pylint: disable=missing-class-docstring
|
2020-09-19 18:25:24 +02:00
|
|
|
def setUp(self):
|
2023-06-18 16:43:48 +02:00
|
|
|
self.unicode_writer = webutils.CSVWriter(mock.MagicMock())
|
2020-09-19 18:25:24 +02:00
|
|
|
|
|
|
|
def test_write_row(self):
|
|
|
|
row = [1, 2, 3]
|
2024-09-24 05:37:30 +02:00
|
|
|
self.assertIsNone(self.unicode_writer.writerow(row))
|
2020-09-19 18:25:24 +02:00
|
|
|
|
|
|
|
def test_write_rows(self):
|
|
|
|
self.unicode_writer.writerow = mock.MagicMock()
|
|
|
|
rows = [1, 2, 3]
|
|
|
|
self.unicode_writer.writerows(rows)
|
|
|
|
self.assertEqual(self.unicode_writer.writerow.call_count, len(rows))
|
|
|
|
|
|
|
|
|
2024-03-11 14:06:26 +01:00
|
|
|
class TestNewHmac(SearxTestCase): # pylint: disable=missing-class-docstring
|
2024-09-24 05:37:30 +02:00
|
|
|
@parameterized.expand(
|
|
|
|
[
|
|
|
|
b'secret',
|
|
|
|
1,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_attribute_error(self, secret_key):
|
2021-12-28 10:14:38 +01:00
|
|
|
data = b'http://example.com'
|
|
|
|
with self.assertRaises(AttributeError):
|
2024-09-24 05:37:30 +02:00
|
|
|
webutils.new_hmac(secret_key, data)
|
2021-12-28 10:14:38 +01:00
|
|
|
|
2024-09-24 05:37:30 +02:00
|
|
|
def test_bytes(self):
|
|
|
|
data = b'http://example.com'
|
2021-12-28 10:14:38 +01:00
|
|
|
res = webutils.new_hmac('secret', data)
|
|
|
|
self.assertEqual(res, '23e2baa2404012a5cc8e4a18b4aabf0dde4cb9b56f679ddc0fd6d7c24339d819')
|