on eecfff2 help sign in
searx/tests/unit/test_webapp.py
181 lines 6.7 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)
62 self.setattr4test(Search, 'search', search_mock)
64 def get_current_theme_name_mock(override=None):
65 if override:
66 return override
67 return 'legacy'
69 self.setattr4test(webapp, 'get_current_theme_name', get_current_theme_name_mock)
71 self.maxDiff = None # to see full diffs
73 def test_index_empty(self):
74 result = self.app.post('/')
75 self.assertEqual(result.status_code, 200)
76 self.assertIn(b'<div class="title"><h1>searx</h1></div>', result.data)
78 def test_index_html(self):
79 result = self.app.post('/', data={'q': 'test'})
80 self.assertIn(
81 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
82 result.data
83 )
84 self.assertIn(
85 b'<p class="content">first <span class="highlight">test</span> content<br class="last"/></p>', # noqa
86 result.data
87 )
89 def test_index_json(self):
90 result = self.app.post('/', data={'q': 'test', 'format': 'json'})
92 result_dict = json.loads(result.data.decode())
94 self.assertEqual('test', result_dict['query'])
95 self.assertEqual(len(result_dict['results']), 2)
96 self.assertEqual(result_dict['results'][0]['content'], 'first test content')
97 self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
99 def test_index_csv(self):
100 result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
102 self.assertEqual(
103 b'title,url,content,host,engine,score,type\r\n'
104 b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,,result\r\n' # noqa
105 b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,,result\r\n', # noqa
106 result.data
107 )
109 def test_index_rss(self):
110 result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
112 self.assertIn(
113 b'<description>Search results for "test" - searx</description>',
114 result.data
115 )
117 self.assertIn(
118 b'<opensearch:totalResults>3</opensearch:totalResults>',
119 result.data
120 )
122 self.assertIn(
123 b'<title>First Test</title>',
124 result.data
125 )
127 self.assertIn(
128 b'<link>http://first.test.xyz</link>',
129 result.data
130 )
132 self.assertIn(
133 b'<description>first test content</description>',
134 result.data
135 )
137 def test_about(self):
138 result = self.app.get('/about')
139 self.assertEqual(result.status_code, 200)
140 self.assertIn(b'<h1>About <a href="/">searx</a></h1>', result.data)
142 def test_preferences(self):
143 result = self.app.get('/preferences')
144 self.assertEqual(result.status_code, 200)
145 self.assertIn(
146 b'<form method="post" action="/preferences" id="search_form">',
147 result.data
148 )
149 self.assertIn(
150 b'<legend>Default categories</legend>',
151 result.data
152 )
153 self.assertIn(
154 b'<legend>Interface language</legend>',
155 result.data
156 )
158 def test_stats(self):
159 result = self.app.get('/stats')
160 self.assertEqual(result.status_code, 200)
161 self.assertIn(b'<h2>Engine stats</h2>', result.data)
163 def test_robots_txt(self):
164 result = self.app.get('/robots.txt')
165 self.assertEqual(result.status_code, 200)
166 self.assertIn(b'Allow: /', result.data)
168 def test_opensearch_xml(self):
169 result = self.app.get('/opensearch.xml')
170 self.assertEqual(result.status_code, 200)
171 self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
173 def test_favicon(self):
174 result = self.app.get('/favicon.ico')
175 self.assertEqual(result.status_code, 200)
177 def test_config(self):
178 result = self.app.get('/config')
179 self.assertEqual(result.status_code, 200)
180 json_result = result.get_json()
181 self.assertTrue(json_result)