test_webapp.py 9.3 KB

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