test_command.py 9.5 KB

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