test_webapp.py 5.5 KB

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