Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndré Apitzsch <git@apitzsch.eu>2018-11-11 03:31:50 +0300
committerPhilipp Hörist <philipp@hoerist.com>2018-11-14 00:00:40 +0300
commit958e1390fdefea382a398bfae8c8809a8617d1d7 (patch)
treefaf530cdba3e79e75137166f7836b7735b4d6a58 /test/no_gui
parent8946af337a9496636ae96ed3c661527b65768c29 (diff)
Restructure test
Diffstat (limited to 'test/no_gui')
-rw-r--r--test/no_gui/__init__.py0
-rw-r--r--test/no_gui/unit/__init__.py5
-rw-r--r--test/no_gui/unit/test_account.py19
-rw-r--r--test/no_gui/unit/test_caps_cache.py182
-rw-r--r--test/no_gui/unit/test_contacts.py126
-rw-r--r--test/no_gui/unit/test_protocol_caps.py52
6 files changed, 384 insertions, 0 deletions
diff --git a/test/no_gui/__init__.py b/test/no_gui/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/test/no_gui/__init__.py
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 = '''
+<iq from='benvolio@capulet.lit/230193' id='disco1' to='juliet@capulet.lit/chamber' type='result'>
+<query xmlns='http://jabber.org/protocol/disco#info' node='http://psi-im.org#q07IKJEyjvHSyhy//CH0CxmKi8w='>
+<identity xml:lang='en' category='client' name='Psi 0.11' type='pc'/>
+<identity xml:lang='el' category='client' name='Ψ 0.11' type='pc'/>
+<feature var='http://jabber.org/protocol/caps'/>
+<feature var='http://jabber.org/protocol/disco#info'/>
+<feature var='http://jabber.org/protocol/disco#items'/>
+<feature var='http://jabber.org/protocol/muc'/>
+<x xmlns='jabber:x:data' type='result'>
+<field var='FORM_TYPE' type='hidden'>
+<value>urn:xmpp:dataforms:softwareinfo</value>
+</field>
+<field var='ip_version'>
+<value>ipv4</value>
+<value>ipv6</value>
+</field>
+<field var='os'>
+<value>Mac</value>
+</field>
+<field var='os_version'>
+<value>10.5.1</value>
+</field>
+<field var='software'>
+<value>Psi</value>
+</field>
+<field var='software_version'>
+<value>0.11</value>
+</field>
+</x>
+</query>
+</iq>'''
+
+
+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 = """<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>
+ """ % (fjid)
+ msg = nbxmpp.protocol.Presence(node=nbxmpp.simplexml.XML2Node(xml))
+ self.module._presence_received(None, msg)
+
+
+if __name__ == '__main__':
+ unittest.main()