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
RevLine 
[47]1# HG changeset patch
2# Parent 9afc7ecd4201daf011d4abe5315fc3e1deb9503d
3Make sure item and subscription elements get the correct namespace.
4
5This complements the changes in [f4aa0d507bc8] and [3fe44cf07366] for item and
6subscription elements that are used in different namespaces. These changesets
7set the namespace to `None`, which works nicely when the elements are
8serialized. However, in situations where the DOM representation is passed
9around (in-process communication) these elements are no longer matched by
10namespace. This change makes sure the elements get the proper namespace just
11before they are sent out.
12
13diff -r 9afc7ecd4201 wokkel/pubsub.py
14--- a/wokkel/pubsub.py  Mon Feb 28 20:37:55 2011 +0100
15+++ b/wokkel/pubsub.py  Mon Feb 28 20:38:27 2011 +0100
16@@ -138,13 +138,13 @@
17                 subscriptionIdentifier=element.getAttribute('subid'))
18 
19 
20-    def toElement(self):
21+    def toElement(self, defaultUri=None):
22         """
23         Return the DOM representation of this subscription.
24 
25         @rtype: L{domish.Element}
26         """
27-        element = domish.Element((None, 'subscription'))
28+        element = domish.Element((defaultUri, 'subscription'))
29         if self.nodeIdentifier:
30             element['node'] = self.nodeIdentifier
31         element['jid'] = unicode(self.subscriber)
32@@ -358,6 +358,7 @@
33         """
34         if self.items:
35             for item in self.items:
36+                item.uri = NS_PUBSUB
37                 verbElement.addChild(item)
38 
39 
40@@ -1213,7 +1214,7 @@
41 
42     def _toResponse_subscribe(self, result, resource, request):
43         response = domish.Element((NS_PUBSUB, "pubsub"))
44-        response.addChild(result.toElement())
45+        response.addChild(result.toElement(NS_PUBSUB))
46         return response
47 
48 
49@@ -1221,7 +1222,7 @@
50         response = domish.Element((NS_PUBSUB, 'pubsub'))
51         subscriptions = response.addElement('subscriptions')
52         for subscription in result:
53-            subscriptions.addChild(subscription.toElement())
54+            subscriptions.addChild(subscription.toElement(NS_PUBSUB))
55         return response
56 
57 
58@@ -1307,6 +1308,7 @@
59         items["node"] = request.nodeIdentifier
60 
61         for item in result:
62+            item.uri = NS_PUBSUB
63             items.addChild(item)
64 
65         return response
66@@ -1357,7 +1359,9 @@
67             message = self._createNotification('items', service,
68                                                nodeIdentifier, subscriber,
69                                                subscriptions)
70-            message.event.items.children = items
71+            for item in items:
72+                item.uri = NS_PUBSUB_EVENT
73+                message.event.items.addChild(item)
74             self.send(message)
75 
76 
77diff -r 9afc7ecd4201 wokkel/test/test_pubsub.py
78--- a/wokkel/test/test_pubsub.py        Mon Feb 28 20:37:55 2011 +0100
79+++ b/wokkel/test/test_pubsub.py        Mon Feb 28 20:38:27 2011 +0100
80@@ -462,12 +462,8 @@
81         self.assertEquals(1, len(children))
82         child = children[0]
83         self.assertEquals('test', child['node'])
84-
85-        items = []
86-        for element in child.elements():
87-            if element.name == 'item' and element.uri in (NS_PUBSUB, None):
88-                items.append(element)
89-
90+        items = list(domish.generateElementsQNamed(child.children,
91+                                                   'item', NS_PUBSUB))
92         self.assertEquals(1, len(items))
93         self.assertIdentical(item, items[0])
94 
95@@ -2217,7 +2213,7 @@
96             self.assertEqual('pubsub', element.name)
97             self.assertEqual(NS_PUBSUB, element.uri)
98             subscription = element.subscription
99-            self.assertIn(subscription.uri, (None, NS_PUBSUB))
100+            self.assertEqual(NS_PUBSUB, subscription.uri)
101             self.assertEqual('test', subscription['node'])
102             self.assertEqual('user@example.org/Home', subscription['jid'])
103             self.assertEqual('subscribed', subscription['subscription'])
104@@ -2430,7 +2426,7 @@
105             self.assertEqual(1, len(children))
106             subscription = children[0]
107             self.assertEqual('subscription', subscription.name)
108-            self.assertIn(subscription.uri, (None, NS_PUBSUB))
109+            self.assertEqual(NS_PUBSUB, subscription.uri, NS_PUBSUB)
110             self.assertEqual('user@example.org', subscription['jid'])
111             self.assertEqual('test', subscription['node'])
112             self.assertEqual('subscribed', subscription['subscription'])
113@@ -2980,7 +2976,7 @@
114             item = element.items.children[-1]
115             self.assertTrue(domish.IElement.providedBy(item))
116             self.assertEqual('item', item.name)
117-            self.assertIn(item.uri, (NS_PUBSUB, None))
118+            self.assertEqual(NS_PUBSUB, item.uri)
119             self.assertEqual('current', item['id'])
120 
121         self.resource.items = items
122@@ -3062,6 +3058,56 @@
123         return self.handleRequest(xml)
124 
125 
126+    def test_notifyPublish(self):
127+        """
128+        Publish notifications are sent to the subscribers.
129+        """
130+        subscriber = JID('user@example.org')
131+        subscriptions = [pubsub.Subscription('test', subscriber, 'subscribed')]
132+        items = [pubsub.Item('current')]
133+        notifications = [(subscriber, subscriptions, items)]
134+        self.service.notifyPublish(JID('pubsub.example.org'), 'test',
135+                                   notifications)
136+        message = self.stub.output[-1]
137+
138+        self.assertEquals('message', message.name)
139+        self.assertIdentical(None, message.uri)
140+        self.assertEquals('user@example.org', message['to'])
141+        self.assertEquals('pubsub.example.org', message['from'])
142+        self.assertTrue(message.event)
143+        self.assertEquals(NS_PUBSUB_EVENT, message.event.uri)
144+        self.assertTrue(message.event.items)
145+        self.assertEquals(NS_PUBSUB_EVENT, message.event.items.uri)
146+        self.assertTrue(message.event.items.hasAttribute('node'))
147+        self.assertEquals('test', message.event.items['node'])
148+        itemElements = list(domish.generateElementsQNamed(
149+            message.event.items.children, 'item', NS_PUBSUB_EVENT))
150+        self.assertEquals(1, len(itemElements))
151+        self.assertEquals('current', itemElements[0].getAttribute('id'))
152+
153+
154+    def test_notifyPublishCollection(self):
155+        """
156+        Publish notifications are sent to the subscribers of collections.
157+
158+        The node the item was published to is on the C{items} element, while
159+        the subscribed-to node is in the C{'Collections'} SHIM header.
160+        """
161+        subscriber = JID('user@example.org')
162+        subscriptions = [pubsub.Subscription('', subscriber, 'subscribed')]
163+        items = [pubsub.Item('current')]
164+        notifications = [(subscriber, subscriptions, items)]
165+        self.service.notifyPublish(JID('pubsub.example.org'), 'test',
166+                                   notifications)
167+        message = self.stub.output[-1]
168+
169+        self.assertTrue(message.event.items.hasAttribute('node'))
170+        self.assertEquals('test', message.event.items['node'])
171+        headers = shim.extractHeaders(message)
172+        self.assertIn('Collection', headers)
173+        self.assertIn('', headers['Collection'])
174+
175+
176     def test_notifyDelete(self):
177         """
178         Subscribers should be sent a delete notification.
Note: See TracBrowser for help on using the repository browser.