123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from mock import Mock
  4. from searx import webapp
  5. from searx.testing import SearxTestCase
  6. from searx.search import Search
  7. from searx.url_utils import ParseResult
  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. self.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. def search_mock(search_self, *args):
  31. search_self.result_container = Mock(get_ordered_results=lambda: self.test_results,
  32. answers=set(),
  33. corrections=set(),
  34. suggestions=set(),
  35. infoboxes=[],
  36. results=self.test_results,
  37. results_number=lambda: 3,
  38. results_length=lambda: len(self.test_results))
  39. Search.search = search_mock
  40. def get_current_theme_name_mock(override=None):
  41. if override:
  42. return override
  43. return 'legacy'
  44. webapp.get_current_theme_name = get_current_theme_name_mock
  45. self.maxDiff = None # to see full diffs
  46. def test_index_empty(self):
  47. result = self.app.post('/')
  48. self.assertEqual(result.status_code, 200)
  49. self.assertIn(b'<div class="title"><h1>searx</h1></div>', result.data)
  50. def test_index_html(self):
  51. result = self.app.post('/', data={'q': 'test'})
  52. self.assertIn(
  53. 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
  54. result.data
  55. )
  56. self.assertIn(
  57. b'<p class="content">first <span class="highlight">test</span> content<br class="last"/></p>', # noqa
  58. result.data
  59. )
  60. def test_index_json(self):
  61. result = self.app.post('/', data={'q': 'test', 'format': 'json'})
  62. result_dict = json.loads(result.data.decode('utf-8'))
  63. self.assertEqual('test', result_dict['query'])
  64. self.assertEqual(result_dict['results'][0]['content'], 'first test content')
  65. self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
  66. def test_index_csv(self):
  67. result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
  68. self.assertEqual(
  69. b'title,url,content,host,engine,score\r\n'
  70. b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,\r\n' # noqa
  71. b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,\r\n', # noqa
  72. result.data
  73. )
  74. def test_index_rss(self):
  75. result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
  76. self.assertIn(
  77. b'<description>Search results for "test" - searx</description>',
  78. result.data
  79. )
  80. self.assertIn(
  81. b'<opensearch:totalResults>3</opensearch:totalResults>',
  82. result.data
  83. )
  84. self.assertIn(
  85. b'<title>First Test</title>',
  86. result.data
  87. )
  88. self.assertIn(
  89. b'<link>http://first.test.xyz</link>',
  90. result.data
  91. )
  92. self.assertIn(
  93. b'<description>first test content</description>',
  94. result.data
  95. )
  96. def test_about(self):
  97. result = self.app.get('/about')
  98. self.assertEqual(result.status_code, 200)
  99. self.assertIn(b'<h1>About <a href="/">searx</a></h1>', result.data)
  100. def test_preferences(self):
  101. result = self.app.get('/preferences')
  102. self.assertEqual(result.status_code, 200)
  103. self.assertIn(
  104. b'<form method="post" action="/preferences" id="search_form">',
  105. result.data
  106. )
  107. self.assertIn(
  108. b'<legend>Default categories</legend>',
  109. result.data
  110. )
  111. self.assertIn(
  112. b'<legend>Interface language</legend>',
  113. result.data
  114. )
  115. def test_stats(self):
  116. result = self.app.get('/stats')
  117. self.assertEqual(result.status_code, 200)
  118. self.assertIn(b'<h2>Engine stats</h2>', result.data)
  119. def test_robots_txt(self):
  120. result = self.app.get('/robots.txt')
  121. self.assertEqual(result.status_code, 200)
  122. self.assertIn(b'Allow: /', result.data)
  123. def test_opensearch_xml(self):
  124. result = self.app.get('/opensearch.xml')
  125. self.assertEqual(result.status_code, 200)
  126. self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
  127. def test_favicon(self):
  128. result = self.app.get('/favicon.ico')
  129. self.assertEqual(result.status_code, 200)