test_webapp.py 9.5 KB

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