Add test for unknown user or account requests

darcs-hash:20061130214656-86b55-b8f2e86f09fd15725ab65ed9e2dab4cdccb2bf7e.gz
This commit is contained in:
David Rousselie
2006-11-30 22:46:56 +01:00
parent 46f0e53eb4
commit 285a2584a8
2 changed files with 362 additions and 56 deletions

View File

@@ -302,7 +302,7 @@ class JCLComponent(Component, object):
name = to_jid.node name = to_jid.node
info_query = info_query.make_result_response() info_query = info_query.make_result_response()
query = info_query.new_query("jabber:iq:register") query = info_query.new_query("jabber:iq:register")
if to_jid and to_jid != self.jid: if name is not None:
self.db_connect() self.db_connect()
for _account in self.account_class.select(\ for _account in self.account_class.select(\
AND(self.account_class.q.name == name, \ AND(self.account_class.q.name == name, \
@@ -439,14 +439,15 @@ class JCLComponent(Component, object):
for _account in accounts: for _account in accounts:
accounts_length += 1 accounts_length += 1
self._send_presence_available(_account, show, lang_class) self._send_presence_available(_account, show, lang_class)
presence = Presence(from_jid = self.jid, \ if (accounts_length > 0):
to_jid = from_jid, \ presence = Presence(from_jid = self.jid, \
status = \ to_jid = from_jid, \
str(accounts_length) \ status = \
+ lang_class.message_status, \ str(accounts_length) \
show = show, \ + lang_class.message_status, \
stanza_type = "available") show = show, \
self.stream.send(presence) stanza_type = "available")
self.stream.send(presence)
else: else:
accounts = self.account_class.select(\ accounts = self.account_class.select(\
AND(self.account_class.q.name == name, \ AND(self.account_class.q.name == name, \
@@ -462,28 +463,55 @@ class JCLComponent(Component, object):
self.__logger.debug("PRESENCE_UNAVAILABLE") self.__logger.debug("PRESENCE_UNAVAILABLE")
from_jid = stanza.get_from() from_jid = stanza.get_from()
base_from_jid = unicode(from_jid.bare()) base_from_jid = unicode(from_jid.bare())
if stanza.get_to() == unicode(self.jid): name = stanza.get_to().node
self.db_connect() self.db_connect()
for _account in self.account_class.select(\ if not name:
self.account_class.q.user_jid == base_from_jid): accounts = self.account_class.select(\
self.account_class.q.user_jid == base_from_jid)
for _account in accounts:
_account.status = jcl.model.account.OFFLINE _account.status = jcl.model.account.OFFLINE
presence = Presence(from_jid = _account.jid, \ presence = Presence(from_jid = _account.jid, \
to_jid = from_jid, \ to_jid = from_jid, \
stanza_type = "unavailable") stanza_type = "unavailable")
self.stream.send(presence) self.stream.send(presence)
self.db_disconnect() if accounts.count() > 0:
presence = Presence(from_jid = stanza.get_to(), \ presence = Presence(from_jid = stanza.get_to(), \
to_jid = from_jid, \ to_jid = from_jid, \
stanza_type = "unavailable") stanza_type = "unavailable")
self.stream.send(presence) self.stream.send(presence)
else:
accounts = self.account_class.select(\
AND(self.account_class.q.name == name, \
self.account_class.q.user_jid == base_from_jid))
if accounts.count() > 0:
presence = Presence(from_jid = stanza.get_to(), \
to_jid = from_jid, \
stanza_type = "unavailable")
self.stream.send(presence)
self.db_disconnect()
return 1 return 1
def handle_presence_subscribe(self, stanza): def handle_presence_subscribe(self, stanza):
"""Handle subscribe presence from user """Handle subscribe presence from user
""" """
self.__logger.debug("PRESENCE_SUBSCRIBE") self.__logger.debug("PRESENCE_SUBSCRIBE")
presence = stanza.make_accept_response() from_jid = stanza.get_from()
self.stream.send(presence) base_from_jid = unicode(from_jid.bare())
name = stanza.get_to().node
accounts = None
self.db_connect()
if not name:
accounts = self.account_class.select(\
self.account_class.q.user_jid == base_from_jid)
else:
accounts = self.account_class.select(\
AND(self.account_class.q.name == name, \
self.account_class.q.user_jid == base_from_jid))
if (accounts is not None \
and accounts.count() > 0):
presence = stanza.make_accept_response()
self.stream.send(presence)
self.db_disconnect()
return 1 return 1
def handle_presence_subscribed(self, stanza): def handle_presence_subscribed(self, stanza):
@@ -510,17 +538,21 @@ class JCLComponent(Component, object):
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())
presence = Presence(from_jid = stanza.get_to(), to_jid = from_jid, \
stanza_type = "unsubscribe")
self.stream.send(presence)
self.db_connect() self.db_connect()
for _account in self.account_class.select(\ accounts = self.account_class.select(\
self.account_class.q.user_jid == base_from_jid \ AND(self.account_class.q.name == name, \
and self.account_class.q.name == name): self.account_class.q.user_jid == base_from_jid))
for _account in accounts:
presence = Presence(from_jid = _account.jid, \
to_jid = from_jid, \
stanza_type = "unsubscribe")
self.stream.send(presence)
_account.destroySelf() _account.destroySelf()
presence = Presence(from_jid = _account.jid, \
to_jid = from_jid, \
stanza_type = "unsubscribed")
self.stream.send(presence)
self.db_disconnect() self.db_disconnect()
presence = stanza.make_accept_response()
self.stream.send(presence)
return 1 return 1
def handle_presence_unsubscribed(self, stanza): def handle_presence_unsubscribed(self, stanza):

View File

@@ -445,13 +445,19 @@ class JCLComponent_TestCase(unittest.TestCase):
self.comp.stream = MockStream() self.comp.stream = MockStream()
self.comp.stream_class = MockStream self.comp.stream_class = MockStream
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
account1 = Account(user_jid = "user1@test.com", \ account11 = Account(user_jid = "user1@test.com", \
name = "account1", \ name = "account11", \
jid = "account1@jcl.test.com") jid = "account11@jcl.test.com")
account12 = Account(user_jid = "user1@test.com", \
name = "account12", \
jid = "account12@jcl.test.com")
account21 = Account(user_jid = "user1@test.com", \
name = "account21", \
jid = "account21@jcl.test.com")
del account.hub.threadConnection del account.hub.threadConnection
self.comp.handle_get_register(Iq(stanza_type = "get", \ self.comp.handle_get_register(Iq(stanza_type = "get", \
from_jid = "user1@test.com", \ from_jid = "user1@test.com", \
to_jid = "account1@jcl.test.com")) to_jid = "account11@jcl.test.com"))
self.assertEquals(len(self.comp.stream.sent), 1) self.assertEquals(len(self.comp.stream.sent), 1)
iq_sent = self.comp.stream.sent[0] iq_sent = self.comp.stream.sent[0]
self.assertEquals(iq_sent.get_to(), "user1@test.com") self.assertEquals(iq_sent.get_to(), "user1@test.com")
@@ -479,7 +485,7 @@ class JCLComponent_TestCase(unittest.TestCase):
{"jir" : "jabber:iq:register", \ {"jir" : "jabber:iq:register", \
"jxd" : "jabber:x:data"}) "jxd" : "jabber:x:data"})
self.assertEquals(len(value), 1) self.assertEquals(len(value), 1)
self.assertEquals(value[0].content, "account1") self.assertEquals(value[0].content, "account11")
def test_handle_get_register_exist_complex(self): def test_handle_get_register_exist_complex(self):
self.comp.account_class = AccountExample self.comp.account_class = AccountExample
@@ -494,6 +500,22 @@ class JCLComponent_TestCase(unittest.TestCase):
store_password = False, \ store_password = False, \
test_enum = "choice3", \ test_enum = "choice3", \
test_int = 21) test_int = 21)
account11 = AccountExample(user_jid = "user1@test.com", \
name = "account11", \
jid = "account11@jcl.test.com", \
login = "mylogin", \
password = "mypassword", \
store_password = False, \
test_enum = "choice3", \
test_int = 21)
account21 = AccountExample(user_jid = "user2@test.com", \
name = "account21", \
jid = "account21@jcl.test.com", \
login = "mylogin", \
password = "mypassword", \
store_password = False, \
test_enum = "choice3", \
test_int = 21)
del account.hub.threadConnection del account.hub.threadConnection
self.comp.handle_get_register(Iq(stanza_type = "get", \ self.comp.handle_get_register(Iq(stanza_type = "get", \
from_jid = "user1@test.com", \ from_jid = "user1@test.com", \
@@ -981,6 +1003,27 @@ class JCLComponent_TestCase(unittest.TestCase):
and isinstance(presence, Presence)]), \ and isinstance(presence, Presence)]), \
1) 1)
def test_handle_presence_available_to_component_unknown_user(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")
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_available(Presence(\
stanza_type = "available", \
from_jid = "unknown@test.com",\
to_jid = "jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_available_to_account(self): def test_handle_presence_available_to_account(self):
self.comp.stream = MockStream() self.comp.stream = MockStream()
self.comp.stream_class = MockStream self.comp.stream_class = MockStream
@@ -1005,6 +1048,48 @@ class JCLComponent_TestCase(unittest.TestCase):
self.assertEqual(presence_sent[0].get_from(), "account11@jcl.test.com") self.assertEqual(presence_sent[0].get_from(), "account11@jcl.test.com")
self.assertTrue(isinstance(presence_sent[0], Presence)) self.assertTrue(isinstance(presence_sent[0], Presence))
def test_handle_presence_available_to_account_unknown_user(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")
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_available(Presence(\
stanza_type = "available", \
from_jid = "unknown@test.com",\
to_jid = "account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_available_to_unknown_account(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")
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_available(Presence(\
stanza_type = "available", \
from_jid = "user1@test.com",\
to_jid = "unknown@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_available_to_account_live_password(self): def test_handle_presence_available_to_account_live_password(self):
self.comp.stream = MockStream() self.comp.stream = MockStream()
self.comp.stream_class = MockStream self.comp.stream_class = MockStream
@@ -1125,6 +1210,26 @@ class JCLComponent_TestCase(unittest.TestCase):
== "unavailable"]), \ == "unavailable"]), \
1) 1)
def test_handle_presence_unavailable_to_component_unknown_user(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")
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_unavailable(Presence(\
stanza_type = "unavailable", \
from_jid = "unknown@test.com",\
to_jid = "jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_unavailable_to_account(self): def test_handle_presence_unavailable_to_account(self):
self.comp.stream = MockStream() self.comp.stream = MockStream()
@@ -1152,9 +1257,62 @@ class JCLComponent_TestCase(unittest.TestCase):
presence_sent[0].xpath_eval("@type")[0].get_content(), \ presence_sent[0].xpath_eval("@type")[0].get_content(), \
"unavailable") "unavailable")
def test_handle_presence_unavailable_to_account_unknown_user(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")
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_unavailable(Presence(\
stanza_type = "unavailable", \
from_jid = "unknown@test.com",\
to_jid = "account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_unavailable_to_unknown_account(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")
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_unavailable(Presence(\
stanza_type = "unavailable", \
from_jid = "user1@test.com",\
to_jid = "unknown@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_subscribe_to_component(self): def test_handle_presence_subscribe_to_component(self):
self.comp.stream = MockStream() self.comp.stream = MockStream()
self.comp.stream_class = 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_subscribe(Presence(\ self.comp.handle_presence_subscribe(Presence(\
stanza_type = "subscribe", \ stanza_type = "subscribe", \
from_jid = "user1@test.com",\ from_jid = "user1@test.com",\
@@ -1167,9 +1325,41 @@ class JCLComponent_TestCase(unittest.TestCase):
presence_sent[0].xpath_eval("@type")[0].get_content(), \ presence_sent[0].xpath_eval("@type")[0].get_content(), \
"subscribed") "subscribed")
def test_handle_presence_subscribe_to_component_unknown_user(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")
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_subscribe(Presence(\
stanza_type = "subscribe", \
from_jid = "unknown@test.com",\
to_jid = "jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_subscribe_to_account(self): def test_handle_presence_subscribe_to_account(self):
self.comp.stream = MockStream() self.comp.stream = MockStream()
self.comp.stream_class = 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_subscribe(Presence(\ self.comp.handle_presence_subscribe(Presence(\
stanza_type = "subscribe", \ stanza_type = "subscribe", \
from_jid = "user1@test.com",\ from_jid = "user1@test.com",\
@@ -1182,9 +1372,47 @@ class JCLComponent_TestCase(unittest.TestCase):
presence_sent[0].xpath_eval("@type")[0].get_content(), \ presence_sent[0].xpath_eval("@type")[0].get_content(), \
"subscribed") "subscribed")
def test_handle_presence_subscribed(self): def test_handle_presence_subscribe_to_account_unknown_user(self):
# TODO self.comp.stream = MockStream()
pass 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_subscribe(Presence(\
stanza_type = "subscribe", \
from_jid = "unknown@test.com",\
to_jid = "account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_subscribe_to_unknown_account(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")
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_subscribe(Presence(\
stanza_type = "subscribe", \
from_jid = "user1@test.com",\
to_jid = "unknown@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
def test_handle_presence_unsubscribe(self): def test_handle_presence_unsubscribe(self):
self.comp.stream = MockStream() self.comp.stream = MockStream()
@@ -1206,31 +1434,77 @@ class JCLComponent_TestCase(unittest.TestCase):
to_jid = "account11@jcl.test.com")) to_jid = "account11@jcl.test.com"))
presence_sent = self.comp.stream.sent presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 2) self.assertEqual(len(presence_sent), 2)
self.assertEqual(len([presence \ presence = presence_sent[0]
for presence in presence_sent \ self.assertEqual(presence.get_from(), "account11@jcl.test.com")
if presence.get_to_jid() == "user1@test.com"]), \ self.assertEqual(presence.get_to(), "user1@test.com")
2) self.assertEqual(presence.xpath_eval("@type")[0].get_content(), \
self.assertEqual(\ "unsubscribe")
len([presence \ presence = presence_sent[1]
for presence in presence_sent \ self.assertEqual(presence.get_from(), "account11@jcl.test.com")
if presence.get_from_jid() == \ self.assertEqual(presence.get_to(), "user1@test.com")
"account11@jcl.test.com" \ self.assertEqual(presence.xpath_eval("@type")[0].get_content(), \
and presence.xpath_eval("@type")[0].get_content() \ "unsubscribed")
== "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) account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
self.assertEquals(self.comp.account_class.select(\ self.assertEquals(self.comp.account_class.select(\
self.comp.account_class.q.user_jid == "user1@test.com" \ self.comp.account_class.q.user_jid == "user1@test.com" \
and self.comp.account_class.q.name == "account11").count(), \ and self.comp.account_class.q.name == "account11").count(), \
0) 0)
self.assertEquals(self.comp.account_class.select(\
self.comp.account_class.q.user_jid == "user1@test.com").count(), \
1)
self.assertEquals(self.comp.account_class.select().count(), \
2)
del account.hub.threadConnection
def test_handle_presence_unsubscribe_to_account_unknown_user(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")
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 = "unknown@test.com",\
to_jid = "account11@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
self.assertEquals(self.comp.account_class.select().count(), \
3)
del account.hub.threadConnection
def test_handle_presence_unsubscribe_to_unknown_account(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")
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 = "unknown@jcl.test.com"))
presence_sent = self.comp.stream.sent
self.assertEqual(len(presence_sent), 0)
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
self.assertEquals(self.comp.account_class.select().count(), \
3)
del account.hub.threadConnection del account.hub.threadConnection
def test_handle_presence_unsubscribed(self): def test_handle_presence_unsubscribed(self):