on fce2d49 help sign in
searx/tests/unit/test_webapp.py
180 lines 6.6 kB history raw
1# -*- coding: utf-8 -*-
3import json
4from mock import Mock
5from searx import webapp
6from searx.testing import SearxTestCase
7from searx.search import Search
8from searx.url_utils import ParseResult
11class ViewsTestCase(SearxTestCase):
13 def setUp(self):
14 webapp.app.config['TESTING'] = True # to get better error messages
15 self.app = webapp.app.test_client()
17 # set some defaults
18 test_results = [
19 {
20 'content': 'first test content',
21 'title': 'First Test',
22 'url': 'http://first.test.xyz',
23 'engines': ['youtube', 'startpage'],
24 'engine': 'startpage',
25 'parsed_url': ParseResult(scheme='http', netloc='first.test.xyz', path='/', params='', query='', fragment=''), # noqa
26 }, {
27 'content': 'second test content',
28 'title': 'Second Test',
29 'url': 'http://second.test.xyz',
30 'engines': ['youtube', 'startpage'],
31 'engine': 'youtube',
32 'parsed_url': ParseResult(scheme='http', netloc='second.test.xyz', path='/', params='', query='', fragment=''), # noqa
33 },
34 ]
36 timings = [
37 {
38 'engine': 'startpage',
39 'total': 0.8,
40 'load': 0.7
41 },
42 {
43 'engine': 'youtube',
44 'total': 0.9,
45 'load': 0.6
46 }
47 ]
49 def search_mock(search_self, *args):
50 search_self.result_container = Mock(get_ordered_results=lambda: test_results,
51 answers=dict(),
52 corrections=set(),
53 suggestions=set(),
54 infoboxes=[],
55 unresponsive_engines=set(),
56 results=test_results,
57 results_number=lambda: 3,
58 results_length=lambda: len(test_results),
59 get_timings=lambda: timings)
61 self.setattr4test(Search, 'search', search_mock)
63 def get_current_theme_name_mock(override=None):
64 if override:
65 return override
66 return 'legacy'
68 self.setattr4test(webapp, 'get_current_theme_name', get_current_theme_name_mock)
70 self.maxDiff = None # to see full diffs
72 def test_index_empty(self):
73 result = self.app.post('/')
74 self.assertEqual(result.status_code, 200)
75 self.assertIn(b'<div class="title"><h1>searx</h1></div>', result.data)
77 def test_index_html(self):
78 result = self.app.post('/', data={'q': 'test'})
79 self.assertIn(
80 b'<h3 class="result_title"><img width="14" height="14" class="favicon" src="/static/themes/legacy/img/icons/icon_youtube.ico" alt="youtube" /><a href="http://second.test.xyz" rel="noreferrer">Second <span class="highlight">Test</span></a></h3>', # noqa
81 result.data
82 )
83 self.assertIn(
84 b'<p class="content">first <span class="highlight">test</span> content<br class="last"/></p>', # noqa
85 result.data
86 )
88 def test_index_json(self):
89 result = self.app.post('/', data={'q': 'test', 'format': 'json'})
91 result_dict = json.loads(result.data.decode('utf-8'))
93 self.assertEqual('test', result_dict['query'])
94 self.assertEqual(len(result_dict['results']), 2)
95 self.assertEqual(result_dict['results'][0]['content'], 'first test content')
96 self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
98 def test_index_csv(self):
99 result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
101 self.assertEqual(
102 b'title,url,content,host,engine,score,type\r\n'
103 b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,,result\r\n' # noqa
104 b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,,result\r\n', # noqa
105 result.data
106 )
108 def test_index_rss(self):
109 result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
111 self.assertIn(
112 b'<description>Search results for "test" - searx</description>',
113 result.data
114 )
116 self.assertIn(
117 b'<opensearch:totalResults>3</opensearch:totalResults>',
118 result.data
119 )
121 self.assertIn(
122 b'<title>First Test</title>',
123 result.data
124 )
126 self.assertIn(
127 b'<link>http://first.test.xyz</link>',
128 result.data
129 )
131 self.assertIn(
132 b'<description>first test content</description>',
133 result.data
134 )
136 def test_about(self):
137 result = self.app.get('/about')
138 self.assertEqual(result.status_code, 200)
139 self.assertIn(b'<h1>About <a href="/">searx</a></h1>', result.data)
141 def test_preferences(self):
142 result = self.app.get('/preferences')
143 self.assertEqual(result.status_code, 200)
144 self.assertIn(
145 b'<form method="post" action="/preferences" id="search_form">',
146 result.data
147 )
148 self.assertIn(
149 b'<legend>Default categories</legend>',
150 result.data
151 )
152 self.assertIn(
153 b'<legend>Interface language</legend>',
154 result.data
155 )
157 def test_stats(self):
158 result = self.app.get('/stats')
159 self.assertEqual(result.status_code, 200)
160 self.assertIn(b'<h2>Engine stats</h2>', result.data)
162 def test_robots_txt(self):
163 result = self.app.get('/robots.txt')
164 self.assertEqual(result.status_code, 200)
165 self.assertIn(b'Allow: /', result.data)
167 def test_opensearch_xml(self):
168 result = self.app.get('/opensearch.xml')
169 self.assertEqual(result.status_code, 200)
170 self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
172 def test_favicon(self):
173 result = self.app.get('/favicon.ico')
174 self.assertEqual(result.status_code, 200)
176 def test_config(self):
177 result = self.app.get('/config')
178 self.assertEqual(result.status_code, 200)
179 json_result = result.get_json()
180 self.assertTrue(json_result)