test_webapp.py 9.5 KB

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