on ec4e48e help sign in
searx/tests/unit/engines/test_command.py
240 lines 9.7 kB history raw
1'''
2searx is free software: you can redistribute it and/or modify
3it under the terms of the GNU Affero General Public License as published by
4the Free Software Foundation, either version 3 of the License, or
5(at your option) any later version.
7searx is distributed in the hope that it will be useful,
8but WITHOUT ANY WARRANTY; without even the implied warranty of
9MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10GNU Affero General Public License for more details.
12You should have received a copy of the GNU Affero General Public License
13along with searx. If not, see < http://www.gnu.org/licenses/ >.
15'''
18from searx.engines import command as command_engine
19from searx.testing import SearxTestCase
22class TestCommandEngine(SearxTestCase):
23 def test_basic_seq_command_engine(self):
24 ls_engine = command_engine
25 ls_engine.command = ['seq', '{{QUERY}}']
26 ls_engine.delimiter = {'chars': ' ', 'keys': ['number']}
27 expected_results = [
28 {'number': '1', 'template': 'key-value.html'},
29 {'number': '2', 'template': 'key-value.html'},
30 {'number': '3', 'template': 'key-value.html'},
31 {'number': '4', 'template': 'key-value.html'},
32 {'number': '5', 'template': 'key-value.html'},
33 ]
34 results = ls_engine.search('5'.encode('utf-8'), {'pageno': 1})
35 self.assertEqual(results, expected_results)
37 def test_delimiter_parsing_command_engine(self):
38 searx_logs = '''DEBUG:searx.webapp:static directory is /home/n/p/searx/searx/static
39DEBUG:searx.webapp:templates directory is /home/n/p/searx/searx/templates
40DEBUG:searx.engines:soundcloud engine: Starting background initialization
41DEBUG:searx.engines:wolframalpha engine: Starting background initialization
42DEBUG:searx.engines:locate engine: Starting background initialization
43DEBUG:searx.engines:regex search in files engine: Starting background initialization
44DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): www.wolframalpha.com
45DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): soundcloud.com
46DEBUG:searx.engines:find engine: Starting background initialization
47DEBUG:searx.engines:pattern search in files engine: Starting background initialization
48DEBUG:searx.webapp:starting webserver on 127.0.0.1:8888
49WARNING:werkzeug: * Debugger is active!
50INFO:werkzeug: * Debugger PIN: 299-578-362'''
51 echo_engine = command_engine
52 echo_engine.command = ['echo', searx_logs]
53 echo_engine.delimiter = {'chars': ':', 'keys': ['level', 'component', 'message']}
55 expected_results_by_page = [
56 [
57 {
58 'component': 'searx.webapp',
59 'message': 'static directory is /home/n/p/searx/searx/static',
60 'template': 'key-value.html',
61 'level': 'DEBUG',
62 },
63 {
64 'component': 'searx.webapp',
65 'message': 'templates directory is /home/n/p/searx/searx/templates',
66 'template': 'key-value.html',
67 'level': 'DEBUG',
68 },
69 {
70 'component': 'searx.engines',
71 'message': 'soundcloud engine: Starting background initialization',
72 'template': 'key-value.html',
73 'level': 'DEBUG',
74 },
75 {
76 'component': 'searx.engines',
77 'message': 'wolframalpha engine: Starting background initialization',
78 'template': 'key-value.html',
79 'level': 'DEBUG',
80 },
81 {
82 'component': 'searx.engines',
83 'message': 'locate engine: Starting background initialization',
84 'template': 'key-value.html',
85 'level': 'DEBUG',
86 },
87 {
88 'component': 'searx.engines',
89 'message': 'regex search in files engine: Starting background initialization',
90 'template': 'key-value.html',
91 'level': 'DEBUG',
92 },
93 {
94 'component': 'urllib3.connectionpool',
95 'message': 'Starting new HTTPS connection (1): www.wolframalpha.com',
96 'template': 'key-value.html',
97 'level': 'DEBUG',
98 },
99 {
100 'component': 'urllib3.connectionpool',
101 'message': 'Starting new HTTPS connection (1): soundcloud.com',
102 'template': 'key-value.html',
103 'level': 'DEBUG',
104 },
105 {
106 'component': 'searx.engines',
107 'message': 'find engine: Starting background initialization',
108 'template': 'key-value.html',
109 'level': 'DEBUG',
110 },
111 {
112 'component': 'searx.engines',
113 'message': 'pattern search in files engine: Starting background initialization',
114 'template': 'key-value.html',
115 'level': 'DEBUG',
116 },
118 ],
119 [
120 {
121 'component': 'searx.webapp',
122 'message': 'starting webserver on 127.0.0.1:8888',
123 'template': 'key-value.html',
124 'level': 'DEBUG',
125 },
126 {
127 'component': 'werkzeug',
128 'message': ' * Debugger is active!',
129 'template': 'key-value.html',
130 'level': 'WARNING',
131 },
132 {
133 'component': 'werkzeug',
134 'message': ' * Debugger PIN: 299-578-362',
135 'template': 'key-value.html',
136 'level': 'INFO',
137 },
138 ],
140 ]
142 for i in [0, 1]:
143 results = echo_engine.search(''.encode('utf-8'), {'pageno': i + 1})
144 self.assertEqual(results, expected_results_by_page[i])
146 def test_regex_parsing_command_engine(self):
147 txt = '''commit 35f9a8c81d162a361b826bbcd4a1081a4fbe76a7
148Author: Noémi Ványi <sitbackandwait@gmail.com>
149Date: Tue Oct 15 11:31:33 2019 +0200
151first interesting message
153commit 6c3c206316153ccc422755512bceaa9ab0b14faa
154Author: Noémi Ványi <sitbackandwait@gmail.com>
155Date: Mon Oct 14 17:10:08 2019 +0200
157second interesting message
159commit d8594d2689b4d5e0d2f80250223886c3a1805ef5
160Author: Noémi Ványi <sitbackandwait@gmail.com>
161Date: Mon Oct 14 14:45:05 2019 +0200
163third interesting message
165commit '''
166 git_log_engine = command_engine
167 git_log_engine.command = ['echo', txt]
168 git_log_engine.result_separator = '\n\ncommit '
169 git_log_engine.delimiter = {}
170 git_log_engine.parse_regex = {
171 'commit': '\w{40}',
172 'author': '[\w* ]* <\w*@?\w*\.?\w*>',
173 'date': 'Date: .*',
174 'message': '\n\n.*$'
175 }
176 expected_results = [
177 {
178 'commit': '35f9a8c81d162a361b826bbcd4a1081a4fbe76a7',
179 'author': ' Noémi Ványi <sitbackandwait@gmail.com>',
180 'date': 'Date: Tue Oct 15 11:31:33 2019 +0200',
181 'message': '\n\nfirst interesting message',
182 'template': 'key-value.html',
183 },
184 {
185 'commit': '6c3c206316153ccc422755512bceaa9ab0b14faa',
186 'author': ' Noémi Ványi <sitbackandwait@gmail.com>',
187 'date': 'Date: Mon Oct 14 17:10:08 2019 +0200',
188 'message': '\n\nsecond interesting message',
189 'template': 'key-value.html',
190 },
191 {
192 'commit': 'd8594d2689b4d5e0d2f80250223886c3a1805ef5',
193 'author': ' Noémi Ványi <sitbackandwait@gmail.com>',
194 'date': 'Date: Mon Oct 14 14:45:05 2019 +0200',
195 'message': '\n\nthird interesting message',
196 'template': 'key-value.html',
197 },
199 ]
201 results = git_log_engine.search(''.encode('utf-8'), {'pageno': 1})
202 self.assertEqual(results, expected_results)
204 def test_working_dir_path_query(self):
205 ls_engine = command_engine
206 ls_engine.command = ['ls', '{{QUERY}}']
207 ls_engine.result_separator = '\n'
208 ls_engine.delimiter = {'chars': ' ', 'keys': ['file']}
209 ls_engine.query_type = 'path'
211 results = ls_engine.search('.'.encode(), {'pageno': 1})
212 self.assertTrue(len(results) != 0)
214 forbidden_paths = [
215 '..',
216 '../..',
217 './..',
218 '~',
219 '/var',
220 ]
221 for forbidden_path in forbidden_paths:
222 self.assertRaises(ValueError, ls_engine.search, '..'.encode(), {'pageno': 1})
224 def test_enum_queries(self):
225 echo_engine = command_engine
226 echo_engine.command = ['echo', '{{QUERY}}']
227 echo_engine.query_type = 'enum'
228 echo_engine.query_enum = ['i-am-allowed-to-say-this', 'and-that']
230 for allowed in echo_engine.query_enum:
231 results = echo_engine.search(allowed.encode(), {'pageno': 1})
232 self.assertTrue(len(results) != 0)
234 forbidden_queries = [
235 'forbidden',
236 'banned',
237 'prohibited',
238 ]
239 for forbidden in forbidden_queries:
240 self.assertRaises(ValueError, echo_engine.search, forbidden.encode(), {'pageno': 1})