on engine-data help sign in
searx/tests/unit/test_webapp.py
239 lines 9.2 kB history raw
1# -*- coding: utf-8 -*-
3import json
4from urllib.parse import ParseResult
5from mock import Mock
6from searx import webapp
7from searx.testing import SearxTestCase
8from searx.search import Search
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,
60 redirect_url=None,
61 engine_data={})
63 self.setattr4test(Search, 'search', search_mock)
65 def get_current_theme_name_mock(override=None):
66 if override:
67 return override
68 return 'oscar'
70 self.setattr4test(webapp, 'get_current_theme_name', get_current_theme_name_mock)
72 self.maxDiff = None # to see full diffs
74 def test_index_empty(self):
75 result = self.app.post('/')
76 self.assertEqual(result.status_code, 200)
77 self.assertIn(b'<div class="text-hide center-block" id="main-logo">'
78 + b'<img class="center-block img-responsive" src="/static/themes/oscar/img/logo_searx_a.png"'
79 + b' alt="searx logo" />searx</div>', result.data)
81 def test_index_html_post(self):
82 result = self.app.post('/', data={'q': 'test'})
83 self.assertEqual(result.status_code, 308)
84 self.assertEqual(result.location, 'http://localhost/search')
86 def test_index_html_get(self):
87 result = self.app.post('/?q=test')
88 self.assertEqual(result.status_code, 308)
89 self.assertEqual(result.location, 'http://localhost/search?q=test')
91 def test_search_empty_html(self):
92 result = self.app.post('/search', data={'q': ''})
93 self.assertEqual(result.status_code, 200)
94 self.assertIn(b'<span class="instance pull-left"><a href="/">searx</a></span>', result.data)
96 def test_search_empty_json(self):
97 result = self.app.post('/search', data={'q': '', 'format': 'json'})
98 self.assertEqual(result.status_code, 400)
100 def test_search_empty_csv(self):
101 result = self.app.post('/search', data={'q': '', 'format': 'csv'})
102 self.assertEqual(result.status_code, 400)
104 def test_search_empty_rss(self):
105 result = self.app.post('/search', data={'q': '', 'format': 'rss'})
106 self.assertEqual(result.status_code, 400)
108 def test_search_html(self):
109 result = self.app.post('/search', data={'q': 'test'})
111 self.assertIn(
112 b'<h4 class="result_header" id="result-2"><img width="32" height="32" class="favicon"'
113 + b' src="/static/themes/oscar/img/icons/youtube.png" alt="youtube" /><a href="http://second.test.xyz"'
114 + b' rel="noreferrer" aria-labelledby="result-2">Second <span class="highlight">Test</span></a></h4>', # noqa
115 result.data
116 )
117 self.assertIn(
118 b'<p class="result-content">second <span class="highlight">test</span> content</p>', # noqa
119 result.data
120 )
122 def test_index_json(self):
123 result = self.app.post('/', data={'q': 'test', 'format': 'json'})
124 self.assertEqual(result.status_code, 308)
126 def test_search_json(self):
127 result = self.app.post('/search', data={'q': 'test', 'format': 'json'})
128 result_dict = json.loads(result.data.decode())
130 self.assertEqual('test', result_dict['query'])
131 self.assertEqual(len(result_dict['results']), 2)
132 self.assertEqual(result_dict['results'][0]['content'], 'first test content')
133 self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
135 def test_index_csv(self):
136 result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
137 self.assertEqual(result.status_code, 308)
139 def test_search_csv(self):
140 result = self.app.post('/search', data={'q': 'test', 'format': 'csv'})
142 self.assertEqual(
143 b'title,url,content,host,engine,score,type\r\n'
144 b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,,result\r\n' # noqa
145 b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,,result\r\n', # noqa
146 result.data
147 )
149 def test_index_rss(self):
150 result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
151 self.assertEqual(result.status_code, 308)
153 def test_search_rss(self):
154 result = self.app.post('/search', data={'q': 'test', 'format': 'rss'})
156 self.assertIn(
157 b'<description>Search results for "test" - searx</description>',
158 result.data
159 )
161 self.assertIn(
162 b'<opensearch:totalResults>3</opensearch:totalResults>',
163 result.data
164 )
166 self.assertIn(
167 b'<title>First Test</title>',
168 result.data
169 )
171 self.assertIn(
172 b'<link>http://first.test.xyz</link>',
173 result.data
174 )
176 self.assertIn(
177 b'<description>first test content</description>',
178 result.data
179 )
181 def test_about(self):
182 result = self.app.get('/about')
183 self.assertEqual(result.status_code, 200)
184 self.assertIn(b'<h1>About <a href="/">searx</a></h1>', result.data)
186 def test_preferences(self):
187 result = self.app.get('/preferences')
188 self.assertEqual(result.status_code, 200)
189 self.assertIn(
190 b'<form method="post" action="/preferences" id="search_form">',
191 result.data
192 )
193 self.assertIn(
194 b'<label class="col-sm-3 col-md-2" for="categories">Default categories</label>',
195 result.data
196 )
197 self.assertIn(
198 b'<label class="col-sm-3 col-md-2" for="locale">Interface language</label>',
199 result.data
200 )
202 def test_browser_locale(self):
203 result = self.app.get('/preferences', headers={'Accept-Language': 'zh-tw;q=0.8'})
204 self.assertEqual(result.status_code, 200)
205 self.assertIn(
206 b'<option value="zh_TW" selected="selected">',
207 result.data,
208 'Interface locale ignored browser preference.'
209 )
210 self.assertIn(
211 b'<option value="zh-TW" selected="selected">',
212 result.data,
213 'Search language ignored browser preference.'
214 )
216 def test_stats(self):
217 result = self.app.get('/stats')
218 self.assertEqual(result.status_code, 200)
219 self.assertIn(b'<h1>Engine stats</h1>', result.data)
221 def test_robots_txt(self):
222 result = self.app.get('/robots.txt')
223 self.assertEqual(result.status_code, 200)
224 self.assertIn(b'Allow: /', result.data)
226 def test_opensearch_xml(self):
227 result = self.app.get('/opensearch.xml')
228 self.assertEqual(result.status_code, 200)
229 self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
231 def test_favicon(self):
232 result = self.app.get('/favicon.ico')
233 self.assertEqual(result.status_code, 200)
235 def test_config(self):
236 result = self.app.get('/config')
237 self.assertEqual(result.status_code, 200)
238 json_result = result.get_json()
239 self.assertTrue(json_result)