on 02bbbf5 help sign in
searx/tests/unit/test_webapp.py
179 lines 6.5 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 self.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: self.test_results,
51 answers=set(),
52 corrections=set(),
53 suggestions=set(),
54 infoboxes=[],
55 unresponsive_engines=set(),
56 results=self.test_results,
57 results_number=lambda: 3,
58 results_length=lambda: len(self.test_results),
59 get_timings=lambda: timings)
61 Search.search = search_mock
63 def get_current_theme_name_mock(override=None):
64 if override:
65 return override
66 return 'legacy'
68 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(result_dict['results'][0]['content'], 'first test content')
95 self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
97 def test_index_csv(self):
98 result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
100 self.assertEqual(
101 b'title,url,content,host,engine,score\r\n'
102 b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,\r\n' # noqa
103 b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,\r\n', # noqa
104 result.data
105 )
107 def test_index_rss(self):
108 result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
110 self.assertIn(
111 b'<description>Search results for "test" - searx</description>',
112 result.data
113 )
115 self.assertIn(
116 b'<opensearch:totalResults>3</opensearch:totalResults>',
117 result.data
118 )
120 self.assertIn(
121 b'<title>First Test</title>',
122 result.data
123 )
125 self.assertIn(
126 b'<link>http://first.test.xyz</link>',
127 result.data
128 )
130 self.assertIn(
131 b'<description>first test content</description>',
132 result.data
133 )
135 def test_about(self):
136 result = self.app.get('/about')
137 self.assertEqual(result.status_code, 200)
138 self.assertIn(b'<h1>About <a href="/">searx</a></h1>', result.data)
140 def test_preferences(self):
141 result = self.app.get('/preferences')
142 self.assertEqual(result.status_code, 200)
143 self.assertIn(
144 b'<form method="post" action="/preferences" id="search_form">',
145 result.data
146 )
147 self.assertIn(
148 b'<legend>Default categories</legend>',
149 result.data
150 )
151 self.assertIn(
152 b'<legend>Interface language</legend>',
153 result.data
154 )
156 def test_stats(self):
157 result = self.app.get('/stats')
158 self.assertEqual(result.status_code, 200)
159 self.assertIn(b'<h2>Engine stats</h2>', result.data)
161 def test_robots_txt(self):
162 result = self.app.get('/robots.txt')
163 self.assertEqual(result.status_code, 200)
164 self.assertIn(b'Allow: /', result.data)
166 def test_opensearch_xml(self):
167 result = self.app.get('/opensearch.xml')
168 self.assertEqual(result.status_code, 200)
169 self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
171 def test_favicon(self):
172 result = self.app.get('/favicon.ico')
173 self.assertEqual(result.status_code, 200)
175 def test_config(self):
176 result = self.app.get('/config')
177 self.assertEqual(result.status_code, 200)
178 json_result = result.get_json()
179 self.assertTrue(json_result)