test_command.py 9.5 KB

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