From 510b52bf3d19e9fde171ebec1bd9625b6b0fc806 Mon Sep 17 00:00:00 2001 From: David Rousselie Date: Wed, 18 Oct 2006 19:49:30 +0200 Subject: [PATCH] More tests darcs-hash:20061018174930-86b55-29433ceb46b2c1f7decade7ff062d670a119400d.gz --- run_tests.py | 4 +- src/jcl/jabber/component.py | 68 +++++----- src/jcl/model/account.py | 30 +---- tests/jcl/jabber/test_component.py | 195 ++++++++++++++++++++++++++--- 4 files changed, 220 insertions(+), 77 deletions(-) diff --git a/run_tests.py b/run_tests.py index 20b0b96..849fa1d 100644 --- a/run_tests.py +++ b/run_tests.py @@ -50,8 +50,8 @@ if __name__ == '__main__': feeder_suite = unittest.makeSuite(Feeder_TestCase, "test") sender_suite = unittest.makeSuite(Sender_TestCase, "test") jcl_suite = unittest.TestSuite() -# jcl_suite.addTest(FeederComponent_TestCase('test_handle_tick')) -# jcl_suite.addTest(FeederComponent_TestCase('test_handle_presence_unavailable_to_account')) +# jcl_suite.addTest(FeederComponent_TestCase('test_handle_get_version')) +# jcl_suite.addTest(FeederComponent_TestCase('test_handle_presence_available_to_account_live_password')) # jcl_suite = unittest.TestSuite((feeder_component_suite)) # jcl_suite = unittest.TestSuite((component_suite)) jcl_suite = unittest.TestSuite((component_suite, diff --git a/src/jcl/jabber/component.py b/src/jcl/jabber/component.py index 8ad1735..9767b70 100644 --- a/src/jcl/jabber/component.py +++ b/src/jcl/jabber/component.py @@ -434,19 +434,25 @@ class JCLComponent(Component): ## from_jid = stanza.get_from() ## base_from_jid = unicode(from_jid.bare()) # TODO : send presence available to subscribed user + # is it necessary to send available presence ? return 1 def handle_presence_unsubscribe(self, stanza): """Handle unsubscribe presence from user """ self.__logger.debug("PRESENCE_UNSUBSCRIBE") -## name = stanza.get_to().node + name = stanza.get_to().node from_jid = stanza.get_from() -## base_from_jid = unicode(from_jid.bare()) - # TODO : delete from account base + base_from_jid = unicode(from_jid.bare()) presence = Presence(from_jid = stanza.get_to(), to_jid = from_jid, \ stanza_type = "unsubscribe") self.stream.send(presence) + self.db_connect() + for _account in self.account_class.select(\ + self.account_class.q.user_jid == base_from_jid \ + and self.account_class.q.name == name): + _account.destroySelf() + self.db_disconnect() presence = stanza.make_accept_response() self.stream.send(presence) return 1 @@ -463,16 +469,20 @@ class JCLComponent(Component): def handle_message(self, message): """Handle new message + Handle password response message """ self.__logger.debug("MESSAGE: " + message.get_body()) lang_class = self.__lang.get_lang_class_from_node(message.get_node()) -## name = message.get_to().node -## base_from_jid = unicode(message.get_from().bare()) + name = message.get_to().node + base_from_jid = unicode(message.get_from().bare()) + self.db_connect() + accounts = self.account_class.select(\ + self.account_class.q.user_jid == base_from_jid \ + and self.account_class.q.name == name) if re.compile("\[PASSWORD\]").search(message.get_subject()) \ - is not None: -## TODO and self.__storage.has_key((base_from_jid, name)): -## account = self.__storage[(base_from_jid, name)] - account = Account() + is not None \ + and accounts.count() == 1: + account = list(accounts)[0] account.password = message.get_body() account.waiting_password_reply = False msg = Message(from_jid = account.jid, \ @@ -481,42 +491,44 @@ class JCLComponent(Component): subject = lang_class.password_saved_for_session, \ body = lang_class.password_saved_for_session) self.stream.send(msg) + self.db_disconnect() return 1 ########################################################################### # Utils ########################################################################### - def _send_presence_available(self, account, show, lang_class): + def _send_presence_available(self, _account, show, lang_class): """Send available presence to account's user and ask for password if necessary""" - account.default_lang_class = lang_class - old_status = account.status + _account.default_lang_class = lang_class + old_status = _account.status if show is None: - account.status = account.ONLINE # TODO get real status = (not show) + _account.status = account.ONLINE # TODO get real status = (not show) else: - account.status = show - p = Presence(from_jid = account.jid, \ - to_jid = account.user_jid, \ - status = account.status_msg, \ + _account.status = show + p = Presence(from_jid = _account.jid, \ + to_jid = _account.user_jid, \ + status = _account.status_msg, \ show = show, \ stanza_type = "available") self.stream.send(p) - if account.store_password == False \ - and old_status == account.OFFLINE: - self._ask_password(account, lang_class) + if _account.store_password == False \ + and old_status == account.OFFLINE \ + and _account.password == None : + self._ask_password(_account, lang_class) - def _ask_password(self, account, lang_class): + def _ask_password(self, _account, lang_class): """Send a Jabber message to ask for account password """ - if not account.waiting_password_reply \ - and account.status != "offline": - account.waiting_password_reply = True - msg = Message(from_jid = account.jid, \ - to_jid = account.user_jid, \ + if not _account.waiting_password_reply \ + and _account.status != account.OFFLINE: + _account.waiting_password_reply = True + msg = Message(from_jid = _account.jid, \ + to_jid = _account.user_jid, \ stanza_type = "normal", \ subject = u"[PASSWORD] " + \ - lang_class.ask_password_subject, \ - body = lang_class.ask_password_body)# % \ + lang_class.ask_password_subject)#, \ +## body = lang_class.ask_password_body)# % \ ## (account.host, account.login)) self.stream.send(msg) diff --git a/src/jcl/model/account.py b/src/jcl/model/account.py index af9fda2..a6c6630 100644 --- a/src/jcl/model/account.py +++ b/src/jcl/model/account.py @@ -46,14 +46,13 @@ class Account(SQLObject): name = StringCol() jid = StringCol() login = StringCol(default = "") - __password = StringCol(default = "") - __store_password = BoolCol(default = True) + password = StringCol(default = None) + store_password = BoolCol(default = True) default_lang_class = Lang.en first_check = True __status = OFFLINE waiting_password_reply = False - __volatile_password = "" def get_long_name(self): """Return Human readable account name""" @@ -83,28 +82,3 @@ class Account(SQLObject): status = property(get_status, set_status) - def set_password(self, password): - """Set password associated to account""" - self.__password = password - - def get_password(self): - """Return password associated to account""" - return self.__password - - password = property(get_password, set_password) - - def set_store_password(self, store_password): - """Set store_password attribut - determine if password is persitent or not""" - if store_password: - self.password = property(get_password, set_password) - else: - self.password = property(get_volatile_password, \ - set_volatile_password) - self.__store_password = store_password - - def get_store_password(self): - """Return store_password boolean""" - return self.__store_password - - store_password = property(get_store_password, set_store_password) diff --git a/tests/jcl/jabber/test_component.py b/tests/jcl/jabber/test_component.py index ebad6a2..8e2f760 100644 --- a/tests/jcl/jabber/test_component.py +++ b/tests/jcl/jabber/test_component.py @@ -4,18 +4,18 @@ ## Login : David Rousselie ## Started on Wed Aug 9 21:34:26 2006 David Rousselie ## $Id$ -## +## ## Copyright (C) 2006 David Rousselie ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. -## +## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. -## +## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA @@ -35,6 +35,7 @@ from sqlobject.dbconnection import TheURIOpener from pyxmpp.iq import Iq from pyxmpp.stanza import Stanza from pyxmpp.presence import Presence +from pyxmpp.message import Message from jcl.jabber.component import JCLComponent from jcl.model import account @@ -56,7 +57,7 @@ class MockStream(object): self.connection_stopped = False self.eof = False self.socket = [] - + def send(self, iq): self.sent.append(iq) @@ -107,7 +108,7 @@ class MockStreamNoConnect(MockStream): def connect(self): self.connection_started = True self.eof = True - + class JCLComponent_TestCase(unittest.TestCase): def setUp(self): if os.path.exists(DB_PATH): @@ -119,7 +120,7 @@ class JCLComponent_TestCase(unittest.TestCase): 'sqlite://' + DB_URL) self.max_tick_count = 2 self.saved_time_handler = None - + def tearDown(self): account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) Account.dropTable(ifExists = True) @@ -147,7 +148,7 @@ class JCLComponent_TestCase(unittest.TestCase): except: # Ignore exception, might be obtain from self.comp.queue pass - + def test_run(self): self.comp.time_unit = 1 # Do not loop, handle_tick is virtual @@ -190,7 +191,7 @@ class JCLComponent_TestCase(unittest.TestCase): self.max_tick_count -= 1 if self.max_tick_count == 0: self.comp.running = False - + def test_time_handler(self): self.comp.time_unit = 1 self.max_tick_count = 2 @@ -207,7 +208,7 @@ class JCLComponent_TestCase(unittest.TestCase): self.assertTrue(True) def test_authenticated_send_probe(self): - account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) + account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account11 = Account(user_jid = "test1@test.com", \ name = "test11", \ jid = "account11@jcl.test.com") @@ -276,7 +277,7 @@ class JCLComponent_TestCase(unittest.TestCase): from_jid = "user1@test.com") disco_items = self.comp.disco_get_items("account1", info_query) self.assertEquals(disco_items.get_items(), []) - + def test_get_reg_form(self): ## self.comp.get_reg_form(Lang.en, Account) # TODO @@ -320,7 +321,7 @@ class JCLComponent_TestCase(unittest.TestCase): self.assertEquals(iq_sent.get_to(), "user1@test.com") # self.assertEquals(len(iq_sent.xpath_eval("*/*")), 1) # TODO - + def test_handle_get_register_exist(self): pass @@ -398,7 +399,51 @@ class JCLComponent_TestCase(unittest.TestCase): self.assertEqual(presence_sent[0].get_to(), "user1@test.com") self.assertEqual(presence_sent[0].get_from(), "account11@jcl.test.com") self.assertEqual(presence_sent[0].get_show(), "online") + + def test_handle_presence_available_to_account_live_password(self): + self.comp.stream = MockStream() + self.comp.stream_class = MockStream + account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) + account11 = Account(user_jid = "user1@test.com", \ + name = "account11", \ + jid = "account11@jcl.test.com") + account11.store_password = False + account12 = Account(user_jid = "user1@test.com", \ + name = "account12", \ + jid = "account12@jcl.test.com") + account2 = Account(user_jid = "user2@test.com", \ + name = "account2", \ + jid = "account2@jcl.test.com") + del account.hub.threadConnection +## TODO: "online" exist ? + self.comp.handle_presence_available(Presence(\ + stanza_type = "available", \ + show = "online", \ + from_jid = "user1@test.com",\ + to_jid = "account11@jcl.test.com")) + messages_sent = self.comp.stream.sent + self.assertEqual(len(messages_sent), 2) + password_message = None + presence = None + for message in messages_sent: + if isinstance(message, Message): + password_message = message + elif isinstance(message, Presence): + presence = message + self.assertTrue(password_message is not None) + self.assertTrue(presence is not None) + self.assertEqual(presence.get_show(), "online") + self.assertEqual(presence.get_from_jid(), "account11@jcl.test.com") + self.assertEqual(presence.get_to_jid(), "user1@test.com") + self.assertEqual(unicode(password_message.get_from_jid()), \ + "account11@jcl.test.com") + self.assertEqual(unicode(password_message.get_to_jid()), \ + "user1@test.com") + self.assertEqual(password_message.get_subject(), \ + "[PASSWORD] Password request") + self.assertEqual(password_message.get_body(), None) + def test_handle_presence_unavailable_to_component(self): self.comp.stream = MockStream() self.comp.stream_class = MockStream @@ -447,7 +492,7 @@ class JCLComponent_TestCase(unittest.TestCase): and presence.xpath_eval("@type")[0].get_content() \ == "unavailable"]), \ 1) - + def test_handle_presence_unavailable_to_account(self): self.comp.stream = MockStream() @@ -474,22 +519,134 @@ class JCLComponent_TestCase(unittest.TestCase): self.assertEqual(\ presence_sent[0].xpath_eval("@type")[0].get_content(), \ "unavailable") - - def test_handle_presence_subscribe(self): - pass + + def test_handle_presence_subscribe_to_component(self): + self.comp.stream = MockStream() + self.comp.stream_class = MockStream + 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") + self.assertEqual(\ + presence_sent[0].xpath_eval("@type")[0].get_content(), \ + "subscribed") + + def test_handle_presence_subscribe_to_account(self): + self.comp.stream = MockStream() + self.comp.stream_class = MockStream + 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") + self.assertEqual(\ + presence_sent[0].xpath_eval("@type")[0].get_content(), \ + "subscribed") def test_handle_presence_subscribed(self): pass def test_handle_presence_unsubscribe(self): - pass + self.comp.stream = MockStream() + self.comp.stream_class = MockStream + 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") + account2 = Account(user_jid = "user2@test.com", \ + name = "account2", \ + jid = "account2@jcl.test.com") + del account.hub.threadConnection + 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) + self.assertEqual(len([presence \ + for presence in presence_sent \ + if presence.get_to_jid() == "user1@test.com"]), \ + 2) + self.assertEqual(\ + len([presence \ + for presence in presence_sent \ + if presence.get_from_jid() == \ + "account11@jcl.test.com" \ + and presence.xpath_eval("@type")[0].get_content() \ + == "unsubscribe"]), \ + 1) + self.assertEqual(\ + len([presence \ + for presence in presence_sent \ + if presence.get_from_jid() == \ + "account11@jcl.test.com" \ + and presence.xpath_eval("@type")[0].get_content() \ + == "unsubscribed"]), \ + 1) + account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) + self.assertEquals(self.comp.account_class.select(\ + self.comp.account_class.q.user_jid == "user1@test.com" \ + and self.comp.account_class.q.name == "account11").count(), \ + 0) + del account.hub.threadConnection def test_handle_presence_unsubscribed(self): - pass + self.comp.stream = MockStream() + self.comp.stream_class = MockStream + 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") + self.assertEqual(\ + presence_sent[0].xpath_eval("@type")[0].get_content(), \ + "unavailable") - def test_handle_message(self): - pass + def test_handle_message_password(self): + self.comp.stream = MockStream() + self.comp.stream_class = MockStream + account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) + account11 = Account(user_jid = "user1@test.com", \ + name = "account11", \ + jid = "account11@jcl.test.com") + account11.waiting_password_reply = True + account12 = Account(user_jid = "user1@test.com", \ + name = "account12", \ + jid = "account12@jcl.test.com") + account2 = Account(user_jid = "user2@test.com", \ + name = "account2", \ + jid = "account2@jcl.test.com") + del account.hub.threadConnection + 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") + self.assertEqual(account11.password, "secret") + self.assertEqual(account11.waiting_password_reply, False) + self.assertEqual(messages_sent[0].get_subject(), \ + "Password will be kept during your Jabber session") + self.assertEqual(messages_sent[0].get_body(), \ + "Password will be kept during your Jabber session") + def test_handle_tick(self): self.assertRaises(NotImplementedError, self.comp.handle_tick)