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 |
---|
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_event_delete(self): |
---|
94 | """ |
---|
95 | Test receiving a delete event resulting in a call to deleteReceived. |
---|
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('delete') |
---|
102 | items['node'] = 'test' |
---|
103 | |
---|
104 | def deleteReceived(event): |
---|
105 | self.assertEquals(JID('user@example.org/home'), event.recipient) |
---|
106 | self.assertEquals(JID('pubsub.example.org'), event.sender) |
---|
107 | self.assertEquals('test', event.nodeIdentifier) |
---|
108 | |
---|
109 | d, self.protocol.deleteReceived = calledAsync(deleteReceived) |
---|
110 | self.stub.send(message) |
---|
111 | return d |
---|
112 | |
---|
113 | |
---|
114 | def test_event_purge(self): |
---|
115 | """ |
---|
116 | Test receiving a purge event resulting in a call to purgeReceived. |
---|
117 | """ |
---|
118 | message = domish.Element((None, 'message')) |
---|
119 | message['from'] = 'pubsub.example.org' |
---|
120 | message['to'] = 'user@example.org/home' |
---|
121 | event = message.addElement((NS_PUBSUB_EVENT, 'event')) |
---|
122 | items = event.addElement('purge') |
---|
123 | items['node'] = 'test' |
---|
124 | |
---|
125 | def purgeReceived(event): |
---|
126 | self.assertEquals(JID('user@example.org/home'), event.recipient) |
---|
127 | self.assertEquals(JID('pubsub.example.org'), event.sender) |
---|
128 | self.assertEquals('test', event.nodeIdentifier) |
---|
129 | |
---|
130 | d, self.protocol.purgeReceived = calledAsync(purgeReceived) |
---|
131 | self.stub.send(message) |
---|
132 | return d |
---|
133 | |
---|
134 | |
---|
135 | def test_createNode(self): |
---|
136 | """ |
---|
137 | Test sending create request. |
---|
138 | """ |
---|
139 | |
---|
140 | def cb(nodeIdentifier): |
---|
141 | self.assertEquals('test', nodeIdentifier) |
---|
142 | |
---|
143 | d = self.protocol.createNode(JID('pubsub.example.org'), 'test') |
---|
144 | d.addCallback(cb) |
---|
145 | |
---|
146 | iq = self.stub.output[-1] |
---|
147 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
148 | self.assertEquals('set', iq.getAttribute('type')) |
---|
149 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
150 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
151 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
152 | 'create', NS_PUBSUB)) |
---|
153 | self.assertEquals(1, len(children)) |
---|
154 | child = children[0] |
---|
155 | self.assertEquals('test', child['node']) |
---|
156 | |
---|
157 | response = toResponse(iq, 'result') |
---|
158 | self.stub.send(response) |
---|
159 | return d |
---|
160 | |
---|
161 | |
---|
162 | def test_createNodeInstant(self): |
---|
163 | """ |
---|
164 | Test sending create request resulting in an instant node. |
---|
165 | """ |
---|
166 | |
---|
167 | def cb(nodeIdentifier): |
---|
168 | self.assertEquals('test', nodeIdentifier) |
---|
169 | |
---|
170 | d = self.protocol.createNode(JID('pubsub.example.org')) |
---|
171 | d.addCallback(cb) |
---|
172 | |
---|
173 | iq = self.stub.output[-1] |
---|
174 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
175 | 'create', NS_PUBSUB)) |
---|
176 | child = children[0] |
---|
177 | self.assertFalse(child.hasAttribute('node')) |
---|
178 | |
---|
179 | response = toResponse(iq, 'result') |
---|
180 | command = response.addElement((NS_PUBSUB, 'pubsub')) |
---|
181 | create = command.addElement('create') |
---|
182 | create['node'] = 'test' |
---|
183 | self.stub.send(response) |
---|
184 | return d |
---|
185 | |
---|
186 | |
---|
187 | def test_createNodeRenamed(self): |
---|
188 | """ |
---|
189 | Test sending create request resulting in renamed node. |
---|
190 | """ |
---|
191 | |
---|
192 | def cb(nodeIdentifier): |
---|
193 | self.assertEquals('test2', nodeIdentifier) |
---|
194 | |
---|
195 | d = self.protocol.createNode(JID('pubsub.example.org'), 'test') |
---|
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.assertEquals('test', child['node']) |
---|
203 | |
---|
204 | response = toResponse(iq, 'result') |
---|
205 | command = response.addElement((NS_PUBSUB, 'pubsub')) |
---|
206 | create = command.addElement('create') |
---|
207 | create['node'] = 'test2' |
---|
208 | self.stub.send(response) |
---|
209 | return d |
---|
210 | |
---|
211 | |
---|
212 | def test_deleteNode(self): |
---|
213 | """ |
---|
214 | Test sending delete request. |
---|
215 | """ |
---|
216 | |
---|
217 | d = self.protocol.deleteNode(JID('pubsub.example.org'), 'test') |
---|
218 | |
---|
219 | iq = self.stub.output[-1] |
---|
220 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
221 | self.assertEquals('set', iq.getAttribute('type')) |
---|
222 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
223 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
224 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
225 | 'delete', NS_PUBSUB)) |
---|
226 | self.assertEquals(1, len(children)) |
---|
227 | child = children[0] |
---|
228 | self.assertEquals('test', child['node']) |
---|
229 | |
---|
230 | response = toResponse(iq, 'result') |
---|
231 | self.stub.send(response) |
---|
232 | return d |
---|
233 | |
---|
234 | |
---|
235 | def test_publish(self): |
---|
236 | """ |
---|
237 | Test sending publish request. |
---|
238 | """ |
---|
239 | |
---|
240 | item = pubsub.Item() |
---|
241 | d = self.protocol.publish(JID('pubsub.example.org'), 'test', [item]) |
---|
242 | |
---|
243 | iq = self.stub.output[-1] |
---|
244 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
245 | self.assertEquals('set', iq.getAttribute('type')) |
---|
246 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
247 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
248 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
249 | 'publish', NS_PUBSUB)) |
---|
250 | self.assertEquals(1, len(children)) |
---|
251 | child = children[0] |
---|
252 | self.assertEquals('test', child['node']) |
---|
253 | items = list(domish.generateElementsQNamed(child.children, |
---|
254 | 'item', NS_PUBSUB)) |
---|
255 | self.assertEquals(1, len(items)) |
---|
256 | self.assertIdentical(item, items[0]) |
---|
257 | |
---|
258 | response = toResponse(iq, 'result') |
---|
259 | self.stub.send(response) |
---|
260 | return d |
---|
261 | |
---|
262 | |
---|
263 | def test_publishNoItems(self): |
---|
264 | """ |
---|
265 | Test sending publish request without items. |
---|
266 | """ |
---|
267 | |
---|
268 | d = self.protocol.publish(JID('pubsub.example.org'), 'test') |
---|
269 | |
---|
270 | iq = self.stub.output[-1] |
---|
271 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
272 | self.assertEquals('set', iq.getAttribute('type')) |
---|
273 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
274 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
275 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
276 | 'publish', NS_PUBSUB)) |
---|
277 | self.assertEquals(1, len(children)) |
---|
278 | child = children[0] |
---|
279 | self.assertEquals('test', child['node']) |
---|
280 | |
---|
281 | response = toResponse(iq, 'result') |
---|
282 | self.stub.send(response) |
---|
283 | return d |
---|
284 | |
---|
285 | |
---|
286 | def test_subscribe(self): |
---|
287 | """ |
---|
288 | Test sending subscription request. |
---|
289 | """ |
---|
290 | d = self.protocol.subscribe(JID('pubsub.example.org'), 'test', |
---|
291 | JID('user@example.org')) |
---|
292 | |
---|
293 | iq = self.stub.output[-1] |
---|
294 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
295 | self.assertEquals('set', iq.getAttribute('type')) |
---|
296 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
297 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
298 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
299 | 'subscribe', NS_PUBSUB)) |
---|
300 | self.assertEquals(1, len(children)) |
---|
301 | child = children[0] |
---|
302 | self.assertEquals('test', child['node']) |
---|
303 | self.assertEquals('user@example.org', child['jid']) |
---|
304 | |
---|
305 | response = toResponse(iq, 'result') |
---|
306 | pubsub = response.addElement((NS_PUBSUB, 'pubsub')) |
---|
307 | subscription = pubsub.addElement('subscription') |
---|
308 | subscription['node'] = 'test' |
---|
309 | subscription['jid'] = 'user@example.org' |
---|
310 | subscription['subscription'] = 'subscribed' |
---|
311 | self.stub.send(response) |
---|
312 | return d |
---|
313 | |
---|
314 | |
---|
315 | def test_subscribePending(self): |
---|
316 | """ |
---|
317 | Test sending subscription request that results in a pending |
---|
318 | subscription. |
---|
319 | """ |
---|
320 | d = self.protocol.subscribe(JID('pubsub.example.org'), 'test', |
---|
321 | JID('user@example.org')) |
---|
322 | |
---|
323 | iq = self.stub.output[-1] |
---|
324 | response = toResponse(iq, 'result') |
---|
325 | command = response.addElement((NS_PUBSUB, 'pubsub')) |
---|
326 | subscription = command.addElement('subscription') |
---|
327 | subscription['node'] = 'test' |
---|
328 | subscription['jid'] = 'user@example.org' |
---|
329 | subscription['subscription'] = 'pending' |
---|
330 | self.stub.send(response) |
---|
331 | self.assertFailure(d, pubsub.SubscriptionPending) |
---|
332 | return d |
---|
333 | |
---|
334 | |
---|
335 | def test_subscribeUnconfigured(self): |
---|
336 | """ |
---|
337 | Test sending subscription request that results in an unconfigured |
---|
338 | subscription. |
---|
339 | """ |
---|
340 | d = self.protocol.subscribe(JID('pubsub.example.org'), 'test', |
---|
341 | JID('user@example.org')) |
---|
342 | |
---|
343 | iq = self.stub.output[-1] |
---|
344 | response = toResponse(iq, 'result') |
---|
345 | command = response.addElement((NS_PUBSUB, 'pubsub')) |
---|
346 | subscription = command.addElement('subscription') |
---|
347 | subscription['node'] = 'test' |
---|
348 | subscription['jid'] = 'user@example.org' |
---|
349 | subscription['subscription'] = 'unconfigured' |
---|
350 | self.stub.send(response) |
---|
351 | self.assertFailure(d, pubsub.SubscriptionUnconfigured) |
---|
352 | return d |
---|
353 | |
---|
354 | |
---|
355 | def test_unsubscribe(self): |
---|
356 | """ |
---|
357 | Test sending unsubscription request. |
---|
358 | """ |
---|
359 | d = self.protocol.unsubscribe(JID('pubsub.example.org'), 'test', |
---|
360 | JID('user@example.org')) |
---|
361 | |
---|
362 | iq = self.stub.output[-1] |
---|
363 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
364 | self.assertEquals('set', iq.getAttribute('type')) |
---|
365 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
366 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
367 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
368 | 'unsubscribe', NS_PUBSUB)) |
---|
369 | self.assertEquals(1, len(children)) |
---|
370 | child = children[0] |
---|
371 | self.assertEquals('test', child['node']) |
---|
372 | self.assertEquals('user@example.org', child['jid']) |
---|
373 | |
---|
374 | self.stub.send(toResponse(iq, 'result')) |
---|
375 | return d |
---|
376 | |
---|
377 | |
---|
378 | def test_items(self): |
---|
379 | """ |
---|
380 | Test sending items request. |
---|
381 | """ |
---|
382 | def cb(items): |
---|
383 | self.assertEquals([], items) |
---|
384 | |
---|
385 | d = self.protocol.items(JID('pubsub.example.org'), 'test') |
---|
386 | d.addCallback(cb) |
---|
387 | |
---|
388 | iq = self.stub.output[-1] |
---|
389 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
390 | self.assertEquals('get', iq.getAttribute('type')) |
---|
391 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
392 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
393 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
394 | 'items', NS_PUBSUB)) |
---|
395 | self.assertEquals(1, len(children)) |
---|
396 | child = children[0] |
---|
397 | self.assertEquals('test', child['node']) |
---|
398 | |
---|
399 | response = toResponse(iq, 'result') |
---|
400 | items = response.addElement((NS_PUBSUB, 'pubsub')).addElement('items') |
---|
401 | items['node'] = 'test' |
---|
402 | |
---|
403 | self.stub.send(response) |
---|
404 | |
---|
405 | return d |
---|
406 | |
---|
407 | |
---|
408 | def test_itemsMaxItems(self): |
---|
409 | """ |
---|
410 | Test sending items request, with limit on the number of items. |
---|
411 | """ |
---|
412 | def cb(items): |
---|
413 | self.assertEquals(2, len(items)) |
---|
414 | self.assertEquals([item1, item2], items) |
---|
415 | |
---|
416 | d = self.protocol.items(JID('pubsub.example.org'), 'test', maxItems=2) |
---|
417 | d.addCallback(cb) |
---|
418 | |
---|
419 | iq = self.stub.output[-1] |
---|
420 | self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
---|
421 | self.assertEquals('get', iq.getAttribute('type')) |
---|
422 | self.assertEquals('pubsub', iq.pubsub.name) |
---|
423 | self.assertEquals(NS_PUBSUB, iq.pubsub.uri) |
---|
424 | children = list(domish.generateElementsQNamed(iq.pubsub.children, |
---|
425 | 'items', NS_PUBSUB)) |
---|
426 | self.assertEquals(1, len(children)) |
---|
427 | child = children[0] |
---|
428 | self.assertEquals('test', child['node']) |
---|
429 | self.assertEquals('2', child['max_items']) |
---|
430 | |
---|
431 | response = toResponse(iq, 'result') |
---|
432 | items = response.addElement((NS_PUBSUB, 'pubsub')).addElement('items') |
---|
433 | items['node'] = 'test' |
---|
434 | item1 = items.addElement('item') |
---|
435 | item1['id'] = 'item1' |
---|
436 | item2 = items.addElement('item') |
---|
437 | item2['id'] = 'item2' |
---|
438 | |
---|
439 | self.stub.send(response) |
---|
440 | |
---|
441 | return d |
---|
442 | |
---|
443 | |
---|
444 | |
---|
445 | class PubSubServiceTest(unittest.TestCase): |
---|
446 | """ |
---|
447 | Tests for L{pubsub.PubSubService}. |
---|
448 | """ |
---|
449 | |
---|
450 | def setUp(self): |
---|
451 | self.service = pubsub.PubSubService() |
---|
452 | |
---|
453 | def handleRequest(self, xml): |
---|
454 | """ |
---|
455 | Find a handler and call it directly |
---|
456 | """ |
---|
457 | handler = None |
---|
458 | iq = parseXml(xml) |
---|
459 | for queryString, method in self.service.iqHandlers.iteritems(): |
---|
460 | if xpath.internQuery(queryString).matches(iq): |
---|
461 | handler = getattr(self.service, method) |
---|
462 | |
---|
463 | if handler: |
---|
464 | d = defer.maybeDeferred(handler, iq) |
---|
465 | else: |
---|
466 | d = defer.fail(NotImplementedError()) |
---|
467 | |
---|
468 | return d |
---|
469 | |
---|
470 | |
---|
471 | def test_interface(self): |
---|
472 | """ |
---|
473 | Do instances of L{pubsub.PubSubService} provide L{iwokkel.IPubSubService}? |
---|
474 | """ |
---|
475 | verify.verifyObject(iwokkel.IPubSubService, self.service) |
---|
476 | |
---|
477 | |
---|
478 | def test_onPublishNoNode(self): |
---|
479 | """ |
---|
480 | The root node is always a collection, publishing is a bad request. |
---|
481 | """ |
---|
482 | xml = """ |
---|
483 | <iq type='set' to='pubsub.example.org' |
---|
484 | from='user@example.org'> |
---|
485 | <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
---|
486 | <publish/> |
---|
487 | </pubsub> |
---|
488 | </iq> |
---|
489 | """ |
---|
490 | |
---|
491 | def cb(result): |
---|
492 | self.assertEquals('bad-request', result.condition) |
---|
493 | |
---|
494 | d = self.handleRequest(xml) |
---|
495 | self.assertFailure(d, error.StanzaError) |
---|
496 | d.addCallback(cb) |
---|
497 | return d |
---|
498 | |
---|
499 | |
---|
500 | def test_onPublish(self): |
---|
501 | """ |
---|
502 | A publish request should result in L{PubSubService.publish} being |
---|
503 | called. |
---|
504 | """ |
---|
505 | |
---|
506 | xml = """ |
---|
507 | <iq type='set' to='pubsub.example.org' |
---|
508 | from='user@example.org'> |
---|
509 | <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
---|
510 | <publish node='test'/> |
---|
511 | </pubsub> |
---|
512 | </iq> |
---|
513 | """ |
---|
514 | |
---|
515 | def publish(requestor, service, nodeIdentifier, items): |
---|
516 | self.assertEqual(JID('user@example.org'), requestor) |
---|
517 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
518 | self.assertEqual('test', nodeIdentifier) |
---|
519 | self.assertEqual([], items) |
---|
520 | return defer.succeed(None) |
---|
521 | |
---|
522 | self.service.publish = publish |
---|
523 | return self.handleRequest(xml) |
---|
524 | |
---|
525 | |
---|
526 | def test_onOptionsGet(self): |
---|
527 | """ |
---|
528 | Subscription options are not supported. |
---|
529 | """ |
---|
530 | |
---|
531 | xml = """ |
---|
532 | <iq type='get' to='pubsub.example.org' |
---|
533 | from='user@example.org'> |
---|
534 | <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
---|
535 | <options/> |
---|
536 | </pubsub> |
---|
537 | </iq> |
---|
538 | """ |
---|
539 | |
---|
540 | def cb(result): |
---|
541 | self.assertEquals('feature-not-implemented', result.condition) |
---|
542 | self.assertEquals('unsupported', result.appCondition.name) |
---|
543 | self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri) |
---|
544 | |
---|
545 | d = self.handleRequest(xml) |
---|
546 | self.assertFailure(d, error.StanzaError) |
---|
547 | d.addCallback(cb) |
---|
548 | return d |
---|
549 | |
---|
550 | |
---|
551 | def test_onDefault(self): |
---|
552 | """ |
---|
553 | A default request should result in |
---|
554 | L{PubSubService.getDefaultConfiguration} being called. |
---|
555 | """ |
---|
556 | |
---|
557 | xml = """ |
---|
558 | <iq type='get' to='pubsub.example.org' |
---|
559 | from='user@example.org'> |
---|
560 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
561 | <default/> |
---|
562 | </pubsub> |
---|
563 | </iq> |
---|
564 | """ |
---|
565 | |
---|
566 | def getConfigurationOptions(): |
---|
567 | return { |
---|
568 | "pubsub#persist_items": |
---|
569 | {"type": "boolean", |
---|
570 | "label": "Persist items to storage"}, |
---|
571 | "pubsub#deliver_payloads": |
---|
572 | {"type": "boolean", |
---|
573 | "label": "Deliver payloads with event notifications"} |
---|
574 | } |
---|
575 | |
---|
576 | def getDefaultConfiguration(requestor, service): |
---|
577 | self.assertEqual(JID('user@example.org'), requestor) |
---|
578 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
579 | return defer.succeed({}) |
---|
580 | |
---|
581 | def cb(element): |
---|
582 | self.assertEqual('pubsub', element.name) |
---|
583 | self.assertEqual(NS_PUBSUB_OWNER, element.uri) |
---|
584 | self.assertEqual(NS_PUBSUB_OWNER, element.default.uri) |
---|
585 | form = data_form.Form.fromElement(element.default.x) |
---|
586 | self.assertEqual(NS_PUBSUB_CONFIG, form.formNamespace) |
---|
587 | |
---|
588 | self.service.getConfigurationOptions = getConfigurationOptions |
---|
589 | self.service.getDefaultConfiguration = getDefaultConfiguration |
---|
590 | d = self.handleRequest(xml) |
---|
591 | d.addCallback(cb) |
---|
592 | return d |
---|
593 | |
---|
594 | |
---|
595 | def test_onConfigureGet(self): |
---|
596 | """ |
---|
597 | On a node configuration get request L{PubSubService.getConfiguration} |
---|
598 | is called and results in a data form with the configuration. |
---|
599 | """ |
---|
600 | |
---|
601 | xml = """ |
---|
602 | <iq type='get' to='pubsub.example.org' |
---|
603 | from='user@example.org'> |
---|
604 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
605 | <configure node='test'/> |
---|
606 | </pubsub> |
---|
607 | </iq> |
---|
608 | """ |
---|
609 | |
---|
610 | def getConfigurationOptions(): |
---|
611 | return { |
---|
612 | "pubsub#persist_items": |
---|
613 | {"type": "boolean", |
---|
614 | "label": "Persist items to storage"}, |
---|
615 | "pubsub#deliver_payloads": |
---|
616 | {"type": "boolean", |
---|
617 | "label": "Deliver payloads with event notifications"} |
---|
618 | } |
---|
619 | |
---|
620 | def getConfiguration(requestor, service, nodeIdentifier): |
---|
621 | self.assertEqual(JID('user@example.org'), requestor) |
---|
622 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
623 | self.assertEqual('test', nodeIdentifier) |
---|
624 | |
---|
625 | return defer.succeed({'pubsub#deliver_payloads': '0', |
---|
626 | 'pubsub#persist_items': '1'}) |
---|
627 | |
---|
628 | def cb(element): |
---|
629 | self.assertEqual('pubsub', element.name) |
---|
630 | self.assertEqual(NS_PUBSUB_OWNER, element.uri) |
---|
631 | self.assertEqual(NS_PUBSUB_OWNER, element.configure.uri) |
---|
632 | form = data_form.Form.fromElement(element.configure.x) |
---|
633 | self.assertEqual(NS_PUBSUB_CONFIG, form.formNamespace) |
---|
634 | fields = dict([(field.var, field) for field in form.fields]) |
---|
635 | |
---|
636 | self.assertIn('pubsub#deliver_payloads', fields) |
---|
637 | field = fields['pubsub#deliver_payloads'] |
---|
638 | self.assertEqual('boolean', field.fieldType) |
---|
639 | self.assertEqual(True, field.value) |
---|
640 | |
---|
641 | self.assertIn('pubsub#persist_items', fields) |
---|
642 | field = fields['pubsub#persist_items'] |
---|
643 | self.assertEqual('boolean', field.fieldType) |
---|
644 | self.assertEqual(True, field.value) |
---|
645 | |
---|
646 | self.service.getConfigurationOptions = getConfigurationOptions |
---|
647 | self.service.getConfiguration = getConfiguration |
---|
648 | d = self.handleRequest(xml) |
---|
649 | d.addCallback(cb) |
---|
650 | return d |
---|
651 | |
---|
652 | |
---|
653 | def test_onConfigureSet(self): |
---|
654 | """ |
---|
655 | On a node configuration set request the Data Form is parsed and |
---|
656 | L{PubSubService.setConfiguration} is called with the passed options. |
---|
657 | """ |
---|
658 | |
---|
659 | xml = """ |
---|
660 | <iq type='set' to='pubsub.example.org' |
---|
661 | from='user@example.org'> |
---|
662 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
663 | <configure node='test'> |
---|
664 | <x xmlns='jabber:x:data' type='submit'> |
---|
665 | <field var='FORM_TYPE' type='hidden'> |
---|
666 | <value>http://jabber.org/protocol/pubsub#node_config</value> |
---|
667 | </field> |
---|
668 | <field var='pubsub#deliver_payloads'><value>0</value></field> |
---|
669 | <field var='pubsub#persist_items'><value>1</value></field> |
---|
670 | </x> |
---|
671 | </configure> |
---|
672 | </pubsub> |
---|
673 | </iq> |
---|
674 | """ |
---|
675 | |
---|
676 | def setConfiguration(requestor, service, nodeIdentifier, options): |
---|
677 | self.assertEqual(JID('user@example.org'), requestor) |
---|
678 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
679 | self.assertEqual('test', nodeIdentifier) |
---|
680 | self.assertEqual({'pubsub#deliver_payloads': '0', |
---|
681 | 'pubsub#persist_items': '1'}, options) |
---|
682 | return defer.succeed(None) |
---|
683 | |
---|
684 | self.service.setConfiguration = setConfiguration |
---|
685 | return self.handleRequest(xml) |
---|
686 | |
---|
687 | |
---|
688 | def test_onConfigureSetCancel(self): |
---|
689 | """ |
---|
690 | The node configuration is cancelled, L{PubSubService.setConfiguration} |
---|
691 | not called. |
---|
692 | """ |
---|
693 | |
---|
694 | xml = """ |
---|
695 | <iq type='set' to='pubsub.example.org' |
---|
696 | from='user@example.org'> |
---|
697 | <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'> |
---|
698 | <configure node='test'> |
---|
699 | <x xmlns='jabber:x:data' type='cancel'> |
---|
700 | <field var='FORM_TYPE' type='hidden'> |
---|
701 | <value>http://jabber.org/protocol/pubsub#node_config</value> |
---|
702 | </field> |
---|
703 | </x> |
---|
704 | </configure> |
---|
705 | </pubsub> |
---|
706 | </iq> |
---|
707 | """ |
---|
708 | |
---|
709 | def setConfiguration(requestor, service, nodeIdentifier, options): |
---|
710 | self.fail("Unexpected call to setConfiguration") |
---|
711 | |
---|
712 | self.service.setConfiguration = setConfiguration |
---|
713 | return self.handleRequest(xml) |
---|
714 | |
---|
715 | |
---|
716 | def test_onItems(self): |
---|
717 | """ |
---|
718 | On a items request, return all items for the given node. |
---|
719 | """ |
---|
720 | xml = """ |
---|
721 | <iq type='get' to='pubsub.example.org' |
---|
722 | from='user@example.org'> |
---|
723 | <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
---|
724 | <items node='test'/> |
---|
725 | </pubsub> |
---|
726 | </iq> |
---|
727 | """ |
---|
728 | |
---|
729 | def items(requestor, service, nodeIdentifier, maxItems, items): |
---|
730 | self.assertEqual(JID('user@example.org'), requestor) |
---|
731 | self.assertEqual(JID('pubsub.example.org'), service) |
---|
732 | self.assertEqual('test', nodeIdentifier) |
---|
733 | self.assertIdentical(None, maxItems) |
---|
734 | self.assertEqual([], items) |
---|
735 | return defer.succeed([pubsub.Item('current')]) |
---|
736 | |
---|
737 | def cb(element): |
---|
738 | self.assertEqual(NS_PUBSUB, element.uri) |
---|
739 | self.assertEqual(NS_PUBSUB, element.items.uri) |
---|
740 | self.assertEqual(1, len(element.items.children)) |
---|
741 | item = element.items.children[-1] |
---|
742 | self.assertTrue(domish.IElement.providedBy(item)) |
---|
743 | self.assertEqual('item', item.name) |
---|
744 | self.assertEqual(NS_PUBSUB, item.uri) |
---|
745 | self.assertEqual('current', item['id']) |
---|
746 | |
---|
747 | self.service.items = items |
---|
748 | d = self.handleRequest(xml) |
---|
749 | d.addCallback(cb) |
---|
750 | return d |
---|