test_webapp.py 5.5KB

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