Add complex caps test
This commit is contained in:
		
							parent
							
								
									737b5ae04a
								
							
						
					
					
						commit
						7dacd51c1d
					
				
					 2 changed files with 69 additions and 26 deletions
				
			
		| 
						 | 
					@ -2318,33 +2318,13 @@ class AgentInfoReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
 | 
				
			||||||
        if self.id_ in self.conn.disco_info_ids:
 | 
					        if self.id_ in self.conn.disco_info_ids:
 | 
				
			||||||
            self.conn.disco_info_ids.remove(self.id_)
 | 
					            self.conn.disco_info_ids.remove(self.id_)
 | 
				
			||||||
        if self.id_ is None:
 | 
					        if self.id_ is None:
 | 
				
			||||||
            log.warning('Invalid IQ received without an ID. Ignoring it: %s' % \
 | 
					            log.warning('Invalid IQ received without an ID. '
 | 
				
			||||||
                self.stanza)
 | 
					                        'Ignoring it: %s', self.stanza)
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        # According to XEP-0030:
 | 
					        # According to XEP-0030:
 | 
				
			||||||
        # For identity: category, type is mandatory, name is optional.
 | 
					        # For identity: category, type is mandatory, name is optional.
 | 
				
			||||||
        # For feature: var is mandatory
 | 
					        # For feature: var is mandatory
 | 
				
			||||||
        self.identities, self.features, self.data = [], [], []
 | 
					        self.identities, self.features, self.data, self.node = self.parse_stanza(self.stanza)
 | 
				
			||||||
        q = self.stanza.getTag('query')
 | 
					 | 
				
			||||||
        self.node = q.getAttr('node')
 | 
					 | 
				
			||||||
        if not self.node:
 | 
					 | 
				
			||||||
            self.node = ''
 | 
					 | 
				
			||||||
        qc = self.stanza.getQueryChildren()
 | 
					 | 
				
			||||||
        if not qc:
 | 
					 | 
				
			||||||
            qc = []
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for i in qc:
 | 
					 | 
				
			||||||
            if i.getName() == 'identity':
 | 
					 | 
				
			||||||
                attr = {}
 | 
					 | 
				
			||||||
                for key in i.getAttrs().keys():
 | 
					 | 
				
			||||||
                    attr[key] = i.getAttr(key)
 | 
					 | 
				
			||||||
                self.identities.append(attr)
 | 
					 | 
				
			||||||
            elif i.getName() == 'feature':
 | 
					 | 
				
			||||||
                var = i.getAttr('var')
 | 
					 | 
				
			||||||
                if var:
 | 
					 | 
				
			||||||
                    self.features.append(var)
 | 
					 | 
				
			||||||
            elif i.getName() == 'x' and i.getNamespace() == nbxmpp.NS_DATA:
 | 
					 | 
				
			||||||
                self.data.append(nbxmpp.DataForm(node=i))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if not self.identities:
 | 
					        if not self.identities:
 | 
				
			||||||
            # ejabberd doesn't send identities when we browse online users
 | 
					            # ejabberd doesn't send identities when we browse online users
 | 
				
			||||||
| 
						 | 
					@ -2354,6 +2334,33 @@ class AgentInfoReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
 | 
				
			||||||
        self.get_jid_resource()
 | 
					        self.get_jid_resource()
 | 
				
			||||||
        return True
 | 
					        return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @classmethod
 | 
				
			||||||
 | 
					    def parse_stanza(cls, stanza):
 | 
				
			||||||
 | 
					        identities, features, data, node = [], [], [], None
 | 
				
			||||||
 | 
					        q = stanza.getTag('query')
 | 
				
			||||||
 | 
					        node = q.getAttr('node')
 | 
				
			||||||
 | 
					        if not node:
 | 
				
			||||||
 | 
					            node = ''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        qc = stanza.getQueryChildren()
 | 
				
			||||||
 | 
					        if not qc:
 | 
				
			||||||
 | 
					            qc = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for i in qc:
 | 
				
			||||||
 | 
					            if i.getName() == 'identity':
 | 
				
			||||||
 | 
					                attr = {}
 | 
				
			||||||
 | 
					                for key in i.getAttrs().keys():
 | 
				
			||||||
 | 
					                    attr[key] = i.getAttr(key)
 | 
				
			||||||
 | 
					                identities.append(attr)
 | 
				
			||||||
 | 
					            elif i.getName() == 'feature':
 | 
				
			||||||
 | 
					                var = i.getAttr('var')
 | 
				
			||||||
 | 
					                if var:
 | 
				
			||||||
 | 
					                    features.append(var)
 | 
				
			||||||
 | 
					            elif i.getName() == 'x' and i.getNamespace() == nbxmpp.NS_DATA:
 | 
				
			||||||
 | 
					                data.append(nbxmpp.DataForm(node=i))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return identities, features, data, node
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AgentInfoErrorReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
 | 
					class AgentInfoErrorReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
 | 
				
			||||||
    name = 'agent-info-error-received'
 | 
					    name = 'agent-info-error-received'
 | 
				
			||||||
    base_network_events = []
 | 
					    base_network_events = []
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,12 +6,46 @@ import unittest
 | 
				
			||||||
import lib
 | 
					import lib
 | 
				
			||||||
lib.setup_env()
 | 
					lib.setup_env()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from nbxmpp import NS_MUC, NS_PING, NS_XHTML_IM
 | 
					from nbxmpp import NS_MUC, NS_PING, NS_XHTML_IM, Iq
 | 
				
			||||||
from gajim.common import caps_cache as caps
 | 
					from gajim.common import caps_cache as caps
 | 
				
			||||||
from gajim.common.contacts import Contact
 | 
					from gajim.common.contacts import Contact
 | 
				
			||||||
 | 
					from gajim.common.connection_handlers_events import AgentInfoReceivedEvent
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from mock import Mock
 | 
					from mock import Mock
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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):
 | 
					class CommonCapsTest(unittest.TestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -93,8 +127,10 @@ class TestCapsCache(CommonCapsTest):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_hash(self):
 | 
					    def test_hash(self):
 | 
				
			||||||
        '''tests the hash computation'''
 | 
					        '''tests the hash computation'''
 | 
				
			||||||
        computed_hash = caps.compute_caps_hash(self.identities, self.features)
 | 
					        stanza = Iq(node=COMPLEX_EXAMPLE)
 | 
				
			||||||
        self.assertEqual(self.caps_hash, computed_hash)
 | 
					        identities, features, data, _ = AgentInfoReceivedEvent.parse_stanza(stanza)
 | 
				
			||||||
 | 
					        computed_hash = caps.compute_caps_hash(identities, features, data)
 | 
				
			||||||
 | 
					        self.assertEqual('q07IKJEyjvHSyhy//CH0CxmKi8w=', computed_hash)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestClientCaps(CommonCapsTest):
 | 
					class TestClientCaps(CommonCapsTest):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue