test_webapp.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name
  3. import json
  4. from urllib.parse import ParseResult
  5. import babel
  6. from mock import Mock
  7. import searx.webapp
  8. import searx.search
  9. import searx.search.processors
  10. from searx.results import Timing
  11. from searx.preferences import Preferences
  12. from tests import SearxTestCase
  13. class ViewsTestCase(SearxTestCase): # pylint: disable=too-many-public-methods
  14. def setUp(self):
  15. super().setUp()
  16. # skip init function (no external HTTP request)
  17. def dummy(*args, **kwargs): # pylint: disable=unused-argument
  18. pass
  19. self.setattr4test(searx.search.processors, 'initialize_processor', dummy)
  20. # remove sha for the static file so the tests don't have to care about
  21. # the changing URLs
  22. self.setattr4test(searx.webapp, 'static_files', {})
  23. # set some defaults
  24. test_results = [
  25. {
  26. 'content': 'first test content',
  27. 'title': 'First Test',
  28. 'url': 'http://first.test.xyz',
  29. 'engines': ['youtube', 'startpage'],
  30. 'engine': 'startpage',
  31. 'parsed_url': ParseResult(
  32. scheme='http', netloc='first.test.xyz', path='/', params='', query='', fragment=''
  33. ),
  34. 'template': 'default.html',
  35. },
  36. {
  37. 'content': 'second test content',
  38. 'title': 'Second Test',
  39. 'url': 'http://second.test.xyz',
  40. 'engines': ['youtube', 'startpage'],
  41. 'engine': 'youtube',
  42. 'parsed_url': ParseResult(
  43. scheme='http', netloc='second.test.xyz', path='/', params='', query='', fragment=''
  44. ),
  45. 'template': 'default.html',
  46. },
  47. ]
  48. timings = [
  49. Timing(engine='startpage', total=0.8, load=0.7),
  50. Timing(engine='youtube', total=0.9, load=0.6),
  51. ]
  52. def search_mock(search_self, *args): # pylint: disable=unused-argument
  53. search_self.result_container = Mock(
  54. get_ordered_results=lambda: test_results,
  55. answers={},
  56. corrections=set(),
  57. suggestions=set(),
  58. infoboxes=[],
  59. unresponsive_engines=set(),
  60. results=test_results,
  61. number_of_results=3,
  62. results_length=lambda: len(test_results),
  63. get_timings=lambda: timings,
  64. redirect_url=None,
  65. engine_data={},
  66. )
  67. search_self.search_query.locale = babel.Locale.parse("en-US", sep='-')
  68. self.setattr4test(searx.search.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.client.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.client.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.client.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.client.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.client.post('/search', data={'q': '', 'format': 'json'})
  98. self.assertEqual(result.status_code, 400)
  99. def test_search_empty_csv(self):
  100. result = self.client.post('/search', data={'q': '', 'format': 'csv'})
  101. self.assertEqual(result.status_code, 400)
  102. def test_search_empty_rss(self):
  103. result = self.client.post('/search', data={'q': '', 'format': 'rss'})
  104. self.assertEqual(result.status_code, 400)
  105. def test_search_html(self):
  106. result = self.client.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.client.post('/', data={'q': 'test', 'format': 'json'})
  117. self.assertEqual(result.status_code, 308)
  118. def test_search_json(self):
  119. result = self.client.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.client.post('/', data={'q': 'test', 'format': 'csv'})
  127. self.assertEqual(result.status_code, 308)
  128. def test_search_csv(self):
  129. result = self.client.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.client.post('/', data={'q': 'test', 'format': 'rss'})
  138. self.assertEqual(result.status_code, 308)
  139. def test_search_rss(self):
  140. result = self.client.post('/search', data={'q': 'test', 'format': 'rss'})
  141. self.assertIn(b'<description>Search results for "test" - SearXNG</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.client.get('/about')
  148. self.assertEqual(result.status_code, 302)
  149. def test_info_page(self):
  150. result = self.client.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.client.get('/healthz')
  155. self.assertEqual(result.status_code, 200)
  156. self.assertIn(b'OK', result.data)
  157. def test_preferences(self):
  158. result = self.client.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.client.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.client.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.client.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.client.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.client.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.client.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.client.get('/favicon.ico')
  204. result.close()
  205. self.assertEqual(result.status_code, 200)
  206. def test_config(self):
  207. result = self.client.get('/config')
  208. self.assertEqual(result.status_code, 200)
  209. json_result = result.get_json()
  210. self.assertTrue(json_result)