2009-01-11 14:49:03 +01:00
|
|
|
'''
|
|
|
|
Tests for capabilities and the capabilities cache
|
|
|
|
'''
|
2009-12-10 18:31:00 +01:00
|
|
|
from common.caps import NullClientCaps
|
2008-06-12 05:56:47 +02:00
|
|
|
import unittest
|
|
|
|
|
2008-08-09 02:24:08 +02:00
|
|
|
import lib
|
|
|
|
lib.setup_env()
|
2008-06-12 05:56:47 +02:00
|
|
|
|
2009-10-26 19:20:16 +01:00
|
|
|
from common.xmpp import NS_MUC, NS_PING, NS_XHTML_IM
|
2009-10-30 23:01:25 +01:00
|
|
|
from common import caps
|
2009-10-27 20:31:09 +01:00
|
|
|
from common.contacts import Contact
|
2008-06-12 05:56:47 +02:00
|
|
|
|
|
|
|
from mock import Mock
|
|
|
|
|
2009-12-10 18:31:00 +01:00
|
|
|
|
|
|
|
class CommonCapsTest(unittest.TestCase):
|
|
|
|
|
2008-06-12 05:56:47 +02:00
|
|
|
def setUp(self):
|
2008-08-27 09:49:11 +02:00
|
|
|
self.caps_method = 'sha-1'
|
2009-10-26 19:20:16 +01:00
|
|
|
self.caps_hash = 'm3P2WeXPMGVH2tZPe7yITnfY0Dw='
|
2009-10-27 20:31:09 +01:00
|
|
|
self.client_caps = (self.caps_method, self.caps_hash)
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-25 21:17:32 +01:00
|
|
|
self.node = "http://gajim.org"
|
|
|
|
self.identity = {'category': 'client', 'type': 'pc', 'name':'Gajim'}
|
2008-06-12 05:56:47 +02:00
|
|
|
|
2008-08-27 09:49:11 +02:00
|
|
|
self.identities = [self.identity]
|
2009-10-26 19:20:16 +01:00
|
|
|
self.features = [NS_MUC, NS_XHTML_IM] # NS_MUC not supported!
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-25 21:17:32 +01:00
|
|
|
# Simulate a filled db
|
|
|
|
db_caps_cache = [
|
|
|
|
(self.caps_method, self.caps_hash, self.identities, self.features),
|
2009-10-25 22:32:18 +01:00
|
|
|
('old', self.node + '#' + self.caps_hash, self.identities, self.features)]
|
2009-10-25 21:17:32 +01:00
|
|
|
self.logger = Mock(returnValues={"iter_caps_data":db_caps_cache})
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-30 23:01:25 +01:00
|
|
|
self.cc = caps.CapsCache(self.logger)
|
|
|
|
caps.capscache = self.cc
|
2009-12-10 18:31:00 +01:00
|
|
|
|
|
|
|
|
2009-10-25 22:32:18 +01:00
|
|
|
class TestCapsCache(CommonCapsTest):
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-25 21:17:32 +01:00
|
|
|
def test_set_retrieve(self):
|
|
|
|
''' Test basic set / retrieve cycle '''
|
2008-06-12 05:56:47 +02:00
|
|
|
|
2009-10-27 20:31:09 +01:00
|
|
|
self.cc[self.client_caps].identities = self.identities
|
|
|
|
self.cc[self.client_caps].features = self.features
|
2008-06-12 05:56:47 +02:00
|
|
|
|
2009-10-27 20:31:09 +01:00
|
|
|
self.assert_(NS_MUC in self.cc[self.client_caps].features)
|
|
|
|
self.assert_(NS_PING not in self.cc[self.client_caps].features)
|
2008-08-27 09:49:11 +02:00
|
|
|
|
2009-10-27 20:31:09 +01:00
|
|
|
identities = self.cc[self.client_caps].identities
|
2009-10-25 21:17:32 +01:00
|
|
|
|
|
|
|
self.assertEqual(1, len(identities))
|
|
|
|
|
|
|
|
identity = identities[0]
|
|
|
|
self.assertEqual('client', identity['category'])
|
|
|
|
self.assertEqual('pc', identity['type'])
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-31 09:14:55 +01:00
|
|
|
def test_set_and_store(self):
|
2009-10-27 20:31:09 +01:00
|
|
|
''' Test client_caps update gets logged into db '''
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-27 20:31:09 +01:00
|
|
|
item = self.cc[self.client_caps]
|
2009-10-31 09:14:55 +01:00
|
|
|
item.set_and_store(self.identities, self.features)
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-25 21:17:32 +01:00
|
|
|
self.logger.mockCheckCall(0, "add_caps_entry", self.caps_method,
|
|
|
|
self.caps_hash, self.identities, self.features)
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-25 21:17:32 +01:00
|
|
|
def test_initialize_from_db(self):
|
2009-12-10 18:31:00 +01:00
|
|
|
''' Read cashed dummy data from db '''
|
2009-11-11 23:14:51 +01:00
|
|
|
self.assertEqual(self.cc[self.client_caps].status, caps.NEW)
|
2009-10-25 21:17:32 +01:00
|
|
|
self.cc.initialize_from_db()
|
2009-11-11 23:14:51 +01:00
|
|
|
self.assertEqual(self.cc[self.client_caps].status, caps.CACHED)
|
2009-10-25 21:17:32 +01:00
|
|
|
|
|
|
|
def test_preload_triggering_query(self):
|
|
|
|
''' Make sure that preload issues a disco '''
|
|
|
|
connection = Mock()
|
2009-10-30 23:01:25 +01:00
|
|
|
client_caps = caps.ClientCaps(self.caps_hash, self.node, self.caps_method)
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-27 20:31:09 +01:00
|
|
|
self.cc.query_client_of_jid_if_unknown(connection, "test@gajim.org",
|
|
|
|
client_caps)
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-27 20:31:09 +01:00
|
|
|
self.assertEqual(1, len(connection.mockGetAllCalls()))
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-25 21:17:32 +01:00
|
|
|
def test_no_preload_query_if_cashed(self):
|
|
|
|
''' Preload must not send a query if the data is already cached '''
|
|
|
|
connection = Mock()
|
2009-10-30 23:01:25 +01:00
|
|
|
client_caps = caps.ClientCaps(self.caps_hash, self.node, self.caps_method)
|
2009-10-27 20:31:09 +01:00
|
|
|
|
2009-10-25 21:17:32 +01:00
|
|
|
self.cc.initialize_from_db()
|
2009-10-27 20:31:09 +01:00
|
|
|
self.cc.query_client_of_jid_if_unknown(connection, "test@gajim.org",
|
|
|
|
client_caps)
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-25 21:17:32 +01:00
|
|
|
self.assertEqual(0, len(connection.mockGetAllCalls()))
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2008-08-27 09:49:11 +02:00
|
|
|
def test_hash(self):
|
|
|
|
'''tests the hash computation'''
|
2009-10-30 23:55:03 +01:00
|
|
|
computed_hash = caps.compute_caps_hash(self.identities, self.features)
|
2008-08-27 09:49:11 +02:00
|
|
|
self.assertEqual(self.caps_hash, computed_hash)
|
|
|
|
|
2009-10-25 21:17:32 +01:00
|
|
|
|
2009-10-26 19:20:16 +01:00
|
|
|
class TestClientCaps(CommonCapsTest):
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-25 22:32:18 +01:00
|
|
|
def setUp(self):
|
|
|
|
CommonCapsTest.setUp(self)
|
2009-12-10 18:31:00 +01:00
|
|
|
self.client_caps = caps.ClientCaps(self.caps_hash, self.node, self.caps_method)
|
|
|
|
|
2009-10-27 20:31:09 +01:00
|
|
|
def test_query_by_get_discover_strategy(self):
|
2009-12-10 18:31:00 +01:00
|
|
|
''' Client must be queried if the data is unkown '''
|
2009-10-25 22:32:18 +01:00
|
|
|
connection = Mock()
|
2009-10-27 20:31:09 +01:00
|
|
|
discover = self.client_caps.get_discover_strategy()
|
2009-12-10 18:31:00 +01:00
|
|
|
discover(connection, "test@gajim.org")
|
|
|
|
|
|
|
|
connection.mockCheckCall(0, "discoverInfo", "test@gajim.org",
|
2009-10-26 19:20:16 +01:00
|
|
|
"http://gajim.org#m3P2WeXPMGVH2tZPe7yITnfY0Dw=")
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-27 20:31:09 +01:00
|
|
|
def test_client_supports(self):
|
2009-11-09 21:26:56 +01:00
|
|
|
self.assertTrue(caps.client_supports(self.client_caps, NS_PING),
|
2009-10-26 19:20:16 +01:00
|
|
|
msg="Assume supported, if we don't have caps")
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-11-09 21:26:56 +01:00
|
|
|
self.assertFalse(caps.client_supports(self.client_caps, NS_XHTML_IM),
|
2009-10-26 19:20:16 +01:00
|
|
|
msg="Must not assume blacklisted feature is supported on default")
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-10-25 22:46:45 +01:00
|
|
|
self.cc.initialize_from_db()
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-11-09 21:26:56 +01:00
|
|
|
self.assertFalse(caps.client_supports(self.client_caps, NS_PING),
|
2009-10-25 22:46:45 +01:00
|
|
|
msg="Must return false on unsupported feature")
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-11-09 21:26:56 +01:00
|
|
|
self.assertTrue(caps.client_supports(self.client_caps, NS_XHTML_IM),
|
2009-10-25 22:46:45 +01:00
|
|
|
msg="Must return True on supported feature")
|
2009-12-10 18:31:00 +01:00
|
|
|
|
2009-11-09 21:26:56 +01:00
|
|
|
self.assertTrue(caps.client_supports(self.client_caps, NS_MUC),
|
|
|
|
msg="Must return True on supported feature")
|
2009-10-26 19:20:16 +01:00
|
|
|
|
2009-12-10 18:31:00 +01:00
|
|
|
|
|
|
|
class TestOldClientCaps(TestClientCaps):
|
2009-10-25 22:32:18 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
2009-10-26 19:20:16 +01:00
|
|
|
TestClientCaps.setUp(self)
|
2009-12-10 18:31:00 +01:00
|
|
|
self.client_caps = caps.OldClientCaps(self.caps_hash, self.node)
|
|
|
|
|
2009-10-27 20:31:09 +01:00
|
|
|
def test_query_by_get_discover_strategy(self):
|
2009-12-10 18:31:00 +01:00
|
|
|
''' Client must be queried if the data is unknown '''
|
2009-10-25 22:32:18 +01:00
|
|
|
connection = Mock()
|
2009-10-27 20:31:09 +01:00
|
|
|
discover = self.client_caps.get_discover_strategy()
|
2009-12-10 18:31:00 +01:00
|
|
|
discover(connection, "test@gajim.org")
|
|
|
|
|
2009-10-25 22:32:18 +01:00
|
|
|
connection.mockCheckCall(0, "discoverInfo", "test@gajim.org")
|
|
|
|
|
2009-10-25 21:17:32 +01:00
|
|
|
|
2009-12-10 18:31:00 +01:00
|
|
|
from common.xmpp import simplexml
|
|
|
|
from common.xmpp import protocol
|
|
|
|
|
|
|
|
class TestableConnectionCaps(caps.ConnectionCaps):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self._mocked_contacts = {}
|
|
|
|
caps.ConnectionCaps.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
def _get_contact_or_gc_contact_for_jid(self, jid):
|
|
|
|
"""
|
|
|
|
Overwrite to decouple form contact handling
|
|
|
|
"""
|
|
|
|
if jid not in self._mocked_contacts:
|
|
|
|
self._mocked_contacts[jid] = Mock(realClass=Contact)
|
|
|
|
self._mocked_contacts[jid].jid = jid
|
|
|
|
return self._mocked_contacts[jid]
|
|
|
|
|
|
|
|
def discoverInfo(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_mocked_contact_for_jid(self, jid):
|
|
|
|
return self._mocked_contacts[jid]
|
|
|
|
|
|
|
|
|
|
|
|
class TestConnectionCaps(CommonCapsTest):
|
|
|
|
|
|
|
|
def test_capsPresenceCB(self):
|
|
|
|
jid = "user@server.com/a"
|
|
|
|
connection_caps = TestableConnectionCaps("account",
|
|
|
|
self._build_assertering_dispatcher_function("CAPS_RECEIVED", jid))
|
|
|
|
|
|
|
|
xml = """<presence from='user@server.com/a'
|
|
|
|
to='%s' id='123'>
|
|
|
|
<c node='http://gajim.org' ver='pRCD6cgQ4SDqNMCjdhRV6TECx5o='
|
|
|
|
hash='sha-1' xmlns='http://jabber.org/protocol/caps'/>
|
|
|
|
</presence>
|
|
|
|
""" % (jid)
|
|
|
|
iq = protocol.Iq(node=simplexml.XML2Node(xml))
|
|
|
|
connection_caps._capsPresenceCB(None, iq)
|
|
|
|
|
|
|
|
self.assertTrue(self._dispatcher_called, msg="Must have received caps")
|
|
|
|
|
|
|
|
client_caps = connection_caps.get_mocked_contact_for_jid(jid).client_caps
|
|
|
|
self.assertTrue(client_caps, msg="Client caps must be set")
|
|
|
|
self.assertFalse(isinstance(client_caps, NullClientCaps),
|
|
|
|
msg="On receive of proper caps, we must not use the fallback")
|
|
|
|
|
|
|
|
def _build_assertering_dispatcher_function(self, expected_event, jid):
|
|
|
|
self._dispatcher_called = False
|
|
|
|
def dispatch(event, data):
|
|
|
|
self.assertFalse(self._dispatcher_called, msg="Must only be called once")
|
|
|
|
self._dispatcher_called = True
|
|
|
|
self.assertEqual(expected_event, event)
|
|
|
|
self.assertEqual(jid, data[0])
|
|
|
|
return dispatch
|
|
|
|
|
|
|
|
|
2008-06-12 05:56:47 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
2008-07-29 21:49:31 +02:00
|
|
|
|
2008-08-09 02:24:08 +02:00
|
|
|
# vim: se ts=3:
|