Handle exception CommandError sent from ad-hoc commands implementations
darcs-hash:20080307071827-86b55-450e960980325a71b51772f3d608f09ae2a2abe8.gz
This commit is contained in:
@@ -53,11 +53,28 @@ account_type_node_re = re.compile("^[^@/]+/.*$")
|
||||
account_node_re = re.compile("^[^@/]+@[^/]+/?.*$")
|
||||
|
||||
class FieldNoType(Field):
|
||||
"""
|
||||
Field with no 'type' attribut
|
||||
"""
|
||||
|
||||
def complete_xml_element(self, xmlnode, doc):
|
||||
"""Remove the 'type' attribut"""
|
||||
result = Field.complete_xml_element(self, xmlnode, doc)
|
||||
result.unsetProp("type")
|
||||
return result
|
||||
|
||||
class CommandError(Exception):
|
||||
"""
|
||||
Exception use by commands to report errors
|
||||
"""
|
||||
|
||||
def __init__ (self, error_type="service-unavailable"):
|
||||
"""
|
||||
CommandError constructor
|
||||
"""
|
||||
Exception.__init__(self)
|
||||
self.type = error_type
|
||||
|
||||
class CommandManager(object):
|
||||
"""Handle Ad-Hoc commands"""
|
||||
|
||||
@@ -233,17 +250,8 @@ class CommandManager(object):
|
||||
self.sessions[session_id] = update_step_func(session_id)
|
||||
step = self.sessions[session_id][0]
|
||||
self.parse_form(info_query, session_id)
|
||||
step_method = "execute_" + short_node + "_" + str(step)
|
||||
if hasattr(self, step_method):
|
||||
(form, result) = getattr(self, step_method)(\
|
||||
info_query,
|
||||
self.sessions[session_id][1],
|
||||
command_node,
|
||||
lang_class)
|
||||
return [response] + result
|
||||
else:
|
||||
# A method that can execute all other step
|
||||
step_method = "execute_" + short_node
|
||||
try:
|
||||
step_method = "execute_" + short_node + "_" + str(step)
|
||||
if hasattr(self, step_method):
|
||||
(form, result) = getattr(self, step_method)(\
|
||||
info_query,
|
||||
@@ -252,8 +260,20 @@ class CommandManager(object):
|
||||
lang_class)
|
||||
return [response] + result
|
||||
else:
|
||||
return [info_query.make_error_response(\
|
||||
"feature-not-implemented")]
|
||||
# A method that can execute all other step
|
||||
step_method = "execute_" + short_node
|
||||
if hasattr(self, step_method):
|
||||
(form, result) = getattr(self, step_method)(\
|
||||
info_query,
|
||||
self.sessions[session_id][1],
|
||||
command_node,
|
||||
lang_class)
|
||||
return [response] + result
|
||||
else:
|
||||
return [info_query.make_error_response(\
|
||||
"feature-not-implemented")]
|
||||
except CommandError, error:
|
||||
return [info_query.make_error_response(error.type)]
|
||||
|
||||
def add_actions(self, command_node, actions, default_action_idx=0):
|
||||
actions_node = command_node.newTextChild(None, "actions", None)
|
||||
|
||||
@@ -38,7 +38,8 @@ import jcl.tests
|
||||
from jcl.lang import Lang
|
||||
from jcl.jabber.component import JCLComponent
|
||||
import jcl.jabber.command as command
|
||||
from jcl.jabber.command import FieldNoType, CommandManager, JCLCommandManager
|
||||
from jcl.jabber.command import FieldNoType, CommandManager, JCLCommandManager, \
|
||||
CommandError
|
||||
import jcl.model.account as account
|
||||
from jcl.model.account import Account, PresenceAccount, LegacyJID, User
|
||||
from jcl.model.tests.account import ExampleAccount, Example2Account
|
||||
@@ -390,7 +391,7 @@ class CommandManager_TestCase(unittest.TestCase):
|
||||
command_node = info_query.set_new_content(command.COMMAND_NS,
|
||||
"command")
|
||||
command_node.setProp("node", "command1")
|
||||
result = self.command_manager.execute_multi_step_command(\
|
||||
self.command_manager.execute_multi_step_command(\
|
||||
info_query, "command1", None)
|
||||
self.assertTrue(self.command_manager.command1_step_1_called)
|
||||
|
||||
@@ -416,10 +417,41 @@ class CommandManager_TestCase(unittest.TestCase):
|
||||
"command")
|
||||
command_node.setProp("sessionid", "session_id")
|
||||
command_node.setProp("node", "command1")
|
||||
result = self.command_manager.execute_multi_step_command(\
|
||||
self.command_manager.execute_multi_step_command(\
|
||||
info_query, "command1", lambda session_id: (2, {}))
|
||||
self.assertTrue(self.multi_step_command1_called)
|
||||
|
||||
def test_multi_step_command_error_in_command(self):
|
||||
"""
|
||||
Test if the multi steps method catch the CommandError exception
|
||||
and translate it into an IQ error
|
||||
"""
|
||||
self.command_manager = MockCommandManager()
|
||||
def execute_command1(info_query, session_context,
|
||||
command_node, lang_class):
|
||||
raise CommandError("feature-not-implemented")
|
||||
|
||||
self.command_manager.__dict__["execute_command1_1"] = execute_command1
|
||||
info_query = Iq(stanza_type="set",
|
||||
from_jid="user@test.com",
|
||||
to_jid="jcl.test.com")
|
||||
command_node = info_query.set_new_content(command.COMMAND_NS,
|
||||
"command")
|
||||
command_node.setProp("node", "command1")
|
||||
result = self.command_manager.execute_multi_step_command(\
|
||||
info_query, "command1", None)
|
||||
result_iq = result[0].xmlnode
|
||||
self.assertTrue(jcl.tests.is_xml_equal(\
|
||||
u"<iq from='" + unicode(self.command_manager.component.jid)
|
||||
+ "' to='user@test.com' type='error' "
|
||||
+ "xmlns='http://pyxmpp.jabberstudio.org/xmlns/common'>"
|
||||
+ "<command xmlns='http://jabber.org/protocol/commands'"
|
||||
+ "node='command1' />"
|
||||
+ "<error type='cancel'><feature-not-implemented "
|
||||
+ "xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>"
|
||||
+ "</iq>",
|
||||
result_iq, True))
|
||||
|
||||
def test_parse_form(self):
|
||||
"""
|
||||
Check if parse_form method correctly set the session variables
|
||||
|
||||
Reference in New Issue
Block a user