Registered Handler exception sent has an error message
darcs-hash:20070530162504-86b55-1f05922de23fc96db715016c3b183dfdcecdb53f.gz
This commit is contained in:
@@ -259,10 +259,20 @@ class JCLComponent(Component, object):
|
||||
"""Execute handler if their filter method does not return None"""
|
||||
result = []
|
||||
self.db_connect()
|
||||
lang = self.lang.get_lang_class_from_node(stanza.get_node())
|
||||
for handler in handlers:
|
||||
accounts = handler.filter(stanza)
|
||||
if accounts is not None:
|
||||
result += handler.handle(stanza, self.lang, accounts)
|
||||
try:
|
||||
accounts = handler.filter(stanza, lang)
|
||||
if accounts is not None:
|
||||
result += handler.handle(stanza, lang, accounts)
|
||||
except Exception, e:
|
||||
self.__logger.error("Error with handler " + str(handler) +
|
||||
" with " + str(stanza))
|
||||
result += [Message(from_jid=stanza.get_to(),
|
||||
to_jid=stanza.get_from(),
|
||||
stanza_type="error",
|
||||
subject=lang.error_subject,
|
||||
body=lang.error_body % (e.message))]
|
||||
self.db_disconnect()
|
||||
self.send_stanzas(result)
|
||||
return result
|
||||
@@ -1102,18 +1112,18 @@ class AccountManager(object):
|
||||
result = []
|
||||
if _account.in_error == False:
|
||||
_account.in_error = True
|
||||
result.append(Message(from_jid = _account.jid, \
|
||||
to_jid = _account.user_jid, \
|
||||
stanza_type = "error", \
|
||||
subject = _account.default_lang_class.check_error_subject, \
|
||||
body = _account.default_lang_class.check_error_body \
|
||||
% (exception)))
|
||||
result.append(Message(from_jid=_account.jid,
|
||||
to_jid=_account.user_jid,
|
||||
stanza_type="error",
|
||||
subject=_account.default_lang_class.error_subject,
|
||||
body=_account.default_lang_class.error_body \
|
||||
% (exception)))
|
||||
return result
|
||||
|
||||
class Handler(object):
|
||||
"""handling class"""
|
||||
|
||||
def filter(self, stanza):
|
||||
def filter(self, stanza, lang):
|
||||
"""Filter account to be processed by the handler
|
||||
return all accounts. DB connection might already be opened."""
|
||||
accounts = Account.select()
|
||||
@@ -1170,7 +1180,7 @@ class PasswordMessageHandler(Handler):
|
||||
"""Ḧandler constructor"""
|
||||
self.password_regexp = re.compile("\[PASSWORD\]")
|
||||
|
||||
def filter(self, stanza):
|
||||
def filter(self, stanza, lang):
|
||||
"""Return the uniq account associated with a name and user JID.
|
||||
DB connection might already be opened."""
|
||||
name = stanza.get_to().node
|
||||
@@ -1193,7 +1203,6 @@ class PasswordMessageHandler(Handler):
|
||||
def handle(self, stanza, lang, accounts):
|
||||
"""Receive password in stanza (must be a Message) for given account"""
|
||||
_account = accounts[0]
|
||||
lang_class = lang.get_lang_class_from_node(stanza.get_node())
|
||||
_account.password = stanza.get_body()
|
||||
_account.waiting_password_reply = False
|
||||
return [Message(from_jid = _account.jid, \
|
||||
|
||||
@@ -127,15 +127,19 @@ class LangExample(Lang):
|
||||
type_example_name = "Type Example"
|
||||
|
||||
class TestSubscribeHandler(DefaultSubscribeHandler):
|
||||
def filter(self, message):
|
||||
def filter(self, message, lang):
|
||||
if re.compile(".*%.*").match(message.get_to().node):
|
||||
# return no account because self.handle does not need an account
|
||||
return []
|
||||
else:
|
||||
return None
|
||||
|
||||
class ErrorHandler(Handler):
|
||||
def filter(self, stanza, lang):
|
||||
raise Exception("test error")
|
||||
|
||||
class TestUnsubscribeHandler(DefaultUnsubscribeHandler):
|
||||
def filter(self, message):
|
||||
def filter(self, message, lang):
|
||||
if re.compile(".*%.*").match(message.get_to().node):
|
||||
# return no account because self.handle does not need an account
|
||||
return []
|
||||
@@ -182,6 +186,19 @@ class JCLComponent_TestCase(unittest.TestCase):
|
||||
del account.hub.threadConnection
|
||||
|
||||
###########################################################################
|
||||
# apply_registered_behavior tests
|
||||
###########################################################################
|
||||
def test_apply_registered_behavior(self):
|
||||
self.comp.stream = MockStreamNoConnect()
|
||||
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()], message)
|
||||
self.assertEquals(len(result), 1)
|
||||
self.assertEquals(result[0].get_type(), "error")
|
||||
self.assertEquals(len(self.comp.stream.sent), 1)
|
||||
self.assertEquals(result[0], self.comp.stream.sent[0])
|
||||
###########################################################################
|
||||
# 'run' tests
|
||||
###########################################################################
|
||||
def __comp_run(self):
|
||||
@@ -2091,8 +2108,8 @@ class JCLComponent_TestCase(unittest.TestCase):
|
||||
self.assertEqual(error_sent.get_to(), _account.user_jid)
|
||||
self.assertEqual(error_sent.get_from(), _account.jid)
|
||||
self.assertEqual(error_sent.get_type(), "error")
|
||||
self.assertEqual(error_sent.get_subject(), _account.default_lang_class.check_error_subject)
|
||||
self.assertEqual(error_sent.get_body(), _account.default_lang_class.check_error_body \
|
||||
self.assertEqual(error_sent.get_subject(), _account.default_lang_class.error_subject)
|
||||
self.assertEqual(error_sent.get_body(), _account.default_lang_class.error_body \
|
||||
% (exception))
|
||||
del account.hub.threadConnection
|
||||
|
||||
@@ -2150,7 +2167,7 @@ class Handler_TestCase(unittest.TestCase):
|
||||
account12 = Account(user_jid = "user1@test.com", \
|
||||
name = "account12", \
|
||||
jid = "account12@jcl.test.com")
|
||||
accounts = self.handler.filter(None)
|
||||
accounts = self.handler.filter(None, None)
|
||||
self.assertEquals(accounts.count(), 2)
|
||||
del account.hub.threadConnection
|
||||
|
||||
@@ -2250,7 +2267,7 @@ class PasswordMessageHandler_TestCase(unittest.TestCase):
|
||||
to_jid = "account11@jcl.test.com", \
|
||||
subject = "[PASSWORD]", \
|
||||
body = "secret")
|
||||
accounts = self.handler.filter(message)
|
||||
accounts = self.handler.filter(message, None)
|
||||
self.assertEquals(accounts.count(), 1)
|
||||
self.assertEquals(accounts[0].name, "account11")
|
||||
del account.hub.threadConnection
|
||||
@@ -2268,7 +2285,7 @@ class PasswordMessageHandler_TestCase(unittest.TestCase):
|
||||
to_jid = "account11@jcl.test.com", \
|
||||
subject = "[PASSWORD]", \
|
||||
body = "secret")
|
||||
accounts = self.handler.filter(message)
|
||||
accounts = self.handler.filter(message, None)
|
||||
self.assertEquals(accounts, None)
|
||||
del account.hub.threadConnection
|
||||
|
||||
@@ -2285,39 +2302,39 @@ class PasswordMessageHandler_TestCase(unittest.TestCase):
|
||||
to_jid = "account11@jcl.test.com", \
|
||||
subject = "[NOT GOOD MESSAGE]", \
|
||||
body = "secret")
|
||||
accounts = self.handler.filter(message)
|
||||
accounts = self.handler.filter(message, None)
|
||||
self.assertEquals(accounts, None)
|
||||
del account.hub.threadConnection
|
||||
|
||||
def test_filter_not_password_account(self):
|
||||
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
|
||||
account11 = Account(user_jid = "user1@test.com", \
|
||||
name = "account11", \
|
||||
jid = "account11@jcl.test.com")
|
||||
account12 = Account(user_jid = "user1@test.com", \
|
||||
name = "account12", \
|
||||
jid = "account12@jcl.test.com")
|
||||
message = Message(from_jid = "user1@test.com", \
|
||||
to_jid = "account11@jcl.test.com", \
|
||||
subject = "[PASSWORD]", \
|
||||
body = "secret")
|
||||
accounts = self.handler.filter(message)
|
||||
account11 = Account(user_jid="user1@test.com",
|
||||
name="account11",
|
||||
jid="account11@jcl.test.com")
|
||||
account12 = Account(user_jid="user1@test.com",
|
||||
name="account12",
|
||||
jid="account12@jcl.test.com")
|
||||
message = Message(from_jid="user1@test.com",
|
||||
to_jid="account11@jcl.test.com",
|
||||
subject="[PASSWORD]",
|
||||
body="secret")
|
||||
accounts = self.handler.filter(message, None)
|
||||
self.assertEquals(accounts, None)
|
||||
del account.hub.threadConnection
|
||||
|
||||
def test_handle(self):
|
||||
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
|
||||
account11 = ExampleAccount(user_jid = "user1@test.com", \
|
||||
name = "account11", \
|
||||
jid = "account11@jcl.test.com")
|
||||
account12 = ExampleAccount(user_jid = "user1@test.com", \
|
||||
name = "account12", \
|
||||
jid = "account12@jcl.test.com")
|
||||
message = Message(from_jid = "user1@test.com", \
|
||||
to_jid = "account11@jcl.test.com", \
|
||||
subject = "[PASSWORD]", \
|
||||
body = "secret")
|
||||
messages = self.handler.handle(message, Lang(), [account11])
|
||||
account11 = ExampleAccount(user_jid="user1@test.com",
|
||||
name="account11",
|
||||
jid="account11@jcl.test.com")
|
||||
account12 = ExampleAccount(user_jid="user1@test.com",
|
||||
name="account12",
|
||||
jid="account12@jcl.test.com")
|
||||
message = Message(from_jid="user1@test.com",
|
||||
to_jid="account11@jcl.test.com",
|
||||
subject="[PASSWORD]",
|
||||
body="secret")
|
||||
messages = self.handler.handle(message, Lang.en, [account11])
|
||||
self.assertEquals(len(messages), 1)
|
||||
self.assertEquals(account11.password, "secret")
|
||||
del account.hub.threadConnection
|
||||
|
||||
@@ -104,8 +104,8 @@ class Lang:
|
||||
field_dnd_action_0 = field_action_0
|
||||
field_offline_action_0 = field_action_0
|
||||
|
||||
check_error_subject = u"Error"
|
||||
check_error_body = u"An error has occured:\n\t%s"
|
||||
error_subject = u"Error"
|
||||
error_body = u"An error has occured:\n\t%s"
|
||||
|
||||
class fr:
|
||||
component_name = u"composant générique Jabber Component Library"
|
||||
@@ -142,8 +142,8 @@ class Lang:
|
||||
field_dnd_action_0 = field_action_0
|
||||
field_offline_action_0 = field_action_0
|
||||
|
||||
check_error_subject = u"Erreur"
|
||||
check_error_body = u"Une erreur est survenue :\n\t%s"
|
||||
error_subject = u"Erreur"
|
||||
error_body = u"Une erreur est survenue :\n\t%s"
|
||||
|
||||
class nl:
|
||||
# TODO: when finish, delete this line and uncomment in tests/lang.py the makeSuite(Language_nl_TestCase, 'test') line
|
||||
|
||||
@@ -108,8 +108,8 @@ class Language_TestCase(unittest.TestCase):
|
||||
self.assertNotEquals(self.lang_class.field_dnd_action_0, None)
|
||||
self.assertNotEquals(self.lang_class.field_offline_action_0, None)
|
||||
|
||||
self.assertNotEquals(self.lang_class.check_error_subject, None)
|
||||
self.assertNotEquals(self.lang_class.check_error_body % (""), None)
|
||||
self.assertNotEquals(self.lang_class.error_subject, None)
|
||||
self.assertNotEquals(self.lang_class.error_body % (""), None)
|
||||
|
||||
|
||||
class Language_fr_TestCase(Language_TestCase):
|
||||
|
||||
Reference in New Issue
Block a user