1 | # Copyright (c) 2003-2008 Ralph Meijer |
---|
2 | # See LICENSE for details. |
---|
3 | |
---|
4 | """ |
---|
5 | Tests for L{wokkel.pubsub} |
---|
6 | """ |
---|
7 | |
---|
8 | from zope.interface import verify |
---|
9 | |
---|
10 | from twisted.trial import unittest |
---|
11 | from twisted.internet import defer |
---|
12 | from twisted.words.xish import domish, xpath |
---|
13 | from twisted.words.protocols.jabber import error |
---|
14 | from twisted.words.protocols.jabber.jid import JID |
---|
15 | |
---|
16 | from wokkel import data_form, iwokkel, pubsub, shim |
---|
17 | from wokkel.generic import parseXml |
---|
18 | from wokkel.test.helpers import XmlStreamStub |
---|
19 | |
---|
20 | try: |
---|
21 | from twisted.words.protocols.jabber.xmlstream import toResponse |
---|
22 | except ImportError: |
---|
23 | from wokkel.compat import toResponse |
---|
24 | |
---|
25 | NS_PUBSUB = 'http://jabber.org/protocol/pubsub' |
---|
26 | NS_PUBSUB_CONFIG = 'http://jabber.org/protocol/pubsub#node_config' |
---|
27 | NS_PUBSUB_ERRORS = 'http://jabber.org/protocol/pubsub#errors' |
---|
28 | NS_PUBSUB_EVENT = 'http://jabber.org/protocol/pubsub#event' |
---|
29 | NS_PUBSUB_OWNER = 'http://jabber.org/protocol/pubsub#owner' |
---|
30 | |
---|
31 | def calledAsync(fn): |
---|
32 | """ |
---|
33 | Function wrapper that fires a deferred upon calling the given function. |
---|
34 | """ |
---|
35 | d = defer.Deferred() |
---|
36 | |
---|
37 | def func(*args, **kwargs): |
---|
38 | try: |
---|
39 | result = fn(*args, **kwargs) |
---|
40 | except: |
---|
41 | d.errback() |
---|
42 | else: |
---|
43 | d.callback(result) |
---|
44 | |
---|
45 | return d, func |
---|
46 | |
---|
47 | |
---|
48 | class PubSubClientTest(unittest.TestCase): |
---|
49 | timeout = 2 |
---|
50 | |
---|
51 | def setUp(self): |
---|
52 | self.stub = XmlStreamStub() |
---|
53 | self.protocol = pubsub.PubSubClient() |
---|
54 | self.protocol.xmlstream = self.stub.xmlstream |
---|
55 | self.protocol.connectionInitialized() |
---|
56 | |
---|
57 | |
---|
58 | def test_interface(self): |
---|
59 | """ |
---|
60 | Do instances of L{pubsub.PubSubClient} provide L{iwokkel.IPubSubClient}? |
---|
61 | """ |
---|
62 | verify.verifyObject(iwokkel.IPubSubClient, self.protocol) |
---|
63 | |
---|
64 | |
---|
65 | def test_eventItems(self): |
---|
66 | """ |
---|
67 | Test receiving an items event resulting in a call to itemsReceived. |
---|
68 | """ |
---|
69 | message = domish.Element((None, 'message')) |
---|
70 | message['from'] = 'pubsub.example.org' |
---|
71 | message['to'] = 'user@example.org/home' |
---|
72 | event = message.addElement((NS_PUBSUB_EVENT, 'event')) |
---|
73 | items = event.addElement('items') |
---|
74 | items['node'] = 'test' |
---|
75 | item1 = items.addElement('item') |
---|
76 | item1['id'] = 'item1' |
---|
77 | item2 = items.addElement('retract') |
---|
78 | item2['id'] = 'item2' |
---|
79 | item3 = items.addElement('item') |
---|
80 | item3['id'] = 'item3' |
---|
81 | |
---|
82 | def itemsReceived(event): |
---|
83 | self.assertEquals(JID('user@example.org/home'), event.recipient) |
---|
84 | self.assertEquals(JID('pubsub.example.org'), event.sender) |
---|
85 | self.assertEquals('test', event.nodeIdentifier) |
---|
86 | self.assertEquals([item1, item2, item3], event.items) |
---|
87 | |
---|
88 | d, self.protocol.itemsReceived = calledAsync(itemsReceived) |
---|
89 | self.stub.send(message) |
---|
90 | return d |
---|
91 | |
---|
92 | |
---|
93 | def test_eventItemsCollection(self): |
---|
94 | """ |
---|
95 | Test receiving an items event resulting in a call to itemsReceived. |
---|
96 | """ |
---|
97 | message = domish.Element((None, 'message')) |
---|
98 | message['from'] = 'pubsub.example.org' |
---|
99 | message['to'] = 'user@example.org/home' |
---|
100 | event = message.addElement((NS_PUBSUB_EVENT, 'event')) |
---|
101 | items = event.addElement('items') |
---|
102 | items['node'] = 'test' |
---|
103 | |
---|
104 | headers = shim.Headers([('Collection', 'collection')]) |
---|
105 | message.addChild(headers) |
---|
106 | |
---|
107 | def itemsReceived(event): |
---|
108 | self.assertEquals(JID('user@example.org/home'), event.recipient) |
---|
109 | self.assertEquals(JID('pubsub.example.org'), event.sender) |
---|
110 | self.assertEquals('test', event.nodeIdentifier) |
---|
111 | self.assertEquals({'Collection': ['collection']}, event.headers) |
---|
112 | |
---|
113 | d, self.protocol.itemsReceived = calledAsync(itemsReceived) |
---|
114 | self.stub.send(message) |
---|
115 | return d |
---|
116 | |
---|
117 | |
---|
118 | def test_event_delete(self): |
---|
119 | """ |
---|
120 | Test receiving a delete event resulting in a call to deleteReceived. |
---|
121 | """ |
---|
122 | message = domish.Element((None, 'message')) |
---|
123 | message['from'] = 'pubsub.example.org' |
---|
124 | message['to'] = 'user@example.org/home' |
---|
125 | event = message.addElement((NS_PUBSUB_EVENT, 'event')) |
---|
126 | items = event.addElement('delete') |
---|
127 | items['node'] = 'test' |
---|
128 | |
---|
129 | def deleteReceived(event): |
---|
130 | self.assertEquals(JID('user@example.org/home'), event.recipient) |
---|
131 | self.assertEquals(JID('pubsub.example.org'), event.sender) |
---|
132 | self.assertEquals('test', event.nodeIdentifier) |
---|
133 | |
---|
134 | d, self.protocol.deleteReceived = calledAsync(deleteReceived) |
---|
135 | self.stub.send(message) |
---|
136 | return d |
---|
137 | |
---|
138 | |
---|
139 | def test_event_purge(self): |
---|
140 | """ |
---|
141 | Test receiving a purge event resulting in a call to purgeReceived. |
---|
142 | """ |
---|
143 | message = domish.Element((None, 'message')) |
---|
144 | message['from'] = 'pubsub.example.org' |
---|
145 | message['to'] = 'user@example.org/home' |
---|
146 | event = message.addElement((NS_PUBSUB_EVENT, 'event')) |
---|
147 | items = event.addElement('purge') |
---|
148 | items['node'] = 'test' |
---|
149 | |
---|
150 | def purgeReceived(event): |
---|
151 | self.assertEquals(JID('user@example.org/home'), event.recipient) |
---|
152 | self.assertEquals(JID('pubsub.example.org'), event.sender) |
---|
153 | self.assertEquals('test', event.nodeIdentifier) |
---|
154 | |
---|
155 | d, self.protocol.purgeReceived = calledAsync(purgeReceived) |
---|
156 | self.stub.send(message) |
---|
157 | return d |
---|
158 | |
---|
159 | |
---|
160 | def test_createNode(self): |
---|
161 | """ |
---|
162 | Test sending create request. |
---|
163 | """ |
---|
164 | |
---|
165 | def cb(nodeIdentifier): |
---|
166 | self.assertEquals('test', nodeIdentifier) |
---|
167 | |
---|
168 | d = self.protocol.createNode(JID('pubsub.example.org'), 'test') |
---|
169 | d.addCallback(cb) |
---|
170 | |
---|
171 | iq = self.stub.output[-1] |
---|
172 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
173 | self.assertEquals('set', iq.getAttribute('type')) |
---|
174 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
175 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
176 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
177 | 'create', NS_PUBSUB)) |
---|
178 | self.assertEquals(1, len(children)) |
---|
179 | child = children[0] |
---|
180 | self.assertEquals('test', child['node']) |
---|
181 | |
---|
182 | response = toResponse(iq, 'result') |
---|
183 | self.stub.send(response) |
---|
184 | return d |
---|
185 | |
---|
186 | |
---|
187 | def test_createNodeInstant(self): |
---|
188 | """ |
---|
189 | Test sending create request resulting in an instant node. |
---|
190 | """ |
---|
191 | |
---|
192 | def cb(nodeIdentifier): |
---|
193 | self.assertEquals('test', nodeIdentifier) |
---|
194 | |
---|
195 | d = self.protocol.createNode(JID('pubsub.example.org')) |
---|
196 | d.addCallback(cb) |
---|
197 | |
---|
198 | iq = self.stub.output[-1] |
---|
199 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
200 | 'create', NS_PUBSUB)) |
---|
201 | child = children[0] |
---|
202 | self.assertFalse(child.hasAttribute('node')) |
---|
203 | |
---|
204 | response = toResponse(iq, 'result') |
---|
205 | command = response.addElement((NS_PUBSUB, 'pubsub')) |
---|
206 | create = command.addElement('create') |
---|
207 | create['node'] = 'test' |
---|
208 | self.stub.send(response) |
---|
209 | return d |
---|
210 | |
---|
211 | |
---|
212 | def test_createNodeRenamed(self): |
---|
213 | """ |
---|
214 | Test sending create request resulting in renamed node. |
---|
215 | """ |
---|
216 | |
---|
217 | def cb(nodeIdentifier): |
---|
218 | self.assertEquals('test2', nodeIdentifier) |
---|
219 | |
---|
220 | d = self.protocol.createNode(JID('pubsub.example.org'), 'test') |
---|
221 | d.addCallback(cb) |
---|
222 | |
---|
223 | iq = self.stub.output[-1] |
---|
224 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
225 | 'create', NS_PUBSUB)) |
---|
226 | child = children[0] |
---|
227 | self.assertEquals('test', child['node']) |
---|
228 | |
---|
229 | response = toResponse(iq, 'result') |
---|
230 | command = response.addElement((NS_PUBSUB, 'pubsub')) |
---|
231 | create = command.addElement('create') |
---|
232 | create['node'] = 'test2' |
---|
233 | self.stub.send(response) |
---|
234 | return d |
---|
235 | |
---|
236 | |
---|
237 | def test_deleteNode(self): |
---|
238 | """ |
---|
239 | Test sending delete request. |
---|
240 | """ |
---|
241 | |
---|
242 | d = self.protocol.deleteNode(JID('pubsub.example.org'), 'test') |
---|
243 | |
---|
244 | iq = self.stub.output[-1] |
---|
245 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
246 | self.assertEquals('set', iq.getAttribute('type')) |
---|
247 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
248 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
249 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
250 | 'delete', NS_PUBSUB)) |
---|
251 | self.assertEquals(1, len(children)) |
---|
252 | child = children[0] |
---|
253 | self.assertEquals('test', child['node']) |
---|
254 | |
---|
255 | response = toResponse(iq, 'result') |
---|
256 | self.stub.send(response) |
---|
257 | return d |
---|
258 | |
---|
259 | |
---|
260 | def test_publish(self): |
---|
261 | """ |
---|
262 | Test sending publish request. |
---|
263 | """ |
---|
264 | |
---|
265 | item = pubsub.Item() |
---|
266 | d = self.protocol.publish(JID('pubsub.example.org'), 'test', [item]) |
---|
267 | |
---|
268 | iq = self.stub.output[-1] |
---|
269 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
270 | self.assertEquals('set', iq.getAttribute('type')) |
---|
271 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
272 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
273 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
274 | 'publish', NS_PUBSUB)) |
---|
275 | self.assertEquals(1, len(children)) |
---|
276 | child = children[0] |
---|
277 | self.assertEquals('test', child['node']) |
---|
278 | items = list(domish.generateElementsQNamed(child.children, |
---|
279 | 'item', NS_PUBSUB)) |
---|
280 | self.assertEquals(1, len(items)) |
---|
281 | self.assertIdentical(item, items[0]) |
---|
282 | |
---|
283 | response = toResponse(iq, 'result') |
---|
284 | self.stub.send(response) |
---|
285 | return d |
---|
286 | |
---|
287 | |
---|
288 | def test_publishNoItems(self): |
---|
289 | """ |
---|
290 | Test sending publish request without items. |
---|
291 | """ |
---|
292 | |
---|
293 | d = self.protocol.publish(JID('pubsub.example.org'), 'test') |
---|
294 | |
---|
295 | iq = self.stub.output[-1] |
---|
296 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
297 | self.assertEquals('set', iq.getAttribute('type')) |
---|
298 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
299 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
300 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
301 | 'publish', NS_PUBSUB)) |
---|
302 | self.assertEquals(1, len(children)) |
---|
303 | child = children[0] |
---|
304 | self.assertEquals('test', child['node']) |
---|
305 | |
---|
306 | response = toResponse(iq, 'result') |
---|
307 | self.stub.send(response) |
---|
308 | return d |
---|
309 | |
---|
310 | |
---|
311 | def test_subscribe(self): |
---|
312 | """ |
---|
313 | Test sending subscription request. |
---|
314 | """ |
---|
315 | d = self.protocol.subscribe(JID('pubsub.example.org'), 'test', |
---|
316 | JID('user@example.org')) |
---|
317 | |
---|
318 | iq = self.stub.output[-1] |
---|
319 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
320 | self.assertEquals('set', iq.getAttribute('type')) |
---|
321 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
322 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
323 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
324 | 'subscribe', NS_PUBSUB)) |
---|
325 | self.assertEquals(1, len(children)) |
---|
326 | child = children[0] |
---|
327 | self.assertEquals('test', child['node']) |
---|
328 | self.assertEquals('user@example.org', child['jid']) |
---|
329 | |
---|
330 | response = toResponse(iq, 'result') |
---|
331 | pubsub = response.addElement((NS_PUBSUB, 'pubsub')) |
---|
332 | subscription = pubsub.addElement('subscription') |
---|
333 | subscription['node'] = 'test' |
---|
334 | subscription['jid'] = 'user@example.org' |
---|
335 | subscription['subscription'] = 'subscribed' |
---|
336 | self.stub.send(response) |
---|
337 | return d |
---|
338 | |
---|
339 | |
---|
340 | def test_subscribePending(self): |
---|
341 | """ |
---|
342 | Test sending subscription request that results in a pending |
---|
343 | subscription. |
---|
344 | """ |
---|
345 | d = self.protocol.subscribe(JID('pubsub.example.org'), 'test', |
---|
346 | JID('user@example.org')) |
---|
347 | |
---|
348 | iq = self.stub.output[-1] |
---|
349 | response = toResponse(iq, 'result') |
---|
350 | command = response.addElement((NS_PUBSUB, 'pubsub')) |
---|
351 | subscription = command.addElement('subscription') |
---|
352 | subscription['node'] = 'test' |
---|
353 | subscription['jid'] = 'user@example.org' |
---|
354 | subscription['subscription'] = 'pending' |
---|
355 | self.stub.send(response) |
---|
356 | self.assertFailure(d, pubsub.SubscriptionPending) |
---|
357 | return d |
---|
358 | |
---|
359 | |
---|
360 | def test_subscribeUnconfigured(self): |
---|
361 | """ |
---|
362 | Test sending subscription request that results in an unconfigured |
---|
363 | subscription. |
---|
364 | """ |
---|
365 | d = self.protocol.subscribe(JID('pubsub.example.org'), 'test', |
---|
366 | JID('user@example.org')) |
---|
367 | |
---|
368 | iq = self.stub.output[-1] |
---|
369 | response = toResponse(iq, 'result') |
---|
370 | command = response.addElement((NS_PUBSUB, 'pubsub')) |
---|
371 | subscription = command.addElement('subscription') |
---|
372 | subscription['node'] = 'test' |
---|
373 | subscription['jid'] = 'user@example.org' |
---|
374 | subscription['subscription'] = 'unconfigured' |
---|
375 | self.stub.send(response) |
---|
376 | self.assertFailure(d, pubsub.SubscriptionUnconfigured) |
---|
377 | return d |
---|
378 | |
---|
379 | |
---|
380 | def test_unsubscribe(self): |
---|
381 | """ |
---|
382 | Test sending unsubscription request. |
---|
383 | """ |
---|
384 | d = self.protocol.unsubscribe(JID('pubsub.example.org'), 'test', |
---|
385 | JID('user@example.org')) |
---|
386 | |
---|
387 | iq = self.stub.output[-1] |
---|
388 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
389 | self.assertEquals('set', iq.getAttribute('type')) |
---|
390 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
391 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
392 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
393 | 'unsubscribe', NS_PUBSUB)) |
---|
394 | self.assertEquals(1, len(children)) |
---|
395 | child = children[0] |
---|
396 | self.assertEquals('test', child['node']) |
---|
397 | self.assertEquals('user@example.org', child['jid']) |
---|
398 | |
---|
399 | self.stub.send(toResponse(iq, 'result')) |
---|
400 | return d |
---|
401 | |
---|
402 | |
---|
403 | def test_items(self): |
---|
404 | """ |
---|
405 | Test sending items request. |
---|
406 | """ |
---|
407 | def cb(items): |
---|
408 | self.assertEquals([], items) |
---|
409 | |
---|
410 | d = self.protocol.items(JID('pubsub.example.org'), 'test') |
---|
411 | d.addCallback(cb) |
---|
412 | |
---|
413 | iq = self.stub.output[-1] |
---|
414 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
415 | self.assertEquals('get', iq.getAttribute('type')) |
---|
416 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
417 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
418 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
419 | 'items', NS_PUBSUB)) |
---|
420 | self.assertEquals(1, len(children)) |
---|
421 | child = children[0] |
---|
422 | self.assertEquals('test', child['node']) |
---|
423 | |
---|
424 | response = toResponse(iq, 'result') |
---|
425 | items = response.addElement((NS_PUBSUB, 'pubsub')).addElement('items') |
---|
426 | items['node'] = 'test' |
---|
427 | |
---|
428 | self.stub.send(response) |
---|
429 | |
---|
430 | return d |
---|
431 | |
---|
432 | |
---|
433 | def test_itemsMaxItems(self): |
---|
434 | """ |
---|
435 | Test sending items request, with limit on the number of items. |
---|
436 | """ |
---|
437 | def cb(items): |
---|
438 | self.assertEquals(2, len(items)) |
---|
439 | self.assertEquals([item1, item2], items) |
---|
440 | |
---|
441 | d = self.protocol.items(JID('pubsub.example.org'), 'test', maxItems=2) |
---|
442 | d.addCallback(cb) |
---|
443 | |
---|
444 | iq = self.stub.output[-1] |
---|
445 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
446 | self.assertEquals('get', iq.getAttribute('type')) |
---|
447 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
448 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
449 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
450 | 'items', NS_PUBSUB)) |
---|
451 | self.assertEquals(1, len(children)) |
---|
452 | child = children[0] |
---|
453 | self.assertEquals('test', child['node']) |
---|
454 | self.assertEquals('2', child['max_items']) |
---|
455 | |
---|
456 | response = toResponse(iq, 'result') |
---|
457 | items = response.addElement((NS_PUBSUB, 'pubsub')).addElement('items') |
---|
458 | items['node'] = 'test' |
---|
459 | item1 = items.addElement('item') |
---|
460 | item1['id'] = 'item1' |
---|
461 | item2 = items.addElement('item') |
---|
462 | item2['id'] = 'item2' |
---|
463 | |
---|
464 | self.stub.send(response) |
---|
465 | |
---|
466 | return d |
---|
467 | |
---|
468 | |
---|
469 | |
---|
470 | class PubSubServiceTest(unittest.TestCase): |
---|
471 | """ |
---|
472 | Tests for L{pubsub.PubSubService}. |
---|
473 | """ |
---|
474 | |
---|
475 | def setUp(self): |
---|
476 | self.service = pubsub.PubSubService() |
---|
477 | |
---|
478 | def handleRequest(self, xml): |
---|
479 | """ |
---|
480 | Find a handler and call it directly |
---|
481 | """ |
---|
482 | handler = None |
---|
483 | iq = parseXml(xml) |
---|
484 | for queryString, method in self.service.iqHandlers.iteritems(): |
---|
485 | if xpath.internQuery(queryString).matches(iq): |
---|
486 | handler = getattr(self.service, method) |
---|
487 | |
---|
488 | if handler: |
---|
489 | d = defer.maybeDeferred(handler, iq) |
---|
490 | else: |
---|
491 | d = defer.fail(NotImplementedError()) |
---|
492 | |
---|
493 | return d |
---|
494 | |
---|
495 | |
---|
496 | def test_interface(self): |
---|
497 | """ |
---|
498 | Do instances of L{pubsub.PubSubService} provide L{iwokkel.IPubSubService}? |
---|
499 | """ |
---|
500 | verify.verifyObject(iwokkel.IPubSubService, self.service) |
---|
501 | |
---|
502 | |
---|
503 | def test_onPublishNoNode(self): |
---|
504 | """ |
---|
505 | The root node is always a collection, publishing is a bad request. |
---|
506 | """ |
---|
507 | xml = """ |
---|
508 | <iq type='set' to='pubsub.example.org' |
---|
509 | from='user@example.org'> |
---|
510 | <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
---|
511 | <publish/> |
---|
512 | </pubsub> |
---|
513 | </iq> |
---|
514 | """ |
---|
515 | |
---|
516 | def cb(result): |
---|
517 | self.assertEquals('bad-request', result.condition) |
---|
518 | |
---|
519 | d = self.handleRequest(xml) |
---|
520 | self.assertFailure(d, error.StanzaError) |
---|
521 | d.addCallback(cb) |
---|
522 | return d |
---|
523 | |
---|
524 | |
---|
525 | def test_onPublish(self): |
---|
526 | """ |
---|
527 | A publish request should result in L{PubSubService.publish} being |
---|
528 | called. |
---|
529 | """ |
---|
530 | |
---|
531 | xml = """ |
---|
532 | <iq type='set' to='pubsub.example.org' |
---|
533 | from='user@example.org'> |
---|
534 | <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
---|
535 | <publish node='test'/> |
---|
536 | </pubsub> |
---|
537 | </iq> |
---|
538 | """ |
---|
539 | |
---|
540 | def publish(requestor, service, nodeIdentifier, items): |
---|
541 | self.assertEqual(JID('user@example.org'), requestor) |
---|
542 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
543 | self.assertEqual('test', nodeIdentifier) |
---|
544 | self.assertEqual([], items) |
---|
545 | return defer.succeed(None) |
---|
546 | |
---|
547 | self.service.publish = publish |
---|
548 | return self.handleRequest(xml) |
---|
549 | |
---|
550 | |
---|
551 | def test_onOptionsGet(self): |
---|
552 | """ |
---|
553 | Subscription options are not supported. |
---|
554 | """ |
---|
555 | |
---|
556 | xml = """ |
---|
557 | <iq type='get' to='pubsub.example.org' |
---|
558 | from='user@example.org'> |
---|
559 | <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
---|
560 | <options/> |
---|
561 | </pubsub> |
---|
562 | </iq> |
---|
563 | """ |
---|
564 | |
---|
565 | def cb(result): |
---|
566 | self.assertEquals('feature-not-implemented', result.condition) |
---|
567 | self.assertEquals('unsupported', result.appCondition.name) |
---|
568 | self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri) |
---|
569 | |
---|
570 | d = self.handleRequest(xml) |
---|
571 | self.assertFailure(d, error.StanzaError) |
---|
572 | d.addCallback(cb) |
---|
573 | return d |
---|
574 | |
---|
575 | |
---|
576 | def test_onSubscriptions(self): |
---|
577 | """ |
---|
578 | A subscriptions request should result in |
---|
579 | L{PubSubService.subscriptions} being called and the result prepared |
---|
580 | for the response. |
---|
581 | """ |
---|
582 | |
---|
583 | xml = """ |
---|
584 | <iq type='get' to='pubsub.example.org' |
---|
585 | from='user@example.org'> |
---|
586 | <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
---|
587 | <subscriptions/> |
---|
588 | </pubsub> |
---|
589 | </iq> |
---|
590 | """ |
---|
591 | |
---|
592 | def cb(element): |
---|
593 | self.assertEqual('pubsub', element.name) |
---|
594 | self.assertEqual(NS_PUBSUB, element.uri) |
---|
595 | self.assertEqual(NS_PUBSUB, element.subscriptions.uri) |
---|
596 | children = list(element.subscriptions.elements()) |
---|
597 | self.assertEqual(1, len(children)) |
---|
598 | subscription = children[0] |
---|
599 | self.assertEqual('subscription', subscription.name) |
---|
600 | self.assertEqual(NS_PUBSUB, subscription.uri) |
---|
601 | self.assertEqual('user@example.org', subscription['jid']) |
---|
602 | self.assertEqual('test', subscription['node']) |
---|
603 | self.assertEqual('subscribed', subscription['subscription']) |
---|
604 | |
---|
605 | |
---|
606 | def subscriptions(requestor, service): |
---|
607 | self.assertEqual(JID('user@example.org'), requestor) |
---|
608 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
609 | subscription = pubsub.Subscription('test', JID('user@example.org'), |
---|
610 | 'subscribed') |
---|
611 | return defer.succeed([subscription]) |
---|
612 | |
---|
613 | self.service.subscriptions = subscriptions |
---|
614 | d = self.handleRequest(xml) |
---|
615 | d.addCallback(cb) |
---|
616 | return d |
---|
617 | |
---|
618 | |
---|
619 | def test_onDefault(self): |
---|
620 | """ |
---|
621 | A default request should result in |
---|
622 | L{PubSubService.getDefaultConfiguration} being called. |
---|
623 | """ |
---|
624 | |
---|
625 | xml = """ |
---|
626 | <iq type='get' to='pubsub.example.org' |
---|
627 | from='user@example.org'> |
---|
628 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
629 | <default/> |
---|
630 | </pubsub> |
---|
631 | </iq> |
---|
632 | """ |
---|
633 | |
---|
634 | def getConfigurationOptions(): |
---|
635 | return { |
---|
636 | "pubsub#persist_items": |
---|
637 | {"type": "boolean", |
---|
638 | "label": "Persist items to storage"}, |
---|
639 | "pubsub#deliver_payloads": |
---|
640 | {"type": "boolean", |
---|
641 | "label": "Deliver payloads with event notifications"} |
---|
642 | } |
---|
643 | |
---|
644 | def getDefaultConfiguration(requestor, service, nodeType): |
---|
645 | self.assertEqual(JID('user@example.org'), requestor) |
---|
646 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
647 | self.assertEqual('leaf', nodeType) |
---|
648 | return defer.succeed({}) |
---|
649 | |
---|
650 | def cb(element): |
---|
651 | self.assertEqual('pubsub', element.name) |
---|
652 | self.assertEqual(NS_PUBSUB_OWNER, element.uri) |
---|
653 | self.assertEqual(NS_PUBSUB_OWNER, element.default.uri) |
---|
654 | form = data_form.Form.fromElement(element.default.x) |
---|
655 | self.assertEqual(NS_PUBSUB_CONFIG, form.formNamespace) |
---|
656 | |
---|
657 | self.service.getConfigurationOptions = getConfigurationOptions |
---|
658 | self.service.getDefaultConfiguration = getDefaultConfiguration |
---|
659 | d = self.handleRequest(xml) |
---|
660 | d.addCallback(cb) |
---|
661 | return d |
---|
662 | |
---|
663 | |
---|
664 | def test_onConfigureGet(self): |
---|
665 | """ |
---|
666 | On a node configuration get request L{PubSubService.getConfiguration} |
---|
667 | is called and results in a data form with the configuration. |
---|
668 | """ |
---|
669 | |
---|
670 | xml = """ |
---|
671 | <iq type='get' to='pubsub.example.org' |
---|
672 | from='user@example.org'> |
---|
673 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
674 | <configure node='test'/> |
---|
675 | </pubsub> |
---|
676 | </iq> |
---|
677 | """ |
---|
678 | |
---|
679 | def getConfigurationOptions(): |
---|
680 | return { |
---|
681 | "pubsub#persist_items": |
---|
682 | {"type": "boolean", |
---|
683 | "label": "Persist items to storage"}, |
---|
684 | "pubsub#deliver_payloads": |
---|
685 | {"type": "boolean", |
---|
686 | "label": "Deliver payloads with event notifications"} |
---|
687 | } |
---|
688 | |
---|
689 | def getConfiguration(requestor, service, nodeIdentifier): |
---|
690 | self.assertEqual(JID('user@example.org'), requestor) |
---|
691 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
692 | self.assertEqual('test', nodeIdentifier) |
---|
693 | |
---|
694 | return defer.succeed({'pubsub#deliver_payloads': '0', |
---|
695 | 'pubsub#persist_items': '1'}) |
---|
696 | |
---|
697 | def cb(element): |
---|
698 | self.assertEqual('pubsub', element.name) |
---|
699 | self.assertEqual(NS_PUBSUB_OWNER, element.uri) |
---|
700 | self.assertEqual(NS_PUBSUB_OWNER, element.configure.uri) |
---|
701 | form = data_form.Form.fromElement(element.configure.x) |
---|
702 | self.assertEqual(NS_PUBSUB_CONFIG, form.formNamespace) |
---|
703 | fields = form.fields |
---|
704 | |
---|
705 | self.assertIn('pubsub#deliver_payloads', fields) |
---|
706 | field = fields['pubsub#deliver_payloads'] |
---|
707 | self.assertEqual('boolean', field.fieldType) |
---|
708 | self.assertEqual(False, field.value) |
---|
709 | |
---|
710 | self.assertIn('pubsub#persist_items', fields) |
---|
711 | field = fields['pubsub#persist_items'] |
---|
712 | self.assertEqual('boolean', field.fieldType) |
---|
713 | self.assertEqual(True, field.value) |
---|
714 | |
---|
715 | self.service.getConfigurationOptions = getConfigurationOptions |
---|
716 | self.service.getConfiguration = getConfiguration |
---|
717 | d = self.handleRequest(xml) |
---|
718 | d.addCallback(cb) |
---|
719 | return d |
---|
720 | |
---|
721 | |
---|
722 | def test_onConfigureSet(self): |
---|
723 | """ |
---|
724 | On a node configuration set request the Data Form is parsed and |
---|
725 | L{PubSubService.setConfiguration} is called with the passed options. |
---|
726 | """ |
---|
727 | |
---|
728 | xml = """ |
---|
729 | <iq type='set' to='pubsub.example.org' |
---|
730 | from='user@example.org'> |
---|
731 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
732 | <configure node='test'> |
---|
733 | <x xmlns='jabber:x:data' type='submit'> |
---|
734 | <field var='FORM_TYPE' type='hidden'> |
---|
735 | <value>http://jabber.org/protocol/pubsub#node_config</value> |
---|
736 | </field> |
---|
737 | <field var='pubsub#deliver_payloads'><value>0</value></field> |
---|
738 | <field var='pubsub#persist_items'><value>1</value></field> |
---|
739 | </x> |
---|
740 | </configure> |
---|
741 | </pubsub> |
---|
742 | </iq> |
---|
743 | """ |
---|
744 | |
---|
745 | def getConfigurationOptions(): |
---|
746 | return { |
---|
747 | "pubsub#persist_items": |
---|
748 | {"type": "boolean", |
---|
749 | "label": "Persist items to storage"}, |
---|
750 | "pubsub#deliver_payloads": |
---|
751 | {"type": "boolean", |
---|
752 | "label": "Deliver payloads with event notifications"} |
---|
753 | } |
---|
754 | |
---|
755 | def setConfiguration(requestor, service, nodeIdentifier, options): |
---|
756 | self.assertEqual(JID('user@example.org'), requestor) |
---|
757 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
758 | self.assertEqual('test', nodeIdentifier) |
---|
759 | self.assertEqual({'pubsub#deliver_payloads': False, |
---|
760 | 'pubsub#persist_items': True}, options) |
---|
761 | return defer.succeed(None) |
---|
762 | |
---|
763 | self.service.getConfigurationOptions = getConfigurationOptions |
---|
764 | self.service.setConfiguration = setConfiguration |
---|
765 | return self.handleRequest(xml) |
---|
766 | |
---|
767 | |
---|
768 | def test_onConfigureSetCancel(self): |
---|
769 | """ |
---|
770 | The node configuration is cancelled, L{PubSubService.setConfiguration} |
---|
771 | not called. |
---|
772 | """ |
---|
773 | |
---|
774 | xml = """ |
---|
775 | <iq type='set' to='pubsub.example.org' |
---|
776 | from='user@example.org'> |
---|
777 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
778 | <configure node='test'> |
---|
779 | <x xmlns='jabber:x:data' type='cancel'> |
---|
780 | <field var='FORM_TYPE' type='hidden'> |
---|
781 | <value>http://jabber.org/protocol/pubsub#node_config</value> |
---|
782 | </field> |
---|
783 | </x> |
---|
784 | </configure> |
---|
785 | </pubsub> |
---|
786 | </iq> |
---|
787 | """ |
---|
788 | |
---|
789 | def setConfiguration(requestor, service, nodeIdentifier, options): |
---|
790 | self.fail("Unexpected call to setConfiguration") |
---|
791 | |
---|
792 | self.service.setConfiguration = setConfiguration |
---|
793 | return self.handleRequest(xml) |
---|
794 | |
---|
795 | |
---|
796 | def test_onConfigureSetIgnoreUnknown(self): |
---|
797 | """ |
---|
798 | On a node configuration set request unknown fields should be ignored. |
---|
799 | """ |
---|
800 | |
---|
801 | xml = """ |
---|
802 | <iq type='set' to='pubsub.example.org' |
---|
803 | from='user@example.org'> |
---|
804 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
805 | <configure node='test'> |
---|
806 | <x xmlns='jabber:x:data' type='submit'> |
---|
807 | <field var='FORM_TYPE' type='hidden'> |
---|
808 | <value>http://jabber.org/protocol/pubsub#node_config</value> |
---|
809 | </field> |
---|
810 | <field var='pubsub#deliver_payloads'><value>0</value></field> |
---|
811 | <field var='x-myfield'><value>1</value></field> |
---|
812 | </x> |
---|
813 | </configure> |
---|
814 | </pubsub> |
---|
815 | </iq> |
---|
816 | """ |
---|
817 | |
---|
818 | def getConfigurationOptions(): |
---|
819 | return { |
---|
820 | "pubsub#persist_items": |
---|
821 | {"type": "boolean", |
---|
822 | "label": "Persist items to storage"}, |
---|
823 | "pubsub#deliver_payloads": |
---|
824 | {"type": "boolean", |
---|
825 | "label": "Deliver payloads with event notifications"} |
---|
826 | } |
---|
827 | |
---|
828 | def setConfiguration(requestor, service, nodeIdentifier, options): |
---|
829 | self.assertEquals(['pubsub#deliver_payloads'], options.keys()) |
---|
830 | |
---|
831 | self.service.getConfigurationOptions = getConfigurationOptions |
---|
832 | self.service.setConfiguration = setConfiguration |
---|
833 | return self.handleRequest(xml) |
---|
834 | |
---|
835 | |
---|
836 | def test_onItems(self): |
---|
837 | """ |
---|
838 | On a items request, return all items for the given node. |
---|
839 | """ |
---|
840 | xml = """ |
---|
841 | <iq type='get' to='pubsub.example.org' |
---|
842 | from='user@example.org'> |
---|
843 | <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
---|
844 | <items node='test'/> |
---|
845 | </pubsub> |
---|
846 | </iq> |
---|
847 | """ |
---|
848 | |
---|
849 | def items(requestor, service, nodeIdentifier, maxItems, items): |
---|
850 | self.assertEqual(JID('user@example.org'), requestor) |
---|
851 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
852 | self.assertEqual('test', nodeIdentifier) |
---|
853 | self.assertIdentical(None, maxItems) |
---|
854 | self.assertEqual([], items) |
---|
855 | return defer.succeed([pubsub.Item('current')]) |
---|
856 | |
---|
857 | def cb(element): |
---|
858 | self.assertEqual(NS_PUBSUB, element.uri) |
---|
859 | self.assertEqual(NS_PUBSUB, element.items.uri) |
---|
860 | self.assertEqual(1, len(element.items.children)) |
---|
861 | item = element.items.children[-1] |
---|
862 | self.assertTrue(domish.IElement.providedBy(item)) |
---|
863 | self.assertEqual('item', item.name) |
---|
864 | self.assertEqual(NS_PUBSUB, item.uri) |
---|
865 | self.assertEqual('current', item['id']) |
---|
866 | |
---|
867 | self.service.items = items |
---|
868 | d = self.handleRequest(xml) |
---|
869 | d.addCallback(cb) |
---|
870 | return d |
---|
871 | |
---|
872 | |
---|
873 | def test_onRetract(self): |
---|
874 | """ |
---|
875 | A retract request should result in L{PubSubService.retract} being |
---|
876 | called. |
---|
877 | """ |
---|
878 | |
---|
879 | xml = """ |
---|
880 | <iq type='set' to='pubsub.example.org' |
---|
881 | from='user@example.org'> |
---|
882 | <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
---|
883 | <retract node='test'> |
---|
884 | <item id='item1'/> |
---|
885 | <item id='item2'/> |
---|
886 | </retract> |
---|
887 | </pubsub> |
---|
888 | </iq> |
---|
889 | """ |
---|
890 | |
---|
891 | def retract(requestor, service, nodeIdentifier, itemIdentifiers): |
---|
892 | self.assertEqual(JID('user@example.org'), requestor) |
---|
893 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
894 | self.assertEqual('test', nodeIdentifier) |
---|
895 | self.assertEqual(['item1', 'item2'], itemIdentifiers) |
---|
896 | return defer.succeed(None) |
---|
897 | |
---|
898 | self.service.retract = retract |
---|
899 | return self.handleRequest(xml) |
---|
900 | |
---|
901 | |
---|
902 | def test_onPurge(self): |
---|
903 | """ |
---|
904 | A purge request should result in L{PubSubService.purge} being |
---|
905 | called. |
---|
906 | """ |
---|
907 | |
---|
908 | xml = """ |
---|
909 | <iq type='set' to='pubsub.example.org' |
---|
910 | from='user@example.org'> |
---|
911 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
912 | <purge node='test'/> |
---|
913 | </pubsub> |
---|
914 | </iq> |
---|
915 | """ |
---|
916 | |
---|
917 | def purge(requestor, service, nodeIdentifier): |
---|
918 | self.assertEqual(JID('user@example.org'), requestor) |
---|
919 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
920 | self.assertEqual('test', nodeIdentifier) |
---|
921 | return defer.succeed(None) |
---|
922 | |
---|
923 | self.service.purge = purge |
---|
924 | return self.handleRequest(xml) |
---|
925 | |
---|
926 | |
---|
927 | def test_onDelete(self): |
---|
928 | """ |
---|
929 | A delete request should result in L{PubSubService.delete} being |
---|
930 | called. |
---|
931 | """ |
---|
932 | |
---|
933 | xml = """ |
---|
934 | <iq type='set' to='pubsub.example.org' |
---|
935 | from='user@example.org'> |
---|
936 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
937 | <delete node='test'/> |
---|
938 | </pubsub> |
---|
939 | </iq> |
---|
940 | """ |
---|
941 | |
---|
942 | def delete(requestor, service, nodeIdentifier): |
---|
943 | self.assertEqual(JID('user@example.org'), requestor) |
---|
944 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
945 | self.assertEqual('test', nodeIdentifier) |
---|
946 | return defer.succeed(None) |
---|
947 | |
---|
948 | self.service.delete = delete |
---|
949 | return self.handleRequest(xml) |
---|