Changeset 60:a763a1289af8 in ralphm-patches


Ignore:
Timestamp:
Apr 30, 2012, 9:12:33 AM (8 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Add tests for RosterRequest? and fix version rendering.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • roster_item.patch

    r59 r60  
    3535 NS_XML = 'http://www.w3.org/XML/1998/namespace'
    3636 NS_ROSTER = 'jabber:iq:roster'
    37 @@ -449,23 +450,373 @@
     37@@ -449,40 +450,621 @@
    3838 
    3939 
     
    4848+        item = xmppim.RosterItem(JID('user@example.org'))
    4949+        element = item.toElement()
    50 +        self.assertEquals('item', element.name)
    51 +        self.assertEquals(NS_ROSTER, element.uri)
     50+        self.assertEqual('item', element.name)
     51+        self.assertEqual(NS_ROSTER, element.uri)
    5252+        self.assertFalse(element.hasAttribute('subscription'))
    5353+        self.assertFalse(element.hasAttribute('ask'))
     
    6060+        item = xmppim.RosterItem(JID('user@example.org'))
    6161+        element = item.toElement()
    62 +        self.assertEquals(u'user@example.org', element.getAttribute('jid'))
     62+        self.assertEqual(u'user@example.org', element.getAttribute('jid'))
    6363+
    6464+
     
    7676+                                 subscriptionFrom=False)
    7777+        element = item.toElement()
    78 +        self.assertEquals('to', element.getAttribute('subscription'))
     78+        self.assertEqual('to', element.getAttribute('subscription'))
    7979+
    8080+
     
    8484+                                 subscriptionFrom=True)
    8585+        element = item.toElement()
    86 +        self.assertEquals('from', element.getAttribute('subscription'))
     86+        self.assertEqual('from', element.getAttribute('subscription'))
    8787+
    8888+
     
    9292+                                 subscriptionFrom=True)
    9393+        element = item.toElement()
    94 +        self.assertEquals('both', element.getAttribute('subscription'))
     94+        self.assertEqual('both', element.getAttribute('subscription'))
    9595+
    9696+
     
    9999+        item.remove = True
    100100+        element = item.toElement()
    101 +        self.assertEquals('remove', element.getAttribute('subscription'))
     101+        self.assertEqual('remove', element.getAttribute('subscription'))
    102102+
    103103+
     
    106106+        item.pendingOut = True
    107107+        element = item.toElement()
    108 +        self.assertEquals('subscribe', element.getAttribute('ask'))
     108+        self.assertEqual('subscribe', element.getAttribute('ask'))
    109109+
    110110+
     
    113113+                                 name='Joe User')
    114114+        element = item.toElement()
    115 +        self.assertEquals(u'Joe User', element.getAttribute('name'))
     115+        self.assertEqual(u'Joe User', element.getAttribute('name'))
    116116+
    117117+
     
    137137+        item.approved = True
    138138+        element = item.toElement()
    139 +        self.assertEquals(u'true', element.getAttribute('approved'))
     139+        self.assertEqual(u'true', element.getAttribute('approved'))
    140140+
    141141+
     
    157157+        self.assertFalse(item.pendingOut)
    158158+        self.assertFalse(item.approved)
    159 +        self.assertEquals(set(), item.groups)
     159+        self.assertEqual(set(), item.groups)
    160160+
    161161+
     
    345345+                         setattr, item, 'jid',
    346346+                         JID('other@example.org'))
    347 +        self.assertEquals(JID('other@example.org'), item.entity)
     347+        self.assertEqual(JID('other@example.org'), item.entity)
    348348+
    349349+
     
    381381+    Tests for L{xmppim.RosterRequest}.
    382382+    """
     383+
     384+    def test_fromElement(self):
     385+        """
     386+        A bare roster request is parsed and missing information is None.
     387+        """
     388+        xml = """
     389+            <iq type='get' to='this@example.org/Home' from='this@example.org'>
     390+              <query xmlns='jabber:iq:roster'/>
     391+            </iq>
     392+        """
     393+
     394+        request = xmppim.RosterRequest.fromElement(parseXml(xml))
     395+        self.assertEqual('get', request.stanzaType)
     396+        self.assertEqual(JID('this@example.org/Home'), request.recipient)
     397+        self.assertEqual(JID('this@example.org'), request.sender)
     398+        self.assertEqual(None, request.item)
     399+        self.assertEqual(None, request.version)
     400+
     401+
     402+    def test_fromElementItem(self):
     403+        """
     404+        If an item is present, parse it and put it in the request item.
     405+        """
     406+        xml = """
     407+            <iq type='set' to='this@example.org/Home' from='this@example.org'>
     408+              <query xmlns='jabber:iq:roster'>
     409+                <item jid='user@example.org'/>
     410+              </query>
     411+            </iq>
     412+        """
     413+
     414+        request = xmppim.RosterRequest.fromElement(parseXml(xml))
     415+        self.assertNotIdentical(None, request.item)
     416+        self.assertEqual(JID('user@example.org'), request.item.entity)
     417+
     418+
     419+    def test_fromElementVersion(self):
     420+        """
     421+        If a ver attribute is present, put it in the request version.
     422+        """
     423+        xml = """
     424+            <iq type='set' to='this@example.org/Home' from='this@example.org'>
     425+              <query xmlns='jabber:iq:roster' ver='ver72'>
     426+                <item jid='user@example.org'/>
     427+              </query>
     428+            </iq>
     429+        """
     430+        request = xmppim.RosterRequest.fromElement(parseXml(xml))
     431+        self.assertEqual('ver72', request.version)
     432+
     433+
     434+    def test_fromElementVersionEmpty(self):
     435+        """
     436+        The ver attribute may be empty.
     437+        """
     438+        xml = """
     439+            <iq type='get' to='this@example.org/Home' from='this@example.org'>
     440+              <query xmlns='jabber:iq:roster' ver=''/>
     441+            </iq>
     442+        """
     443+        request = xmppim.RosterRequest.fromElement(parseXml(xml))
     444+        self.assertEqual('', request.version)
     445+
     446+
     447+    def test_toElement(self):
     448+        """
     449+        A roster request has a query element in the roster namespace.
     450+        """
     451+        request = xmppim.RosterRequest()
     452+        element = request.toElement()
     453+        children = element.elements()
     454+        child = children.next()
     455+        self.assertEqual(NS_ROSTER, child.uri)
     456+        self.assertEqual('query', child.name)
     457+
     458+
     459+    def test_toElementItem(self):
     460+        """
     461+        If an item is set, it is rendered as a child of the query.
     462+        """
     463+        request = xmppim.RosterRequest()
     464+        request.item = xmppim.RosterItem(JID('user@example.org'))
     465+        element = request.toElement()
     466+        children = element.query.elements()
     467+        child = children.next()
     468+        self.assertEqual(NS_ROSTER, child.uri)
     469+        self.assertEqual('item', child.name)
     470+
     471+
     472+    def test_toElementVersion(self):
     473+        """
     474+        If the roster version is set, a 'ver' attribute is added.
     475+        """
     476+        request = xmppim.RosterRequest()
     477+        request.version = 'ver72'
     478+        element = request.toElement()
     479+        self.assertEqual('ver72', element.query.getAttribute('ver'))
     480+
     481+
     482+    def test_toElementVersionEmpty(self):
     483+        """
     484+        If the roster version is the empty string, it should add 'ver', too.
     485+        """
     486+        request = xmppim.RosterRequest()
     487+        request.version = ''
     488+        element = request.toElement()
     489+        self.assertEqual('', element.query.getAttribute('ver'))
     490+
    383491+
    384492+
     
    400508+
    401509+    def request(self, request):
     510+        """
     511+        Stub for the request method on XMPPHandler.
     512+        """
    402513+        element = request.toElement()
    403514+        self.stub.xmlstream.send(element)
     
    414525         # Inspect outgoing iq request
    415526 
    416 @@ -479,10 +830,128 @@
    417          self.assertEquals(1, len(children))
     527         iq = self.stub.output[-1]
     528-        self.assertEquals('set', iq.getAttribute('type'))
     529+        self.assertEqual('set', iq.getAttribute('type'))
     530         self.assertNotIdentical(None, iq.query)
     531-        self.assertEquals(NS_ROSTER, iq.query.uri)
     532+        self.assertEqual(NS_ROSTER, iq.query.uri)
     533 
     534         children = list(domish.generateElementsQNamed(iq.query.children,
     535                                                       'item', NS_ROSTER))
     536-        self.assertEquals(1, len(children))
     537+        self.assertEqual(1, len(children))
    418538         child = children[0]
    419          self.assertEquals('test@example.org', child['jid'])
     539-        self.assertEquals('test@example.org', child['jid'])
    420540-        self.assertEquals('remove', child['subscription'])
    421 +        self.assertEquals('remove', child.getAttribute('subscription'))
     541+        self.assertEqual('test@example.org', child['jid'])
     542+        self.assertEqual('remove', child.getAttribute('subscription'))
    422543 
    423544         # Fake successful response
     
    430551+
    431552+    def test_getRoster(self):
     553+        """
     554+        A request for the roster is sent out and the response is parsed.
     555+        """
    432556+        def cb(roster):
    433557+            self.assertIn(JID('user@example.org'), roster)
    434558+
    435 +
    436559+        d = self.service.getRoster()
    437560+        d.addCallback(cb)
     
    440563+
    441564+        iq = self.stub.output[-1]
    442 +        self.assertEquals('get', iq.getAttribute('type'))
     565+        self.assertEqual('get', iq.getAttribute('type'))
    443566+        self.assertNotIdentical(None, iq.query)
    444 +        self.assertEquals(NS_ROSTER, iq.query.uri)
     567+        self.assertEqual(NS_ROSTER, iq.query.uri)
    445568+
    446569+        # Fake successful response
     
    472595+
    473596+        def cb(result):
    474 +            self.assertEquals(1, len(items))
    475 +            self.assertEquals(JID('user@example.org'), items[0].entity)
     597+            self.assertEqual(1, len(items))
     598+            self.assertEqual(JID('user@example.org'), items[0].entity)
    476599+
    477600+        self.service.onRosterSet = onRosterSet
     
    505628+
    506629+        def cb(result):
    507 +            self.assertEquals([JID('user@example.org')], entities)
     630+            self.assertEqual([JID('user@example.org')], entities)
    508631+
    509632+        self.service.onRosterRemove = onRosterRemove
     
    537660+
    538661+        def cb(result):
    539 +            self.assertEquals(1, len(requests), "push was not called")
    540 +            self.assertEquals(JID('user@example.org'), requests[0].item.entity)
     662+            self.assertEqual(1, len(requests), "push was not called")
     663+            self.assertEqual(JID('user@example.org'), requests[0].item.entity)
    541664+
    542665+        self.service.push = push
     
    591714     @type name: C{unicode}
    592715     @ivar subscriptionTo: Subscription state to contact's presence. If C{True},
    593 @@ -616,45 +621,172 @@
     716@@ -616,45 +621,174 @@
    594717     @ivar subscriptionFrom: Contact's subscription state. If C{True}, the
    595718                             contact is subscribed to the presence information
     
    754877+    def toElement(self):
    755878+        element = Request.toElement(self)
    756 +        element.addElement((NS_ROSTER, 'query'))
     879+        query = element.addElement((NS_ROSTER, 'query'))
     880+        if self.version is not None:
     881+            query['ver'] = self.version
    757882+        if self.item:
    758 +            element.query.addChild(self.item.toElement())
     883+            query.addChild(self.item.toElement())
    759884+        return element
    760885+
     
    790915     def getRoster(self):
    791916         """
    792 @@ -668,14 +800,13 @@
     917@@ -668,14 +802,13 @@
    793918             roster = {}
    794919             for element in domish.generateElementsQNamed(result.query.children,
     
    809934         return d
    810935 
    811 @@ -688,44 +819,50 @@
     936@@ -688,44 +821,50 @@
    812937         @type entity: L{JID<twisted.words.protocols.jabber.jid.JID>}
    813938         @rtype: L{twisted.internet.defer.Deferred}
Note: See TracChangeset for help on using the changeset viewer.