logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://anongit.hacktivis.me/git/searx.git/

test_webapp.py (5941B)


  1. # -*- coding: utf-8 -*-
  2. import json
  3. from mock import Mock
  4. from searx import webapp
  5. from searx.testing import SearxTestCase
  6. from searx.search import Search
  7. from searx.url_utils import ParseResult
  8. class ViewsTestCase(SearxTestCase):
  9. def setUp(self):
  10. webapp.app.config['TESTING'] = True # to get better error messages
  11. self.app = webapp.app.test_client()
  12. # set some defaults
  13. self.test_results = [
  14. {
  15. 'content': 'first test content',
  16. 'title': 'First Test',
  17. 'url': 'http://first.test.xyz',
  18. 'engines': ['youtube', 'startpage'],
  19. 'engine': 'startpage',
  20. 'parsed_url': ParseResult(scheme='http', netloc='first.test.xyz', path='/', params='', query='', fragment=''), # noqa
  21. }, {
  22. 'content': 'second test content',
  23. 'title': 'Second Test',
  24. 'url': 'http://second.test.xyz',
  25. 'engines': ['youtube', 'startpage'],
  26. 'engine': 'youtube',
  27. 'parsed_url': ParseResult(scheme='http', netloc='second.test.xyz', path='/', params='', query='', fragment=''), # noqa
  28. },
  29. ]
  30. def search_mock(search_self, *args):
  31. search_self.result_container = Mock(get_ordered_results=lambda: self.test_results,
  32. answers=set(),
  33. corrections=set(),
  34. suggestions=set(),
  35. infoboxes=[],
  36. unresponsive_engines=set(),
  37. results=self.test_results,
  38. results_number=lambda: 3,
  39. results_length=lambda: len(self.test_results))
  40. Search.search = search_mock
  41. def get_current_theme_name_mock(override=None):
  42. if override:
  43. return override
  44. return 'legacy'
  45. webapp.get_current_theme_name = get_current_theme_name_mock
  46. self.maxDiff = None # to see full diffs
  47. def test_index_empty(self):
  48. result = self.app.post('/')
  49. self.assertEqual(result.status_code, 200)
  50. self.assertIn(b'<div class="title"><h1>searx</h1></div>', result.data)
  51. def test_index_html(self):
  52. result = self.app.post('/', data={'q': 'test'})
  53. self.assertIn(
  54. b'<h3 class="result_title"><img width="14" height="14" class="favicon" src="/static/themes/legacy/img/icons/icon_youtube.ico" alt="youtube" /><a href="http://second.test.xyz" rel="noreferrer">Second <span class="highlight">Test</span></a></h3>', # noqa
  55. result.data
  56. )
  57. self.assertIn(
  58. b'<p class="content">first <span class="highlight">test</span> content<br class="last"/></p>', # noqa
  59. result.data
  60. )
  61. def test_index_json(self):
  62. result = self.app.post('/', data={'q': 'test', 'format': 'json'})
  63. result_dict = json.loads(result.data.decode('utf-8'))
  64. self.assertEqual('test', result_dict['query'])
  65. self.assertEqual(result_dict['results'][0]['content'], 'first test content')
  66. self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
  67. def test_index_csv(self):
  68. result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
  69. self.assertEqual(
  70. b'title,url,content,host,engine,score\r\n'
  71. b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,\r\n' # noqa
  72. b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,\r\n', # noqa
  73. result.data
  74. )
  75. def test_index_rss(self):
  76. result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
  77. self.assertIn(
  78. b'<description>Search results for "test" - searx</description>',
  79. result.data
  80. )
  81. self.assertIn(
  82. b'<opensearch:totalResults>3</opensearch:totalResults>',
  83. result.data
  84. )
  85. self.assertIn(
  86. b'<title>First Test</title>',
  87. result.data
  88. )
  89. self.assertIn(
  90. b'<link>http://first.test.xyz</link>',
  91. result.data
  92. )
  93. self.assertIn(
  94. b'<description>first test content</description>',
  95. result.data
  96. )
  97. def test_about(self):
  98. result = self.app.get('/about')
  99. self.assertEqual(result.status_code, 200)
  100. self.assertIn(b'<h1>About <a href="/">searx</a></h1>', result.data)
  101. def test_preferences(self):
  102. result = self.app.get('/preferences')
  103. self.assertEqual(result.status_code, 200)
  104. self.assertIn(
  105. b'<form method="post" action="/preferences" id="search_form">',
  106. result.data
  107. )
  108. self.assertIn(
  109. b'<legend>Default categories</legend>',
  110. result.data
  111. )
  112. self.assertIn(
  113. b'<legend>Interface language</legend>',
  114. result.data
  115. )
  116. def test_stats(self):
  117. result = self.app.get('/stats')
  118. self.assertEqual(result.status_code, 200)
  119. self.assertIn(b'<h2>Engine stats</h2>', result.data)
  120. def test_robots_txt(self):
  121. result = self.app.get('/robots.txt')
  122. self.assertEqual(result.status_code, 200)
  123. self.assertIn(b'Allow: /', result.data)
  124. def test_opensearch_xml(self):
  125. result = self.app.get('/opensearch.xml')
  126. self.assertEqual(result.status_code, 200)
  127. self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
  128. def test_favicon(self):
  129. result = self.app.get('/favicon.ico')
  130. self.assertEqual(result.status_code, 200)