test_webapp.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from urllib.parse import ParseResult
  4. from mock import Mock
  5. import searx.search.processors
  6. from searx.search import Search
  7. from tests import SearxTestCase
  8. class ViewsTestCase(SearxTestCase):
  9. def setUp(self):
  10. # skip init function (no external HTTP request)
  11. def dummy(*args, **kwargs):
  12. pass
  13. self.setattr4test(searx.search.processors, 'initialize_processor', dummy)
  14. from searx import webapp # pylint disable=import-outside-toplevel
  15. webapp.app.config['TESTING'] = True # to get better error messages
  16. self.app = webapp.app.test_client()
  17. # set some defaults
  18. test_results = [
  19. {
  20. 'content': 'first test content',
  21. 'title': 'First Test',
  22. 'url': 'http://first.test.xyz',
  23. 'engines': ['youtube', 'startpage'],
  24. 'engine': 'startpage',
  25. 'parsed_url': ParseResult(scheme='http', netloc='first.test.xyz', path='/', params='', query='', fragment=''), # noqa
  26. }, {
  27. 'content': 'second test content',
  28. 'title': 'Second Test',
  29. 'url': 'http://second.test.xyz',
  30. 'engines': ['youtube', 'startpage'],
  31. 'engine': 'youtube',
  32. 'parsed_url': ParseResult(scheme='http', netloc='second.test.xyz', path='/', params='', query='', fragment=''), # noqa
  33. },
  34. ]
  35. timings = [
  36. {
  37. 'engine': 'startpage',
  38. 'total': 0.8,
  39. 'load': 0.7
  40. },
  41. {
  42. 'engine': 'youtube',
  43. 'total': 0.9,
  44. 'load': 0.6
  45. }
  46. ]
  47. def search_mock(search_self, *args):
  48. search_self.result_container = Mock(get_ordered_results=lambda: test_results,
  49. answers=dict(),
  50. corrections=set(),
  51. suggestions=set(),
  52. infoboxes=[],
  53. unresponsive_engines=set(),
  54. results=test_results,
  55. results_number=lambda: 3,
  56. results_length=lambda: len(test_results),
  57. get_timings=lambda: timings,
  58. redirect_url=None,
  59. engine_data={})
  60. self.setattr4test(Search, 'search', search_mock)
  61. def get_current_theme_name_mock(override=None):
  62. if override:
  63. return override
  64. return 'oscar'
  65. self.setattr4test(webapp, 'get_current_theme_name', get_current_theme_name_mock)
  66. self.maxDiff = None # to see full diffs
  67. def test_index_empty(self):
  68. result = self.app.post('/')
  69. self.assertEqual(result.status_code, 200)
  70. self.assertIn(b'<div class="text-hide center-block" id="main-logo">'
  71. + b'<img class="center-block img-responsive" src="/static/themes/oscar/img/logo_searx_a.png"'
  72. + b' alt="searx logo" />SearXNG</div>', result.data)
  73. def test_index_html_post(self):
  74. result = self.app.post('/', data={'q': 'test'})
  75. self.assertEqual(result.status_code, 308)
  76. self.assertEqual(result.location, 'http://localhost/search')
  77. def test_index_html_get(self):
  78. result = self.app.post('/?q=test')
  79. self.assertEqual(result.status_code, 308)
  80. self.assertEqual(result.location, 'http://localhost/search?q=test')
  81. def test_search_empty_html(self):
  82. result = self.app.post('/search', data={'q': ''})
  83. self.assertEqual(result.status_code, 200)
  84. self.assertIn(b'<span class="instance pull-left"><a href="/">SearXNG</a></span>', result.data)
  85. def test_search_empty_json(self):
  86. result = self.app.post('/search', data={'q': '', 'format': 'json'})
  87. self.assertEqual(result.status_code, 400)
  88. def test_search_empty_csv(self):
  89. result = self.app.post('/search', data={'q': '', 'format': 'csv'})
  90. self.assertEqual(result.status_code, 400)
  91. def test_search_empty_rss(self):
  92. result = self.app.post('/search', data={'q': '', 'format': 'rss'})
  93. self.assertEqual(result.status_code, 400)
  94. def test_search_html(self):
  95. result = self.app.post('/search', data={'q': 'test'})
  96. self.assertIn(
  97. b'<h4 class="result_header" id="result-2"><img width="32" height="32" class="favicon"'
  98. + b' src="/static/themes/oscar/img/icons/youtube.png" alt="youtube" /><a href="http://second.test.xyz"'
  99. + b' rel="noreferrer" aria-labelledby="result-2">Second <span class="highlight">Test</span></a></h4>', # noqa
  100. result.data
  101. )
  102. self.assertIn(
  103. b'<p class="result-content">second <span class="highlight">test</span> content</p>', # noqa
  104. result.data
  105. )
  106. def test_index_json(self):
  107. result = self.app.post('/', data={'q': 'test', 'format': 'json'})
  108. self.assertEqual(result.status_code, 308)
  109. def test_search_json(self):
  110. result = self.app.post('/search', data={'q': 'test', 'format': 'json'})
  111. result_dict = json.loads(result.data.decode())
  112. self.assertEqual('test', result_dict['query'])
  113. self.assertEqual(len(result_dict['results']), 2)
  114. self.assertEqual(result_dict['results'][0]['content'], 'first test content')
  115. self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
  116. def test_index_csv(self):
  117. result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
  118. self.assertEqual(result.status_code, 308)
  119. def test_search_csv(self):
  120. result = self.app.post('/search', data={'q': 'test', 'format': 'csv'})
  121. self.assertEqual(
  122. b'title,url,content,host,engine,score,type\r\n'
  123. b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,,result\r\n' # noqa
  124. b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,,result\r\n', # noqa
  125. result.data
  126. )
  127. def test_index_rss(self):
  128. result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
  129. self.assertEqual(result.status_code, 308)
  130. def test_search_rss(self):
  131. result = self.app.post('/search', data={'q': 'test', 'format': 'rss'})
  132. self.assertIn(
  133. b'<description>Search results for "test" - searx</description>',
  134. result.data
  135. )
  136. self.assertIn(
  137. b'<opensearch:totalResults>3</opensearch:totalResults>',
  138. result.data
  139. )
  140. self.assertIn(
  141. b'<title>First Test</title>',
  142. result.data
  143. )
  144. self.assertIn(
  145. b'<link>http://first.test.xyz</link>',
  146. result.data
  147. )
  148. self.assertIn(
  149. b'<description>first test content</description>',
  150. result.data
  151. )
  152. def test_about(self):
  153. result = self.app.get('/about')
  154. self.assertEqual(result.status_code, 200)
  155. self.assertIn(b'<h1>About <a href="/">searxng</a></h1>', result.data)
  156. def test_health(self):
  157. result = self.app.get('/healthz')
  158. self.assertEqual(result.status_code, 200)
  159. self.assertIn(b'OK', result.data)
  160. def test_preferences(self):
  161. result = self.app.get('/preferences')
  162. self.assertEqual(result.status_code, 200)
  163. self.assertIn(
  164. b'<form method="post" action="/preferences" id="search_form">',
  165. result.data
  166. )
  167. self.assertIn(
  168. b'<label class="col-sm-3 col-md-2" for="categories">Default categories</label>',
  169. result.data
  170. )
  171. self.assertIn(
  172. b'<label class="col-sm-3 col-md-2" for="locale">Interface language</label>',
  173. result.data
  174. )
  175. def test_browser_locale(self):
  176. result = self.app.get('/preferences', headers={'Accept-Language': 'zh-tw;q=0.8'})
  177. self.assertEqual(result.status_code, 200)
  178. self.assertIn(
  179. b'<option value="zh-Hant-TW" selected="selected">',
  180. result.data,
  181. 'Interface locale ignored browser preference.'
  182. )
  183. self.assertIn(
  184. b'<option value="zh-Hant-TW" selected="selected">',
  185. result.data,
  186. 'Search language ignored browser preference.'
  187. )
  188. def test_brower_empty_locale(self):
  189. result = self.app.get('/preferences', headers={'Accept-Language': ''})
  190. self.assertEqual(result.status_code, 200)
  191. self.assertIn(
  192. b'<option value="en" selected="selected">',
  193. result.data,
  194. 'Interface locale ignored browser preference.'
  195. )
  196. def test_locale_occitan(self):
  197. result = self.app.get('/preferences?locale=oc')
  198. self.assertEqual(result.status_code, 200)
  199. self.assertIn(
  200. b'<option value="oc" selected="selected">',
  201. result.data,
  202. 'Interface locale ignored browser preference.'
  203. )
  204. def test_stats(self):
  205. result = self.app.get('/stats')
  206. self.assertEqual(result.status_code, 200)
  207. self.assertIn(b'<h1>Engine stats</h1>', result.data)
  208. def test_robots_txt(self):
  209. result = self.app.get('/robots.txt')
  210. self.assertEqual(result.status_code, 200)
  211. self.assertIn(b'Allow: /', result.data)
  212. def test_opensearch_xml(self):
  213. result = self.app.get('/opensearch.xml')
  214. self.assertEqual(result.status_code, 200)
  215. self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
  216. def test_favicon(self):
  217. result = self.app.get('/favicon.ico')
  218. self.assertEqual(result.status_code, 200)
  219. def test_config(self):
  220. result = self.app.get('/config')
  221. self.assertEqual(result.status_code, 200)
  222. json_result = result.get_json()
  223. self.assertTrue(json_result)