123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. class ViewsTestCase(SearxTestCase):
  8. def setUp(self):
  9. webapp.app.config['TESTING'] = True # to get better error messages
  10. self.app = webapp.app.test_client()
  11. # set some defaults
  12. self.test_results = [
  13. {
  14. 'content': 'first test content',
  15. 'title': 'First Test',
  16. 'url': 'http://first.test.xyz',
  17. 'engines': ['youtube', 'startpage'],
  18. 'engine': 'startpage',
  19. 'parsed_url': ParseResult(scheme='http', netloc='first.test.xyz', path='/', params='', query='', fragment=''), # noqa
  20. }, {
  21. 'content': 'second test content',
  22. 'title': 'Second Test',
  23. 'url': 'http://second.test.xyz',
  24. 'engines': ['youtube', 'startpage'],
  25. 'engine': 'youtube',
  26. 'parsed_url': ParseResult(scheme='http', netloc='second.test.xyz', path='/', params='', query='', fragment=''), # noqa
  27. },
  28. ]
  29. def search_mock(search_self, *args):
  30. search_self.result_container = Mock(get_ordered_results=lambda: self.test_results,
  31. answers=set(),
  32. suggestions=set(),
  33. infoboxes=[],
  34. results=self.test_results,
  35. results_number=lambda: 3,
  36. results_length=lambda: len(self.test_results))
  37. webapp.Search.search = search_mock
  38. def get_current_theme_name_mock(override=None):
  39. return 'default'
  40. webapp.get_current_theme_name = get_current_theme_name_mock
  41. self.maxDiff = None # to see full diffs
  42. def test_index_empty(self):
  43. result = self.app.post('/')
  44. self.assertEqual(result.status_code, 200)
  45. self.assertIn('<div class="title"><h1>searx</h1></div>', result.data)
  46. def test_index_html(self):
  47. result = self.app.post('/', data={'q': 'test'})
  48. self.assertIn(
  49. '<h3 class="result_title"><img width="14" height="14" class="favicon" src="/static/themes/default/img/icons/icon_youtube.ico" alt="youtube" /><a href="http://second.test.xyz" rel="noreferrer">Second <span class="highlight">Test</span></a></h3>', # noqa
  50. result.data
  51. )
  52. self.assertIn(
  53. '<p class="content">first <span class="highlight">test</span> content<br class="last"/></p>', # noqa
  54. result.data
  55. )
  56. def test_index_json(self):
  57. result = self.app.post('/', data={'q': 'test', 'format': 'json'})
  58. result_dict = json.loads(result.data)
  59. self.assertEqual('test', result_dict['query'])
  60. self.assertEqual(
  61. result_dict['results'][0]['content'], 'first test content')
  62. self.assertEqual(
  63. result_dict['results'][0]['url'], 'http://first.test.xyz')
  64. def test_index_csv(self):
  65. result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
  66. self.assertEqual(
  67. 'title,url,content,host,engine,score\r\n'
  68. 'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,\r\n' # noqa
  69. 'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,\r\n', # noqa
  70. result.data
  71. )
  72. def test_index_rss(self):
  73. result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
  74. self.assertIn(
  75. '<description>Search results for "test" - searx</description>',
  76. result.data
  77. )
  78. self.assertIn(
  79. '<opensearch:totalResults>3</opensearch:totalResults>',
  80. result.data
  81. )
  82. self.assertIn(
  83. '<title>First Test</title>',
  84. result.data
  85. )
  86. self.assertIn(
  87. '<link>http://first.test.xyz</link>',
  88. result.data
  89. )
  90. self.assertIn(
  91. '<description>first test content</description>',
  92. result.data
  93. )
  94. def test_about(self):
  95. result = self.app.get('/about')
  96. self.assertEqual(result.status_code, 200)
  97. self.assertIn('<h1>About <a href="/">searx</a></h1>', result.data)
  98. def test_preferences(self):
  99. result = self.app.get('/preferences')
  100. self.assertEqual(result.status_code, 200)
  101. self.assertIn(
  102. '<form method="post" action="/preferences" id="search_form">',
  103. result.data
  104. )
  105. self.assertIn(
  106. '<legend>Default categories</legend>',
  107. result.data
  108. )
  109. self.assertIn(
  110. '<legend>Interface language</legend>',
  111. result.data
  112. )
  113. def test_stats(self):
  114. result = self.app.get('/stats')
  115. self.assertEqual(result.status_code, 200)
  116. self.assertIn('<h2>Engine stats</h2>', result.data)
  117. def test_robots_txt(self):
  118. result = self.app.get('/robots.txt')
  119. self.assertEqual(result.status_code, 200)
  120. self.assertIn('Allow: /', result.data)
  121. def test_opensearch_xml(self):
  122. result = self.app.get('/opensearch.xml')
  123. self.assertEqual(result.status_code, 200)
  124. self.assertIn('<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
  125. def test_favicon(self):
  126. result = self.app.get('/favicon.ico')
  127. self.assertEqual(result.status_code, 200)