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