test_webapp.py 9.4 KB

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