test_webutils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. import mock
  4. from parameterized.parameterized import parameterized
  5. from searx import webutils
  6. from tests import SearxTestCase
  7. class TestWebUtils(SearxTestCase): # pylint: disable=missing-class-docstring
  8. @parameterized.expand(
  9. [
  10. ('https://searx.me/', 'https://searx.me/'),
  11. ('https://searx.me/ű', 'https://searx.me/ű'),
  12. ('https://searx.me/' + (100 * 'a'), 'https://searx.me/[...]aaaaaaaaaaaaaaaaa'),
  13. ('https://searx.me/' + (100 * 'ű'), 'https://searx.me/[...]űűűűűűűűűűűűűűűűű'),
  14. ]
  15. )
  16. def test_prettify_url(self, test_url: str, expected: str):
  17. self.assertEqual(webutils.prettify_url(test_url, max_length=32), expected)
  18. @parameterized.expand(
  19. [
  20. (0, None, None),
  21. (None, None, None),
  22. ('', None, None),
  23. (False, None, None),
  24. ]
  25. )
  26. def test_highlight_content_none(self, content, query, expected):
  27. self.assertEqual(webutils.highlight_content(content, query), expected)
  28. def test_highlight_content_same(self):
  29. content = '<html></html>not<'
  30. self.assertEqual(webutils.highlight_content(content, None), content)
  31. @parameterized.expand(
  32. [
  33. ('test', 'a', 'a'),
  34. ('a test', 'a', '<span class="highlight">a</span>'),
  35. ('" test "', 'a test string', 'a <span class="highlight">test</span> string'),
  36. ('"a"', 'this is a test string', 'this is <span class="highlight">a</span> test string'),
  37. (
  38. 'a test',
  39. 'this is a test string that matches entire query',
  40. 'this is <span class="highlight">a</span>'
  41. ' <span class="highlight">test</span>'
  42. ' string that matches entire query',
  43. ),
  44. (
  45. 'this a test',
  46. 'this is a string to test.',
  47. (
  48. '<span class="highlight">this</span>'
  49. ' is <span class="highlight">a</span>'
  50. ' string to <span class="highlight">test</span>.'
  51. ),
  52. ),
  53. (
  54. 'match this "exact phrase"',
  55. 'this string contains the exact phrase we want to match',
  56. ''.join(
  57. [
  58. '<span class="highlight">this</span> string contains the <span class="highlight">exact</span> ',
  59. '<span class="highlight">phrase</span> we want to <span class="highlight">match</span>',
  60. ]
  61. ),
  62. ),
  63. (
  64. 'a class',
  65. 'a string with class.',
  66. '<span class="highlight">a</span> string with <span class="highlight">class</span>.',
  67. ),
  68. ]
  69. )
  70. def test_highlight_content_equal(self, query: str, content: str, expected: str):
  71. self.assertEqual(webutils.highlight_content(content, query), expected)
  72. class TestUnicodeWriter(SearxTestCase): # pylint: disable=missing-class-docstring
  73. def setUp(self):
  74. self.unicode_writer = webutils.CSVWriter(mock.MagicMock())
  75. def test_write_row(self):
  76. row = [1, 2, 3]
  77. self.assertIsNone(self.unicode_writer.writerow(row))
  78. def test_write_rows(self):
  79. self.unicode_writer.writerow = mock.MagicMock()
  80. rows = [1, 2, 3]
  81. self.unicode_writer.writerows(rows)
  82. self.assertEqual(self.unicode_writer.writerow.call_count, len(rows))
  83. class TestNewHmac(SearxTestCase): # pylint: disable=missing-class-docstring
  84. @parameterized.expand(
  85. [
  86. b'secret',
  87. 1,
  88. ]
  89. )
  90. def test_attribute_error(self, secret_key):
  91. data = b'http://example.com'
  92. with self.assertRaises(AttributeError):
  93. webutils.new_hmac(secret_key, data)
  94. def test_bytes(self):
  95. data = b'http://example.com'
  96. res = webutils.new_hmac('secret', data)
  97. self.assertEqual(res, '23e2baa2404012a5cc8e4a18b4aabf0dde4cb9b56f679ddc0fd6d7c24339d819')