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