def test_whos_out(self, testbot): push_message("who's out?") msg = pop_message(0.2) assert len(msg) > 0 assert 'Sarah Skiver:' in msg assert 'Charlie Brown:' in msg print(msg)
def test_mycommand_tellme_something_returns_expected_output(self): p = get_plugin_obj_by_name('GarakBot') fp = StringIO(json.dumps(self.random_quotes)) p.garak_quote_ds = fp p.clear_garak_quote_datasource() push_message('! tellme something') self.assertRegex(pop_message(), r'^\w+ Quote!')
def test_bad_config_can_be_overriden(self, testbot): '''So we can't stop err unloading the plugin, but this state should at least be recoverable from''' # Set a bad config push_message("""!config HolidayBot { 'BAMBOOHR_APIKEY': 'IchangedYou', 'BAMBOOHR_HOST': 'IchangedYou', 'BLAH_BLAH_BLAH': 'IchangedYou'}""") check_reply("Plugin configuration done") # Plugin should be unloaded push_message("!status plugins") check_reply(["[U] HolidayBot"]) # Set a good config push_message("""!config HolidayBot { 'BAMBOOHR_APIKEY': 'IchangedYou', 'BAMBOOHR_HOST': '""" + TEST_HOST + """', 'BAMBOOHR_COMPANY': '""" + TEST_COMPANY + """'}""") check_reply("Plugin configuration done") # Plugin should be loaded push_message("!status plugins") check_reply(["[L] HolidayBot"]) # and plugin should now function self.start_test_server(run_fn) push_message("is x in?") check_reply(["could not find any employee named x"]) self.stop_test_server()
def webserver(testbot): push_message( "!config Webserver {{'HOST': 'localhost', 'PORT': {}, 'SSL': None}}". format(WEBSERVER_PORT)) pop_message() while not webserver_ready('localhost', WEBSERVER_PORT): logging.debug("Webserver not ready yet, sleeping 0.1 second") sleep(0.1)
def test_help(self): push_message('!help ErrBot') response = pop_message() self.assertIn('Available commands for ErrBot', response) self.assertIn('!about', response) push_message('!help beurk') self.assertEqual('That command is not defined.', pop_message())
def test_coffeetime_muc(self, patch_msg_type, testbot): """TODO: Also test the other message if more than 1 user is present Unfortunately I was not able to patch this correctly yet.""" push_message('!coffeetime') assert ("You are the only online person in the group :( \n" "What about getting your own coffee?" ) in pop_message()
def test_time(self, testbot): push_message('!time Uranus') assert 'Mine is well...What about in yours?' in pop_message() # def test_location(self, testbot): # push_message('!location set Interwebs') # assert 'Ok, None, trying to set your location to Interwebs' in pop_message() # assert self['None'] == 'Interwebs' # assert 0
def test_set_config_can_now_check_is_x_in(self, testbot): '''After setting config via chat, can ask whos out''' self.start_test_server(run_fn) push_message("""!config HolidayBot { 'BAMBOOHR_APIKEY': 'testApikey', 'BAMBOOHR_HOST': 'http://localhost:8080', 'BAMBOOHR_COMPANY': 'reynholm-industries'}""") check_reply("Plugin configuration done") push_message("Is Julie in?") check_reply(["could not find", "Julie"]) self.stop_test_server()
def test_set_config(self, testbot): '''Should allow a new config to be set and read''' push_message("""!config HolidayBot { 'BAMBOOHR_APIKEY': 'IchangedYou', 'BAMBOOHR_HOST': 'IchangedYou', 'BAMBOOHR_COMPANY': 'IchangedYou'}""") check_reply("Plugin configuration done") push_message("""!config HolidayBot""") check_reply(["'BAMBOOHR_APIKEY': 'IchangedYou'", "'BAMBOOHR_HOST': 'IchangedYou'", "'BAMBOOHR_COMPANY': 'IchangedYou'"])
def test_is_x_in_using_hipchat_handle(self, testbot): push_message("is @SarahSkiver in?") check_reply('Sarah Skiver is currently on leave') push_message('where is @SarahSkiver?') check_reply('Sarah Skiver is currently on leave') push_message("is @Hugo in?") check_reply('Hugo Boss (hugs) is not on leave') push_message("is @WillSam in?") check_reply('Willem Samuel (Will) is not on leave')
def test_delete_kudos_list(self, testbot): push_message('!sijis++') assert 'kudos updated for sijis' in pop_message() push_message('!tom++') assert 'kudos updated for tom' in pop_message() push_message('!kudos list') assert 'sijis, tom' in pop_message()
def test_delete_karma_list(self, testbot): push_message("!sijis++") assert "karma updated for sijis" in pop_message() push_message("!tom++") assert "karma updated for tom" in pop_message() push_message("!karma list") assert "sijis, tom" in pop_message()
def test_config_cycle(self): push_message('!config Webserver') m = pop_message() self.assertIn('Default configuration for this plugin (you can copy and paste this directly as a command)', m) self.assertNotIn('Current configuration', m) push_message("!config Webserver {'HOST': 'localhost', 'PORT': 3141, 'SSL': None}") self.assertIn('Plugin configuration done.', pop_message()) push_message('!config Webserver') m = pop_message() self.assertIn('Current configuration', m) self.assertIn('localhost', m) push_message('!config Webserver') self.assertIn('localhost', pop_message())
def test_generate_certificate_creates_usable_cert(self): from config import BOT_DATA_DIR key_path = os.sep.join((BOT_DATA_DIR, "webserver_key.pem")) cert_path = os.sep.join((BOT_DATA_DIR, "webserver_certificate.pem")) push_message("!generate_certificate") assert "Generating" in pop_message(timeout=1) # Generating a certificate could be slow on weak hardware, so keep a safe # timeout on the first pop_message() assert "successfully generated" in pop_message(timeout=60) assert "is recommended" in pop_message(timeout=1) assert key_path in pop_message(timeout=1) webserver_config = { 'HOST': 'localhost', 'PORT': WEBSERVER_PORT, 'SSL': { 'certificate': cert_path, 'key': key_path, 'host': 'localhost', 'port': WEBSERVER_SSL_PORT, 'enabled': True, } } push_message("!config Webserver {!r}".format(webserver_config)) pop_message() while not webserver_ready('localhost', WEBSERVER_SSL_PORT): log.debug("Webserver not ready yet, sleeping 0.1 second") sleep(0.1) assert requests.post( 'https://localhost:{}/webhook1/'.format(WEBSERVER_SSL_PORT), JSONOBJECT, verify=False ).text == repr(json.loads(JSONOBJECT))
def test_generate_certificate_creates_usable_cert(self): from config import BOT_DATA_DIR key_path = os.sep.join((BOT_DATA_DIR, "webserver_key.pem")) cert_path = os.sep.join((BOT_DATA_DIR, "webserver_certificate.pem")) push_message("!generate_certificate") assert "Generating" in pop_message(timeout=1) # Generating a certificate could be slow on weak hardware, so keep a safe # timeout on the first pop_message() assert "successfully generated" in pop_message(timeout=60) assert "is recommended" in pop_message(timeout=1) assert key_path in pop_message(timeout=1) webserver_config = { 'HOST': 'localhost', 'PORT': WEBSERVER_PORT, 'SSL': { 'certificate': cert_path, 'key': key_path, 'host': 'localhost', 'port': WEBSERVER_SSL_PORT, 'enabled': True, } } push_message("!config Webserver {!r}".format(webserver_config)) pop_message() while not webserver_ready('localhost', WEBSERVER_SSL_PORT): logging.debug("Webserver not ready yet, sleeping 0.1 second") sleep(0.1) assert requests.post( 'https://localhost:{}/webhook1/'.format(WEBSERVER_SSL_PORT), JSONOBJECT, verify=False ).text == repr(json.loads(JSONOBJECT))
def test_backup(self): push_message('!repos install git://github.com/gbin/err-helloworld.git') self.assertIn('err-helloworld', pop_message(timeout=60)) self.assertIn('reload', pop_message()) push_message('!backup') msg = pop_message() self.assertIn('has been written in', msg) filename = re.search(r"'([A-Za-z0-9_\./\\-]*)'", msg).group(1) # At least the backup should mention the installed plugin self.assertIn('err-helloworld', open(filename).read()) push_message('!repos uninstall err-helloworld')
def test_mycommand_tellme_something_returns_random_output(self): p = get_plugin_obj_by_name('GarakBot') fp = StringIO(json.dumps(self.random_quotes2)) p.garak_quote_ds = fp p.clear_garak_quote_datasource() push_message('! tellme something') a = pop_message() push_message('! tellme something') b = pop_message() push_message('! tellme something') c = pop_message() assert a is not None assert b is not None assert c is not None assert a != b != c
def test_uptime(self): push_message('!uptime') self.assertIn('I\'ve been up for', pop_message())
def test_logtail(self): push_message('!log tail') self.assertIn('DEBUG', pop_message())
def test_plugin_cycle(self): push_message('!repos install git://github.com/gbin/err-helloworld.git') self.assertIn('err-helloworld', pop_message(timeout=60)) self.assertIn('reload', pop_message()) push_message('!help hello') # should appear in the help self.assertEqual("this command says hello", pop_message()) push_message('!hello') # should respond self.assertEqual('Hello World !', pop_message()) push_message('!reload HelloWorld') self.assertEqual('Plugin HelloWorld deactivated', pop_message()) self.assertEqual('Plugin HelloWorld activated', pop_message()) push_message('!hello') # should still respond self.assertEqual('Hello World !', pop_message()) push_message('!blacklist HelloWorld') self.assertEqual('Plugin HelloWorld is now blacklisted', pop_message()) push_message('!unload HelloWorld') self.assertEqual('Plugin HelloWorld deactivated', pop_message()) push_message('!hello') # should not respond self.assertIn('Command "hello" not found', pop_message()) push_message('!unblacklist HelloWorld') self.assertEqual('Plugin HelloWorld removed from blacklist', pop_message()) push_message('!load HelloWorld') self.assertEqual('Plugin HelloWorld activated', pop_message()) push_message('!hello') # should respond back self.assertEqual('Hello World !', pop_message()) push_message('!repos uninstall err-helloworld') self.assertEqual('/me is unloading plugin HelloWorld', pop_message()) self.assertEqual('Plugins unloaded and repo err-helloworld removed', pop_message()) push_message('!hello') # should not respond self.assertIn('Command "hello" not found', pop_message())
def test_about(self): push_message('!about') self.assertIn('Err version', pop_message())
def test_status_load(self): push_message('!status load') self.assertIn('Load ', pop_message())
def test_encoding_preservation(self): push_message('!echo へようこそ') self.assertEquals('へようこそ', pop_message())
def test_load_reload_and_unload(self): for command in ('load', 'reload', 'unload'): push_message("!{}".format(command)) m = pop_message() self.assertIn('Please tell me which of the following plugins to reload', m) self.assertIn('ChatRoom', m) push_message('!{} nosuchplugin'.format(command)) m = pop_message() self.assertIn("nosuchplugin isn't a valid plugin name. The current plugins are", m) self.assertIn('ChatRoom', m) push_message('!reload ChatRoom') self.assertEqual('Plugin ChatRoom deactivated', pop_message()) self.assertEqual('Plugin ChatRoom activated', pop_message()) push_message("!status plugins") self.assertIn("[L] ChatRoom", pop_message()) push_message('!unload ChatRoom') self.assertEqual('Plugin ChatRoom deactivated', pop_message()) push_message("!status plugins") self.assertIn("[U] ChatRoom", pop_message()) push_message('!unload ChatRoom') self.assertEqual('ChatRoom is not currently loaded', pop_message()) push_message('!load ChatRoom') self.assertEqual('Plugin ChatRoom activated', pop_message()) push_message("!status plugins") self.assertIn("[L] ChatRoom", pop_message()) push_message('!load ChatRoom') self.assertEqual('ChatRoom is already loaded', pop_message()) push_message('!unload ChatRoom') self.assertEqual('Plugin ChatRoom deactivated', pop_message()) push_message('!reload ChatRoom') self.assertEqual('Plugin ChatRoom not in active list', pop_message()) self.assertEqual('Plugin ChatRoom activated', pop_message()) push_message('!blacklist ChatRoom') self.assertEqual("Plugin ChatRoom is now blacklisted", pop_message()) push_message("!status plugins") self.assertIn("[B,L] ChatRoom", pop_message()) # Needed else configuration for this plugin gets saved which screws up # other tests push_message('!unblacklist ChatRoom') pop_message()
def test_root_help(self): push_message('!help') self.assertIn('Available help', pop_message())
def test_unblacklist_and_blacklist(self): push_message('!unblacklist nosuchplugin') m = pop_message() self.assertIn("nosuchplugin isn't a valid plugin name. The current plugins are", m) self.assertIn('ChatRoom', m) push_message('!blacklist nosuchplugin') m = pop_message() self.assertIn("nosuchplugin isn't a valid plugin name. The current plugins are", m) self.assertIn('ChatRoom', m) push_message('!blacklist ChatRoom') self.assertEqual("Plugin ChatRoom is now blacklisted", pop_message()) push_message('!blacklist ChatRoom') self.assertEqual("Plugin ChatRoom is already blacklisted", pop_message()) push_message("!status plugins") self.assertIn("[B,L] ChatRoom", pop_message()) push_message('!unblacklist ChatRoom') self.assertEqual('Plugin ChatRoom removed from blacklist', pop_message()) push_message('!unblacklist ChatRoom') self.assertEqual('Plugin ChatRoom is not blacklisted', pop_message()) push_message("!status plugins") self.assertIn("[L] ChatRoom", pop_message())
def test_history(self): from errbot.holder import bot push_message('!uptime') pop_message() push_message('!history') self.assertIn('uptime', pop_message()) orig_sender = bot.sender try: # Pretend to be someone else. History should be empty bot.sender = 'non_default_person@localhost' push_message('!history') self.assertRaises(Empty, pop_message, block=False) push_message('!echo should be seperate history') pop_message() push_message('!history') self.assertIn('should be seperate history', pop_message()) finally: bot.sender = orig_sender # Pretend to be the original person again. History should still contain uptime push_message('!history') self.assertIn('uptime', pop_message())
def test_apropos(self): push_message('!apropos about') self.assertIn('!about: Returns some', pop_message())
def test_config_cycle(self): # test the full configuration cycle help, get set and export, import push_message('!zap configs') self.assertIn('Done', pop_message()) push_message('!config Webserver') m = pop_message() self.assertIn('Default configuration for this plugin (you can copy and paste this directly as a command)', m) self.assertNotIn('Current configuration', m) push_message("!config Webserver {'HOST': 'localhost', 'PORT': 3141, 'SSL': None}") self.assertIn('Plugin configuration done.', pop_message()) push_message('!config Webserver') m = pop_message() self.assertIn('Current configuration', m) self.assertIn('localhost', m) push_message('!export configs') configs = pop_message() self.assertIn('localhost', configs) obj = literal_eval(configs) # be sure it is parseable obj['Webserver']['HOST'] = 'localhost' push_message('!import configs ' + repr(obj)) self.assertIn('Import is done correctly', pop_message()) push_message('!config Webserver') self.assertIn('localhost', pop_message())
def test_status_gc(self): push_message('!status gc') self.assertIn('GC 0->', pop_message())
def test_status(self): push_message('!status') self.assertIn('Yes I am alive', pop_message())
def test_webserver_plugin_ok(self, testbot): push_message("!webstatus") assert "/echo/" in pop_message()
def test_webserver_webhook_test(self): push_message("!config Webserver {'HOST': 'localhost', 'PORT': 3141, 'SSL': None}") self.assertIn('Plugin configuration done.', pop_message()) self.assertCommand("!webhook test /echo/ toto", 'Status code : 200')
def test_plugin_cycle(self): push_message('!repos install git://github.com/gbin/err-helloworld.git') self.assertIn('err-helloworld', pop_message(timeout=60)) self.assertIn('reload', pop_message()) push_message('!repos export') # should appear in the export self.assertEqual("{'err-helloworld': 'git://github.com/gbin/err-helloworld.git'}", pop_message()) push_message('!help hello') # should appear in the help self.assertEqual("this command says hello", pop_message()) push_message('!hello') # should respond self.assertEqual('Hello World !', pop_message()) push_message('!reload HelloWorld') self.assertEqual('Plugin HelloWorld deactivated', pop_message()) self.assertEqual('Plugin HelloWorld activated', pop_message()) push_message('!hello') # should still respond self.assertEqual('Hello World !', pop_message()) push_message('!blacklist HelloWorld') self.assertEqual('Plugin HelloWorld is now blacklisted', pop_message()) push_message('!unload HelloWorld') self.assertEqual('Plugin HelloWorld deactivated', pop_message()) push_message('!hello') # should not respond self.assertIn('Command "hello" not found', pop_message()) push_message('!unblacklist HelloWorld') self.assertEqual('Plugin HelloWorld removed from blacklist', pop_message()) push_message('!load HelloWorld') self.assertEqual('Plugin HelloWorld activated', pop_message()) push_message('!hello') # should respond back self.assertEqual('Hello World !', pop_message()) push_message('!repos uninstall err-helloworld') self.assertEqual('/me is unloading plugin HelloWorld', pop_message()) self.assertEqual('Plugins unloaded and repo err-helloworld removed', pop_message()) push_message('!hello') # should not respond self.assertIn('Command "hello" not found', pop_message())
def test_status_plugins(self): push_message('!status plugins') self.assertIn('L=Loaded, U=Unloaded', pop_message())
def test_ping(self, testbot): push_message('!ping') assert 'pong' in pop_message()
def test_history(self): push_message('!uptime') pop_message() push_message('!history') self.assertIn('uptime', pop_message()) orig_sender = self.bot.sender try: # Pretend to be someone else. History should be empty self.bot.sender = 'non_default_person@localhost' push_message('!history') self.assertRaises(Empty, pop_message, block=False) push_message('!echo should be a separate history') pop_message() push_message('!history') self.assertIn('should be a separate history', pop_message()) finally: self.bot.sender = orig_sender # Pretend to be the original person again. History should still contain uptime push_message('!history') self.assertIn('uptime', pop_message())
def test_botcommands(self, testbot): from errbot import holder rooms = holder.bot.rooms() room = holder.bot.query_room('*****@*****.**') assert len(rooms) == 1 assert rooms[0] == room assert room.joined push_message("!room leave [email protected]") assert pop_message() == "Left the room [email protected]" room = holder.bot.query_room('*****@*****.**') assert not room.joined push_message("!room list") assert pop_message() == "I'm not currently in any rooms." push_message("!room destroy [email protected]") assert pop_message() == "Destroyed the room [email protected]" rooms = holder.bot.rooms() room = holder.bot.query_room('*****@*****.**') assert not room.exists assert room not in rooms push_message("!room create [email protected]") assert pop_message() == "Created the room [email protected]" rooms = holder.bot.rooms() room = holder.bot.query_room('*****@*****.**') assert room.exists assert room not in rooms assert not room.joined push_message("!room join [email protected]") assert pop_message() == "Joined the room [email protected]" rooms = holder.bot.rooms() room = holder.bot.query_room('*****@*****.**') assert room.exists assert room.joined assert room in rooms push_message("!room list") assert pop_message( ) == "I'm currently in these rooms:\n\[email protected]" push_message("!room occupants [email protected]") assert pop_message( ) == "Occupants in [email protected]:\n\terr@localhost" push_message("!room topic [email protected]") assert pop_message() == "No topic is set for [email protected]" push_message("!room topic [email protected] 'Err rocks!'") assert pop_message() == "Topic for [email protected] set." push_message("!room topic [email protected]") assert pop_message( ) == "Topic for [email protected]: Err rocks!"