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"
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
)
21 <pubsub xmlns="http://jabber.org/protocol/pubsub">
23 <affiliation node="testnode" affiliation="owner" />
24 <affiliation node="testnode2" affiliation="publisher" />
29 def testSubscriptions(self
):
30 "Testing iq/pubsub/subscriptions/subscription stanzas"
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
)
43 <pubsub xmlns="http://jabber.org/protocol/pubsub">
45 <subscription node="testnode" jid="steve@myserver.tld/someresource" />
46 <subscription node="testnode2" jid="boogers@bork.top/bill" subscription="subscribed" />
51 def testOptionalSettings(self
):
52 "Testing iq/pubsub/subscription/subscribe-options stanzas"
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'
60 <pubsub xmlns="http://jabber.org/protocol/pubsub">
61 <subscription node="testnode alsdkjfas" jid="fritzy@netflint.net/sleekxmpp" subscription="unconfigured">
70 "Testing iq/pubsub/items stanzas"
72 iq
['pubsub']['items']['node'] = 'crap'
73 payload
= ET
.fromstring("""
74 <thinger xmlns="http://andyet.net/protocol/thinger" x="1" y='2'>
76 <child2 normandy='cheese' foo='bar' />
78 payload2
= ET
.fromstring("""
79 <thinger2 xmlns="http://andyet.net/protocol/thinger2" x="12" y='22'>
81 <child22 normandy='cheese2' foo='bar2' />
85 item
['payload'] = payload
88 item2
['payload'] = payload2
89 iq
['pubsub']['items'].append(item
)
90 iq
['pubsub']['items'].append(item2
)
93 <pubsub xmlns="http://jabber.org/protocol/pubsub">
96 <thinger xmlns="http://andyet.net/protocol/thinger" y="2" x="1">
98 <child2 foo="bar" normandy="cheese" />
102 <thinger2 xmlns="http://andyet.net/protocol/thinger2" y="22" x="12">
104 <child22 foo="bar2" normandy="cheese2" />
111 def testCreate(self
):
112 "Testing iq/pubsub/create&configure stanzas"
114 iq
['pubsub']['create']['node'] = 'mynode'
115 iq
['pubsub']['configure']['form'].addField('pubsub#title',
117 value
='This thing is awesome')
118 self
.check_iq(iq
, """
120 <pubsub xmlns="http://jabber.org/protocol/pubsub">
121 <create node="mynode" />
123 <x xmlns="jabber:x:data" type="form">
124 <field var="pubsub#title" type="text-single">
125 <value>This thing is awesome</value>
133 "Testing iq/psstate stanzas"
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
, """
141 <state xmlns="http://jabber.org/protocol/psstate" node="mynode" item="myitem">
142 <claimed xmlns="http://andyet.net/protocol/pubsubqueue" />
146 def testDefault(self
):
147 "Testing iq/pubsub_owner/default stanzas"
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',
154 value
='This thing is awesome')
155 self
.check_iq(iq
, """
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>
166 </iq>""", use_values
=False)
168 def testSubscribe(self
):
169 "testing iq/pubsub/subscribe stanzas"
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
, """
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>
192 </iq>""", use_values
=False)
194 def testPublish(self
):
195 "Testing iq/pubsub/publish stanzas"
197 iq
['pubsub']['publish']['node'] = 'thingers'
198 payload
= ET
.fromstring("""
199 <thinger xmlns="http://andyet.net/protocol/thinger" x="1" y='2'>
201 <child2 normandy='cheese' foo='bar' />
203 payload2
= ET
.fromstring("""
204 <thinger2 xmlns="http://andyet.net/protocol/thinger2" x="12" y='22'>
206 <child22 normandy='cheese2' foo='bar2' />
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
, """
219 <pubsub xmlns="http://jabber.org/protocol/pubsub">
220 <publish node="thingers">
222 <thinger xmlns="http://andyet.net/protocol/thinger" y="2" x="1">
224 <child2 foo="bar" normandy="cheese" />
228 <thinger2 xmlns="http://andyet.net/protocol/thinger2" y="22" x="12">
230 <child22 foo="bar2" normandy="cheese2" />
237 def testDelete(self
):
238 "Testing iq/pubsub_owner/delete stanzas"
240 iq
['pubsub_owner']['delete']['node'] = 'thingers'
241 self
.check_iq(iq
, """
243 <pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
244 <delete node="thingers" />
248 def testCreateConfigGet(self
):
249 """Testing getting config from full create"""
251 iq
['to'] = 'pubsub.asdf'
252 iq
['from'] = 'fritzy@asdf/87292ede-524d-4117-9076-d934ed3db8e7'
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',
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',
270 ('pubsub#deliver_payloads', {'type': 'boolean',
271 'label': 'Deliver payloads with event notifications',
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',
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',
287 ('pubsub#subscribe', {'type': 'boolean',
288 'label': 'Whether to allow subscriptions',
290 ('pubsub#access_model', {'type': 'list-single',
291 'label': 'Specify the subscriber model',
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',
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" />
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>
312 <field var="pubsub#node_type" type="list-single" label="Select the node type">
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">
319 <field var="pubsub#deliver_payloads" type="boolean" label="Deliver payloads with event notifications">
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">
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">
332 <field var="pubsub#subscribe" type="boolean" label="Whether to allow subscriptions">
335 <field var="pubsub#access_model" type="list-single" label="Specify the subscriber model">
338 <field var="pubsub#publish_model" type="list-single" label="Specify the publisher model">
339 <value>publishers</value>
341 <field var="pubsub#send_last_published_item" type="list-single" label="Send last published item">
344 <field var="pubsub#presence_based_delivery" type="boolean" label="Deliver notification only to available users" />
350 def testItemEvent(self
):
351 """Testing message/pubsub_event/items/item"""
353 item
= pubsub
.EventItem()
354 pl
= ET
.Element('{http://netflint.net/protocol/test}test', {'failed':'3', 'passed':'24'})
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">
365 <test xmlns="http://netflint.net/protocol/test" failed="3" passed="24" />
371 def testItemsEvent(self
):
372 """Testing multiple message/pubsub_event/items/item"""
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
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">
391 <test xmlns="http://netflint.net/protocol/test" failed="3" passed="24" />
394 <test xmlns="http://netflint.net/protocol/test-other" failed="3" total="27" />
400 def testItemsEvent(self
):
401 """Testing message/pubsub_event/items/item & retract mix"""
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'
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">
423 <test xmlns="http://netflint.net/protocol/test" failed="3" passed="24" />
424 </item><retract id="aabbcc" />
426 <test xmlns="http://netflint.net/protocol/test-other" failed="3" total="27" />
432 def testCollectionAssociate(self
):
433 """Testing message/pubsub_event/collection/associate"""
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" />
447 def testCollectionDisassociate(self
):
448 """Testing message/pubsub_event/collection/disassociate"""
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" />
462 def testEventConfiguration(self
):
463 """Testing message/pubsub_event/configuration/config"""
465 msg
['pubsub_event']['configuration']['node'] = 'cheese'
466 msg
['pubsub_event']['configuration']['form'].addField('pubsub#title',
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>
483 def testEventPurge(self
):
484 """Testing message/pubsub_event/purge"""
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" />
495 def testEventSubscription(self
):
496 """Testing message/pubsub_event/subscription"""
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" />
511 suite
= unittest
.TestLoader().loadTestsFromTestCase(TestPubsubStanzas
)