Debian Codegraph @

tests/commands_test.py

raw
1 # coding=utf-8
2 import logging
3 import os
4 import re
5 import tarfile
6 from os import mkdir, path
7 from queue import Empty
8 from shutil import rmtree
9 from tempfile import mkdtemp
10
11 import pytest
12 from unittest.mock import MagicMock
13
14 extra_plugin_dir = path.join(path.dirname(path.realpath(__file__)), "dummy_plugin")
15
16
17 def test_root_help(testbot):
18 assert "All commands" in testbot.exec_command("!help")
19
20
21 def test_help(testbot):
22 assert "!about" in testbot.exec_command("!help Help")
23 assert "That command is not defined." in testbot.exec_command("!help beurk")
24
25 # Ensure that help reports on re_commands.
26 assert "runs foo" in testbot.exec_command("!help foo") # Part of Dummy
27 assert "runs re_foo" in testbot.exec_command("!help re_foo") # Part of Dummy
28 assert "runs re_foo" in testbot.exec_command("!help re foo") # Part of Dummy
29
30
31 def test_about(testbot):
32 assert "Errbot version" in testbot.exec_command("!about")
33
34
35 def test_uptime(testbot):
36 assert "I've been up for" in testbot.exec_command("!uptime")
37
38
39 def test_status(testbot):
40 assert "Yes I am alive" in testbot.exec_command("!status")
41
42
43 def test_status_plugins(testbot):
44 assert "A = Activated, D = Deactivated" in testbot.exec_command("!status plugins")
45
46
47 def test_status_load(testbot):
48 assert "Load " in testbot.exec_command("!status load")
49
50
51 def test_whoami(testbot):
52 assert "person" in testbot.exec_command("!whoami")
53 assert "gbin@localhost" in testbot.exec_command("!whoami")
54
55
56 def test_echo(testbot):
57 assert "foo" in testbot.exec_command("!echo foo")
58
59
60 def test_status_gc(testbot):
61 assert "GC 0->" in testbot.exec_command("!status gc")
62
63
64 def test_config_cycle(testbot):
65 testbot.push_message("!plugin config Webserver")
66 m = testbot.pop_message()
67 assert (
68 "Default configuration for this plugin (you can copy and paste this directly as a command)"
69 in m
70 )
71 assert "Current configuration" not in m
72
73 testbot.assertInCommand(
74 "!plugin config Webserver {'HOST': 'localhost', 'PORT': 3141, 'SSL': None}",
75 "Plugin configuration done.",
76 )
77
78 assert "Current configuration" in testbot.exec_command("!plugin config Webserver")
79 assert "localhost" in testbot.exec_command("!plugin config Webserver")
80
81
82 def test_apropos(testbot):
83 assert "!about: Return information about" in testbot.exec_command("!apropos about")
84
85
86 def test_logtail(testbot):
87 assert "DEBUG" in testbot.exec_command("!log tail")
88
89
90 def test_history(testbot):
91 assert "up" in testbot.exec_command("!uptime")
92 assert "uptime" in testbot.exec_command("!history")
93
94 orig_sender = testbot.bot.sender
95 # Pretend to be someone else. History should be empty
96 testbot.bot.sender = testbot.bot.build_identifier("non_default_person")
97 testbot.push_message("!history")
98 with pytest.raises(Empty):
99 testbot.pop_message(timeout=1)
100 assert "should be a separate history" in testbot.exec_command(
101 "!echo should be a separate history"
102 )
103 assert "should be a separate history" in testbot.exec_command("!history")
104 testbot.bot.sender = orig_sender
105 # Pretend to be the original person again. History should still contain uptime
106 assert "uptime" in testbot.exec_command("!history")
107
108
109 def test_plugin_cycle(testbot):
110 plugins = [
111 "errbotio/err-helloworld",
112 ]
113
114 for plugin in plugins:
115 testbot.assertInCommand(f"!repos install {plugin}", f"Installing {plugin}..."),
116 assert (
117 "A new plugin repository has been installed correctly from errbotio/err-helloworld"
118 in testbot.pop_message(timeout=60)
119 )
120 assert "Plugins reloaded" in testbot.pop_message()
121
122 assert "this command says hello" in testbot.exec_command("!help hello")
123 assert "Hello World !" in testbot.exec_command("!hello")
124
125 testbot.push_message("!plugin reload HelloWorld")
126 assert "Plugin HelloWorld reloaded." == testbot.pop_message()
127
128 testbot.push_message("!hello") # should still respond
129 assert "Hello World !" == testbot.pop_message()
130
131 testbot.push_message("!plugin blacklist HelloWorld")
132 assert "Plugin HelloWorld is now blacklisted." == testbot.pop_message()
133 testbot.push_message("!plugin deactivate HelloWorld")
134 assert "HelloWorld is already deactivated." == testbot.pop_message()
135
136 testbot.push_message("!hello") # should not respond
137 assert 'Command "hello" not found' in testbot.pop_message()
138
139 testbot.push_message("!plugin unblacklist HelloWorld")
140 assert "Plugin HelloWorld removed from blacklist." == testbot.pop_message()
141 testbot.push_message("!plugin activate HelloWorld")
142 assert "HelloWorld is already activated." == testbot.pop_message()
143
144 testbot.push_message("!hello") # should respond back
145 assert "Hello World !" == testbot.pop_message()
146
147 testbot.push_message("!repos uninstall errbotio/err-helloworld")
148 assert "Repo errbotio/err-helloworld removed." == testbot.pop_message()
149
150 testbot.push_message("!hello") # should not respond
151 assert 'Command "hello" not found' in testbot.pop_message()
152
153
154 def test_broken_plugin(testbot):
155 borken_plugin_dir = path.join(
156 path.dirname(path.realpath(__file__)), "borken_plugin"
157 )
158 try:
159 tempd = mkdtemp()
160 tgz = os.path.join(tempd, "borken.tar.gz")
161 with tarfile.open(tgz, "w:gz") as tar:
162 tar.add(borken_plugin_dir, arcname="borken")
163 assert "Installing" in testbot.exec_command(
164 "!repos install file://" + tgz, timeout=120
165 )
166 assert "import borken # fails" in testbot.pop_message()
167 assert "as it did not load correctly." in testbot.pop_message()
168 assert (
169 "Error: Broken failed to activate: "
170 "'NoneType' object has no attribute 'is_activated'"
171 ) in testbot.pop_message()
172 assert "Plugins reloaded." in testbot.pop_message()
173 finally:
174 rmtree(tempd)
175
176
177 def test_backup(testbot):
178 bot = testbot.bot # used while restoring
179 bot.push_message("!repos install https://github.com/errbotio/err-helloworld.git")
180 assert "Installing" in testbot.pop_message()
181 assert "err-helloworld" in testbot.pop_message(timeout=60)
182 assert "reload" in testbot.pop_message()
183 bot.push_message("!backup")
184 msg = testbot.pop_message()
185 assert "has been written in" in msg
186 filename = re.search(r'"(.*)"', msg).group(1)
187
188 # At least the backup should mention the installed plugin
189 assert "errbotio/err-helloworld" in open(filename).read()
190
191 # Now try to clean the bot and restore
192 for p in testbot.bot.plugin_manager.get_all_active_plugins():
193 p.close_storage()
194
195 assert "Plugin HelloWorld deactivated." in testbot.exec_command(
196 "!plugin deactivate HelloWorld"
197 )
198
199 plugins_dir = path.join(testbot.bot_config.BOT_DATA_DIR, "plugins")
200 bot.repo_manager["installed_repos"] = {}
201 bot.plugin_manager["configs"] = {}
202 rmtree(plugins_dir)
203 mkdir(plugins_dir)
204
205 from errbot.bootstrap import restore_bot_from_backup
206
207 log = logging.getLogger(__name__) # noqa
208 restore_bot_from_backup(filename, bot=bot, log=log)
209
210 assert "Plugin HelloWorld activated." in testbot.exec_command(
211 "!plugin activate HelloWorld"
212 )
213 assert "Hello World !" in testbot.exec_command("!hello")
214 testbot.push_message("!repos uninstall errbotio/err-helloworld")
215
216
217 def test_encoding_preservation(testbot):
218 testbot.push_message("!echo へようこそ")
219 assert "へようこそ" == testbot.pop_message()
220
221
222 def test_webserver_webhook_test(testbot):
223 testbot.push_message(
224 "!plugin config Webserver {'HOST': 'localhost', 'PORT': 3141, 'SSL': None}"
225 )
226 assert "Plugin configuration done." in testbot.pop_message()
227 testbot.assertInCommand("!webhook test /echo toto", "Status code: 200")
228
229
230 def test_activate_reload_and_deactivate(testbot):
231 for command in ("activate", "reload", "deactivate"):
232 testbot.push_message(f"!plugin {command}")
233 m = testbot.pop_message()
234 assert "Please tell me which of the following plugins to" in m
235 assert "ChatRoom" in m
236
237 testbot.push_message(f"!plugin {command} nosuchplugin")
238 m = testbot.pop_message()
239 assert "nosuchplugin isn't a valid plugin name. The current plugins are" in m
240 assert "ChatRoom" in m
241
242 testbot.push_message("!plugin reload ChatRoom")
243 assert "Plugin ChatRoom reloaded." == testbot.pop_message()
244
245 testbot.push_message("!status plugins")
246 assert "A │ ChatRoom" in testbot.pop_message()
247
248 testbot.push_message("!plugin deactivate ChatRoom")
249 assert "Plugin ChatRoom deactivated." == testbot.pop_message()
250
251 testbot.push_message("!status plugins")
252 assert "D │ ChatRoom" in testbot.pop_message()
253
254 testbot.push_message("!plugin deactivate ChatRoom")
255 assert "ChatRoom is already deactivated." in testbot.pop_message()
256
257 testbot.push_message("!plugin activate ChatRoom")
258 assert "Plugin ChatRoom activated." in testbot.pop_message()
259
260 testbot.push_message("!status plugins")
261 assert "A │ ChatRoom" in testbot.pop_message()
262
263 testbot.push_message("!plugin activate ChatRoom")
264 assert "ChatRoom is already activated." == testbot.pop_message()
265
266 testbot.push_message("!plugin deactivate ChatRoom")
267 assert "Plugin ChatRoom deactivated." == testbot.pop_message()
268 testbot.push_message("!plugin reload ChatRoom")
269 assert (
270 "Warning: plugin ChatRoom is currently not activated. Use !plugin activate ChatRoom to activate it."
271 == testbot.pop_message()
272 )
273 assert "Plugin ChatRoom reloaded." == testbot.pop_message()
274
275 testbot.push_message("!plugin blacklist ChatRoom")
276 assert "Plugin ChatRoom is now blacklisted." == testbot.pop_message()
277
278 testbot.push_message("!status plugins")
279 assert "B,D │ ChatRoom" in testbot.pop_message()
280
281 # Needed else configuration for this plugin gets saved which screws up
282 # other tests
283 testbot.push_message("!plugin unblacklist ChatRoom")
284 testbot.pop_message()
285
286
287 def test_unblacklist_and_blacklist(testbot):
288 testbot.push_message("!plugin unblacklist nosuchplugin")
289 m = testbot.pop_message()
290 assert "nosuchplugin isn't a valid plugin name. The current plugins are" in m
291 assert "ChatRoom" in m
292
293 testbot.push_message("!plugin blacklist nosuchplugin")
294 m = testbot.pop_message()
295 assert "nosuchplugin isn't a valid plugin name. The current plugins are" in m
296 assert "ChatRoom" in m
297
298 testbot.push_message("!plugin blacklist ChatRoom")
299 assert "Plugin ChatRoom is now blacklisted" in testbot.pop_message()
300
301 testbot.push_message("!plugin blacklist ChatRoom")
302 assert "Plugin ChatRoom is already blacklisted." == testbot.pop_message()
303
304 testbot.push_message("!status plugins")
305 assert "B,D │ ChatRoom" in testbot.pop_message()
306
307 testbot.push_message("!plugin unblacklist ChatRoom")
308 assert "Plugin ChatRoom removed from blacklist." == testbot.pop_message()
309
310 testbot.push_message("!plugin unblacklist ChatRoom")
311 assert "Plugin ChatRoom is not blacklisted." == testbot.pop_message()
312
313 testbot.push_message("!status plugins")
314 assert "A │ ChatRoom" in testbot.pop_message()
315
316
317 def test_optional_prefix(testbot):
318 testbot.bot_config.BOT_PREFIX_OPTIONAL_ON_CHAT = False
319 assert "Yes I am alive" in testbot.exec_command("!status")
320
321 testbot.bot_config.BOT_PREFIX_OPTIONAL_ON_CHAT = True
322 assert "Yes I am alive" in testbot.exec_command("!status")
323 assert "Yes I am alive" in testbot.exec_command("status")
324
325
326 def test_optional_prefix_re_cmd(testbot):
327 testbot.bot_config.BOT_PREFIX_OPTIONAL_ON_CHAT = False
328 assert "bar" in testbot.exec_command("!plz dont match this")
329
330 testbot.bot_config.BOT_PREFIX_OPTIONAL_ON_CHAT = True
331 assert "bar" in testbot.exec_command("!plz dont match this")
332 assert "bar" in testbot.exec_command("plz dont match this")
333
334
335 def test_simple_match(testbot):
336 assert "bar" in testbot.exec_command("match this")
337
338
339 def test_no_suggest_on_re_commands(testbot):
340 testbot.push_message("!re_ba")
341 # Don't suggest a regexp command.
342 assert "!re bar" not in testbot.pop_message()
343
344
345 def test_callback_no_command(testbot):
346 extra_plugin_dir = os.path.join(
347 os.path.dirname(os.path.realpath(__file__)), "commandnotfound_plugin"
348 )
349
350 cmd = "!this_is_not_a_real_command_at_all"
351 expected_str = f"Command fell through: {cmd}"
352
353 testbot.exec_command("!plugin deactivate CommandNotFoundFilter")
354 testbot.bot.plugin_manager._extra_plugin_dir = extra_plugin_dir
355 testbot.bot.plugin_manager.update_plugin_places([])
356 testbot.exec_command("!plugin activate TestCommandNotFoundFilter")
357 assert expected_str == testbot.exec_command(cmd)
358
359
360 def test_subcommands(testbot):
361 # test single subcommand (method is run_subcommands())
362 cmd = "!run subcommands with these args"
363 cmd_underscore = "!run_subcommands with these args"
364 expected_args = "with these args"
365 assert expected_args == testbot.exec_command(cmd)
366 assert expected_args == testbot.exec_command(cmd_underscore)
367
368 # test multiple subcmomands (method is run_lots_of_subcommands())
369 cmd = "!run lots of subcommands with these args"
370 cmd_underscore = "!run_lots_of_subcommands with these args"
371 assert expected_args == testbot.exec_command(cmd)
372 assert expected_args == testbot.exec_command(cmd_underscore)
373
374
375 def test_command_not_found_with_space_in_bot_prefix(testbot):
376 testbot.bot_config.BOT_PREFIX = "! "
377 assert 'Command "blah" not found.' in testbot.exec_command("! blah")
378 assert 'Command "blah" / "blah toto" not found.' in testbot.exec_command(
379 "! blah toto"
380 )
381
382
383 def test_mock_injection(testbot):
384 helper_mock = MagicMock()
385 helper_mock.return_value = "foo"
386 mock_dict = {"helper_method": helper_mock}
387 testbot.inject_mocks("Dummy", mock_dict)
388 assert "foo" in testbot.exec_command("!baz")
389
390
391 def test_multiline_command(testbot):
392 testbot.assertInCommand(
393 """
394 !bar title
395 first line of body
396 second line of body
397 """,
398 "!bar title\nfirst line of body\nsecond line of body",
399 dedent=True,
400 )
401
402
403 def test_plugin_info_command(testbot):
404 output = testbot.exec_command("!plugin info Help")
405 assert "name: Help" in output
406 assert "module: help" in output
407 assert "help.py" in output
408 assert "log level: NOTSET" in output