replace send_stanzas by returned value in handler. Pyxmpp will take care to send these stanzas

This commit is contained in:
David Rousselie
2010-10-21 09:05:15 +02:00
parent d16396c4e4
commit 5f5a0cb7fa
10 changed files with 248 additions and 238 deletions

View File

@@ -40,6 +40,7 @@ from jcl.model.account import Account, User
COMMAND_NS = "http://jabber.org/protocol/commands"
ACTION_COMPLETE = "complete"
ACTION_EXECUTE = "execute"
ACTION_NEXT = "next"
ACTION_PREVIOUS = "prev"
ACTION_CANCEL = "cancel"
@@ -157,6 +158,9 @@ class CommandManager(object):
return [info_query.make_error_response(\
"feature-not-implemented")]
def apply_complete_command(self, info_query, short_command_name):
return self.apply_execute_command(info_query, short_command_name)
def apply_execute_command(self, info_query, short_command_name):
return self.execute_multi_step_command(\
info_query,

View File

@@ -898,8 +898,8 @@ class JCLComponent(Component, object):
Handle IQ-get "jabber:iq:last" requests.
"""
self.__logger.debug("IQ:Last request")
self.apply_registered_behavior(self.iqlast_handlers, info_query)
return 1
return self.apply_registered_behavior(self.iqlast_handlers, info_query,
send_result=False)
def handle_get_gateway(self, info_query):
"""Handle IQ-get "jabber:iq:gateway" requests.
@@ -911,8 +911,7 @@ class JCLComponent(Component, object):
query = info_query.new_query("jabber:iq:gateway")
query.newTextChild(query.ns(), "desc", lang_class.get_gateway_desc)
query.newTextChild(query.ns(), "prompt", lang_class.get_gateway_prompt)
self.send_stanzas([info_query])
return 1
return [info_query]
def handle_set_gateway(self, info_query):
"""
@@ -929,8 +928,7 @@ class JCLComponent(Component, object):
query.newTextChild(query.ns(), "jid", jid)
# XEP-0100 - section 6: should be <jid> but PSI only work with <prompt>
query.newTextChild(query.ns(), "prompt", jid)
self.send_stanzas([info_query])
return 1
return [info_query]
def disco_get_info(self, node, info_query):
"""
@@ -974,15 +972,14 @@ class JCLComponent(Component, object):
query = info_query.new_query("jabber:iq:version")
query.newTextChild(query.ns(), "name", self.name)
query.newTextChild(query.ns(), "version", self.version)
self.send_stanzas([info_query])
return 1
return [info_query]
def handle_get_register(self, info_query):
"""Send back register form to user
see node structure in disco_get_items()
"""
self.__logger.debug("GET_REGISTER")
self.apply_behavior(\
return self.apply_behavior(\
info_query,
lambda name, from_jid, account_type, lang_class: \
self.account_manager.account_get_register(info_query,
@@ -997,21 +994,17 @@ class JCLComponent(Component, object):
lambda name, from_jid, account_type, lang_class: \
self.account_manager.root_get_register(info_query,
lang_class),
send_result=True)
send_result=False)
def handle_set_register(self, info_query):
"""Handle user registration response
"""
self.__logger.debug("SET_REGISTER")
lang_class = \
self.lang.get_lang_class_from_node(info_query.get_node())
from_jid = info_query.get_from()
remove = info_query.xpath_eval("r:query/r:remove",
{"r": "jabber:iq:register"})
if remove:
result = self.account_manager.remove_all_accounts(from_jid)
self.send_stanzas(result)
return 1
return self.account_manager.remove_all_accounts(from_jid)
x_node = info_query.xpath_eval("jir:query/jxd:x",
{"jir": "jabber:iq:register",
@@ -1023,65 +1016,61 @@ class JCLComponent(Component, object):
apply_filter_func=lambda filter_func, stanza, lang_class: \
filter_func(stanza, lang_class, x_data),
apply_handle_func=lambda handle_func, stanza, lang_class, data, result: \
handle_func(stanza, lang_class, data, x_data))
handle_func(stanza, lang_class, data, x_data),
send_result=False)
def handle_presence_available(self, stanza):
"""Handle presence availability
if presence sent to the component ('if not name'), presence is sent to
all accounts for current user. Otherwise, send presence from current account.
all accounts for current user. Otherwise, send presence from current
account.
"""
result = self.apply_registered_behavior(self.presence_available_handlers,
stanza)
return result
self.__logger.debug("PRESENCE_AVAILABLE")
return self.apply_registered_behavior(self.presence_available_handlers,
stanza, send_result=False)
def handle_presence_unavailable(self, stanza):
"""Handle presence unavailability
"""
self.__logger.debug("PRESENCE_UNAVAILABLE")
result = self.apply_registered_behavior(self.presence_unavailable_handlers,
stanza)
return result
return self.apply_registered_behavior(self.presence_unavailable_handlers,
stanza, send_result=False)
def handle_presence_subscribe(self, stanza):
"""Handle subscribe presence from user
"""
self.__logger.debug("PRESENCE_SUBSCRIBE")
result = self.apply_registered_behavior(self.presence_subscribe_handlers,
stanza)
return result
return self.apply_registered_behavior(self.presence_subscribe_handlers,
stanza, send_result=False)
def handle_presence_subscribed(self, stanza):
"""Handle subscribed presence from user
"""
self.__logger.debug("PRESENCE_SUBSCRIBED")
return 1
return []
def handle_presence_unsubscribe(self, stanza):
"""Handle unsubscribe presence from user
"""
self.__logger.debug("PRESENCE_UNSUBSCRIBE")
result = self.apply_registered_behavior(self.presence_unsubscribe_handlers,
stanza)
return result
return self.apply_registered_behavior(self.presence_unsubscribe_handlers,
stanza, send_result=False)
def handle_presence_unsubscribed(self, stanza):
"""Handle unsubscribed presence from user
"""
self.__logger.debug("PRESENCE_UNSUBSCRIBED")
presence = Presence(from_jid=stanza.get_to(),
return [Presence(from_jid=stanza.get_to(),
to_jid=stanza.get_from(),
stanza_type="unavailable")
self.send_stanzas([presence])
return 1
stanza_type="unavailable")]
def handle_message(self, message):
"""Handle new message
Handle password response message
"""
self.__logger.debug("MESSAGE: " + str(message.get_body()))
self.apply_registered_behavior(self.msg_handlers, message)
return 1
return self.apply_registered_behavior(self.msg_handlers, message,
send_result=False)
def handle_command(self, info_query):
"""
@@ -1096,23 +1085,22 @@ class JCLComponent(Component, object):
if action is None:
action = "execute"
try:
result = command.command_manager.apply_command_action(info_query,
return command.command_manager.apply_command_action(info_query,
command_node,
action)
self.send_stanzas(result)
except:
self.__logger.error("Error in command " + str(command_node) +
" with " + str(info_query) + ":",
exc_info=True)
return 1
return []
def handle_vcard(self, info_query):
"""
Handle VCard request
"""
self.__logger.debug("VCard request")
self.apply_registered_behavior(self.vcard_handlers, info_query)
return 1
return self.apply_registered_behavior(self.vcard_handlers, info_query,
send_result=False)
###########################################################################
# Utils

View File

@@ -27,6 +27,7 @@ import traceback
import re
import pyxmpp.error as error
import pyxmpp.jid
from jcl.error import FieldError, MandatoryFieldError, NotWellFormedFieldError
import jcl.jabber as jabber
@@ -68,10 +69,10 @@ class SetRegisterHandler(object):
return self.handle_error(\
MandatoryFieldError("name"),
info_query, lang_class)
if not self.field_name_regexp.match(form["name"].value):
if pyxmpp.jid.node_invalid_re.search(form["name"].value):
return self.handle_error(\
NotWellFormedFieldError("name",
lang_class.arobase_in_name_forbidden),
lang_class.forbidden_char_in_name),
info_query, lang_class)
return None

View File

@@ -248,6 +248,20 @@ class CommandManager_TestCase(unittest.TestCase):
"execute")
self.assertEquals(result, [])
def test_complete_admin_command_action_as_admin(self):
self.command_manager.commands["command1"] = (True,
command.root_node_re)
self.command_manager.apply_execute_command = \
lambda iq, command_name: []
self.command_manager.component = MockComponent()
info_query = Iq(stanza_type="set",
from_jid="admin@test.com",
to_jid="jcl.test.com")
result = self.command_manager.apply_command_action(info_query,
"command1",
"complete")
self.assertEquals(result, [])
def test_apply_admin_command_action_as_admin_fulljid(self):
self.command_manager.commands["command1"] = (True,
command.root_node_re)

View File

@@ -56,6 +56,7 @@ from jcl.tests import JCLTestCase
logger = logging.getLogger()
class MockStream(object):
def __init__(self,
jid="",
@@ -120,6 +121,7 @@ class MockStream(object):
def close(self):
pass
class MockStreamNoConnect(MockStream):
def connect(self):
self.connection_started = True
@@ -127,14 +129,17 @@ class MockStreamNoConnect(MockStream):
def loop_iter(self, timeout):
return
class MockStreamRaiseException(MockStream):
def loop_iter(self, timeout):
raise Exception("in loop error")
class LangExample(Lang):
class en(Lang.en):
type_example_name = "Type Example"
class TestSubscribeHandler(DefaultSubscribeHandler):
def filter(self, message, lang_class):
if re.compile(".*%.*").match(message.get_to().node):
@@ -143,10 +148,12 @@ class TestSubscribeHandler(DefaultSubscribeHandler):
else:
return None
class ErrorHandler(Handler):
def filter(self, stanza, lang_class):
raise Exception("test error")
class TestUnsubscribeHandler(DefaultUnsubscribeHandler):
def filter(self, message, lang_class):
if re.compile(".*%.*").match(message.get_to().node):
@@ -155,6 +162,7 @@ class TestUnsubscribeHandler(DefaultUnsubscribeHandler):
else:
return None
class HandlerMock(object):
def __init__(self):
self.handled = []
@@ -166,6 +174,7 @@ class HandlerMock(object):
self.handled.append((stanza, lang_class, data))
return [(stanza, lang_class, data)]
class JCLComponent_TestCase(JCLTestCase):
def _handle_tick_test_time_handler(self):
self.max_tick_count -= 1
@@ -184,6 +193,7 @@ class JCLComponent_TestCase(JCLTestCase):
self.comp.time_unit = 0
self.saved_time_handler = None
class JCLComponent_constructor_TestCase(JCLComponent_TestCase):
"""Constructor tests"""
@@ -192,6 +202,7 @@ class JCLComponent_constructor_TestCase(JCLComponent_TestCase):
self.assertTrue(Account._connection.tableExists("account"))
model.db_disconnect()
class JCLComponent_apply_registered_behavior_TestCase(JCLComponent_TestCase):
"""apply_registered_behavior tests"""
@@ -200,7 +211,8 @@ class JCLComponent_apply_registered_behavior_TestCase(JCLComponent_TestCase):
self.comp.stream_class = MockStreamNoConnect
message = Message(from_jid="user1@test.com",
to_jid="account11@jcl.test.com")
result = self.comp.apply_registered_behavior([[ErrorHandler(None)]], message)
result = self.comp.apply_registered_behavior([[ErrorHandler(None)]],
message)
self.assertEquals(len(result), 1)
self.assertEquals(result[0].get_type(), "error")
self.assertEquals(len(self.comp.stream.sent), 1)
@@ -272,6 +284,7 @@ class JCLComponent_apply_registered_behavior_TestCase(JCLComponent_TestCase):
self.assertEquals(len(handler1.handled), 1)
self.assertEquals(len(handler2.handled), 0)
class JCLComponent_time_handler_TestCase(JCLComponent_TestCase):
"""time_handler' tests"""
@@ -285,6 +298,7 @@ class JCLComponent_time_handler_TestCase(JCLComponent_TestCase):
self.assertEquals(self.max_tick_count, 0)
self.assertFalse(self.comp.running)
class JCLComponent_authenticated_handler_TestCase(JCLComponent_TestCase):
"""authenticated handler' tests"""
@@ -335,6 +349,7 @@ class JCLComponent_authenticated_handler_TestCase(JCLComponent_TestCase):
self.assertEquals(presence.get_to(), "test2@test.com")
self.assertEquals(presence.get_node().prop("type"), "probe")
class JCLComponent_signal_handler_TestCase(JCLComponent_TestCase):
"""signal_handler' tests"""
@@ -343,6 +358,7 @@ class JCLComponent_signal_handler_TestCase(JCLComponent_TestCase):
self.comp.signal_handler(42, None)
self.assertFalse(self.comp.running)
class JCLComponent_handle_get_gateway_TestCase(JCLComponent_TestCase):
"""handle_get_gateway' tests"""
@@ -352,9 +368,9 @@ class JCLComponent_handle_get_gateway_TestCase(JCLComponent_TestCase):
info_query = Iq(stanza_type="get",
from_jid="user1@test.com")
info_query.new_query("jabber:iq:gateway")
self.comp.handle_get_gateway(info_query)
self.assertEquals(len(self.comp.stream.sent), 1)
iq_sent = self.comp.stream.sent[0]
iqs_sent = self.comp.handle_get_gateway(info_query)
self.assertEquals(len(iqs_sent), 1)
iq_sent = iqs_sent[0]
self.assertEquals(iq_sent.get_to(), "user1@test.com")
self.assertEquals(len(iq_sent.xpath_eval("*/*")), 2)
desc_nodes = iq_sent.xpath_eval("jig:query/jig:desc",
@@ -366,6 +382,7 @@ class JCLComponent_handle_get_gateway_TestCase(JCLComponent_TestCase):
self.assertEquals(len(prompt_nodes), 1)
self.assertEquals(prompt_nodes[0].content, Lang.en.get_gateway_prompt)
class JCLComponent_handle_set_gateway_TestCase(JCLComponent_TestCase):
"""handle_set_gateway' tests"""
@@ -377,9 +394,9 @@ class JCLComponent_handle_set_gateway_TestCase(JCLComponent_TestCase):
query = info_query.new_query("jabber:iq:gateway")
prompt = query.newChild(None, "prompt", None)
prompt.addContent("user@test.com")
self.comp.handle_set_gateway(info_query)
self.assertEquals(len(self.comp.stream.sent), 1)
iq_sent = self.comp.stream.sent[0]
iqs_sent = self.comp.handle_set_gateway(info_query)
self.assertEquals(len(iqs_sent), 1)
iq_sent = iqs_sent[0]
self.assertEquals(iq_sent.get_to(), "user1@test.com")
self.assertEquals(len(iq_sent.xpath_eval("*/*")), 2)
jid_nodes = iq_sent.xpath_eval("jig:query/jig:jid",
@@ -387,6 +404,7 @@ class JCLComponent_handle_set_gateway_TestCase(JCLComponent_TestCase):
self.assertEquals(len(jid_nodes), 1)
self.assertEquals(jid_nodes[0].content, "user%test.com@jcl.test.com")
class JCLComponent_disco_get_info_TestCase(JCLComponent_TestCase):
"""disco_get_info' tests"""
@@ -399,14 +417,16 @@ class JCLComponent_disco_get_info_TestCase(JCLComponent_TestCase):
disco_info = self.comp.disco_get_info(None, info_query)
self.assertEquals(disco_info.get_node(), None)
self.assertEquals(len(self.comp.stream.sent), 0)
self.assertEquals(disco_info.get_identities()[0].get_name(), self.comp.name)
self.assertEquals(disco_info.get_identities()[0].get_name(),
self.comp.name)
self.assertTrue(disco_info.has_feature("jabber:iq:version"))
self.assertTrue(disco_info.has_feature(vcard.VCARD_NS))
self.assertTrue(disco_info.has_feature("jabber:iq:last"))
self.assertTrue(disco_info.has_feature("jabber:iq:register"))
def test_disco_get_info_multiple_account_type(self):
self.comp.account_manager.account_classes = (ExampleAccount, Example2Account)
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
info_query = Iq(stanza_type="get",
@@ -436,7 +456,8 @@ class JCLComponent_disco_get_info_TestCase(JCLComponent_TestCase):
self.assertTrue(disco_info.has_feature("jabber:iq:register"))
def test_disco_get_info_long_node(self):
self.comp.account_manager.account_classes = (ExampleAccount, Example2Account)
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
info_query = Iq(stanza_type="get",
@@ -479,6 +500,7 @@ class JCLComponent_disco_get_info_TestCase(JCLComponent_TestCase):
self.assertEquals(disco_info.get_identities()[0].get_name(),
Lang.en.command_get_disabled_users_num)
class JCLComponent_disco_get_items_TestCase(JCLComponent_TestCase):
"""disco_get_items' tests"""
@@ -512,7 +534,8 @@ class JCLComponent_disco_get_items_TestCase(JCLComponent_TestCase):
self.assertEquals(disco_items, None)
def test_disco_get_items_unknown_node_multiple_account_types(self):
self.comp.account_manager.account_classes = (ExampleAccount, Example2Account)
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
user1 = User(jid="user1@test.com")
account11 = ExampleAccount(user=user1,
name="account11",
@@ -541,7 +564,8 @@ class JCLComponent_disco_get_items_TestCase(JCLComponent_TestCase):
def test_disco_get_items_2types_no_node(self):
"""get_items on main entity. Must account types"""
self.comp.lang = LangExample()
self.comp.account_manager.account_classes = (ExampleAccount, Example2Account)
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
user1 = User(jid="user1@test.com")
@@ -575,7 +599,8 @@ class JCLComponent_disco_get_items_TestCase(JCLComponent_TestCase):
def test_disco_get_items_2types_with_node(self):
"""get_items on the first account type node. Must return account list of
that type for the current user"""
self.comp.account_manager.account_classes = (ExampleAccount, Example2Account)
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
user1 = User(jid="user1@test.com")
@@ -599,14 +624,16 @@ class JCLComponent_disco_get_items_TestCase(JCLComponent_TestCase):
self.assertEquals(disco_items.get_node(), "Example")
self.assertEquals(len(disco_items.get_items()), 1)
disco_item = disco_items.get_items()[0]
self.assertEquals(unicode(disco_item.get_jid()), unicode(account11.jid) + "/Example")
self.assertEquals(unicode(disco_item.get_jid()),
unicode(account11.jid) + "/Example")
self.assertEquals(disco_item.get_node(), "Example/" + account11.name)
self.assertEquals(disco_item.get_name(), account11.long_name)
def test_disco_get_items_2types_with_node2(self):
"""get_items on the second account type node. Must return account list of
that type for the current user"""
self.comp.account_manager.account_classes = (ExampleAccount, Example2Account)
"""get_items on the second account type node. Must return account list
of that type for the current user"""
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
user1 = User(jid="user1@test.com")
@@ -636,7 +663,8 @@ class JCLComponent_disco_get_items_TestCase(JCLComponent_TestCase):
def test_disco_get_items_2types_with_long_node(self):
"""get_items on a first type account. Must return nothing"""
self.comp.account_manager.account_classes = (ExampleAccount, Example2Account)
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
account1 = ExampleAccount(user=User(jid="user1@test.com"),
name="account1",
jid="account1@jcl.test.com")
@@ -648,7 +676,8 @@ class JCLComponent_disco_get_items_TestCase(JCLComponent_TestCase):
def test_disco_get_items_2types_with_long_node2(self):
"""get_items on a second type account. Must return nothing"""
self.comp.account_manager.account_classes = (ExampleAccount, Example2Account)
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
account1 = Example2Account(user=User(jid="user1@test.com"),
name="account1",
jid="account1@jcl.test.com")
@@ -681,16 +710,17 @@ class JCLComponent_disco_get_items_TestCase(JCLComponent_TestCase):
"http://jabber.org/protocol/commands")
self.assertEquals(len(disco_items.get_items()), 22)
class JCLComponent_handle_get_version_TestCase(JCLComponent_TestCase):
"""handle_get_version' tests"""
def test_handle_get_version(self):
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
self.comp.handle_get_version(Iq(stanza_type = "get", \
iqs_sent = self.comp.handle_get_version(Iq(stanza_type = "get", \
from_jid = "user1@test.com"))
self.assertEquals(len(self.comp.stream.sent), 1)
iq_sent = self.comp.stream.sent[0]
self.assertEquals(len(iqs_sent), 1)
iq_sent = iqs_sent[0]
self.assertEquals(iq_sent.get_to(), "user1@test.com")
self.assertEquals(len(iq_sent.xpath_eval("*/*")), 2)
name_nodes = iq_sent.xpath_eval("jiv:query/jiv:name", \
@@ -702,17 +732,18 @@ class JCLComponent_handle_get_version_TestCase(JCLComponent_TestCase):
self.assertEquals(len(version_nodes), 1)
self.assertEquals(version_nodes[0].content, self.comp.version)
class JCLComponent_handle_get_register_TestCase(JCLComponent_TestCase):
"""handle_get_register' tests"""
def test_handle_get_register_new(self):
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
self.comp.handle_get_register(Iq(stanza_type = "get", \
iqs_sent = self.comp.handle_get_register(Iq(stanza_type = "get", \
from_jid = "user1@test.com", \
to_jid = "jcl.test.com"))
self.assertEquals(len(self.comp.stream.sent), 1)
iq_sent = self.comp.stream.sent[0]
self.assertEquals(len(iqs_sent), 1)
iq_sent = iqs_sent[0]
self.assertEquals(iq_sent.get_to(), "user1@test.com")
titles = iq_sent.xpath_eval("jir:query/jxd:x/jxd:title", \
{"jir": "jabber:iq:register", \
@@ -735,9 +766,9 @@ class JCLComponent_handle_get_register_TestCase(JCLComponent_TestCase):
self.assertEquals(fields[0].prop("label"), Lang.en.account_name)
self.assertEquals(fields[0].children.name, "required")
def __check_get_register_new_type(self):
self.assertEquals(len(self.comp.stream.sent), 1)
iq_sent = self.comp.stream.sent[0]
def __check_get_register_new_type(self, iqs_sent):
self.assertEquals(len(iqs_sent), 1)
iq_sent = iqs_sent[0]
self.assertEquals(iq_sent.get_to(), "user1@test.com")
titles = iq_sent.xpath_eval("jir:query/jxd:x/jxd:title", \
{"jir": "jabber:iq:register", \
@@ -798,10 +829,10 @@ class JCLComponent_handle_get_register_TestCase(JCLComponent_TestCase):
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
self.comp.account_manager.account_classes = (ExampleAccount,)
self.comp.handle_get_register(Iq(stanza_type = "get", \
iqs_sent = self.comp.handle_get_register(Iq(stanza_type = "get", \
from_jid = "user1@test.com", \
to_jid = "jcl.test.com"))
self.__check_get_register_new_type()
self.__check_get_register_new_type(iqs_sent)
def test_handle_get_register_exist(self):
self.comp.stream = MockStream()
@@ -818,11 +849,11 @@ class JCLComponent_handle_get_register_TestCase(JCLComponent_TestCase):
name="account21",
jid="account21@jcl.test.com")
model.db_disconnect()
self.comp.handle_get_register(Iq(stanza_type="get",
iqs_sent = self.comp.handle_get_register(Iq(stanza_type="get",
from_jid="user1@test.com",
to_jid="account11@jcl.test.com"))
self.assertEquals(len(self.comp.stream.sent), 1)
iq_sent = self.comp.stream.sent[0]
self.assertEquals(len(iqs_sent), 1)
iq_sent = iqs_sent[0]
self.assertEquals(iq_sent.get_to(), "user1@test.com")
titles = iq_sent.xpath_eval("jir:query/jxd:x/jxd:title",
{"jir": "jabber:iq:register",
@@ -880,11 +911,11 @@ class JCLComponent_handle_get_register_TestCase(JCLComponent_TestCase):
test_enum="choice1",
test_int=21)
model.db_disconnect()
self.comp.handle_get_register(Iq(stanza_type="get",
iqs_sent = self.comp.handle_get_register(Iq(stanza_type="get",
from_jid="user1@test.com",
to_jid="account1@jcl.test.com"))
self.assertEquals(len(self.comp.stream.sent), 1)
iq_sent = self.comp.stream.sent[0]
self.assertEquals(len(iqs_sent), 1)
iq_sent = iqs_sent[0]
self.assertEquals(iq_sent.get_to(), "user1@test.com")
titles = iq_sent.xpath_eval("jir:query/jxd:x/jxd:title",
{"jir": "jabber:iq:register",
@@ -958,21 +989,23 @@ class JCLComponent_handle_get_register_TestCase(JCLComponent_TestCase):
def test_handle_get_register_new_type1(self):
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
self.comp.account_manager.account_classes = (ExampleAccount, Example2Account)
self.comp.handle_get_register(Iq(stanza_type="get",
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
iqs_sent = self.comp.handle_get_register(Iq(stanza_type="get",
from_jid="user1@test.com",
to_jid="jcl.test.com/example"))
self.__check_get_register_new_type()
self.__check_get_register_new_type(iqs_sent)
def test_handle_get_register_new_type2(self):
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
self.comp.account_manager.account_classes = (ExampleAccount, Example2Account)
self.comp.handle_get_register(Iq(stanza_type="get",
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
iqs_sent = self.comp.handle_get_register(Iq(stanza_type="get",
from_jid=JID("user1@test.com"),
to_jid=JID("jcl.test.com/example2")))
self.assertEquals(len(self.comp.stream.sent), 1)
iq_sent = self.comp.stream.sent[0]
self.assertEquals(len(iqs_sent), 1)
iq_sent = iqs_sent[0]
self.assertEquals(iq_sent.get_to(), "user1@test.com")
titles = iq_sent.xpath_eval("jir:query/jxd:x/jxd:title",
{"jir": "jabber:iq:register",
@@ -1014,7 +1047,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
to_jid="jcl.test.com")
query = iq_set.new_query("jabber:iq:register")
x_data.as_xml(query, None)
self.comp.handle_set_register(iq_set)
stanza_sent = self.comp.handle_set_register(iq_set)
model.db_connect()
_account = account.get_account("user1@test.com", "account1")
@@ -1024,7 +1057,6 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
self.assertEquals(_account.jid, "account1@jcl.test.com")
model.db_disconnect()
stanza_sent = self.comp.stream.sent
self.assertEquals(len(stanza_sent), 4)
iq_result = stanza_sent[0]
self.assertTrue(isinstance(iq_result, Iq))
@@ -1075,7 +1107,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
to_jid="jcl.test.com")
query = iq_set.new_query("jabber:iq:register")
x_data.as_xml(query, None)
self.comp.handle_set_register(iq_set)
stanza_sent = self.comp.handle_set_register(iq_set)
model.db_connect()
_account = account.get_account("user1@test.com", "account1")
@@ -1085,7 +1117,6 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
self.assertEquals(_account.jid, "account1@jcl.test.com")
model.db_disconnect()
stanza_sent = self.comp.stream.sent
self.assertEquals(len(stanza_sent), 5)
iq_result = stanza_sent[0]
self.assertTrue(isinstance(iq_result, Iq))
@@ -1139,7 +1170,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
to_jid="jcl.test.com/Example2")
query = iq_set.new_query("jabber:iq:register")
x_data.as_xml(query, None)
self.comp.handle_set_register(iq_set)
stanza_sent = self.comp.handle_set_register(iq_set)
model.db_connect()
_account = account.get_account("user1@test.com", "account1")
@@ -1149,7 +1180,6 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
self.assertEquals(_account.jid, "account1@jcl.test.com")
model.db_disconnect()
stanza_sent = self.comp.stream.sent
self.assertEquals(len(stanza_sent), 4)
iq_result = stanza_sent[0]
self.assertTrue(isinstance(iq_result, Iq))
@@ -1208,7 +1238,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
to_jid="jcl.test.com")
query = iq_set.new_query("jabber:iq:register")
x_data.as_xml(query, None)
self.comp.handle_set_register(iq_set)
stanza_sent = self.comp.handle_set_register(iq_set)
model.db_connect()
_account = account.get_account("user1@test.com", "account1")
@@ -1223,7 +1253,6 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
self.assertEquals(_account.test_int, 43)
model.db_disconnect()
stanza_sent = self.comp.stream.sent
self.assertEquals(len(stanza_sent), 4)
iq_result = stanza_sent[0]
self.assertTrue(isinstance(iq_result, Iq))
@@ -1355,14 +1384,13 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
to_jid="jcl.test.com")
query = iq_set.new_query("jabber:iq:register")
x_data.as_xml(query)
self.comp.handle_set_register(iq_set)
stanza_sent = self.comp.handle_set_register(iq_set)
model.db_connect()
_account = account.get_account("user1@test.com", "account1")
self.assertEquals(_account, None)
model.db_disconnect()
stanza_sent = self.comp.stream.sent
self.assertEquals(len(stanza_sent), 1)
self.assertTrue(isinstance(stanza_sent[0], Iq))
self.assertEquals(stanza_sent[0].get_node().prop("type"), "error")
@@ -1386,14 +1414,13 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
to_jid="jcl.test.com")
query = iq_set.new_query("jabber:iq:register")
x_data.as_xml(query)
self.comp.handle_set_register(iq_set)
stanza_sent = self.comp.handle_set_register(iq_set)
model.db_connect()
_account = account.get_account("user1@test.com", "account1")
self.assertEquals(_account, None)
model.db_disconnect()
stanza_sent = self.comp.stream.sent
self.assertEquals(len(stanza_sent), 1)
self.assertTrue(isinstance(stanza_sent[0], Iq))
self.assertEquals(stanza_sent[0].get_node().prop("type"), "error")
@@ -1416,7 +1443,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
to_jid="account1@jcl.test.com")
query = iq_set.new_query("jabber:iq:register")
x_data.as_xml(query, None)
self.comp.handle_set_register(iq_set)
stanza_sent = self.comp.handle_set_register(iq_set)
model.db_connect()
_account = account.get_account("user1@test.com", "account1")
@@ -1426,7 +1453,6 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
self.assertEquals(_account.jid, "account1@jcl.test.com")
model.db_disconnect()
stanza_sent = self.comp.stream.sent
self.assertEquals(len(stanza_sent), 4)
iq_result = stanza_sent[0]
self.assertTrue(isinstance(iq_result, Iq))
@@ -1504,7 +1530,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
to_jid="account1@jcl.test.com/Example")
query = iq_set.new_query("jabber:iq:register")
x_data.as_xml(query)
self.comp.handle_set_register(iq_set)
stanza_sent = self.comp.handle_set_register(iq_set)
model.db_connect()
_account = account.get_account("user1@test.com", "account1")
@@ -1520,7 +1546,6 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
self.assertEquals(_account.test_int, 43)
model.db_disconnect()
stanza_sent = self.comp.stream.sent
self.assertEquals(len(stanza_sent), 2)
iq_result = stanza_sent[0]
self.assertTrue(isinstance(iq_result, Iq))
@@ -1557,7 +1582,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
to_jid="jcl.test.com")
query = iq_set.new_query("jabber:iq:register")
query.newChild(None, "remove", None)
self.comp.handle_set_register(iq_set)
stanza_sent = self.comp.handle_set_register(iq_set)
model.db_connect()
self.assertEquals(account.get_accounts_count("user1@test.com"),
@@ -1574,7 +1599,6 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase):
self.assertEquals(_account.jid, "account1@jcl.test.com")
model.db_disconnect()
stanza_sent = self.comp.stream.sent
self.assertEquals(len(stanza_sent), 6)
presence = stanza_sent[0]
self.assertTrue(isinstance(presence, Presence))
@@ -1623,11 +1647,10 @@ class JCLComponent_handle_presence_available_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_available(Presence(\
presence_sent = self.comp.handle_presence_available(Presence(\
stanza_type="available",
from_jid="user1@test.com/resource",
to_jid="jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 3)
self.assertEqual(len([presence
for presence in presence_sent
@@ -1686,11 +1709,10 @@ class JCLComponent_handle_presence_available_TestCase(JCLComponent_TestCase):
jid="u21%test.com@jcl.test.com",
account=account2)
model.db_disconnect()
self.comp.handle_presence_available(Presence(\
presence_sent = self.comp.handle_presence_available(Presence(\
stanza_type="available",
from_jid="user1@test.com/resource",
to_jid="jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 7)
self.assertEqual(len([presence
for presence in presence_sent
@@ -1762,11 +1784,10 @@ class JCLComponent_handle_presence_available_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_available(Presence(\
presence_sent = self.comp.handle_presence_available(Presence(\
stanza_type="available",
from_jid="unknown@test.com",
to_jid="jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_available_to_account(self):
@@ -1784,11 +1805,10 @@ class JCLComponent_handle_presence_available_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_available(Presence(\
presence_sent = self.comp.handle_presence_available(Presence(\
stanza_type="available",
from_jid="user1@test.com/resource",
to_jid="account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 1)
self.assertEqual(presence_sent[0].get_to(), "user1@test.com/resource")
self.assertEqual(presence_sent[0].get_from(), "account11@jcl.test.com")
@@ -1811,11 +1831,10 @@ class JCLComponent_handle_presence_available_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_available(Presence(\
presence_sent = self.comp.handle_presence_available(Presence(\
stanza_type="available",
from_jid="user1@test.com/resource",
to_jid="user1%test.com@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 1)
self.assertEqual(presence_sent[0].get_to(), "user1@test.com/resource")
self.assertEqual(presence_sent[0].get_from(),
@@ -1838,11 +1857,10 @@ class JCLComponent_handle_presence_available_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_available(Presence(\
presence_sent = self.comp.handle_presence_available(Presence(\
stanza_type="available",
from_jid="unknown@test.com",
to_jid="account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_available_to_unknown_account(self):
@@ -1860,11 +1878,10 @@ class JCLComponent_handle_presence_available_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_available(Presence(\
presence_sent = self.comp.handle_presence_available(Presence(\
stanza_type="available",
from_jid="user1@test.com",
to_jid="unknown@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_available_to_account_live_password(self):
@@ -1883,11 +1900,10 @@ class JCLComponent_handle_presence_available_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_available(Presence(\
messages_sent = self.comp.handle_presence_available(Presence(\
stanza_type="available",
from_jid="user1@test.com/resource",
to_jid="account11@jcl.test.com"))
messages_sent = self.comp.stream.sent
self.assertEqual(len(messages_sent), 1)
presence = messages_sent[0]
self.assertTrue(presence is not None)
@@ -1912,11 +1928,10 @@ class JCLComponent_handle_presence_available_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_available(Presence(\
messages_sent = self.comp.handle_presence_available(Presence(\
stanza_type="available",
from_jid="user1@test.com/resource",
to_jid="account11@jcl.test.com"))
messages_sent = self.comp.stream.sent
self.assertEqual(len(messages_sent), 2)
password_message = None
presence = None
@@ -1957,11 +1972,10 @@ class JCLComponent_handle_presence_unavailable_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unavailable(Presence(\
presence_sent = self.comp.handle_presence_unavailable(Presence(\
stanza_type="unavailable",
from_jid="user1@test.com/resource",
to_jid="jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 3)
self.assertEqual(len([presence
for presence in presence_sent
@@ -2023,11 +2037,10 @@ class JCLComponent_handle_presence_unavailable_TestCase(JCLComponent_TestCase):
jid="u21%test.com@jcl.test.com",
account=account2)
model.db_disconnect()
self.comp.handle_presence_unavailable(Presence(\
presence_sent = self.comp.handle_presence_unavailable(Presence(\
stanza_type="unavailable",
from_jid="user1@test.com/resource",
to_jid="jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 7)
self.assertEqual(len([presence
for presence in presence_sent
@@ -2099,11 +2112,10 @@ class JCLComponent_handle_presence_unavailable_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unavailable(Presence(\
presence_sent = self.comp.handle_presence_unavailable(Presence(\
stanza_type="unavailable",
from_jid="unknown@test.com",
to_jid="jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_unavailable_to_account(self):
@@ -2121,11 +2133,10 @@ class JCLComponent_handle_presence_unavailable_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unavailable(Presence(\
presence_sent = self.comp.handle_presence_unavailable(Presence(\
stanza_type="unavailable",
from_jid="user1@test.com/resource",
to_jid="account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 1)
self.assertEqual(presence_sent[0].get_to(), "user1@test.com/resource")
self.assertEqual(presence_sent[0].get_from(), "account11@jcl.test.com")
@@ -2149,11 +2160,10 @@ class JCLComponent_handle_presence_unavailable_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unavailable(Presence(\
presence_sent = self.comp.handle_presence_unavailable(Presence(\
stanza_type="unavailable",
from_jid="user1@test.com/resource",
to_jid="user1%test.com@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 1)
self.assertEqual(presence_sent[0].get_to(), "user1@test.com/resource")
self.assertEqual(presence_sent[0].get_from(),
@@ -2177,11 +2187,10 @@ class JCLComponent_handle_presence_unavailable_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unavailable(Presence(\
presence_sent = self.comp.handle_presence_unavailable(Presence(\
stanza_type="unavailable",
from_jid="unknown@test.com",
to_jid="account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_unavailable_to_unknown_account(self):
@@ -2199,11 +2208,10 @@ class JCLComponent_handle_presence_unavailable_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unavailable(Presence(\
presence_sent = self.comp.handle_presence_unavailable(Presence(\
stanza_type="unavailable",
from_jid="user1@test.com",
to_jid="unknown@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
class JCLComponent_handle_presence_subscribe_TestCase(JCLComponent_TestCase):
@@ -2222,11 +2230,10 @@ class JCLComponent_handle_presence_subscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_subscribe(Presence(\
presence_sent = self.comp.handle_presence_subscribe(Presence(\
stanza_type="subscribe",
from_jid="user1@test.com",
to_jid="jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 1)
self.assertEqual(presence_sent[0].get_to(), "user1@test.com")
self.assertEqual(presence_sent[0].get_from(), "jcl.test.com")
@@ -2249,11 +2256,10 @@ class JCLComponent_handle_presence_subscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_subscribe(Presence(\
presence_sent = self.comp.handle_presence_subscribe(Presence(\
stanza_type="subscribe",
from_jid="unknown@test.com",
to_jid="jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_subscribe_to_account(self):
@@ -2271,11 +2277,10 @@ class JCLComponent_handle_presence_subscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_subscribe(Presence(\
presence_sent = self.comp.handle_presence_subscribe(Presence(\
stanza_type="subscribe",
from_jid="user1@test.com",
to_jid="account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 1)
self.assertEqual(presence_sent[0].get_to(), "user1@test.com")
self.assertEqual(presence_sent[0].get_from(), "account11@jcl.test.com")
@@ -2298,11 +2303,10 @@ class JCLComponent_handle_presence_subscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_subscribe(Presence(\
presence_sent = self.comp.handle_presence_subscribe(Presence(\
stanza_type="subscribe",
from_jid="unknown@test.com",
to_jid="account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_subscribe_to_unknown_account(self):
@@ -2320,11 +2324,10 @@ class JCLComponent_handle_presence_subscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_subscribe(Presence(\
presence_sent = self.comp.handle_presence_subscribe(Presence(\
stanza_type="subscribe",
from_jid="user1@test.com",
to_jid="unknown@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_subscribe_to_registered_handlers(self):
@@ -2343,11 +2346,10 @@ class JCLComponent_handle_presence_subscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
result = self.comp.handle_presence_subscribe(Presence(\
sent = self.comp.handle_presence_subscribe(Presence(\
stanza_type="subscribe",
from_jid="user1@test.com",
to_jid="user1%test.com@jcl.test.com"))
sent = self.comp.stream.sent
self.assertEqual(len(sent), 2)
self.assertTrue(type(sent[0]), Presence)
self.assertEquals(sent[0].get_type(), "subscribe")
@@ -2377,11 +2379,10 @@ class JCLComponent_handle_presence_unsubscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unsubscribe(Presence(\
presence_sent = self.comp.handle_presence_unsubscribe(Presence(\
stanza_type="unsubscribe",
from_jid="user1@test.com",
to_jid="account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 2)
presence = presence_sent[0]
self.assertEqual(presence.get_from(), "account11@jcl.test.com")
@@ -2416,11 +2417,10 @@ class JCLComponent_handle_presence_unsubscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unsubscribe(Presence(\
presence_sent = self.comp.handle_presence_unsubscribe(Presence(\
stanza_type="unsubscribe",
from_jid="user1@test.com",
to_jid="account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 2)
presence = presence_sent[0]
self.assertEqual(presence.get_from(), "account11@jcl.test.com")
@@ -2458,11 +2458,10 @@ class JCLComponent_handle_presence_unsubscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unsubscribe(Presence(\
presence_sent = self.comp.handle_presence_unsubscribe(Presence(\
stanza_type="unsubscribe",
from_jid="user1@test.com",
to_jid="jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 6)
presence = presence_sent[0]
self.assertEqual(presence.get_from(), "account11@jcl.test.com")
@@ -2523,11 +2522,10 @@ class JCLComponent_handle_presence_unsubscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unsubscribe(Presence(\
presence_sent = self.comp.handle_presence_unsubscribe(Presence(\
stanza_type="unsubscribe",
from_jid="user1@test.com",
to_jid="user1%test.com@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 2)
presence = presence_sent[0]
self.assertEqual(presence.get_from(), "user1%test.com@jcl.test.com")
@@ -2555,11 +2553,10 @@ class JCLComponent_handle_presence_unsubscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unsubscribe(Presence(\
presence_sent = self.comp.handle_presence_unsubscribe(Presence(\
stanza_type="unsubscribe",
from_jid="unknown@test.com",
to_jid="account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
model.db_connect()
self.assertEquals(account.get_all_accounts_count(),
@@ -2581,11 +2578,10 @@ class JCLComponent_handle_presence_unsubscribe_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_presence_unsubscribe(Presence(\
presence_sent = self.comp.handle_presence_unsubscribe(Presence(\
stanza_type="unsubscribe",
from_jid="user1@test.com",
to_jid="unknown@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
model.db_connect()
self.assertEquals(account.get_all_accounts_count(),
@@ -2596,11 +2592,10 @@ class JCLComponent_handle_presence_unsubscribed_TestCase(JCLComponent_TestCase):
def test_handle_presence_unsubscribed(self):
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
self.comp.handle_presence_unsubscribed(Presence(\
presence_sent = self.comp.handle_presence_unsubscribed(Presence(\
stanza_type = "unsubscribed", \
from_jid = "user1@test.com",\
to_jid = "jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 1)
self.assertEqual(presence_sent[0].get_to(), "user1@test.com")
self.assertEqual(presence_sent[0].get_from(), "jcl.test.com")
@@ -2611,11 +2606,10 @@ class JCLComponent_handle_presence_unsubscribed_TestCase(JCLComponent_TestCase):
def test_handle_presence_unsubscribed_to_registered_handler(self):
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
self.comp.handle_presence_unsubscribed(Presence(\
presence_sent = self.comp.handle_presence_unsubscribed(Presence(\
stanza_type = "unsubscribed", \
from_jid = "user1@test.com",\
to_jid = "user1%test.com@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 1)
self.assertEqual(presence_sent[0].get_to(), "user1@test.com")
self.assertEqual(presence_sent[0].get_from(), "user1%test.com@jcl.test.com")
@@ -2641,12 +2635,11 @@ class JCLComponent_handle_message_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_message(Message(\
messages_sent = self.comp.handle_message(Message(\
from_jid="user1@test.com",
to_jid="account11@jcl.test.com",
subject="[PASSWORD]",
body="secret"))
messages_sent = self.comp.stream.sent
self.assertEqual(len(messages_sent), 0)
def test_handle_message_password_complex(self):
@@ -2666,12 +2659,11 @@ class JCLComponent_handle_message_TestCase(JCLComponent_TestCase):
name="account2",
jid="account2@jcl.test.com")
model.db_disconnect()
self.comp.handle_message(Message(\
messages_sent = self.comp.handle_message(Message(\
from_jid="user1@test.com",
to_jid="account11@jcl.test.com",
subject="[PASSWORD]",
body="secret"))
messages_sent = self.comp.stream.sent
self.assertEqual(len(messages_sent), 1)
self.assertEqual(messages_sent[0].get_to(), "user1@test.com")
self.assertEqual(messages_sent[0].get_from(), "account11@jcl.test.com")
@@ -2955,8 +2947,7 @@ class JCLComponent_handle_command_TestCase(JCLComponent_TestCase):
"command")
command_node.setProp("node", "http://jabber.org/protocol/admin#get-disabled-users-num")
command_node.setProp("action", "execute")
self.comp.handle_command(info_query)
result = self.comp.stream.sent
result = self.comp.handle_command(info_query)
self.assertNotEquals(result, None)
self.assertEquals(len(result), 1)
command_result = result[0].xpath_eval("c:command",

View File

@@ -71,7 +71,7 @@ class SetRegisterHandler_TestCase(JCLTestCase):
error = result[0].get_error()
self.assertEquals(error.get_condition().name, "not-acceptable")
self.assertEquals(error.get_text(), Lang.en.field_error \
% ("name", Lang.en.arobase_in_name_forbidden))
% ("name", Lang.en.forbidden_char_in_name))
def test_handle_invalid_name_with_whitespace(self):
"""Test with invalid supplied name"""
@@ -88,7 +88,7 @@ class SetRegisterHandler_TestCase(JCLTestCase):
error = result[0].get_error()
self.assertEquals(error.get_condition().name, "not-acceptable")
self.assertEquals(error.get_text(), Lang.en.field_error \
% ("name", Lang.en.arobase_in_name_forbidden))
% ("name", Lang.en.forbidden_char_in_name))
def test_handle_invalid_empty_name(self):
"""Test with empty supplied name"""

View File

@@ -92,7 +92,8 @@ class Lang:
field_error = u"Error with '%s' field: %s"
mandatory_field = u"required field"
not_well_formed_field = u"not well formed field"
arobase_in_name_forbidden = u"'@' is not allowed in account's name"
forbidden_char_in_name = u"Account's name contains an invalid character"
not_a_number = u"field must be a number"
field_chat_action = u"Action when state is 'Free For Chat'"
field_online_action = u"Action when state is 'Online'"
@@ -270,7 +271,8 @@ class Lang:
field_error = u"Erreur dans le champs '%s': %s"
mandatory_field = u"%s est requis"
not_well_formed_field = u"Le champs %s n'est pas acceptable"
arobase_in_name_forbidden = u"Le nom du compte ne peux contenir le charactère '@'"
forbidden_char_in_name = u"Le nom du compte contient un charactère invalide"
not_a_number = u"Le champs doit être un nombre"
field_chat_action = u"Action lorsque l'état est 'Free For Chat'"
field_online_action = u"Action lorsque l'état est 'Online'"

View File

@@ -28,6 +28,7 @@ __revision__ = "$Id: account.py,v 1.3 2005/09/18 20:24:07 dax Exp $"
import datetime
import re
import exceptions
from sqlobject.inheritance import InheritableSQLObject
from sqlobject.col import StringCol, IntCol, BoolCol, ForeignKey, DateTimeCol
@@ -64,7 +65,10 @@ def int_post_func(field_value, default_func, bare_from_jid):
"""Return an integer from integer field value"""
if field_value is None or str(field_value) == "":
return int(default_func(bare_from_jid))
try:
return int(field_value)
except exceptions.ValueError:
raise FieldError("TODO", message_property="not_a_number")
def mandatory_field(field_name, field_value):
"""Used as default function for field that must be specified

View File

@@ -182,11 +182,16 @@ class AccountModule_TestCase(JCLTestCase):
"user1@jcl.test.com")
self.assertEquals(result, 42)
def test_int_post_func_default_value(self):
def test_int_post_func_default_value2(self):
result = account.int_post_func(None, lambda bare_from_jid: 42, \
"user1@jcl.test.com")
self.assertEquals(result, 42)
def test_int_post_func_invalid_value(self):
self.assertRaises(FieldError,
account.int_post_func,
"notanint", None, "user1@jcl.test.com")
def test_mandatory_field_empty(self):
self.assertRaises(FieldError,
account.mandatory_field,

View File

@@ -94,7 +94,8 @@ class Language_TestCase(unittest.TestCase):
self.assertNotEquals(self.lang_class.field_error % ("", ""), None)
self.assertNotEquals(self.lang_class.mandatory_field, None)
self.assertNotEquals(self.lang_class.not_well_formed_field, None)
self.assertNotEquals(self.lang_class.arobase_in_name_forbidden, None)
self.assertNotEquals(self.lang_class.forbidden_char_in_name, None)
self.assertNotEquals(self.lang_class.not_a_number, None)
self.assertNotEquals(self.lang_class.field_chat_action, None)
self.assertNotEquals(self.lang_class.field_online_action, None)