test_webapp.py 5.5KB

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