Back to index

moin  1.9.0~rc2
test_xmppbot.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - tests for the XMPP component
00004 
00005     @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 import py
00010 from Queue import Queue
00011 
00012 try:
00013     import pyxmpp
00014     from jabberbot.xmppbot import XMPPBot
00015 except ImportError:
00016     py.test.skip("Skipping jabber bot tests - pyxmpp is not installed")
00017 
00018 import jabberbot.commands as commands
00019 from jabberbot.config import BotConfig
00020 
00021 class TestXMPPBotCommands:
00022     """Various tests for the XMPP bot receiving commands from Wiki"""
00023 
00024     def setup_class(self):
00025         self.from_xmlrpc = Queue()
00026         self.to_xmlrpc = Queue()
00027         self.bot = XMPPBot(BotConfig, self.from_xmlrpc, self.to_xmlrpc)
00028 
00029     def setup_method(self, method):
00030         self.called = False
00031         self.bot.send_message = self.dummy_method
00032         self.bot.ask_for_subscription = self.dummy_method
00033         self.bot.remove_subscription = self.dummy_method
00034 
00035     def dummy_method(self, *args, **kwargs):
00036         self.called = True
00037 
00038     def testNotificationCommand(self):
00039         """Check if send_message is triggered for tested commands"""
00040 
00041         data = {'text': 'Some notification', 'subject': 'It is optional', 'url_list': []}
00042         cmds = []
00043         cmds.append(commands.NotificationCommand(["dude@example.com"], data, True))
00044         cmds.append(commands.NotificationCommandI18n(["dude@example.com"], data, True))
00045         cmds.append(commands.GetPage("dude@example.com", "TestPage"))
00046         cmds.append(commands.GetPageHTML("dude@example.com", "TestPage"))
00047 
00048         tmp_cmd = commands.GetPageList("dude@example.com")
00049         tmp_cmd.data = ""
00050         cmds.append(tmp_cmd)
00051 
00052         tmp_cmd = commands.GetPageInfo("dude@example.com", "TestPage")
00053         tmp_cmd.data = {'author': 'dude', 'lastModified': '200708060T34350', 'version': 42}
00054         cmds.append(tmp_cmd)
00055 
00056         for cmd in cmds:
00057             self.called = False
00058             self.bot.handle_command(cmd)
00059             if not self.called:
00060                 print "The bot should send a notification when %s arrives!" % (cmd.__class__.__name__, )
00061                 raise Exception()
00062 
00063     def testRosterCommands(self):
00064         """Test if appropriate functions are called for (Add|Remove)JIDFromRosterCommand"""
00065 
00066         command = commands.AddJIDToRosterCommand("dude@example.com")
00067         self.bot.handle_command(command)
00068 
00069         if not self.called:
00070             print "The bot should do something when AddJIDToRosterCommand arrives!"
00071             raise Exception()
00072 
00073         self.called = False
00074         command = commands.RemoveJIDFromRosterCommand("dude@example.com")
00075         self.bot.handle_command(command)
00076 
00077         if not self.called:
00078             print "The bot should do something when RemoveJIDFromRosterCommand arrives!"
00079             raise Exception()
00080 
00081     def testInternalHelp(self):
00082         """Check if there's help for every known command"""
00083 
00084         commands = self.bot.internal_commands + self.bot.xmlrpc_commands.values()
00085         for cmd in commands:
00086             print "There should be help on %s command!" % (cmd, )
00087             assert self.bot.help_on("dude@example.com", cmd)