test_webapp.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from urllib.parse import ParseResult
  4. from mock import Mock
  5. from searx import webapp
  6. from searx.testing import SearxTestCase
  7. from searx.search import Search
  8. class ViewsTestCase(SearxTestCase):
  9. def setUp(self):
  10. webapp.app.config['TESTING'] = True # to get better error messages
  11. self.app = webapp.app.test_client()
  12. # set some defaults
  13. test_results = [
  14. {
  15. 'content': 'first test content',
  16. 'title': 'First Test',
  17. 'url': 'http://first.test.xyz',
  18. 'engines': ['youtube', 'startpage'],
  19. 'engine': 'startpage',
  20. 'parsed_url': ParseResult(scheme='http', netloc='first.test.xyz', path='/', params='', query='', fragment=''), # noqa
  21. }, {
  22. 'content': 'second test content',
  23. 'title': 'Second Test',
  24. 'url': 'http://second.test.xyz',
  25. 'engines': ['youtube', 'startpage'],
  26. 'engine': 'youtube',
  27. 'parsed_url': ParseResult(scheme='http', netloc='second.test.xyz', path='/', params='', query='', fragment=''), # noqa
  28. },
  29. ]
  30. timings = [
  31. {
  32. 'engine': 'startpage',
  33. 'total': 0.8,
  34. 'load': 0.7
  35. },
  36. {
  37. 'engine': 'youtube',
  38. 'total': 0.9,
  39. 'load': 0.6
  40. }
  41. ]
  42. def search_mock(search_self, *args):
  43. search_self.result_container = Mock(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. self.setattr4test(Search, 'search', search_mock)
  55. def get_current_theme_name_mock(override=None):
  56. if override:
  57. return override
  58. return 'legacy'
  59. self.setattr4test(webapp, 'get_current_theme_name', get_current_theme_name_mock)
  60. self.maxDiff = None # to see full diffs
  61. def test_index_empty(self):
  62. result = self.app.post('/')
  63. self.assertEqual(result.status_code, 200)
  64. self.assertIn(b'<div class="title"><h1>searx</h1></div>', result.data)
  65. def test_index_html(self):
  66. result = self.app.post('/', data={'q': 'test'})
  67. self.assertEqual(result.status_code, 308)
  68. def test_search_html(self):
  69. result = self.app.post('/search', data={'q': 'test'})
  70. self.assertIn(
  71. b'<h3 class="result_title"><img width="14" height="14" class="favicon" src="/static/themes/legacy/img/icons/icon_youtube.ico" alt="youtube" /><a href="http://second.test.xyz" rel="noreferrer">Second <span class="highlight">Test</span></a></h3>', # noqa
  72. result.data
  73. )
  74. self.assertIn(
  75. b'<p class="content">first <span class="highlight">test</span> content<br class="last"/></p>', # noqa
  76. result.data
  77. )
  78. def test_index_json(self):
  79. result = self.app.post('/', data={'q': 'test', 'format': 'json'})
  80. self.assertEqual(result.status_code, 308)
  81. def test_search_json(self):
  82. result = self.app.post('/search', data={'q': 'test', 'format': 'json'})
  83. result_dict = json.loads(result.data.decode())
  84. self.assertEqual('test', result_dict['query'])
  85. self.assertEqual(len(result_dict['results']), 2)
  86. self.assertEqual(result_dict['results'][0]['content'], 'first test content')
  87. self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
  88. def test_index_csv(self):
  89. result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
  90. self.assertEqual(result.status_code, 308)
  91. def test_search_csv(self):
  92. result = self.app.post('/search', data={'q': 'test', 'format': 'csv'})
  93. self.assertEqual(
  94. b'title,url,content,host,engine,score,type\r\n'
  95. b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,,result\r\n' # noqa
  96. b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,,result\r\n', # noqa
  97. result.data
  98. )
  99. def test_index_rss(self):
  100. result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
  101. self.assertEqual(result.status_code, 308)
  102. def test_index_rss(self):
  103. result = self.app.post('/search', data={'q': 'test', 'format': 'rss'})
  104. self.assertIn(
  105. b'<description>Search results for "test" - searx</description>',
  106. result.data
  107. )
  108. self.assertIn(
  109. b'<opensearch:totalResults>3</opensearch:totalResults>',
  110. result.data
  111. )
  112. self.assertIn(
  113. b'<title>First Test</title>',
  114. result.data
  115. )
  116. self.assertIn(
  117. b'<link>http://first.test.xyz</link>',
  118. result.data
  119. )
  120. self.assertIn(
  121. b'<description>first test content</description>',
  122. result.data
  123. )
  124. def test_about(self):
  125. result = self.app.get('/about')
  126. self.assertEqual(result.status_code, 200)
  127. self.assertIn(b'<h1>About <a href="/">searx</a></h1>', result.data)
  128. def test_preferences(self):
  129. result = self.app.get('/preferences')
  130. self.assertEqual(result.status_code, 200)
  131. self.assertIn(
  132. b'<form method="post" action="/preferences" id="search_form">',
  133. result.data
  134. )
  135. self.assertIn(
  136. b'<legend>Default categories</legend>',
  137. result.data
  138. )
  139. self.assertIn(
  140. b'<legend>Interface language</legend>',
  141. result.data
  142. )
  143. def test_stats(self):
  144. result = self.app.get('/stats')
  145. self.assertEqual(result.status_code, 200)
  146. self.assertIn(b'<h2>Engine stats</h2>', result.data)
  147. def test_robots_txt(self):
  148. result = self.app.get('/robots.txt')
  149. self.assertEqual(result.status_code, 200)
  150. self.assertIn(b'Allow: /', result.data)
  151. def test_opensearch_xml(self):
  152. result = self.app.get('/opensearch.xml')
  153. self.assertEqual(result.status_code, 200)
  154. self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
  155. def test_favicon(self):
  156. result = self.app.get('/favicon.ico')
  157. self.assertEqual(result.status_code, 200)
  158. def test_config(self):
  159. result = self.app.get('/config')
  160. self.assertEqual(result.status_code, 200)
  161. json_result = result.get_json()
  162. self.assertTrue(json_result)