From 02108f2a34db809425c233de9dc0fada83cfef39 Mon Sep 17 00:00:00 2001 From: David Rousselie Date: Wed, 25 Oct 2006 20:25:16 +0200 Subject: [PATCH] Optional login/password in account Remove login/password related attribut in Account class but still support live password if account_class in component has correct attributs. darcs-hash:20061025182516-86b55-4743189d47ba481692f16ff52f8b50903df49689.gz --- src/jcl/jabber/component.py | 11 ++- src/jcl/model/account.py | 29 +++++--- tests/jcl/jabber/test_component.py | 105 ++++++++++++++++++++++------- 3 files changed, 105 insertions(+), 40 deletions(-) diff --git a/src/jcl/jabber/component.py b/src/jcl/jabber/component.py index 9767b70..0390c19 100644 --- a/src/jcl/jabber/component.py +++ b/src/jcl/jabber/component.py @@ -479,7 +479,9 @@ class JCLComponent(Component): 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()) \ + if hasattr(self.account_class, 'password') \ + and hasattr(self.account_class, 'waiting_password_reply') \ + and re.compile("\[PASSWORD\]").search(message.get_subject()) \ is not None \ and accounts.count() == 1: account = list(accounts)[0] @@ -512,7 +514,9 @@ class JCLComponent(Component): show = show, \ stanza_type = "available") self.stream.send(p) - if _account.store_password == False \ + if hasattr(self.account_class, 'store_password') \ + and hasattr(self.account_class, 'password') \ + and _account.store_password == False \ and old_status == account.OFFLINE \ and _account.password == None : self._ask_password(_account, lang_class) @@ -520,7 +524,8 @@ class JCLComponent(Component): def _ask_password(self, _account, lang_class): """Send a Jabber message to ask for account password """ - if not _account.waiting_password_reply \ + if hasattr(self.account_class, 'waiting_password_reply') \ + and not _account.waiting_password_reply \ and _account.status != account.OFFLINE: _account.waiting_password_reply = True msg = Message(from_jid = _account.jid, \ diff --git a/src/jcl/model/account.py b/src/jcl/model/account.py index a6c6630..42d9620 100644 --- a/src/jcl/model/account.py +++ b/src/jcl/model/account.py @@ -32,8 +32,8 @@ from sqlobject.dbconnection import ConnectionHub from jcl.lang import Lang -OFFLINE = 0 -ONLINE = 1 +OFFLINE = "offline" +ONLINE = "online" # create a hub to attach a per thread connection hub = ConnectionHub() @@ -45,14 +45,16 @@ class Account(SQLObject): user_jid = StringCol() name = StringCol() jid = StringCol() - login = StringCol(default = "") - password = StringCol(default = None) - store_password = BoolCol(default = True) +## Not yet used first_check = BoolCol(default = True) + __status = StringCol(default = OFFLINE, dbName = "status") + +## Use these attributs to support volatile password +## login = StringCol(default = "") +## password = StringCol(default = None) +## store_password = BoolCol(default = True) +## waiting_password_reply = BoolCol(default = False) default_lang_class = Lang.en - first_check = True - __status = OFFLINE - waiting_password_reply = False def get_long_name(self): """Return Human readable account name""" @@ -73,10 +75,15 @@ class Account(SQLObject): def set_status(self, status): """Set current Jabber status""" if status == OFFLINE: - self.waiting_password_reply = False - if not self.store_password: - self.password = None + if hasattr(self.__class__, 'waiting_password_reply') \ + and hasattr(self.__class__, 'store_password') \ + and hasattr(self.__class__, 'password'): + self.waiting_password_reply = False + if not self.store_password: + self.password = None else: + # TODO seems a bug : first_check = True only if previous status + # was OFFLINE self.first_check = True self.__status = status diff --git a/tests/jcl/jabber/test_component.py b/tests/jcl/jabber/test_component.py index 8e2f760..b27bf3a 100644 --- a/tests/jcl/jabber/test_component.py +++ b/tests/jcl/jabber/test_component.py @@ -422,27 +422,57 @@ class JCLComponent_TestCase(unittest.TestCase): 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.assertEqual(len(messages_sent), 1) + presence = messages_sent[0] 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) +# Use it in real live password implementation +# 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() @@ -615,7 +645,6 @@ class JCLComponent_TestCase(unittest.TestCase): presence_sent[0].xpath_eval("@type")[0].get_content(), \ "unavailable") - def test_handle_message_password(self): self.comp.stream = MockStream() self.comp.stream_class = MockStream @@ -637,15 +666,39 @@ class JCLComponent_TestCase(unittest.TestCase): 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") + self.assertEqual(len(messages_sent), 0) + +# TODO Use it with real implementation live password +# 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)