on 5ceaf21 help sign in
searx/tests/unit/test_webapp.py
159 lines 5.9 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 def search_mock(search_self, *args):
37 search_self.result_container = Mock(get_ordered_results=lambda: self.test_results,
38 answers=set(),
39 corrections=set(),
40 suggestions=set(),
41 infoboxes=[],
42 unresponsive_engines=set(),
43 results=self.test_results,
44 results_number=lambda: 3,
45 results_length=lambda: len(self.test_results))
47 Search.search = search_mock
49 def get_current_theme_name_mock(override=None):
50 if override:
51 return override
52 return 'legacy'
54 webapp.get_current_theme_name = get_current_theme_name_mock
56 self.maxDiff = None # to see full diffs
58 def test_index_empty(self):
59 result = self.app.post('/')
60 self.assertEqual(result.status_code, 200)
61 self.assertIn(b'<div class="title"><h1>searx</h1></div>', result.data)
63 def test_index_html(self):
64 result = self.app.post('/', data={'q': 'test'})
65 self.assertIn(
66 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
67 result.data
68 )
69 self.assertIn(
70 b'<p class="content">first <span class="highlight">test</span> content<br class="last"/></p>', # noqa
71 result.data
72 )
74 def test_index_json(self):
75 result = self.app.post('/', data={'q': 'test', 'format': 'json'})
77 result_dict = json.loads(result.data.decode('utf-8'))
79 self.assertEqual('test', result_dict['query'])
80 self.assertEqual(result_dict['results'][0]['content'], 'first test content')
81 self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
83 def test_index_csv(self):
84 result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
86 self.assertEqual(
87 b'title,url,content,host,engine,score\r\n'
88 b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,\r\n' # noqa
89 b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,\r\n', # noqa
90 result.data
91 )
93 def test_index_rss(self):
94 result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
96 self.assertIn(
97 b'<description>Search results for "test" - searx</description>',
98 result.data
99 )
101 self.assertIn(
102 b'<opensearch:totalResults>3</opensearch:totalResults>',
103 result.data
104 )
106 self.assertIn(
107 b'<title>First Test</title>',
108 result.data
109 )
111 self.assertIn(
112 b'<link>http://first.test.xyz</link>',
113 result.data
114 )
116 self.assertIn(
117 b'<description>first test content</description>',
118 result.data
119 )
121 def test_about(self):
122 result = self.app.get('/about')
123 self.assertEqual(result.status_code, 200)
124 self.assertIn(b'<h1>About <a href="/">searx</a></h1>', result.data)
126 def test_preferences(self):
127 result = self.app.get('/preferences')
128 self.assertEqual(result.status_code, 200)
129 self.assertIn(
130 b'<form method="post" action="/preferences" id="search_form">',
131 result.data
132 )
133 self.assertIn(
134 b'<legend>Default categories</legend>',
135 result.data
136 )
137 self.assertIn(
138 b'<legend>Interface language</legend>',
139 result.data
140 )
142 def test_stats(self):
143 result = self.app.get('/stats')
144 self.assertEqual(result.status_code, 200)
145 self.assertIn(b'<h2>Engine stats</h2>', result.data)
147 def test_robots_txt(self):
148 result = self.app.get('/robots.txt')
149 self.assertEqual(result.status_code, 200)
150 self.assertIn(b'Allow: /', result.data)
152 def test_opensearch_xml(self):
153 result = self.app.get('/opensearch.xml')
154 self.assertEqual(result.status_code, 200)
155 self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
157 def test_favicon(self):
158 result = self.app.get('/favicon.ico')
159 self.assertEqual(result.status_code, 200)