|
@@ -0,0 +1,167 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+from mock import patch
|
|
4
|
+from searx import webapp
|
|
5
|
+from searx.testing import SearxTestCase
|
|
6
|
+from urlparse import ParseResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+import json
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+class ViewsTestCase(SearxTestCase):
|
|
13
|
+
|
|
14
|
+ def setUp(self):
|
|
15
|
+ webapp.app.config['TESTING'] = True # to get better error messages
|
|
16
|
+ self.app = webapp.app.test_client()
|
|
17
|
+
|
|
18
|
+ # set some defaults
|
|
19
|
+ self.test_results = [
|
|
20
|
+ {
|
|
21
|
+ 'content': 'first test content',
|
|
22
|
+ 'title': 'First Test',
|
|
23
|
+ 'url': 'http://first.test.xyz',
|
|
24
|
+ 'engines': ['youtube', 'startpage'],
|
|
25
|
+ 'engine': 'startpage',
|
|
26
|
+ 'parsed_url': ParseResult(scheme='http', netloc='first.test.xyz', path='/', params='', query='', fragment=''), # noqa
|
|
27
|
+ }, {
|
|
28
|
+ 'content': 'second test content',
|
|
29
|
+ 'title': 'Second Test',
|
|
30
|
+ 'url': 'http://second.test.xyz',
|
|
31
|
+ 'engines': ['youtube', 'startpage'],
|
|
32
|
+ 'engine': 'youtube',
|
|
33
|
+ 'parsed_url': ParseResult(scheme='http', netloc='second.test.xyz', path='/', params='', query='', fragment=''), # noqa
|
|
34
|
+ },
|
|
35
|
+ ]
|
|
36
|
+
|
|
37
|
+ self.maxDiff = None # to see full diffs
|
|
38
|
+
|
|
39
|
+ def test_index_empty(self):
|
|
40
|
+ result = self.app.post('/')
|
|
41
|
+ self.assertEqual(result.status_code, 200)
|
|
42
|
+ self.assertIn('<div class="title"><h1>searx</h1></div>', result.data)
|
|
43
|
+
|
|
44
|
+ @patch('searx.webapp.search')
|
|
45
|
+ def test_index_html(self, search):
|
|
46
|
+ search.return_value = (
|
|
47
|
+ self.test_results,
|
|
48
|
+ set()
|
|
49
|
+ )
|
|
50
|
+ result = self.app.post('/', data={'q': 'test'})
|
|
51
|
+ self.assertIn(
|
|
52
|
+ '<h3 class="result_title"><a href="http://first.test.xyz">First <b>Test</b></a></h3>', # noqa
|
|
53
|
+ result.data
|
|
54
|
+ )
|
|
55
|
+ self.assertIn(
|
|
56
|
+ '<p class="content">first <b>test</b> content<br /></p>',
|
|
57
|
+ result.data
|
|
58
|
+ )
|
|
59
|
+
|
|
60
|
+ @patch('searx.webapp.search')
|
|
61
|
+ def test_index_json(self, search):
|
|
62
|
+ search.return_value = (
|
|
63
|
+ self.test_results,
|
|
64
|
+ set()
|
|
65
|
+ )
|
|
66
|
+ result = self.app.post('/', data={'q': 'test', 'format': 'json'})
|
|
67
|
+
|
|
68
|
+ result_dict = json.loads(result.data)
|
|
69
|
+
|
|
70
|
+ self.assertEqual('test', result_dict['query'])
|
|
71
|
+ self.assertEqual(
|
|
72
|
+ result_dict['results'][0]['content'], 'first test content')
|
|
73
|
+ self.assertEqual(
|
|
74
|
+ result_dict['results'][0]['url'], 'http://first.test.xyz')
|
|
75
|
+
|
|
76
|
+ @patch('searx.webapp.search')
|
|
77
|
+ def test_index_csv(self, search):
|
|
78
|
+ search.return_value = (
|
|
79
|
+ self.test_results,
|
|
80
|
+ set()
|
|
81
|
+ )
|
|
82
|
+ result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
|
|
83
|
+
|
|
84
|
+ self.assertEqual(
|
|
85
|
+ 'title,url,content,host,engine,score\r\n'
|
|
86
|
+ 'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,\r\n'
|
|
87
|
+ 'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,\r\n',
|
|
88
|
+ result.data
|
|
89
|
+ )
|
|
90
|
+
|
|
91
|
+ @patch('searx.webapp.search')
|
|
92
|
+ def test_index_rss(self, search):
|
|
93
|
+ search.return_value = (
|
|
94
|
+ self.test_results,
|
|
95
|
+ set()
|
|
96
|
+ )
|
|
97
|
+ result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
|
|
98
|
+
|
|
99
|
+ self.assertIn(
|
|
100
|
+ '<description>Search results for "test" - searx</description>',
|
|
101
|
+ result.data
|
|
102
|
+ )
|
|
103
|
+
|
|
104
|
+ self.assertIn(
|
|
105
|
+ '<opensearch:totalResults>2</opensearch:totalResults>',
|
|
106
|
+ result.data
|
|
107
|
+ )
|
|
108
|
+
|
|
109
|
+ self.assertIn(
|
|
110
|
+ '<title>First Test</title>',
|
|
111
|
+ result.data
|
|
112
|
+ )
|
|
113
|
+
|
|
114
|
+ self.assertIn(
|
|
115
|
+ '<link>http://first.test.xyz</link>',
|
|
116
|
+ result.data
|
|
117
|
+ )
|
|
118
|
+
|
|
119
|
+ self.assertIn(
|
|
120
|
+ '<description>first test content</description>',
|
|
121
|
+ result.data
|
|
122
|
+ )
|
|
123
|
+
|
|
124
|
+ def test_about(self):
|
|
125
|
+ result = self.app.get('/about')
|
|
126
|
+ self.assertEqual(result.status_code, 200)
|
|
127
|
+ self.assertIn('<h1>About <a href="/">searx</a></h1>', result.data)
|
|
128
|
+
|
|
129
|
+ def test_engines(self):
|
|
130
|
+ result = self.app.get('/engines')
|
|
131
|
+ self.assertEqual(result.status_code, 200)
|
|
132
|
+ self.assertIn('<h2>Currently used search engines</h2>', result.data)
|
|
133
|
+
|
|
134
|
+ def test_preferences(self):
|
|
135
|
+ result = self.app.get('/preferences')
|
|
136
|
+ self.assertEqual(result.status_code, 200)
|
|
137
|
+ self.assertIn(
|
|
138
|
+ '<form method="post" action="/preferences" id="search_form">',
|
|
139
|
+ result.data
|
|
140
|
+ )
|
|
141
|
+ self.assertIn(
|
|
142
|
+ '<legend>Default categories</legend>',
|
|
143
|
+ result.data
|
|
144
|
+ )
|
|
145
|
+ self.assertIn(
|
|
146
|
+ '<legend>Interface language</legend>',
|
|
147
|
+ result.data
|
|
148
|
+ )
|
|
149
|
+
|
|
150
|
+ def test_stats(self):
|
|
151
|
+ result = self.app.get('/stats')
|
|
152
|
+ self.assertEqual(result.status_code, 200)
|
|
153
|
+ self.assertIn('<h2>Engine stats</h2>', result.data)
|
|
154
|
+
|
|
155
|
+ def test_robots_txt(self):
|
|
156
|
+ result = self.app.get('/robots.txt')
|
|
157
|
+ self.assertEqual(result.status_code, 200)
|
|
158
|
+ self.assertIn('Allow: /', result.data)
|
|
159
|
+
|
|
160
|
+ def test_opensearch_xml(self):
|
|
161
|
+ result = self.app.get('/opensearch.xml')
|
|
162
|
+ self.assertEqual(result.status_code, 200)
|
|
163
|
+ self.assertIn('<Description>Search searx</Description>', result.data)
|
|
164
|
+
|
|
165
|
+ def test_favicon(self):
|
|
166
|
+ result = self.app.get('/favicon.ico')
|
|
167
|
+ self.assertEqual(result.status_code, 200)
|