Registered Handler exception sent has an error message

darcs-hash:20070530162504-86b55-1f05922de23fc96db715016c3b183dfdcecdb53f.gz
This commit is contained in:
David Rousselie
2007-05-30 18:25:04 +02:00
parent 62cca9bbc9
commit 399109270d
4 changed files with 74 additions and 48 deletions

View File

@@ -259,10 +259,20 @@ class JCLComponent(Component, object):
"""Execute handler if their filter method does not return None""" """Execute handler if their filter method does not return None"""
result = [] result = []
self.db_connect() self.db_connect()
lang = self.lang.get_lang_class_from_node(stanza.get_node())
for handler in handlers: for handler in handlers:
accounts = handler.filter(stanza) try:
if accounts is not None: accounts = handler.filter(stanza, lang)
result += handler.handle(stanza, self.lang, accounts) 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.db_disconnect()
self.send_stanzas(result) self.send_stanzas(result)
return result return result
@@ -1102,18 +1112,18 @@ class AccountManager(object):
result = [] result = []
if _account.in_error == False: if _account.in_error == False:
_account.in_error = True _account.in_error = True
result.append(Message(from_jid = _account.jid, \ result.append(Message(from_jid=_account.jid,
to_jid = _account.user_jid, \ to_jid=_account.user_jid,
stanza_type = "error", \ stanza_type="error",
subject = _account.default_lang_class.check_error_subject, \ subject=_account.default_lang_class.error_subject,
body = _account.default_lang_class.check_error_body \ body=_account.default_lang_class.error_body \
% (exception))) % (exception)))
return result return result
class Handler(object): class Handler(object):
"""handling class""" """handling class"""
def filter(self, stanza): def filter(self, stanza, lang):
"""Filter account to be processed by the handler """Filter account to be processed by the handler
return all accounts. DB connection might already be opened.""" return all accounts. DB connection might already be opened."""
accounts = Account.select() accounts = Account.select()
@@ -1170,7 +1180,7 @@ class PasswordMessageHandler(Handler):
"""Ḧandler constructor""" """Ḧandler constructor"""
self.password_regexp = re.compile("\[PASSWORD\]") 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. """Return the uniq account associated with a name and user JID.
DB connection might already be opened.""" DB connection might already be opened."""
name = stanza.get_to().node name = stanza.get_to().node
@@ -1193,7 +1203,6 @@ class PasswordMessageHandler(Handler):
def handle(self, stanza, lang, accounts): def handle(self, stanza, lang, accounts):
"""Receive password in stanza (must be a Message) for given account""" """Receive password in stanza (must be a Message) for given account"""
_account = accounts[0] _account = accounts[0]
lang_class = lang.get_lang_class_from_node(stanza.get_node())
_account.password = stanza.get_body() _account.password = stanza.get_body()
_account.waiting_password_reply = False _account.waiting_password_reply = False
return [Message(from_jid = _account.jid, \ return [Message(from_jid = _account.jid, \

View File

@@ -127,15 +127,19 @@ class LangExample(Lang):
type_example_name = "Type Example" type_example_name = "Type Example"
class TestSubscribeHandler(DefaultSubscribeHandler): class TestSubscribeHandler(DefaultSubscribeHandler):
def filter(self, message): def filter(self, message, lang):
if re.compile(".*%.*").match(message.get_to().node): if re.compile(".*%.*").match(message.get_to().node):
# return no account because self.handle does not need an account # return no account because self.handle does not need an account
return [] return []
else: else:
return None return None
class ErrorHandler(Handler):
def filter(self, stanza, lang):
raise Exception("test error")
class TestUnsubscribeHandler(DefaultUnsubscribeHandler): class TestUnsubscribeHandler(DefaultUnsubscribeHandler):
def filter(self, message): def filter(self, message, lang):
if re.compile(".*%.*").match(message.get_to().node): if re.compile(".*%.*").match(message.get_to().node):
# return no account because self.handle does not need an account # return no account because self.handle does not need an account
return [] return []
@@ -182,6 +186,19 @@ class JCLComponent_TestCase(unittest.TestCase):
del account.hub.threadConnection 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 # 'run' tests
########################################################################### ###########################################################################
def __comp_run(self): 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_to(), _account.user_jid)
self.assertEqual(error_sent.get_from(), _account.jid) self.assertEqual(error_sent.get_from(), _account.jid)
self.assertEqual(error_sent.get_type(), "error") 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_subject(), _account.default_lang_class.error_subject)
self.assertEqual(error_sent.get_body(), _account.default_lang_class.check_error_body \ self.assertEqual(error_sent.get_body(), _account.default_lang_class.error_body \
% (exception)) % (exception))
del account.hub.threadConnection del account.hub.threadConnection
@@ -2150,7 +2167,7 @@ class Handler_TestCase(unittest.TestCase):
account12 = Account(user_jid = "user1@test.com", \ account12 = Account(user_jid = "user1@test.com", \
name = "account12", \ name = "account12", \
jid = "account12@jcl.test.com") jid = "account12@jcl.test.com")
accounts = self.handler.filter(None) accounts = self.handler.filter(None, None)
self.assertEquals(accounts.count(), 2) self.assertEquals(accounts.count(), 2)
del account.hub.threadConnection del account.hub.threadConnection
@@ -2250,7 +2267,7 @@ class PasswordMessageHandler_TestCase(unittest.TestCase):
to_jid = "account11@jcl.test.com", \ to_jid = "account11@jcl.test.com", \
subject = "[PASSWORD]", \ subject = "[PASSWORD]", \
body = "secret") body = "secret")
accounts = self.handler.filter(message) accounts = self.handler.filter(message, None)
self.assertEquals(accounts.count(), 1) self.assertEquals(accounts.count(), 1)
self.assertEquals(accounts[0].name, "account11") self.assertEquals(accounts[0].name, "account11")
del account.hub.threadConnection del account.hub.threadConnection
@@ -2268,7 +2285,7 @@ class PasswordMessageHandler_TestCase(unittest.TestCase):
to_jid = "account11@jcl.test.com", \ to_jid = "account11@jcl.test.com", \
subject = "[PASSWORD]", \ subject = "[PASSWORD]", \
body = "secret") body = "secret")
accounts = self.handler.filter(message) accounts = self.handler.filter(message, None)
self.assertEquals(accounts, None) self.assertEquals(accounts, None)
del account.hub.threadConnection del account.hub.threadConnection
@@ -2285,39 +2302,39 @@ class PasswordMessageHandler_TestCase(unittest.TestCase):
to_jid = "account11@jcl.test.com", \ to_jid = "account11@jcl.test.com", \
subject = "[NOT GOOD MESSAGE]", \ subject = "[NOT GOOD MESSAGE]", \
body = "secret") body = "secret")
accounts = self.handler.filter(message) accounts = self.handler.filter(message, None)
self.assertEquals(accounts, None) self.assertEquals(accounts, None)
del account.hub.threadConnection del account.hub.threadConnection
def test_filter_not_password_account(self): def test_filter_not_password_account(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
account11 = Account(user_jid = "user1@test.com", \ account11 = Account(user_jid="user1@test.com",
name = "account11", \ name="account11",
jid = "account11@jcl.test.com") jid="account11@jcl.test.com")
account12 = Account(user_jid = "user1@test.com", \ account12 = Account(user_jid="user1@test.com",
name = "account12", \ name="account12",
jid = "account12@jcl.test.com") jid="account12@jcl.test.com")
message = Message(from_jid = "user1@test.com", \ message = Message(from_jid="user1@test.com",
to_jid = "account11@jcl.test.com", \ to_jid="account11@jcl.test.com",
subject = "[PASSWORD]", \ subject="[PASSWORD]",
body = "secret") body="secret")
accounts = self.handler.filter(message) accounts = self.handler.filter(message, None)
self.assertEquals(accounts, None) self.assertEquals(accounts, None)
del account.hub.threadConnection del account.hub.threadConnection
def test_handle(self): def test_handle(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
account11 = ExampleAccount(user_jid = "user1@test.com", \ account11 = ExampleAccount(user_jid="user1@test.com",
name = "account11", \ name="account11",
jid = "account11@jcl.test.com") jid="account11@jcl.test.com")
account12 = ExampleAccount(user_jid = "user1@test.com", \ account12 = ExampleAccount(user_jid="user1@test.com",
name = "account12", \ name="account12",
jid = "account12@jcl.test.com") jid="account12@jcl.test.com")
message = Message(from_jid = "user1@test.com", \ message = Message(from_jid="user1@test.com",
to_jid = "account11@jcl.test.com", \ to_jid="account11@jcl.test.com",
subject = "[PASSWORD]", \ subject="[PASSWORD]",
body = "secret") body="secret")
messages = self.handler.handle(message, Lang(), [account11]) messages = self.handler.handle(message, Lang.en, [account11])
self.assertEquals(len(messages), 1) self.assertEquals(len(messages), 1)
self.assertEquals(account11.password, "secret") self.assertEquals(account11.password, "secret")
del account.hub.threadConnection del account.hub.threadConnection

View File

@@ -104,8 +104,8 @@ class Lang:
field_dnd_action_0 = field_action_0 field_dnd_action_0 = field_action_0
field_offline_action_0 = field_action_0 field_offline_action_0 = field_action_0
check_error_subject = u"Error" error_subject = u"Error"
check_error_body = u"An error has occured:\n\t%s" error_body = u"An error has occured:\n\t%s"
class fr: class fr:
component_name = u"composant générique Jabber Component Library" component_name = u"composant générique Jabber Component Library"
@@ -142,8 +142,8 @@ class Lang:
field_dnd_action_0 = field_action_0 field_dnd_action_0 = field_action_0
field_offline_action_0 = field_action_0 field_offline_action_0 = field_action_0
check_error_subject = u"Erreur" error_subject = u"Erreur"
check_error_body = u"Une erreur est survenue :\n\t%s" error_body = u"Une erreur est survenue :\n\t%s"
class nl: class nl:
# TODO: when finish, delete this line and uncomment in tests/lang.py the makeSuite(Language_nl_TestCase, 'test') line # TODO: when finish, delete this line and uncomment in tests/lang.py the makeSuite(Language_nl_TestCase, 'test') line

View File

@@ -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_dnd_action_0, None)
self.assertNotEquals(self.lang_class.field_offline_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.error_subject, None)
self.assertNotEquals(self.lang_class.check_error_body % (""), None) self.assertNotEquals(self.lang_class.error_body % (""), None)
class Language_fr_TestCase(Language_TestCase): class Language_fr_TestCase(Language_TestCase):