source: ralphm-patches/pubsub-correct-namespace.patch @ 47:f6d222b68f1c

Last change on this file since 47:f6d222b68f1c was 47:f6d222b68f1c, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Many new patches, upstreamed affiliations and worked on pubsub item.
Upstreamed pubsub_manage_affiliations

File size: 7.2 KB
  • wokkel/pubsub.py

    # HG changeset patch
    # Parent 9afc7ecd4201daf011d4abe5315fc3e1deb9503d
    Make sure item and subscription elements get the correct namespace.
    
    This complements the changes in [f4aa0d507bc8] and [3fe44cf07366] for item and
    subscription elements that are used in different namespaces. These changesets
    set the namespace to `None`, which works nicely when the elements are
    serialized. However, in situations where the DOM representation is passed
    around (in-process communication) these elements are no longer matched by
    namespace. This change makes sure the elements get the proper namespace just
    before they are sent out.
    
    diff -r 9afc7ecd4201 wokkel/pubsub.py
    a b  
    138138                subscriptionIdentifier=element.getAttribute('subid'))
    139139
    140140
    141     def toElement(self):
     141    def toElement(self, defaultUri=None):
    142142        """
    143143        Return the DOM representation of this subscription.
    144144
    145145        @rtype: L{domish.Element}
    146146        """
    147         element = domish.Element((None, 'subscription'))
     147        element = domish.Element((defaultUri, 'subscription'))
    148148        if self.nodeIdentifier:
    149149            element['node'] = self.nodeIdentifier
    150150        element['jid'] = unicode(self.subscriber)
     
    358358        """
    359359        if self.items:
    360360            for item in self.items:
     361                item.uri = NS_PUBSUB
    361362                verbElement.addChild(item)
    362363
    363364
     
    12131214
    12141215    def _toResponse_subscribe(self, result, resource, request):
    12151216        response = domish.Element((NS_PUBSUB, "pubsub"))
    1216         response.addChild(result.toElement())
     1217        response.addChild(result.toElement(NS_PUBSUB))
    12171218        return response
    12181219
    12191220
     
    12211222        response = domish.Element((NS_PUBSUB, 'pubsub'))
    12221223        subscriptions = response.addElement('subscriptions')
    12231224        for subscription in result:
    1224             subscriptions.addChild(subscription.toElement())
     1225            subscriptions.addChild(subscription.toElement(NS_PUBSUB))
    12251226        return response
    12261227
    12271228
     
    13071308        items["node"] = request.nodeIdentifier
    13081309
    13091310        for item in result:
     1311            item.uri = NS_PUBSUB
    13101312            items.addChild(item)
    13111313
    13121314        return response
     
    13571359            message = self._createNotification('items', service,
    13581360                                               nodeIdentifier, subscriber,
    13591361                                               subscriptions)
    1360             message.event.items.children = items
     1362            for item in items:
     1363                item.uri = NS_PUBSUB_EVENT
     1364                message.event.items.addChild(item)
    13611365            self.send(message)
    13621366
    13631367
  • wokkel/test/test_pubsub.py

    diff -r 9afc7ecd4201 wokkel/test/test_pubsub.py
    a b  
    462462        self.assertEquals(1, len(children))
    463463        child = children[0]
    464464        self.assertEquals('test', child['node'])
    465 
    466         items = []
    467         for element in child.elements():
    468             if element.name == 'item' and element.uri in (NS_PUBSUB, None):
    469                 items.append(element)
    470 
     465        items = list(domish.generateElementsQNamed(child.children,
     466                                                   'item', NS_PUBSUB))
    471467        self.assertEquals(1, len(items))
    472468        self.assertIdentical(item, items[0])
    473469
     
    22172213            self.assertEqual('pubsub', element.name)
    22182214            self.assertEqual(NS_PUBSUB, element.uri)
    22192215            subscription = element.subscription
    2220             self.assertIn(subscription.uri, (None, NS_PUBSUB))
     2216            self.assertEqual(NS_PUBSUB, subscription.uri)
    22212217            self.assertEqual('test', subscription['node'])
    22222218            self.assertEqual('user@example.org/Home', subscription['jid'])
    22232219            self.assertEqual('subscribed', subscription['subscription'])
     
    24302426            self.assertEqual(1, len(children))
    24312427            subscription = children[0]
    24322428            self.assertEqual('subscription', subscription.name)
    2433             self.assertIn(subscription.uri, (None, NS_PUBSUB))
     2429            self.assertEqual(NS_PUBSUB, subscription.uri, NS_PUBSUB)
    24342430            self.assertEqual('user@example.org', subscription['jid'])
    24352431            self.assertEqual('test', subscription['node'])
    24362432            self.assertEqual('subscribed', subscription['subscription'])
     
    29802976            item = element.items.children[-1]
    29812977            self.assertTrue(domish.IElement.providedBy(item))
    29822978            self.assertEqual('item', item.name)
    2983             self.assertIn(item.uri, (NS_PUBSUB, None))
     2979            self.assertEqual(NS_PUBSUB, item.uri)
    29842980            self.assertEqual('current', item['id'])
    29852981
    29862982        self.resource.items = items
     
    30623058        return self.handleRequest(xml)
    30633059
    30643060
     3061    def test_notifyPublish(self):
     3062        """
     3063        Publish notifications are sent to the subscribers.
     3064        """
     3065        subscriber = JID('user@example.org')
     3066        subscriptions = [pubsub.Subscription('test', subscriber, 'subscribed')]
     3067        items = [pubsub.Item('current')]
     3068        notifications = [(subscriber, subscriptions, items)]
     3069        self.service.notifyPublish(JID('pubsub.example.org'), 'test',
     3070                                   notifications)
     3071        message = self.stub.output[-1]
     3072
     3073        self.assertEquals('message', message.name)
     3074        self.assertIdentical(None, message.uri)
     3075        self.assertEquals('user@example.org', message['to'])
     3076        self.assertEquals('pubsub.example.org', message['from'])
     3077        self.assertTrue(message.event)
     3078        self.assertEquals(NS_PUBSUB_EVENT, message.event.uri)
     3079        self.assertTrue(message.event.items)
     3080        self.assertEquals(NS_PUBSUB_EVENT, message.event.items.uri)
     3081        self.assertTrue(message.event.items.hasAttribute('node'))
     3082        self.assertEquals('test', message.event.items['node'])
     3083        itemElements = list(domish.generateElementsQNamed(
     3084            message.event.items.children, 'item', NS_PUBSUB_EVENT))
     3085        self.assertEquals(1, len(itemElements))
     3086        self.assertEquals('current', itemElements[0].getAttribute('id'))
     3087
     3088
     3089    def test_notifyPublishCollection(self):
     3090        """
     3091        Publish notifications are sent to the subscribers of collections.
     3092
     3093        The node the item was published to is on the C{items} element, while
     3094        the subscribed-to node is in the C{'Collections'} SHIM header.
     3095        """
     3096        subscriber = JID('user@example.org')
     3097        subscriptions = [pubsub.Subscription('', subscriber, 'subscribed')]
     3098        items = [pubsub.Item('current')]
     3099        notifications = [(subscriber, subscriptions, items)]
     3100        self.service.notifyPublish(JID('pubsub.example.org'), 'test',
     3101                                   notifications)
     3102        message = self.stub.output[-1]
     3103
     3104        self.assertTrue(message.event.items.hasAttribute('node'))
     3105        self.assertEquals('test', message.event.items['node'])
     3106        headers = shim.extractHeaders(message)
     3107        self.assertIn('Collection', headers)
     3108        self.assertIn('', headers['Collection'])
     3109
     3110
    30653111    def test_notifyDelete(self):
    30663112        """
    30673113        Subscribers should be sent a delete notification.
Note: See TracBrowser for help on using the repository browser.