source: ralphm-patches/pubsub_manage_affiliations.patch @ 45:51ac2274f989

Last change on this file since 45:51ac2274f989 was 45:51ac2274f989, checked in by Ralph Meijer <ralphm@…>, 12 years ago

Work on using a real Item class and managing affiliations support.

File size: 9.0 KB
RevLine 
[45]1# HG changeset patch
2# Parent f69b08281becebb5483f3f2dfb0451ad3086a463
3
4diff -r f69b08281bec wokkel/iwokkel.py
5--- a/wokkel/iwokkel.py Sat Feb 12 00:08:30 2011 +0100
6+++ b/wokkel/iwokkel.py Sat Feb 12 09:41:40 2011 +0100
7@@ -767,3 +767,17 @@
8                  deleted.
9         @rtype: L{defer.Deferred}
10         """
11+
12+
13+    def affiliationsGet(request):
14+        """
15+        Called when a affiliations retrieval request (owner) has been received.
16+
17+        @param request: The publish-subscribe request.
18+        @type request: L{wokkel.pubsub.PubSubRequest}
19+        @return: A deferred that fires with a C{dict} of affiliations with the
20+            entity as key (L{JID}) and the affiliation state as value
21+            (C{unicode}).  The affiliation can be C{u'owner'}, C{u'publisher'},
22+            or C{u'outcast'}.
23+        @rtype: L{defer.Deferred}
24+        """
25diff -r f69b08281bec wokkel/pubsub.py
26--- a/wokkel/pubsub.py  Sat Feb 12 00:08:30 2011 +0100
27+++ b/wokkel/pubsub.py  Sat Feb 12 09:41:40 2011 +0100
28@@ -220,6 +220,9 @@
29     @ivar subscriptions: Subscriptions to be modified, as a set of
30                          L{Subscription}.
31     @type subscriptions: C{set}
32+    @ivar affiliations: Affiliations to be modified, as a dictionary of entity
33+                        (L{JID} to affiliation (C{unicode}).
34+    @type affiliations: C{dict}
35     """
36 
37     verb = None
38@@ -234,6 +237,7 @@
39     subscriber = None
40     subscriptionIdentifier = None
41     subscriptions = None
42+    affiliations = None
43 
44     # Map request iq type and subelement name to request verb
45     _requestVerbMap = {
46@@ -279,7 +283,7 @@
47         'purge': ['node'],
48         'delete': ['node'],
49         'affiliationsGet': ['nodeOrEmpty'],
50-        'affiliationsSet': [],
51+        'affiliationsSet': ['nodeOrEmpty', 'affiliations'],
52         'subscriptionsGet': ['nodeOrEmpty'],
53         'subscriptionsSet': [],
54     }
55@@ -526,6 +530,22 @@
56             self._render_options(optionsElement)
57 
58 
59+    def _parse_affiliations(self, verbElement):
60+        self.affiliations = {}
61+        for element in verbElement.elements():
62+            if (element.uri == NS_PUBSUB_OWNER and
63+                element.name == 'affiliation'):
64+                try:
65+                    entity = jid.internJID(element['jid'])
66+                except KeyError:
67+                    raise BadRequest(text='Missing jid attribute')
68+                try:
69+                    affiliation = element['affiliation']
70+                except KeyError:
71+                    raise BadRequest(text='Missing affiliation attribute')
72+                self.affiliations[entity] = affiliation
73+
74+
75     def parseElement(self, element):
76         """
77         Parse the publish-subscribe verb and parameters out of a request.
78@@ -1303,6 +1323,22 @@
79 
80         return message
81 
82+
83+    def _toResponse_affiliationsGet(self, result, resource, request):
84+        response = domish.Element((NS_PUBSUB_OWNER, 'pubsub'))
85+        affiliations = response.addElement('affiliations')
86+
87+        if request.nodeIdentifier:
88+            affiliations['node'] = request.nodeIdentifier
89+
90+        for entity, affiliation in result.iteritems():
91+            item = affiliations.addElement('affiliation')
92+            item['jid'] = entity.full()
93+            item['affiliation'] = affiliation
94+
95+        return response
96+
97+
98     # public methods
99 
100     def notifyPublish(self, service, nodeIdentifier, notifications):
101diff -r f69b08281bec wokkel/test/test_pubsub.py
102--- a/wokkel/test/test_pubsub.py        Sat Feb 12 00:08:30 2011 +0100
103+++ b/wokkel/test/test_pubsub.py        Sat Feb 12 09:41:40 2011 +0100
104@@ -3192,7 +3192,45 @@
105 
106     def test_on_affiliationsGet(self):
107         """
108-        Getting subscription options is not supported.
109+        Getting node affiliations should have.
110+        """
111+
112+        xml = """
113+        <iq type='get' to='pubsub.example.org'
114+                       from='user@example.org'>
115+          <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>
116+            <affiliations node='test'/>
117+          </pubsub>
118+        </iq>
119+        """
120+
121+        def affiliationsGet(request):
122+            self.assertEquals('test', request.nodeIdentifier)
123+            return defer.succeed({JID('user@example.org'): 'owner'})
124+
125+        def cb(element):
126+            self.assertEquals(u'pubsub', element.name)
127+            self.assertEquals(NS_PUBSUB_OWNER, element.uri)
128+            self.assertEquals(NS_PUBSUB_OWNER, element.affiliations.uri)
129+            self.assertEquals(u'test', element.affiliations[u'node'])
130+            children = list(element.affiliations.elements())
131+            self.assertEquals(1, len(children))
132+            affiliation = children[0]
133+            self.assertEquals(u'affiliation', affiliation.name)
134+            self.assertEquals(NS_PUBSUB_OWNER, affiliation.uri)
135+            self.assertEquals(u'user@example.org', affiliation[u'jid'])
136+            self.assertEquals(u'owner', affiliation[u'affiliation'])
137+
138+        self.resource.affiliationsGet = affiliationsGet
139+        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
140+        d = self.handleRequest(xml)
141+        d.addCallback(cb)
142+        return d
143+
144+
145+    def test_on_affiliationsGetEmptyNode(self):
146+        """
147+        Getting node affiliations without node should assume empty node.
148         """
149 
150         xml = """
151@@ -3204,44 +3242,44 @@
152         </iq>
153         """
154 
155-        def cb(result):
156-            self.assertEquals('feature-not-implemented', result.condition)
157-            self.assertEquals('unsupported', result.appCondition.name)
158-            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
159-            self.assertEquals('modify-affiliations',
160-                              result.appCondition['feature'])
161-
162+        def affiliationsGet(request):
163+            self.assertIdentical('', request.nodeIdentifier)
164+            return defer.succeed({})
165+
166+        def cb(element):
167+            self.assertFalse(element.affiliations.hasAttribute(u'node'))
168+
169+        self.resource.affiliationsGet = affiliationsGet
170+        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
171         d = self.handleRequest(xml)
172-        self.assertFailure(d, error.StanzaError)
173         d.addCallback(cb)
174         return d
175 
176 
177     def test_on_affiliationsSet(self):
178         """
179-        Setting subscription options is not supported.
180+        Setting node affiliations has the affiliations to be modified.
181         """
182 
183         xml = """
184         <iq type='set' to='pubsub.example.org'
185                        from='user@example.org'>
186           <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>
187-            <affiliations/>
188+            <affiliations node='test'>
189+              <affiliation jid='other@example.org' affiliation='publisher'/>
190+            </affiliations>
191           </pubsub>
192         </iq>
193         """
194 
195-        def cb(result):
196-            self.assertEquals('feature-not-implemented', result.condition)
197-            self.assertEquals('unsupported', result.appCondition.name)
198-            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
199-            self.assertEquals('modify-affiliations',
200-                              result.appCondition['feature'])
201-
202-        d = self.handleRequest(xml)
203-        self.assertFailure(d, error.StanzaError)
204-        d.addCallback(cb)
205-        return d
206+        def affiliationsSet(request):
207+            self.assertEquals(u'test', request.nodeIdentifier)
208+            otherJID = JID(u'other@example.org')
209+            self.assertIn(otherJID, request.affiliations)
210+            self.assertEquals(u'publisher', request.affiliations[otherJID])
211+
212+        self.resource.affiliationsSet = affiliationsSet
213+        return self.handleRequest(xml)
214 
215 
216 
217@@ -3914,3 +3952,39 @@
218         self.assertFailure(d, error.StanzaError)
219         d.addCallback(cb)
220         return d
221+
222+
223+    def test_affiliationsGet(self):
224+        """
225+        Non-overridden owner affiliations get yields unsupported error.
226+        """
227+
228+        def cb(result):
229+            self.assertEquals('feature-not-implemented', result.condition)
230+            self.assertEquals('unsupported', result.appCondition.name)
231+            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
232+            self.assertEquals('modify-affiliations',
233+                              result.appCondition['feature'])
234+
235+        d = self.resource.affiliationsGet(pubsub.PubSubRequest())
236+        self.assertFailure(d, error.StanzaError)
237+        d.addCallback(cb)
238+        return d
239+
240+
241+    def test_affiliationsSet(self):
242+        """
243+        Non-overridden owner affiliations set yields unsupported error.
244+        """
245+
246+        def cb(result):
247+            self.assertEquals('feature-not-implemented', result.condition)
248+            self.assertEquals('unsupported', result.appCondition.name)
249+            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
250+            self.assertEquals('modify-affiliations',
251+                              result.appCondition['feature'])
252+
253+        d = self.resource.affiliationsGet(pubsub.PubSubRequest())
254+        self.assertFailure(d, error.StanzaError)
255+        d.addCallback(cb)
256+        return d
Note: See TracBrowser for help on using the repository browser.