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