test_webapp.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. import json
  4. from urllib.parse import ParseResult
  5. from mock import Mock
  6. from searx.results import Timing
  7. import searx.search.processors
  8. from searx.search import Search
  9. from searx.preferences import Preferences
  10. from tests import SearxTestCase
  11. class ViewsTestCase(SearxTestCase): # pylint: disable=missing-class-docstring, too-many-public-methods
  12. def setUp(self):
  13. # skip init function (no external HTTP request)
  14. def dummy(*args, **kwargs): # pylint: disable=unused-argument
  15. pass
  16. self.setattr4test(searx.search.processors, 'initialize_processor', dummy)
  17. from searx import webapp # pylint: disable=import-outside-toplevel
  18. webapp.app.config['TESTING'] = True # to get better error messages
  19. self.app = webapp.app.test_client()
  20. # remove sha for the static file
  21. # so the tests don't have to care about the changing URLs
  22. for k in webapp.static_files:
  23. webapp.static_files[k] = None
  24. # set some defaults
  25. test_results = [
  26. {
  27. 'content': 'first test content',
  28. 'title': 'First Test',
  29. 'url': 'http://first.test.xyz',
  30. 'engines': ['youtube', 'startpage'],
  31. 'engine': 'startpage',
  32. 'parsed_url': ParseResult(
  33. scheme='http', netloc='first.test.xyz', path='/', params='', query='', fragment=''
  34. ),
  35. 'template': 'default.html',
  36. },
  37. {
  38. 'content': 'second test content',
  39. 'title': 'Second Test',
  40. 'url': 'http://second.test.xyz',
  41. 'engines': ['youtube', 'startpage'],
  42. 'engine': 'youtube',
  43. 'parsed_url': ParseResult(
  44. scheme='http', netloc='second.test.xyz', path='/', params='', query='', fragment=''
  45. ),
  46. 'template': 'default.html',
  47. },
  48. ]
  49. timings = [
  50. Timing(engine='startpage', total=0.8, load=0.7),
  51. Timing(engine='youtube', total=0.9, load=0.6),
  52. ]
  53. def search_mock(search_self, *args): # pylint: disable=unused-argument
  54. search_self.result_container = Mock(
  55. get_ordered_results=lambda: test_results,
  56. answers={},
  57. corrections=set(),
  58. suggestions=set(),
  59. infoboxes=[],
  60. unresponsive_engines=set(),
  61. results=test_results,
  62. number_of_results=3,
  63. results_length=lambda: len(test_results),
  64. get_timings=lambda: timings,
  65. redirect_url=None,
  66. engine_data={},
  67. )
  68. self.setattr4test(Search, 'search', search_mock)
  69. original_preferences_get_value = Preferences.get_value
  70. def preferences_get_value(preferences_self, user_setting_name: str):
  71. if user_setting_name == 'theme':
  72. return 'simple'
  73. return original_preferences_get_value(preferences_self, user_setting_name)
  74. self.setattr4test(Preferences, 'get_value', preferences_get_value)
  75. # to see full diffs
  76. self.maxDiff = None # pylint: disable=invalid-name
  77. def test_index_empty(self):
  78. result = self.app.post('/')
  79. self.assertEqual(result.status_code, 200)
  80. self.assertIn(
  81. b'<div class="title"><h1>SearXNG</h1></div>',
  82. result.data,
  83. )
  84. def test_index_html_post(self):
  85. result = self.app.post('/', data={'q': 'test'})
  86. self.assertEqual(result.status_code, 308)
  87. self.assertEqual(result.location, '/search')
  88. def test_index_html_get(self):
  89. result = self.app.post('/?q=test')
  90. self.assertEqual(result.status_code, 308)
  91. self.assertEqual(result.location, '/search?q=test')
  92. def test_search_empty_html(self):
  93. result = self.app.post('/search', data={'q': ''})
  94. self.assertEqual(result.status_code, 200)
  95. self.assertIn(b'<div class="title"><h1>SearXNG</h1></div>', result.data)
  96. def test_search_empty_json(self):
  97. result = self.app.post('/search', data={'q': '', 'format': 'json'})
  98. self.assertEqual(result.status_code, 400)
  99. def test_search_empty_csv(self):
  100. result = self.app.post('/search', data={'q': '', 'format': 'csv'})
  101. self.assertEqual(result.status_code, 400)
  102. def test_search_empty_rss(self):
  103. result = self.app.post('/search', data={'q': '', 'format': 'rss'})
  104. self.assertEqual(result.status_code, 400)
  105. def test_search_html(self):
  106. result = self.app.post('/search', data={'q': 'test'})
  107. self.assertIn(
  108. b'<span class="url_o1"><span class="url_i1">http://second.test.xyz</span></span>',
  109. result.data,
  110. )
  111. self.assertIn(
  112. b'<p class="content">\n second <span class="highlight">test</span> ',
  113. result.data,
  114. )
  115. def test_index_json(self):
  116. result = self.app.post('/', data={'q': 'test', 'format': 'json'})
  117. self.assertEqual(result.status_code, 308)
  118. def test_search_json(self):
  119. result = self.app.post('/search', data={'q': 'test', 'format': 'json'})
  120. result_dict = json.loads(result.data.decode())
  121. self.assertEqual('test', result_dict['query'])
  122. self.assertEqual(len(result_dict['results']), 2)
  123. self.assertEqual(result_dict['results'][0]['content'], 'first test content')
  124. self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
  125. def test_index_csv(self):
  126. result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
  127. self.assertEqual(result.status_code, 308)
  128. def test_search_csv(self):
  129. result = self.app.post('/search', data={'q': 'test', 'format': 'csv'})
  130. self.assertEqual(
  131. b'title,url,content,host,engine,score,type\r\n'
  132. b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,,result\r\n' # noqa
  133. b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,,result\r\n', # noqa
  134. result.data,
  135. )
  136. def test_index_rss(self):
  137. result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
  138. self.assertEqual(result.status_code, 308)
  139. def test_search_rss(self):
  140. result = self.app.post('/search', data={'q': 'test', 'format': 'rss'})
  141. self.assertIn(b'<description>Search results for "test" - searx</description>', result.data)
  142. self.assertIn(b'<opensearch:totalResults>3</opensearch:totalResults>', result.data)
  143. self.assertIn(b'<title>First Test</title>', result.data)
  144. self.assertIn(b'<link>http://first.test.xyz</link>', result.data)
  145. self.assertIn(b'<description>first test content</description>', result.data)
  146. def test_redirect_about(self):
  147. result = self.app.get('/about')
  148. self.assertEqual(result.status_code, 302)
  149. def test_info_page(self):
  150. result = self.app.get('/info/en/search-syntax')
  151. self.assertEqual(result.status_code, 200)
  152. self.assertIn(b'<h1>Search syntax</h1>', result.data)
  153. def test_health(self):
  154. result = self.app.get('/healthz')
  155. self.assertEqual(result.status_code, 200)
  156. self.assertIn(b'OK', result.data)
  157. def test_preferences(self):
  158. result = self.app.get('/preferences')
  159. self.assertEqual(result.status_code, 200)
  160. self.assertIn(b'<form id="search_form" method="post" action="/preferences"', result.data)
  161. self.assertIn(b'<div id="categories_container">', result.data)
  162. self.assertIn(b'<legend id="pref_ui_locale">Interface language</legend>', result.data)
  163. def test_browser_locale(self):
  164. result = self.app.get('/preferences', headers={'Accept-Language': 'zh-tw;q=0.8'})
  165. self.assertEqual(result.status_code, 200)
  166. self.assertIn(
  167. b'<option value="zh-Hant-TW" selected="selected">',
  168. result.data,
  169. 'Interface locale ignored browser preference.',
  170. )
  171. self.assertIn(
  172. b'<option value="zh-Hant-TW" selected="selected">',
  173. result.data,
  174. 'Search language ignored browser preference.',
  175. )
  176. def test_browser_empty_locale(self):
  177. result = self.app.get('/preferences', headers={'Accept-Language': ''})
  178. self.assertEqual(result.status_code, 200)
  179. self.assertIn(
  180. b'<option value="en" selected="selected">', result.data, 'Interface locale ignored browser preference.'
  181. )
  182. def test_locale_occitan(self):
  183. result = self.app.get('/preferences?locale=oc')
  184. self.assertEqual(result.status_code, 200)
  185. self.assertIn(
  186. b'<option value="oc" selected="selected">', result.data, 'Interface locale ignored browser preference.'
  187. )
  188. def test_stats(self):
  189. result = self.app.get('/stats')
  190. self.assertEqual(result.status_code, 200)
  191. self.assertIn(b'<h1>Engine stats</h1>', result.data)
  192. def test_robots_txt(self):
  193. result = self.app.get('/robots.txt')
  194. self.assertEqual(result.status_code, 200)
  195. self.assertIn(b'Allow: /', result.data)
  196. def test_opensearch_xml(self):
  197. result = self.app.get('/opensearch.xml')
  198. self.assertEqual(result.status_code, 200)
  199. self.assertIn(
  200. b'<Description>SearXNG is a metasearch engine that respects your privacy.</Description>', result.data
  201. )
  202. def test_favicon(self):
  203. result = self.app.get('/favicon.ico')
  204. self.assertEqual(result.status_code, 200)
  205. def test_config(self):
  206. result = self.app.get('/config')
  207. self.assertEqual(result.status_code, 200)
  208. json_result = result.get_json()
  209. self.assertTrue(json_result)