Add the 'connecting' event
[slixmpp.git] / tests / test_stanza_xep_0060.py
blob567757cbdbf9f019a4a542a648c2e15ca21a9e76
1 import unittest
2 from slixmpp.test import SlixTest
3 import slixmpp.plugins.xep_0004 as xep_0004
4 import slixmpp.plugins.xep_0060.stanza as pubsub
5 from slixmpp.xmlstream.stanzabase import ET
8 class TestPubsubStanzas(SlixTest):
10 def testAffiliations(self):
11 "Testing iq/pubsub/affiliations/affiliation stanzas"
12 iq = self.Iq()
13 aff1 = pubsub.Affiliation()
14 aff1['node'] = 'testnode'
15 aff1['affiliation'] = 'owner'
16 aff2 = pubsub.Affiliation()
17 aff2['node'] = 'testnode2'
18 aff2['affiliation'] = 'publisher'
19 iq['pubsub']['affiliations'].append(aff1)
20 iq['pubsub']['affiliations'].append(aff2)
21 self.check(iq, """
22 <iq id="0">
23 <pubsub xmlns="http://jabber.org/protocol/pubsub">
24 <affiliations>
25 <affiliation node="testnode" affiliation="owner" />
26 <affiliation node="testnode2" affiliation="publisher" />
27 </affiliations>
28 </pubsub>
29 </iq>""")
31 def testSubscriptions(self):
32 "Testing iq/pubsub/subscriptions/subscription stanzas"
33 iq = self.Iq()
34 sub1 = pubsub.Subscription()
35 sub1['node'] = 'testnode'
36 sub1['jid'] = 'steve@myserver.tld/someresource'
37 sub2 = pubsub.Subscription()
38 sub2['node'] = 'testnode2'
39 sub2['jid'] = 'boogers@bork.top/bill'
40 sub2['subscription'] = 'subscribed'
41 iq['pubsub']['subscriptions'].append(sub1)
42 iq['pubsub']['subscriptions'].append(sub2)
43 self.check(iq, """
44 <iq id="0">
45 <pubsub xmlns="http://jabber.org/protocol/pubsub">
46 <subscriptions>
47 <subscription node="testnode" jid="steve@myserver.tld/someresource" />
48 <subscription node="testnode2" jid="boogers@bork.top/bill" subscription="subscribed" />
49 </subscriptions>
50 </pubsub>
51 </iq>""")
53 def testOptionalSettings(self):
54 "Testing iq/pubsub/subscription/subscribe-options stanzas"
55 iq = self.Iq()
56 iq['pubsub']['subscription']['suboptions']['required'] = True
57 iq['pubsub']['subscription']['node'] = 'testnode alsdkjfas'
58 iq['pubsub']['subscription']['jid'] = "fritzy@netflint.net/slixmpp"
59 iq['pubsub']['subscription']['subscription'] = 'unconfigured'
60 self.check(iq, """
61 <iq id="0">
62 <pubsub xmlns="http://jabber.org/protocol/pubsub">
63 <subscription node="testnode alsdkjfas" jid="fritzy@netflint.net/slixmpp" subscription="unconfigured">
64 <subscribe-options>
65 <required />
66 </subscribe-options>
67 </subscription>
68 </pubsub>
69 </iq>""")
71 def testItems(self):
72 "Testing iq/pubsub/items stanzas"
73 iq = self.Iq()
74 iq['pubsub']['items']['node'] = 'crap'
75 payload = ET.fromstring("""
76 <thinger xmlns="http://andyet.net/protocol/thinger" x="1" y='2'>
77 <child1 />
78 <child2 normandy='cheese' foo='bar' />
79 </thinger>""")
80 payload2 = ET.fromstring("""
81 <thinger2 xmlns="http://andyet.net/protocol/thinger2" x="12" y='22'>
82 <child12 />
83 <child22 normandy='cheese2' foo='bar2' />
84 </thinger2>""")
85 item = pubsub.Item()
86 item['id'] = 'asdf'
87 item['payload'] = payload
88 item2 = pubsub.Item()
89 item2['id'] = 'asdf2'
90 item2['payload'] = payload2
91 iq['pubsub']['items'].append(item)
92 iq['pubsub']['items'].append(item2)
93 self.check(iq, """
94 <iq id="0">
95 <pubsub xmlns="http://jabber.org/protocol/pubsub">
96 <items node="crap">
97 <item id="asdf">
98 <thinger xmlns="http://andyet.net/protocol/thinger" y="2" x="1">
99 <child1 />
100 <child2 foo="bar" normandy="cheese" />
101 </thinger>
102 </item>
103 <item id="asdf2">
104 <thinger2 xmlns="http://andyet.net/protocol/thinger2" y="22" x="12">
105 <child12 />
106 <child22 foo="bar2" normandy="cheese2" />
107 </thinger2>
108 </item>
109 </items>
110 </pubsub>
111 </iq>""")
113 def testCreate(self):
114 "Testing iq/pubsub/create&configure stanzas"
115 iq = self.Iq()
116 iq['pubsub']['create']['node'] = 'mynode'
117 iq['pubsub']['configure']['form'].addField('pubsub#title',
118 ftype='text-single',
119 value='This thing is awesome')
120 self.check(iq, """
121 <iq id="0">
122 <pubsub xmlns="http://jabber.org/protocol/pubsub">
123 <create node="mynode" />
124 <configure>
125 <x xmlns="jabber:x:data" type="form">
126 <field var="pubsub#title" type="text-single">
127 <value>This thing is awesome</value>
128 </field>
129 </x>
130 </configure>
131 </pubsub>
132 </iq>""")
134 def testDefault(self):
135 "Testing iq/pubsub_owner/default stanzas"
136 iq = self.Iq()
137 iq['pubsub_owner']['default']
138 iq['pubsub_owner']['default']['node'] = 'mynode'
139 iq['pubsub_owner']['default']['form'].addField('pubsub#title',
140 ftype='text-single',
141 value='This thing is awesome')
142 self.check(iq, """
143 <iq id="0">
144 <pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
145 <default node="mynode">
146 <x xmlns="jabber:x:data" type="form">
147 <field var="pubsub#title" type="text-single">
148 <value>This thing is awesome</value>
149 </field>
150 </x>
151 </default>
152 </pubsub>
153 </iq>""", use_values=False)
155 def testSubscribe(self):
156 "testing iq/pubsub/subscribe stanzas"
157 iq = self.Iq()
158 iq['pubsub']['subscribe']['options']
159 iq['pubsub']['subscribe']['node'] = 'cheese'
160 iq['pubsub']['subscribe']['jid'] = 'fritzy@netflint.net/slixmpp'
161 iq['pubsub']['subscribe']['options']['node'] = 'cheese'
162 iq['pubsub']['subscribe']['options']['jid'] = 'fritzy@netflint.net/slixmpp'
163 form = xep_0004.Form()
164 form.addField('pubsub#title', ftype='text-single', value='this thing is awesome')
165 iq['pubsub']['subscribe']['options']['options'] = form
166 self.check(iq, """
167 <iq id="0">
168 <pubsub xmlns="http://jabber.org/protocol/pubsub">
169 <subscribe node="cheese" jid="fritzy@netflint.net/slixmpp">
170 <options node="cheese" jid="fritzy@netflint.net/slixmpp">
171 <x xmlns="jabber:x:data" type="submit">
172 <field var="pubsub#title">
173 <value>this thing is awesome</value>
174 </field>
175 </x>
176 </options>
177 </subscribe>
178 </pubsub>
179 </iq>""", use_values=False)
181 def testPublish(self):
182 "Testing iq/pubsub/publish stanzas"
183 iq = self.Iq()
184 iq['pubsub']['publish']['node'] = 'thingers'
185 payload = ET.fromstring("""
186 <thinger xmlns="http://andyet.net/protocol/thinger" x="1" y='2'>
187 <child1 />
188 <child2 normandy='cheese' foo='bar' />
189 </thinger>""")
190 payload2 = ET.fromstring("""
191 <thinger2 xmlns="http://andyet.net/protocol/thinger2" x="12" y='22'>
192 <child12 />
193 <child22 normandy='cheese2' foo='bar2' />
194 </thinger2>""")
195 item = pubsub.Item()
196 item['id'] = 'asdf'
197 item['payload'] = payload
198 item2 = pubsub.Item()
199 item2['id'] = 'asdf2'
200 item2['payload'] = payload2
201 iq['pubsub']['publish'].append(item)
202 iq['pubsub']['publish'].append(item2)
203 form = xep_0004.Form()
204 form.addField('pubsub#description', ftype='text-single', value='this thing is awesome')
205 iq['pubsub']['publish_options'] = form
207 self.check(iq, """
208 <iq id="0">
209 <pubsub xmlns="http://jabber.org/protocol/pubsub">
210 <publish node="thingers">
211 <item id="asdf">
212 <thinger xmlns="http://andyet.net/protocol/thinger" y="2" x="1">
213 <child1 />
214 <child2 foo="bar" normandy="cheese" />
215 </thinger>
216 </item>
217 <item id="asdf2">
218 <thinger2 xmlns="http://andyet.net/protocol/thinger2" y="22" x="12">
219 <child12 />
220 <child22 foo="bar2" normandy="cheese2" />
221 </thinger2>
222 </item>
223 </publish>
224 <publish-options>
225 <x xmlns="jabber:x:data" type="submit">
226 <field var="pubsub#description">
227 <value>this thing is awesome</value>
228 </field>
229 </x>
230 </publish-options>
231 </pubsub>
232 </iq>""")
234 def testDelete(self):
235 "Testing iq/pubsub_owner/delete stanzas"
236 iq = self.Iq()
237 iq['pubsub_owner']['delete']['node'] = 'thingers'
238 self.check(iq, """
239 <iq id="0">
240 <pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
241 <delete node="thingers" />
242 </pubsub>
243 </iq>""")
245 def testCreateConfigGet(self):
246 """Testing getting config from full create"""
247 iq = self.Iq()
248 iq['to'] = 'pubsub.asdf'
249 iq['from'] = 'fritzy@asdf/87292ede-524d-4117-9076-d934ed3db8e7'
250 iq['type'] = 'set'
251 iq['id'] = 'E'
253 pub = iq['pubsub']
254 pub['create']['node'] = 'testnode2'
255 pub['configure']['form']['type'] = 'submit'
256 pub['configure']['form'].setFields([
257 ('FORM_TYPE', {'type': 'hidden',
258 'value': 'http://jabber.org/protocol/pubsub#node_config'}),
259 ('pubsub#node_type', {'type': 'list-single',
260 'label': 'Select the node type',
261 'value': 'leaf'}),
262 ('pubsub#title', {'type': 'text-single',
263 'label': 'A friendly name for the node'}),
264 ('pubsub#deliver_notifications', {'type': 'boolean',
265 'label': 'Deliver event notifications',
266 'value': True}),
267 ('pubsub#deliver_payloads', {'type': 'boolean',
268 'label': 'Deliver payloads with event notifications',
269 'value': True}),
270 ('pubsub#notify_config', {'type': 'boolean',
271 'label': 'Notify subscribers when the node configuration changes'}),
272 ('pubsub#notify_delete', {'type': 'boolean',
273 'label': 'Notify subscribers when the node is deleted'}),
274 ('pubsub#notify_retract', {'type': 'boolean',
275 'label': 'Notify subscribers when items are removed from the node',
276 'value': True}),
277 ('pubsub#notify_sub', {'type': 'boolean',
278 'label': 'Notify owners about new subscribers and unsubscribes'}),
279 ('pubsub#persist_items', {'type': 'boolean',
280 'label': 'Persist items in storage'}),
281 ('pubsub#max_items', {'type': 'text-single',
282 'label': 'Max # of items to persist',
283 'value': '10'}),
284 ('pubsub#subscribe', {'type': 'boolean',
285 'label': 'Whether to allow subscriptions',
286 'value': True}),
287 ('pubsub#access_model', {'type': 'list-single',
288 'label': 'Specify the subscriber model',
289 'value': 'open'}),
290 ('pubsub#publish_model', {'type': 'list-single',
291 'label': 'Specify the publisher model',
292 'value': 'publishers'}),
293 ('pubsub#send_last_published_item', {'type': 'list-single',
294 'label': 'Send last published item',
295 'value': 'never'}),
296 ('pubsub#presence_based_delivery', {'type': 'boolean',
297 'label': 'Deliver notification only to available users'}),
300 self.check(iq, """
301 <iq to="pubsub.asdf" type="set" id="E" from="fritzy@asdf/87292ede-524d-4117-9076-d934ed3db8e7">
302 <pubsub xmlns="http://jabber.org/protocol/pubsub">
303 <create node="testnode2" />
304 <configure>
305 <x xmlns="jabber:x:data" type="submit">
306 <field var="FORM_TYPE">
307 <value>http://jabber.org/protocol/pubsub#node_config</value>
308 </field>
309 <field var="pubsub#node_type">
310 <value>leaf</value>
311 </field>
312 <field var="pubsub#title" />
313 <field var="pubsub#deliver_notifications">
314 <value>1</value>
315 </field>
316 <field var="pubsub#deliver_payloads">
317 <value>1</value>
318 </field>
319 <field var="pubsub#notify_config" />
320 <field var="pubsub#notify_delete" />
321 <field var="pubsub#notify_retract">
322 <value>1</value>
323 </field>
324 <field var="pubsub#notify_sub" />
325 <field var="pubsub#persist_items" />
326 <field var="pubsub#max_items">
327 <value>10</value>
328 </field>
329 <field var="pubsub#subscribe">
330 <value>1</value>
331 </field>
332 <field var="pubsub#access_model">
333 <value>open</value>
334 </field>
335 <field var="pubsub#publish_model">
336 <value>publishers</value>
337 </field>
338 <field var="pubsub#send_last_published_item">
339 <value>never</value>
340 </field>
341 <field var="pubsub#presence_based_delivery" />
342 </x>
343 </configure>
344 </pubsub>
345 </iq>""")
347 def testItemEvent(self):
348 """Testing message/pubsub_event/items/item"""
349 msg = self.Message()
350 item = pubsub.EventItem()
351 pl = ET.Element('{http://netflint.net/protocol/test}test', {'failed':'3', 'passed':'24'})
352 item['payload'] = pl
353 item['id'] = 'abc123'
354 msg['pubsub_event']['items'].append(item)
355 msg['pubsub_event']['items']['node'] = 'cheese'
356 msg['type'] = 'normal'
357 self.check(msg, """
358 <message type="normal">
359 <event xmlns="http://jabber.org/protocol/pubsub#event">
360 <items node="cheese">
361 <item id="abc123">
362 <test xmlns="http://netflint.net/protocol/test" failed="3" passed="24" />
363 </item>
364 </items>
365 </event>
366 </message>""")
368 def testItemsEvent(self):
369 """Testing multiple message/pubsub_event/items/item"""
370 msg = self.Message()
371 item = pubsub.EventItem()
372 item2 = pubsub.EventItem()
373 pl = ET.Element('{http://netflint.net/protocol/test}test', {'failed':'3', 'passed':'24'})
374 pl2 = ET.Element('{http://netflint.net/protocol/test-other}test', {'total':'27', 'failed':'3'})
375 item2['payload'] = pl2
376 item['payload'] = pl
377 item['id'] = 'abc123'
378 item2['id'] = '123abc'
379 msg['pubsub_event']['items'].append(item)
380 msg['pubsub_event']['items'].append(item2)
381 msg['pubsub_event']['items']['node'] = 'cheese'
382 msg['type'] = 'normal'
383 self.check(msg, """
384 <message type="normal">
385 <event xmlns="http://jabber.org/protocol/pubsub#event">
386 <items node="cheese">
387 <item id="abc123">
388 <test xmlns="http://netflint.net/protocol/test" failed="3" passed="24" />
389 </item>
390 <item id="123abc">
391 <test xmlns="http://netflint.net/protocol/test-other" failed="3" total="27" />
392 </item>
393 </items>
394 </event>
395 </message>""")
397 def testItemsEvent(self):
398 """Testing message/pubsub_event/items/item & retract mix"""
399 msg = self.Message()
400 item = pubsub.EventItem()
401 item2 = pubsub.EventItem()
402 pl = ET.Element('{http://netflint.net/protocol/test}test', {'failed':'3', 'passed':'24'})
403 pl2 = ET.Element('{http://netflint.net/protocol/test-other}test', {'total':'27', 'failed':'3'})
404 item2['payload'] = pl2
405 retract = pubsub.EventRetract()
406 retract['id'] = 'aabbcc'
407 item['payload'] = pl
408 item['id'] = 'abc123'
409 item2['id'] = '123abc'
410 msg['pubsub_event']['items'].append(item)
411 msg['pubsub_event']['items'].append(retract)
412 msg['pubsub_event']['items'].append(item2)
413 msg['pubsub_event']['items']['node'] = 'cheese'
414 msg['type'] = 'normal'
415 self.check(msg, """
416 <message type="normal">
417 <event xmlns="http://jabber.org/protocol/pubsub#event">
418 <items node="cheese">
419 <item id="abc123">
420 <test xmlns="http://netflint.net/protocol/test" failed="3" passed="24" />
421 </item><retract id="aabbcc" />
422 <item id="123abc">
423 <test xmlns="http://netflint.net/protocol/test-other" failed="3" total="27" />
424 </item>
425 </items>
426 </event>
427 </message>""")
429 def testCollectionAssociate(self):
430 """Testing message/pubsub_event/collection/associate"""
431 msg = self.Message()
432 msg['pubsub_event']['collection']['associate']['node'] = 'cheese'
433 msg['pubsub_event']['collection']['node'] = 'cheeseburger'
434 msg['type'] = 'headline'
435 self.check(msg, """
436 <message type="headline">
437 <event xmlns="http://jabber.org/protocol/pubsub#event">
438 <collection node="cheeseburger">
439 <associate node="cheese" />
440 </collection>
441 </event>
442 </message>""")
444 def testCollectionDisassociate(self):
445 """Testing message/pubsub_event/collection/disassociate"""
446 msg = self.Message()
447 msg['pubsub_event']['collection']['disassociate']['node'] = 'cheese'
448 msg['pubsub_event']['collection']['node'] = 'cheeseburger'
449 msg['type'] = 'headline'
450 self.check(msg, """
451 <message type="headline">
452 <event xmlns="http://jabber.org/protocol/pubsub#event">
453 <collection node="cheeseburger">
454 <disassociate node="cheese" />
455 </collection>
456 </event>
457 </message>""")
459 def testEventConfiguration(self):
460 """Testing message/pubsub_event/configuration/config"""
461 msg = self.Message()
462 msg['pubsub_event']['configuration']['node'] = 'cheese'
463 msg['pubsub_event']['configuration']['form'].addField('pubsub#title',
464 ftype='text-single',
465 value='This thing is awesome')
466 msg['type'] = 'headline'
467 self.check(msg, """
468 <message type="headline">
469 <event xmlns="http://jabber.org/protocol/pubsub#event">
470 <configuration node="cheese">
471 <x xmlns="jabber:x:data" type="form">
472 <field var="pubsub#title" type="text-single">
473 <value>This thing is awesome</value>
474 </field>
475 </x>
476 </configuration>
477 </event>
478 </message>""")
480 def testEventPurge(self):
481 """Testing message/pubsub_event/purge"""
482 msg = self.Message()
483 msg['pubsub_event']['purge']['node'] = 'pickles'
484 msg['type'] = 'headline'
485 self.check(msg, """
486 <message type="headline">
487 <event xmlns="http://jabber.org/protocol/pubsub#event">
488 <purge node="pickles" />
489 </event>
490 </message>""")
492 def testEventSubscription(self):
493 """Testing message/pubsub_event/subscription"""
494 msg = self.Message()
495 msg['pubsub_event']['subscription']['node'] = 'pickles'
496 msg['pubsub_event']['subscription']['jid'] = 'fritzy@netflint.net/test'
497 msg['pubsub_event']['subscription']['subid'] = 'aabb1122'
498 msg['pubsub_event']['subscription']['subscription'] = 'subscribed'
499 msg['pubsub_event']['subscription']['expiry'] = 'presence'
500 msg['type'] = 'headline'
501 self.check(msg, """
502 <message type="headline">
503 <event xmlns="http://jabber.org/protocol/pubsub#event">
504 <subscription node="pickles" subid="aabb1122" jid="fritzy@netflint.net/test" subscription="subscribed" expiry="presence" />
505 </event>
506 </message>""")
508 def testPubsubError(self):
509 """Test getting a pubsub specific condition from an error stanza"""
510 iq = self.Iq()
511 iq['error']['type'] = 'cancel'
512 iq['error']['code'] = '501'
513 iq['error']['condition'] = 'feature-not-implemented'
514 iq['error']['pubsub']['condition'] = 'subid-required'
515 self.check(iq, """
516 <iq type="error">
517 <error type="cancel" code="501">
518 <feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
519 <subid-required xmlns="http://jabber.org/protocol/pubsub#errors" />
520 </error>
521 </iq>
522 """, use_values=False)
524 del iq['error']['pubsub']['condition']
525 self.check(iq, """
526 <iq type="error">
527 <error type="cancel" code="501">
528 <feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
529 </error>
530 </iq>
531 """, use_values=False)
533 def testPubsubUnsupportedError(self):
534 """Test getting the feature from an unsupported error"""
535 iq = self.Iq()
536 iq['error']['type'] = 'cancel'
537 iq['error']['code'] = '501'
538 iq['error']['condition'] = 'feature-not-implemented'
539 iq['error']['pubsub']['condition'] = 'unsupported'
540 iq['error']['pubsub']['unsupported'] = 'instant-node'
541 self.check(iq, """
542 <iq type="error">
543 <error type="cancel" code="501">
544 <feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
545 <unsupported xmlns="http://jabber.org/protocol/pubsub#errors" feature="instant-node" />
546 </error>
547 </iq>
548 """, use_values=False)
550 self.assertEqual(iq['error']['pubsub']['condition'], 'unsupported')
551 self.assertEqual(iq['error']['pubsub']['unsupported'], 'instant-node')
553 del iq['error']['pubsub']['unsupported']
554 self.check(iq, """
555 <iq type="error">
556 <error type="cancel" code="501">
557 <feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
558 </error>
559 </iq>
560 """, use_values=False)
563 suite = unittest.TestLoader().loadTestsFromTestCase(TestPubsubStanzas)