test_webapp.py 4.9KB

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