More tests

darcs-hash:20061018174930-86b55-29433ceb46b2c1f7decade7ff062d670a119400d.gz
This commit is contained in:
David Rousselie
2006-10-18 19:49:30 +02:00
parent 226fc220e3
commit 510b52bf3d
4 changed files with 220 additions and 77 deletions

View File

@@ -50,8 +50,8 @@ if __name__ == '__main__':
feeder_suite = unittest.makeSuite(Feeder_TestCase, "test") feeder_suite = unittest.makeSuite(Feeder_TestCase, "test")
sender_suite = unittest.makeSuite(Sender_TestCase, "test") sender_suite = unittest.makeSuite(Sender_TestCase, "test")
jcl_suite = unittest.TestSuite() jcl_suite = unittest.TestSuite()
# jcl_suite.addTest(FeederComponent_TestCase('test_handle_tick')) # jcl_suite.addTest(FeederComponent_TestCase('test_handle_get_version'))
# jcl_suite.addTest(FeederComponent_TestCase('test_handle_presence_unavailable_to_account')) # jcl_suite.addTest(FeederComponent_TestCase('test_handle_presence_available_to_account_live_password'))
# jcl_suite = unittest.TestSuite((feeder_component_suite)) # jcl_suite = unittest.TestSuite((feeder_component_suite))
# jcl_suite = unittest.TestSuite((component_suite)) # jcl_suite = unittest.TestSuite((component_suite))
jcl_suite = unittest.TestSuite((component_suite, jcl_suite = unittest.TestSuite((component_suite,

View File

@@ -434,19 +434,25 @@ class JCLComponent(Component):
## from_jid = stanza.get_from() ## from_jid = stanza.get_from()
## base_from_jid = unicode(from_jid.bare()) ## base_from_jid = unicode(from_jid.bare())
# TODO : send presence available to subscribed user # TODO : send presence available to subscribed user
# is it necessary to send available presence ?
return 1 return 1
def handle_presence_unsubscribe(self, stanza): def handle_presence_unsubscribe(self, stanza):
"""Handle unsubscribe presence from user """Handle unsubscribe presence from user
""" """
self.__logger.debug("PRESENCE_UNSUBSCRIBE") self.__logger.debug("PRESENCE_UNSUBSCRIBE")
## name = stanza.get_to().node name = stanza.get_to().node
from_jid = stanza.get_from() from_jid = stanza.get_from()
## base_from_jid = unicode(from_jid.bare()) base_from_jid = unicode(from_jid.bare())
# TODO : delete from account base
presence = Presence(from_jid = stanza.get_to(), to_jid = from_jid, \ presence = Presence(from_jid = stanza.get_to(), to_jid = from_jid, \
stanza_type = "unsubscribe") stanza_type = "unsubscribe")
self.stream.send(presence) 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() presence = stanza.make_accept_response()
self.stream.send(presence) self.stream.send(presence)
return 1 return 1
@@ -463,16 +469,20 @@ class JCLComponent(Component):
def handle_message(self, message): def handle_message(self, message):
"""Handle new message """Handle new message
Handle password response message
""" """
self.__logger.debug("MESSAGE: " + message.get_body()) self.__logger.debug("MESSAGE: " + message.get_body())
lang_class = self.__lang.get_lang_class_from_node(message.get_node()) lang_class = self.__lang.get_lang_class_from_node(message.get_node())
## name = message.get_to().node name = message.get_to().node
## base_from_jid = unicode(message.get_from().bare()) 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()) \ if re.compile("\[PASSWORD\]").search(message.get_subject()) \
is not None: is not None \
## TODO and self.__storage.has_key((base_from_jid, name)): and accounts.count() == 1:
## account = self.__storage[(base_from_jid, name)] account = list(accounts)[0]
account = Account()
account.password = message.get_body() account.password = message.get_body()
account.waiting_password_reply = False account.waiting_password_reply = False
msg = Message(from_jid = account.jid, \ msg = Message(from_jid = account.jid, \
@@ -481,42 +491,44 @@ class JCLComponent(Component):
subject = lang_class.password_saved_for_session, \ subject = lang_class.password_saved_for_session, \
body = lang_class.password_saved_for_session) body = lang_class.password_saved_for_session)
self.stream.send(msg) self.stream.send(msg)
self.db_disconnect()
return 1 return 1
########################################################################### ###########################################################################
# Utils # 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 """Send available presence to account's user and ask for password
if necessary""" if necessary"""
account.default_lang_class = lang_class _account.default_lang_class = lang_class
old_status = account.status old_status = _account.status
if show is None: 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: else:
account.status = show _account.status = show
p = Presence(from_jid = account.jid, \ p = Presence(from_jid = _account.jid, \
to_jid = account.user_jid, \ to_jid = _account.user_jid, \
status = account.status_msg, \ status = _account.status_msg, \
show = show, \ show = show, \
stanza_type = "available") stanza_type = "available")
self.stream.send(p) self.stream.send(p)
if account.store_password == False \ if _account.store_password == False \
and old_status == account.OFFLINE: and old_status == account.OFFLINE \
self._ask_password(account, lang_class) 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 """Send a Jabber message to ask for account password
""" """
if not account.waiting_password_reply \ if not _account.waiting_password_reply \
and account.status != "offline": and _account.status != account.OFFLINE:
account.waiting_password_reply = True _account.waiting_password_reply = True
msg = Message(from_jid = account.jid, \ msg = Message(from_jid = _account.jid, \
to_jid = account.user_jid, \ to_jid = _account.user_jid, \
stanza_type = "normal", \ stanza_type = "normal", \
subject = u"[PASSWORD] " + \ subject = u"[PASSWORD] " + \
lang_class.ask_password_subject, \ lang_class.ask_password_subject)#, \
body = lang_class.ask_password_body)# % \ ## body = lang_class.ask_password_body)# % \
## (account.host, account.login)) ## (account.host, account.login))
self.stream.send(msg) self.stream.send(msg)

View File

@@ -46,14 +46,13 @@ class Account(SQLObject):
name = StringCol() name = StringCol()
jid = StringCol() jid = StringCol()
login = StringCol(default = "") login = StringCol(default = "")
__password = StringCol(default = "") password = StringCol(default = None)
__store_password = BoolCol(default = True) store_password = BoolCol(default = True)
default_lang_class = Lang.en default_lang_class = Lang.en
first_check = True first_check = True
__status = OFFLINE __status = OFFLINE
waiting_password_reply = False waiting_password_reply = False
__volatile_password = ""
def get_long_name(self): def get_long_name(self):
"""Return Human readable account name""" """Return Human readable account name"""
@@ -83,28 +82,3 @@ class Account(SQLObject):
status = property(get_status, set_status) 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)

View File

@@ -4,18 +4,18 @@
## Login : David Rousselie <dax@happycoders.org> ## Login : David Rousselie <dax@happycoders.org>
## Started on Wed Aug 9 21:34:26 2006 David Rousselie ## Started on Wed Aug 9 21:34:26 2006 David Rousselie
## $Id$ ## $Id$
## ##
## Copyright (C) 2006 David Rousselie ## Copyright (C) 2006 David Rousselie
## This program is free software; you can redistribute it and/or modify ## 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 ## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or ## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version. ## (at your option) any later version.
## ##
## This program is distributed in the hope that it will be useful, ## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of ## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details. ## GNU General Public License for more details.
## ##
## You should have received a copy of the GNU General Public License ## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software ## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## 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.iq import Iq
from pyxmpp.stanza import Stanza from pyxmpp.stanza import Stanza
from pyxmpp.presence import Presence from pyxmpp.presence import Presence
from pyxmpp.message import Message
from jcl.jabber.component import JCLComponent from jcl.jabber.component import JCLComponent
from jcl.model import account from jcl.model import account
@@ -56,7 +57,7 @@ class MockStream(object):
self.connection_stopped = False self.connection_stopped = False
self.eof = False self.eof = False
self.socket = [] self.socket = []
def send(self, iq): def send(self, iq):
self.sent.append(iq) self.sent.append(iq)
@@ -107,7 +108,7 @@ class MockStreamNoConnect(MockStream):
def connect(self): def connect(self):
self.connection_started = True self.connection_started = True
self.eof = True self.eof = True
class JCLComponent_TestCase(unittest.TestCase): class JCLComponent_TestCase(unittest.TestCase):
def setUp(self): def setUp(self):
if os.path.exists(DB_PATH): if os.path.exists(DB_PATH):
@@ -119,7 +120,7 @@ class JCLComponent_TestCase(unittest.TestCase):
'sqlite://' + DB_URL) 'sqlite://' + DB_URL)
self.max_tick_count = 2 self.max_tick_count = 2
self.saved_time_handler = None self.saved_time_handler = None
def tearDown(self): def tearDown(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
Account.dropTable(ifExists = True) Account.dropTable(ifExists = True)
@@ -147,7 +148,7 @@ class JCLComponent_TestCase(unittest.TestCase):
except: except:
# Ignore exception, might be obtain from self.comp.queue # Ignore exception, might be obtain from self.comp.queue
pass pass
def test_run(self): def test_run(self):
self.comp.time_unit = 1 self.comp.time_unit = 1
# Do not loop, handle_tick is virtual # Do not loop, handle_tick is virtual
@@ -190,7 +191,7 @@ class JCLComponent_TestCase(unittest.TestCase):
self.max_tick_count -= 1 self.max_tick_count -= 1
if self.max_tick_count == 0: if self.max_tick_count == 0:
self.comp.running = False self.comp.running = False
def test_time_handler(self): def test_time_handler(self):
self.comp.time_unit = 1 self.comp.time_unit = 1
self.max_tick_count = 2 self.max_tick_count = 2
@@ -207,7 +208,7 @@ class JCLComponent_TestCase(unittest.TestCase):
self.assertTrue(True) self.assertTrue(True)
def test_authenticated_send_probe(self): 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", \ account11 = Account(user_jid = "test1@test.com", \
name = "test11", \ name = "test11", \
jid = "account11@jcl.test.com") jid = "account11@jcl.test.com")
@@ -276,7 +277,7 @@ class JCLComponent_TestCase(unittest.TestCase):
from_jid = "user1@test.com") from_jid = "user1@test.com")
disco_items = self.comp.disco_get_items("account1", info_query) disco_items = self.comp.disco_get_items("account1", info_query)
self.assertEquals(disco_items.get_items(), []) self.assertEquals(disco_items.get_items(), [])
def test_get_reg_form(self): def test_get_reg_form(self):
## self.comp.get_reg_form(Lang.en, Account) ## self.comp.get_reg_form(Lang.en, Account)
# TODO # TODO
@@ -320,7 +321,7 @@ class JCLComponent_TestCase(unittest.TestCase):
self.assertEquals(iq_sent.get_to(), "user1@test.com") self.assertEquals(iq_sent.get_to(), "user1@test.com")
# self.assertEquals(len(iq_sent.xpath_eval("*/*")), 1) # self.assertEquals(len(iq_sent.xpath_eval("*/*")), 1)
# TODO # TODO
def test_handle_get_register_exist(self): def test_handle_get_register_exist(self):
pass 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_to(), "user1@test.com")
self.assertEqual(presence_sent[0].get_from(), "account11@jcl.test.com") self.assertEqual(presence_sent[0].get_from(), "account11@jcl.test.com")
self.assertEqual(presence_sent[0].get_show(), "online") 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): def test_handle_presence_unavailable_to_component(self):
self.comp.stream = MockStream() self.comp.stream = MockStream()
self.comp.stream_class = MockStream self.comp.stream_class = MockStream
@@ -447,7 +492,7 @@ class JCLComponent_TestCase(unittest.TestCase):
and presence.xpath_eval("@type")[0].get_content() \ and presence.xpath_eval("@type")[0].get_content() \
== "unavailable"]), \ == "unavailable"]), \
1) 1)
def test_handle_presence_unavailable_to_account(self): def test_handle_presence_unavailable_to_account(self):
self.comp.stream = MockStream() self.comp.stream = MockStream()
@@ -474,22 +519,134 @@ class JCLComponent_TestCase(unittest.TestCase):
self.assertEqual(\ self.assertEqual(\
presence_sent[0].xpath_eval("@type")[0].get_content(), \ presence_sent[0].xpath_eval("@type")[0].get_content(), \
"unavailable") "unavailable")
def test_handle_presence_subscribe(self): def test_handle_presence_subscribe_to_component(self):
pass 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): def test_handle_presence_subscribed(self):
pass pass
def test_handle_presence_unsubscribe(self): 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): 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): def test_handle_tick(self):
self.assertRaises(NotImplementedError, self.comp.handle_tick) self.assertRaises(NotImplementedError, self.comp.handle_tick)