From 958e1390fdefea382a398bfae8c8809a8617d1d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Apitzsch?= Date: Sun, 11 Nov 2018 01:31:50 +0100 Subject: Restructure test --- test/no_gui/__init__.py | 0 test/no_gui/unit/__init__.py | 5 + test/no_gui/unit/test_account.py | 19 ++++ test/no_gui/unit/test_caps_cache.py | 182 +++++++++++++++++++++++++++++++++ test/no_gui/unit/test_contacts.py | 126 +++++++++++++++++++++++ test/no_gui/unit/test_protocol_caps.py | 52 ++++++++++ 6 files changed, 384 insertions(+) create mode 100644 test/no_gui/__init__.py create mode 100644 test/no_gui/unit/__init__.py create mode 100644 test/no_gui/unit/test_account.py create mode 100644 test/no_gui/unit/test_caps_cache.py create mode 100644 test/no_gui/unit/test_contacts.py create mode 100644 test/no_gui/unit/test_protocol_caps.py (limited to 'test/no_gui') diff --git a/test/no_gui/__init__.py b/test/no_gui/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/test/no_gui/unit/__init__.py b/test/no_gui/unit/__init__.py new file mode 100644 index 000000000..0252a7b2f --- /dev/null +++ b/test/no_gui/unit/__init__.py @@ -0,0 +1,5 @@ +''' + +This package just contains plain unit tests + +''' diff --git a/test/no_gui/unit/test_account.py b/test/no_gui/unit/test_account.py new file mode 100644 index 000000000..75d94542e --- /dev/null +++ b/test/no_gui/unit/test_account.py @@ -0,0 +1,19 @@ +''' +Tests for Account classes +''' +import unittest + +from gajim.common.account import Account + + +class Test(unittest.TestCase): + + def testInstantiate(self): + account = Account(name='MyAcc', contacts=None, gc_contacts=None) + + self.assertEqual('MyAcc', account.name) + self.assertTrue(account.gc_contacts is None) + self.assertTrue(account.contacts is None) + +if __name__ == "__main__": + unittest.main() diff --git a/test/no_gui/unit/test_caps_cache.py b/test/no_gui/unit/test_caps_cache.py new file mode 100644 index 000000000..0fb2204ae --- /dev/null +++ b/test/no_gui/unit/test_caps_cache.py @@ -0,0 +1,182 @@ +''' +Tests for capabilities and the capabilities cache +''' +import unittest + +from unittest.mock import MagicMock, Mock +from nbxmpp import NS_MUC, NS_PING, NS_XHTML_IM, Iq +from gajim.common import caps_cache as caps +from gajim.common.modules.discovery import Discovery + +COMPLEX_EXAMPLE = ''' + + + + + + + + + + +urn:xmpp:dataforms:softwareinfo + + +ipv4 +ipv6 + + +Mac + + +10.5.1 + + +Psi + + +0.11 + + + +''' + + +class CommonCapsTest(unittest.TestCase): + + def setUp(self): + self.caps_method = 'sha-1' + self.caps_hash = 'm3P2WeXPMGVH2tZPe7yITnfY0Dw=' + self.client_caps = (self.caps_method, self.caps_hash) + + self.node = "http://gajim.org" + self.identity = {'category': 'client', 'type': 'pc', 'name':'Gajim'} + + self.identities = [self.identity] + self.features = [NS_MUC, NS_XHTML_IM] # NS_MUC not supported! + + # Simulate a filled db + db_caps_cache = [ + (self.caps_method, self.caps_hash, self.identities, self.features), + ('old', self.node + '#' + self.caps_hash, self.identities, self.features)] + self.logger = Mock() + self.logger.iter_caps_data = Mock(return_value=db_caps_cache) + + self.cc = caps.CapsCache(self.logger) + caps.capscache = self.cc + + +class TestCapsCache(CommonCapsTest): + + def test_set_retrieve(self): + ''' Test basic set / retrieve cycle ''' + + self.cc[self.client_caps].identities = self.identities + self.cc[self.client_caps].features = self.features + + self.assertTrue(NS_MUC in self.cc[self.client_caps].features) + self.assertTrue(NS_PING not in self.cc[self.client_caps].features) + + identities = self.cc[self.client_caps].identities + + self.assertEqual(1, len(identities)) + + identity = identities[0] + self.assertEqual('client', identity['category']) + self.assertEqual('pc', identity['type']) + + def test_set_and_store(self): + ''' Test client_caps update gets logged into db ''' + + item = self.cc[self.client_caps] + item.set_and_store(self.identities, self.features) + + self.logger.add_caps_entry.assert_called_once_with(self.caps_method, + self.caps_hash, + self.identities, + self.features) + + def test_initialize_from_db(self): + ''' Read cashed dummy data from db ''' + self.assertEqual(self.cc[self.client_caps].status, caps.NEW) + self.cc.initialize_from_db() + self.assertEqual(self.cc[self.client_caps].status, caps.CACHED) + + def test_preload_triggering_query(self): + ''' Make sure that preload issues a disco ''' + connection = MagicMock() + client_caps = caps.ClientCaps(self.caps_hash, self.node, self.caps_method) + + self.cc.query_client_of_jid_if_unknown( + connection, "test@gajim.org", client_caps) + + self.assertEqual(1, connection.get_module('Discovery').disco_contact.call_count) + + def test_no_preload_query_if_cashed(self): + ''' Preload must not send a query if the data is already cached ''' + connection = MagicMock() + client_caps = caps.ClientCaps(self.caps_hash, self.node, self.caps_method) + + self.cc.initialize_from_db() + self.cc.query_client_of_jid_if_unknown( + connection, "test@gajim.org", client_caps) + + self.assertEqual(0, connection.get_module('Discovery').disco_contact.call_count) + + def test_hash(self): + '''tests the hash computation''' + stanza = Iq(node=COMPLEX_EXAMPLE) + identities, features, data, _ = Discovery.parse_info_response(stanza) + computed_hash = caps.compute_caps_hash(identities, features, data) + self.assertEqual('q07IKJEyjvHSyhy//CH0CxmKi8w=', computed_hash) + + +class TestClientCaps(CommonCapsTest): + + def setUp(self): + CommonCapsTest.setUp(self) + self.client_caps = caps.ClientCaps(self.caps_hash, self.node, self.caps_method) + + def test_query_by_get_discover_strategy(self): + ''' Client must be queried if the data is unkown ''' + connection = MagicMock() + discover = self.client_caps.get_discover_strategy() + discover(connection, "test@gajim.org") + connection.get_module('Discovery').disco_contact.assert_called_once_with( + 'test@gajim.org', 'http://gajim.org#m3P2WeXPMGVH2tZPe7yITnfY0Dw=') + + def test_client_supports(self): + self.assertTrue(caps.client_supports(self.client_caps, NS_PING), + msg="Assume supported, if we don't have caps") + + self.assertFalse(caps.client_supports(self.client_caps, NS_XHTML_IM), + msg="Must not assume blacklisted feature is supported on default") + + self.cc.initialize_from_db() + + self.assertFalse(caps.client_supports(self.client_caps, NS_PING), + msg="Must return false on unsupported feature") + + self.assertTrue(caps.client_supports(self.client_caps, NS_XHTML_IM), + msg="Must return True on supported feature") + + self.assertTrue(caps.client_supports(self.client_caps, NS_MUC), + msg="Must return True on supported feature") + + +class TestOldClientCaps(TestClientCaps): + + def setUp(self): + TestClientCaps.setUp(self) + self.client_caps = caps.OldClientCaps(self.caps_hash, self.node) + + def test_query_by_get_discover_strategy(self): + ''' Client must be queried if the data is unknown ''' + connection = MagicMock() + discover = self.client_caps.get_discover_strategy() + discover(connection, "test@gajim.org") + + connection.get_module('Discovery').disco_contact.assert_called_once_with('test@gajim.org') + +if __name__ == '__main__': + unittest.main() diff --git a/test/no_gui/unit/test_contacts.py b/test/no_gui/unit/test_contacts.py new file mode 100644 index 000000000..8ca9810cb --- /dev/null +++ b/test/no_gui/unit/test_contacts.py @@ -0,0 +1,126 @@ +''' +Test for Contact, GC_Contact and Contacts +''' +import unittest +from nbxmpp import NS_MUC + +from gajim.common.contacts import CommonContact, Contact, GC_Contact, LegacyContactsAPI + +from gajim.common import caps_cache + +class TestCommonContact(unittest.TestCase): + + def setUp(self): + self.contact = CommonContact( + jid='', account="", resource='', show='', + status='', name='', chatstate=None, client_caps=None) + + def test_default_client_supports(self): + ''' + Test the caps support method of contacts. + See test_caps for more enhanced tests. + ''' + caps_cache.capscache = caps_cache.CapsCache() + self.assertTrue(self.contact.supports(NS_MUC), + msg="Must not backtrace on simple check for supported feature") + + self.contact.client_caps = caps_cache.NullClientCaps() + + self.assertTrue(self.contact.supports(NS_MUC), + msg="Must not backtrace on simple check for supported feature") + + +class TestContact(TestCommonContact): + + def setUp(self): + TestCommonContact.setUp(self) + self.contact = Contact(jid="test@gajim.org", account="account") + + def test_attributes_available(self): + '''This test supports the migration from the old to the new contact + domain model by smoke testing that no attribute values are lost''' + + attributes = ["jid", "resource", "show", "status", "name", + "chatstate", "client_caps", "priority", "sub"] + for attr in attributes: + self.assertTrue(hasattr(self.contact, attr), msg="expected: " + attr) + + +class TestGC_Contact(TestCommonContact): + + def setUp(self): + TestCommonContact.setUp(self) + self.contact = GC_Contact(room_jid="confernce@gajim.org", account="account") + + def test_attributes_available(self): + '''This test supports the migration from the old to the new contact + domain model by asserting no attributes have been lost''' + + attributes = ["jid", "resource", "show", "status", "name", + "chatstate", "client_caps", "role", "room_jid"] + for attr in attributes: + self.assertTrue(hasattr(self.contact, attr), msg="expected: " + attr) + + +class TestContacts(unittest.TestCase): + + def setUp(self): + self.contacts = LegacyContactsAPI() + + def test_create_add_get_contact(self): + jid = 'test@gajim.org' + account = "account" + + contact = self.contacts.create_contact(jid=jid, account=account) + self.contacts.add_contact(account, contact) + + retrieved_contact = self.contacts.get_contact(account, jid) + self.assertEqual(contact, retrieved_contact, "Contact must be known") + + self.contacts.remove_contact(account, contact) + + retrieved_contact = self.contacts.get_contact(account, jid) + self.assertNotEqual(contact, retrieved_contact, + msg="Contact must not be known any longer") + + + def test_copy_contact(self): + jid = 'test@gajim.org' + account = "account" + + contact = self.contacts.create_contact(jid=jid, account=account) + copy = self.contacts.copy_contact(contact) + self.assertFalse(contact is copy, msg="Must not be the same") + + # Not yet implemented to remain backwart compatible + # self.assertEqual(contact, copy, msg="Must be equal") + + def test_legacy_accounts_handling(self): + self.contacts.add_account("one") + self.contacts.add_account("two") + + self.contacts.change_account_name("two", "old") + self.contacts.remove_account("one") + + self.assertEqual(["old"], self.contacts.get_accounts()) + + def test_legacy_contacts_from_groups(self): + jid1 = "test1@gajim.org" + jid2 = "test2@gajim.org" + account = "account" + group = "GroupA" + + contact1 = self.contacts.create_contact(jid=jid1, account=account, + groups=[group]) + self.contacts.add_contact(account, contact1) + + contact2 = self.contacts.create_contact(jid=jid2, account=account, + groups=[group]) + self.contacts.add_contact(account, contact2) + + self.assertEqual(2, len(self.contacts.get_contacts_from_group(account, group))) + self.assertEqual(0, len(self.contacts.get_contacts_from_group(account, ''))) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/no_gui/unit/test_protocol_caps.py b/test/no_gui/unit/test_protocol_caps.py new file mode 100644 index 000000000..e3e0bbece --- /dev/null +++ b/test/no_gui/unit/test_protocol_caps.py @@ -0,0 +1,52 @@ +''' +Tests for caps network coding +''' + +import unittest +from unittest.mock import MagicMock + +import nbxmpp + +from gajim.common import app +from gajim.common import nec +from gajim.common import ged +from gajim.common import caps_cache +from gajim.common.modules.caps import Caps + + +class TestConnectionCaps(unittest.TestCase): + + def setUp(self): + app.contacts.add_account('account') + contact = app.contacts.create_contact( + 'user@server.com', 'account', resource='a') + app.contacts.add_contact('account', contact) + + app.nec = nec.NetworkEventsController() + app.ged.register_event_handler( + 'caps-presence-received', ged.GUI2, + self._nec_caps_presence_received) + + self.module = Caps(MagicMock()) + self.module._account = 'account' + self.module._capscache = MagicMock() + + def _nec_caps_presence_received(self, obj): + self.assertTrue( + isinstance(obj.client_caps, caps_cache.ClientCaps), + msg="On receive of valid caps, ClientCaps should be returned") + + def test_capsPresenceCB(self): + fjid = "user@server.com/a" + + xml = """ + + + """ % (fjid) + msg = nbxmpp.protocol.Presence(node=nbxmpp.simplexml.XML2Node(xml)) + self.module._presence_received(None, msg) + + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.3