10
|
1 # Copyright (c) Adrien Cossa. |
|
2 # See LICENSE for details. |
|
3 |
|
4 """ |
|
5 Tests for L{wokkel.rsm}. |
|
6 """ |
|
7 |
|
8 from zope.interface import verify |
|
9 |
|
10 from twisted.trial import unittest |
|
11 from twisted.words.xish import domish |
|
12 from twisted.words.protocols.jabber.jid import JID |
|
13 from twisted.words.protocols.jabber.xmlstream import toResponse |
|
14 from twisted.internet import defer |
|
15 |
|
16 from wokkel.generic import parseXml |
|
17 from wokkel import iwokkel |
|
18 from wokkel.test.helpers import XmlStreamStub, TestableRequestHandlerMixin |
|
19 |
|
20 from sat.tmp.wokkel import pubsub |
|
21 from sat.tmp.wokkel.rsm import NS_RSM, RSMRequest, RSMResponse, PubSubClient, PubSubService |
|
22 |
|
23 import uuid |
|
24 |
|
25 |
|
26 class RSMRequestTest(unittest.TestCase): |
|
27 """ |
|
28 Tests for L{rsm.RSMRequest}. |
|
29 """ |
|
30 |
|
31 def test___init__(self): |
|
32 """ |
|
33 Fail to initialize a RSMRequest with wrong attribute values. |
|
34 """ |
|
35 self.assertRaises(AssertionError, RSMRequest, index=371, after=u'test') |
|
36 self.assertRaises(AssertionError, RSMRequest, index=371, before=u'test') |
|
37 self.assertRaises(AssertionError, RSMRequest, before=117) |
|
38 self.assertRaises(AssertionError, RSMRequest, after=312) |
|
39 self.assertRaises(AssertionError, RSMRequest, after=u'117', before=u'312') |
|
40 |
|
41 def test_parse(self): |
|
42 """ |
|
43 Parse a request element asking for the first page. |
|
44 """ |
|
45 xml = """ |
|
46 <query xmlns='jabber:iq:search'> |
|
47 <nick>Pete</nick> |
|
48 <set xmlns='http://jabber.org/protocol/rsm'> |
|
49 <max>1</max> |
|
50 </set> |
|
51 </query> |
|
52 """ |
|
53 request = RSMRequest.parse(parseXml(xml)) |
|
54 self.assertEqual(1, request.max) |
|
55 self.assertIdentical(None, request.index) |
|
56 self.assertIdentical(None, request.after) |
|
57 self.assertIdentical(None, request.before) |
|
58 |
|
59 def test_parseSecondPage(self): |
|
60 """ |
|
61 Parse a request element asking for a next page. |
|
62 """ |
|
63 xml = """ |
|
64 <query xmlns='jabber:iq:search'> |
|
65 <nick>Pete</nick> |
|
66 <set xmlns='http://jabber.org/protocol/rsm'> |
|
67 <max>3</max> |
|
68 <after>peterpan@neverland.lit</after> |
|
69 </set> |
|
70 </query> |
|
71 """ |
|
72 request = RSMRequest.parse(parseXml(xml)) |
|
73 self.assertEqual(3, request.max) |
|
74 self.assertIdentical(None, request.index) |
|
75 self.assertEqual(u'peterpan@neverland.lit', request.after) |
|
76 self.assertIdentical(None, request.before) |
|
77 |
|
78 def test_parsePreviousPage(self): |
|
79 """ |
|
80 Parse a request element asking for a previous page. |
|
81 """ |
|
82 xml = """ |
|
83 <query xmlns='jabber:iq:search'> |
|
84 <nick>Pete</nick> |
|
85 <set xmlns='http://jabber.org/protocol/rsm'> |
|
86 <max>5</max> |
|
87 <before>peterpan@pixyland.org</before> |
|
88 </set> |
|
89 </query> |
|
90 """ |
|
91 request = RSMRequest.parse(parseXml(xml)) |
|
92 self.assertEqual(5, request.max) |
|
93 self.assertIdentical(None, request.index) |
|
94 self.assertIdentical(None, request.after) |
|
95 self.assertEqual(u'peterpan@pixyland.org', request.before) |
|
96 |
|
97 def test_parseLastPage(self): |
|
98 """ |
|
99 Parse a request element asking for the last page. |
|
100 """ |
|
101 xml = """ |
|
102 <query xmlns='jabber:iq:search'> |
|
103 <nick>Pete</nick> |
|
104 <set xmlns='http://jabber.org/protocol/rsm'> |
|
105 <max>7</max> |
|
106 <before/> |
|
107 </set> |
|
108 </query> |
|
109 """ |
|
110 request = RSMRequest.parse(parseXml(xml)) |
|
111 self.assertEqual(7, request.max) |
|
112 self.assertIdentical(None, request.index) |
|
113 self.assertIdentical(None, request.after) |
|
114 self.assertEqual('', request.before) |
|
115 |
|
116 def test_parseOutOfOrderPage(self): |
|
117 """ |
|
118 Parse a request element asking for a page out of order. |
|
119 """ |
|
120 xml = """ |
|
121 <query xmlns='jabber:iq:search'> |
|
122 <nick>Pete</nick> |
|
123 <set xmlns='http://jabber.org/protocol/rsm'> |
|
124 <max>9</max> |
|
125 <index>371</index> |
|
126 </set> |
|
127 </query> |
|
128 """ |
|
129 request = RSMRequest.parse(parseXml(xml)) |
|
130 self.assertEqual(9, request.max) |
|
131 self.assertEqual(371, request.index) |
|
132 self.assertIdentical(None, request.after) |
|
133 self.assertIdentical(None, request.before) |
|
134 |
|
135 def test_parseItemCount(self): |
|
136 """ |
|
137 Parse a request element asking for the items count. |
|
138 """ |
|
139 xml = """ |
|
140 <query xmlns='jabber:iq:search'> |
|
141 <nick>Pete</nick> |
|
142 <set xmlns='http://jabber.org/protocol/rsm'> |
|
143 <max>0</max> |
|
144 </set> |
|
145 </query> |
|
146 """ |
|
147 request = RSMRequest.parse(parseXml(xml)) |
|
148 self.assertEqual(0, request.max) |
|
149 self.assertIdentical(None, request.index) |
|
150 self.assertIdentical(None, request.after) |
|
151 self.assertIdentical(None, request.before) |
|
152 |
|
153 def test_render(self): |
|
154 """ |
|
155 Embed a page request in the element. |
|
156 """ |
|
157 element = domish.Element(('jabber:iq:search', 'query')) |
|
158 element.addElement('items')['max_items'] = u'10' |
|
159 RSMRequest(1).render(element) |
|
160 |
|
161 self.assertEqual(u'10', element.items['max_items']) # not changed |
|
162 |
|
163 self.assertEqual(NS_RSM, element.set.uri) |
|
164 self.assertEqual(u'1', ''.join(element.set.max.children)) |
|
165 self.assertIdentical(None, element.set.after) |
|
166 self.assertIdentical(None, element.set.before) |
|
167 self.assertIdentical(None, element.set.index) |
|
168 |
|
169 def test_renderPubSub(self): |
|
170 """ |
|
171 Embed a page request in the pubsub element. |
|
172 """ |
|
173 element = domish.Element((pubsub.NS_PUBSUB, 'pubsub')) |
|
174 element.addElement('items')['max_items'] = u'10' |
|
175 RSMRequest(3).render(element) |
|
176 |
|
177 self.assertEqual(u'3', element.items['max_items']) # changed |
|
178 |
|
179 self.assertEqual(NS_RSM, element.set.uri) |
|
180 self.assertEqual(u'3', ''.join(element.set.max.children)) |
|
181 self.assertIdentical(None, element.set.after) |
|
182 self.assertIdentical(None, element.set.before) |
|
183 self.assertIdentical(None, element.set.index) |
|
184 |
|
185 def test_renderItems(self): |
|
186 """ |
|
187 Embed a page request in the element, specify items. |
|
188 """ |
|
189 element = domish.Element(('jabber:iq:search', 'query')) |
|
190 RSMRequest(5, 127).render(element) |
|
191 self.assertEqual(NS_RSM, element.set.uri) |
|
192 self.assertEqual(u'5', ''.join(element.set.max.children)) |
|
193 self.assertIdentical(None, element.set.after) |
|
194 self.assertIdentical(None, element.set.before) |
|
195 self.assertEqual(u'127', ''.join(element.set.index.children)) |
|
196 |
|
197 def test_renderAfter(self): |
|
198 """ |
|
199 Embed a page request in the element, specify after. |
|
200 """ |
|
201 element = domish.Element(('jabber:iq:search', 'query')) |
|
202 RSMRequest(5, after=u'test').render(element) |
|
203 self.assertEqual(NS_RSM, element.set.uri) |
|
204 self.assertEqual(u'5', ''.join(element.set.max.children)) |
|
205 self.assertEqual(u'test', ''.join(element.set.after.children)) |
|
206 self.assertIdentical(None, element.set.before) |
|
207 self.assertIdentical(None, element.set.index) |
|
208 |
|
209 def test_renderBefore(self): |
|
210 """ |
|
211 Embed a page request in the element, specify before. |
|
212 """ |
|
213 element = domish.Element(('jabber:iq:search', 'query')) |
|
214 RSMRequest(5, before=u'test').render(element) |
|
215 self.assertEqual(NS_RSM, element.set.uri) |
|
216 self.assertEqual(u'5', ''.join(element.set.max.children)) |
|
217 self.assertIdentical(None, element.set.after) |
|
218 self.assertEqual(u'test', ''.join(element.set.before.children)) |
|
219 self.assertIdentical(None, element.set.index) |
|
220 |
|
221 |
|
222 class RSMResponseTest(unittest.TestCase): |
|
223 """ |
|
224 Tests for L{rsm.RSMResponse}. |
|
225 """ |
|
226 |
|
227 def test___init__(self): |
|
228 """ |
|
229 Fail to initialize a RSMResponse with wrong attribute values. |
|
230 """ |
|
231 self.assertRaises(AssertionError, RSMResponse, count=u'1') |
|
232 self.assertRaises(AssertionError, RSMResponse, index=127, first=u'127') |
|
233 self.assertRaises(AssertionError, RSMResponse, index=127, last=u'351') |
|
234 self.assertRaises(AssertionError, RSMResponse, first=u'127', last=u'351') |
|
235 self.assertRaises(AssertionError, RSMResponse, index=u'127', |
|
236 first=u'127', last=u'351') |
|
237 self.assertRaises(AssertionError, RSMResponse, index=127, |
|
238 first=127, last=u'351') |
|
239 self.assertRaises(AssertionError, RSMResponse, index=127, |
|
240 first=u'127', last=351) |
|
241 |
|
242 def test_parse(self): |
|
243 """ |
|
244 Parse a response element returning a page. |
|
245 """ |
|
246 xml = """ |
|
247 <query xmlns='jabber:iq:search'> |
|
248 <set xmlns='http://jabber.org/protocol/rsm'> |
|
249 <first index='20'>stpeter@jabber.org</first> |
|
250 <last>peterpan@neverland.lit</last> |
|
251 <count>800</count> |
|
252 </set> |
|
253 </query> |
|
254 """ |
|
255 response = RSMResponse.parse(parseXml(xml)) |
|
256 self.assertEqual(800, response.count) |
|
257 self.assertEqual(20, response.index) |
|
258 self.assertEqual(u'stpeter@jabber.org', response.first) |
|
259 self.assertEqual(u'peterpan@neverland.lit', response.last) |
|
260 |
|
261 def test_parseEmptySet(self): |
|
262 """ |
|
263 Parse a response element returning an empty set. |
|
264 """ |
|
265 xml = """ |
|
266 <query xmlns='jabber:iq:search'> |
|
267 <set xmlns='http://jabber.org/protocol/rsm'> |
|
268 <count>800</count> |
|
269 </set> |
|
270 </query> |
|
271 """ |
|
272 response = RSMResponse.parse(parseXml(xml)) |
|
273 self.assertEqual(800, response.count) |
|
274 self.assertIdentical(None, response.first) |
|
275 self.assertIdentical(None, response.last) |
|
276 self.assertIdentical(None, response.index) |
|
277 |
|
278 def test_render(self): |
|
279 """ |
|
280 Embed a page response in the element. |
|
281 """ |
|
282 element = domish.Element(('jabber:iq:search', 'query')) |
|
283 RSMResponse(800, 20, u'stpeter@jabber.org', |
|
284 u'peterpan@neverland.lit').render(element) |
|
285 |
|
286 self.assertEqual(NS_RSM, element.set.uri) |
|
287 self.assertEqual(u'800', ''.join(element.set.count.children)) |
|
288 self.assertEqual(u'stpeter@jabber.org', |
|
289 ''.join(element.set.first.children)) |
|
290 self.assertEqual(u'peterpan@neverland.lit', |
|
291 ''.join(element.set.last.children)) |
|
292 self.assertEqual(u'20', element.set.first['index']) |
|
293 |
|
294 def test_renderEmptySet(self): |
|
295 """ |
|
296 Embed a page response in the element, for empty set. |
|
297 """ |
|
298 element = domish.Element(('jabber:iq:search', 'query')) |
|
299 RSMResponse(800).render(element) |
|
300 |
|
301 self.assertEqual(NS_RSM, element.set.uri) |
|
302 self.assertEqual(u'800', ''.join(element.set.count.children)) |
|
303 self.assertIdentical(None, element.set.first) |
|
304 self.assertIdentical(None, element.set.last) |
|
305 |
|
306 |
|
307 class PubSubClientTest(unittest.TestCase): |
|
308 """ |
|
309 Tests for L{rsm.PubSubClient}. |
|
310 """ |
|
311 timeout = 2 |
|
312 |
|
313 def setUp(self): |
|
314 self.stub = XmlStreamStub() |
|
315 self.protocol = PubSubClient() |
|
316 self.protocol.xmlstream = self.stub.xmlstream |
|
317 self.protocol.connectionInitialized() |
|
318 |
|
319 def test_items(self): |
|
320 """ |
|
321 Test sending items request to get the first page. |
|
322 """ |
|
323 def cb(items): |
|
324 self.assertEquals(2, len(items)) |
|
325 self.assertEquals([item1, item2], items) |
|
326 rsm = self.protocol.getRSMResponse(ext_data['id']) |
|
327 self.assertDictEqual(rsm, {'count': '800', 'index': '0', |
|
328 'first': 'item1', 'last': 'item2'}) |
|
329 |
|
330 ext_data = {'id': unicode(uuid.uuid4()), 'rsm': RSMRequest(2)} |
|
331 d = self.protocol.items(JID('pubsub.example.org'), 'test', |
|
332 ext_data=ext_data) |
|
333 d.addCallback(cb) |
|
334 |
|
335 iq = self.stub.output[-1] |
|
336 self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
|
337 self.assertEquals('get', iq.getAttribute('type')) |
|
338 self.assertEquals('pubsub', iq.pubsub.name) |
|
339 self.assertEquals(pubsub.NS_PUBSUB, iq.pubsub.uri) |
|
340 children = list(domish.generateElementsQNamed(iq.pubsub.children, |
|
341 'items', pubsub.NS_PUBSUB)) |
|
342 self.assertEquals(1, len(children)) |
|
343 child = children[0] |
|
344 self.assertEquals('test', child['node']) |
|
345 |
|
346 set_elts = list(domish.generateElementsQNamed(iq.pubsub.children, |
|
347 'set', NS_RSM)) |
|
348 self.assertEquals(1, len(set_elts)) |
|
349 set_elt = set_elts[0] |
|
350 self.assertEquals(u'2', ''.join(set_elt.max.children)) |
|
351 |
|
352 response = toResponse(iq, 'result') |
|
353 items = response.addElement((pubsub.NS_PUBSUB, |
|
354 'pubsub')).addElement('items') |
|
355 items['node'] = 'test' |
|
356 item1 = items.addElement('item') |
|
357 item1['id'] = 'item1' |
|
358 item2 = items.addElement('item') |
|
359 item2['id'] = 'item2' |
|
360 RSMResponse(800, 0, u'item1', u'item2').render(response.pubsub) |
|
361 self.stub.send(response) |
|
362 |
|
363 return d |
|
364 |
|
365 def test_itemsAfter(self): |
|
366 """ |
|
367 Test sending items request to get the next page. |
|
368 """ |
|
369 def cb(items): |
|
370 self.assertEquals(2, len(items)) |
|
371 self.assertEquals([item1, item2], items) |
|
372 rsm = self.protocol.getRSMResponse(ext_data['id']) |
|
373 self.assertDictEqual(rsm, {'count': '800', 'index': '2', |
|
374 'first': 'item3', 'last': 'item4'}) |
|
375 |
|
376 ext_data = {'id': unicode(uuid.uuid4()), |
|
377 'rsm': RSMRequest(2, after=u'item2')} |
|
378 d = self.protocol.items(JID('pubsub.example.org'), 'test', |
|
379 ext_data=ext_data) |
|
380 d.addCallback(cb) |
|
381 |
|
382 iq = self.stub.output[-1] |
|
383 self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
|
384 self.assertEquals('get', iq.getAttribute('type')) |
|
385 self.assertEquals('pubsub', iq.pubsub.name) |
|
386 self.assertEquals(pubsub.NS_PUBSUB, iq.pubsub.uri) |
|
387 children = list(domish.generateElementsQNamed(iq.pubsub.children, |
|
388 'items', pubsub.NS_PUBSUB)) |
|
389 self.assertEquals(1, len(children)) |
|
390 child = children[0] |
|
391 self.assertEquals('test', child['node']) |
|
392 |
|
393 set_elts = list(domish.generateElementsQNamed(iq.pubsub.children, |
|
394 'set', NS_RSM)) |
|
395 self.assertEquals(1, len(set_elts)) |
|
396 set_elt = set_elts[0] |
|
397 self.assertEquals(u'2', ''.join(set_elt.max.children)) |
|
398 self.assertEquals(u'item2', ''.join(set_elt.after.children)) |
|
399 |
|
400 response = toResponse(iq, 'result') |
|
401 items = response.addElement((pubsub.NS_PUBSUB, |
|
402 'pubsub')).addElement('items') |
|
403 items['node'] = 'test' |
|
404 item1 = items.addElement('item') |
|
405 item1['id'] = 'item3' |
|
406 item2 = items.addElement('item') |
|
407 item2['id'] = 'item4' |
|
408 RSMResponse(800, 2, u'item3', u'item4').render(response.pubsub) |
|
409 self.stub.send(response) |
|
410 |
|
411 return d |
|
412 |
|
413 def test_itemsBefore(self): |
|
414 """ |
|
415 Test sending items request to get the previous page. |
|
416 """ |
|
417 def cb(items): |
|
418 self.assertEquals(2, len(items)) |
|
419 self.assertEquals([item1, item2], items) |
|
420 rsm = self.protocol.getRSMResponse(ext_data['id']) |
|
421 self.assertDictEqual(rsm, {'count': '800', 'index': '0', |
|
422 'first': 'item1', 'last': 'item2'}) |
|
423 |
|
424 ext_data = {'id': unicode(uuid.uuid4()), |
|
425 'rsm': RSMRequest(2, before=u'item3')} |
|
426 d = self.protocol.items(JID('pubsub.example.org'), 'test', |
|
427 ext_data=ext_data) |
|
428 d.addCallback(cb) |
|
429 |
|
430 iq = self.stub.output[-1] |
|
431 self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
|
432 self.assertEquals('get', iq.getAttribute('type')) |
|
433 self.assertEquals('pubsub', iq.pubsub.name) |
|
434 self.assertEquals(pubsub.NS_PUBSUB, iq.pubsub.uri) |
|
435 children = list(domish.generateElementsQNamed(iq.pubsub.children, |
|
436 'items', pubsub.NS_PUBSUB)) |
|
437 self.assertEquals(1, len(children)) |
|
438 child = children[0] |
|
439 self.assertEquals('test', child['node']) |
|
440 |
|
441 set_elts = list(domish.generateElementsQNamed(iq.pubsub.children, |
|
442 'set', NS_RSM)) |
|
443 self.assertEquals(1, len(set_elts)) |
|
444 set_elt = set_elts[0] |
|
445 self.assertEquals(u'2', ''.join(set_elt.max.children)) |
|
446 self.assertEquals(u'item3', ''.join(set_elt.before.children)) |
|
447 |
|
448 response = toResponse(iq, 'result') |
|
449 items = response.addElement((pubsub.NS_PUBSUB, |
|
450 'pubsub')).addElement('items') |
|
451 items['node'] = 'test' |
|
452 item1 = items.addElement('item') |
|
453 item1['id'] = 'item1' |
|
454 item2 = items.addElement('item') |
|
455 item2['id'] = 'item2' |
|
456 RSMResponse(800, 0, u'item1', u'item2').render(response.pubsub) |
|
457 self.stub.send(response) |
|
458 |
|
459 return d |
|
460 |
|
461 def test_itemsIndex(self): |
|
462 """ |
|
463 Test sending items request to get a page out of order. |
|
464 """ |
|
465 def cb(items): |
|
466 self.assertEquals(3, len(items)) |
|
467 self.assertEquals([item1, item2, item3], items) |
|
468 rsm = self.protocol.getRSMResponse(ext_data['id']) |
|
469 self.assertDictEqual(rsm, {'count': '800', 'index': '3', |
|
470 'first': 'item4', 'last': 'item6'}) |
|
471 |
|
472 ext_data = {'id': unicode(uuid.uuid4()), 'rsm': RSMRequest(3, 3)} |
|
473 d = self.protocol.items(JID('pubsub.example.org'), 'test', |
|
474 ext_data=ext_data) |
|
475 d.addCallback(cb) |
|
476 |
|
477 iq = self.stub.output[-1] |
|
478 self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
|
479 self.assertEquals('get', iq.getAttribute('type')) |
|
480 self.assertEquals('pubsub', iq.pubsub.name) |
|
481 self.assertEquals(pubsub.NS_PUBSUB, iq.pubsub.uri) |
|
482 children = list(domish.generateElementsQNamed(iq.pubsub.children, |
|
483 'items', pubsub.NS_PUBSUB)) |
|
484 self.assertEquals(1, len(children)) |
|
485 child = children[0] |
|
486 self.assertEquals('test', child['node']) |
|
487 |
|
488 set_elts = list(domish.generateElementsQNamed(iq.pubsub.children, |
|
489 'set', NS_RSM)) |
|
490 self.assertEquals(1, len(set_elts)) |
|
491 set_elt = set_elts[0] |
|
492 self.assertEquals(u'3', ''.join(set_elt.max.children)) |
|
493 self.assertEquals(u'3', ''.join(set_elt.index.children)) |
|
494 |
|
495 response = toResponse(iq, 'result') |
|
496 items = response.addElement((pubsub.NS_PUBSUB, |
|
497 'pubsub')).addElement('items') |
|
498 items['node'] = 'test' |
|
499 item1 = items.addElement('item') |
|
500 item1['id'] = 'item4' |
|
501 item2 = items.addElement('item') |
|
502 item2['id'] = 'item5' |
|
503 item3 = items.addElement('item') |
|
504 item3['id'] = 'item6' |
|
505 RSMResponse(800, 3, u'item4', u'item6').render(response.pubsub) |
|
506 self.stub.send(response) |
|
507 |
|
508 return d |
|
509 |
|
510 def test_itemsCount(self): |
|
511 """ |
|
512 Test sending items request to count them. |
|
513 """ |
|
514 def cb(items): |
|
515 self.assertEquals(0, len(items)) |
|
516 rsm = self.protocol.getRSMResponse(ext_data['id']) |
|
517 self.assertDictEqual(rsm, {'count': '800'}) |
|
518 |
|
519 ext_data = {'id': unicode(uuid.uuid4()), 'rsm': RSMRequest(0)} |
|
520 d = self.protocol.items(JID('pubsub.example.org'), 'test', |
|
521 ext_data=ext_data) |
|
522 d.addCallback(cb) |
|
523 |
|
524 iq = self.stub.output[-1] |
|
525 self.assertEquals('pubsub.example.org', iq.getAttribute('to')) |
|
526 self.assertEquals('get', iq.getAttribute('type')) |
|
527 self.assertEquals('pubsub', iq.pubsub.name) |
|
528 self.assertEquals(pubsub.NS_PUBSUB, iq.pubsub.uri) |
|
529 children = list(domish.generateElementsQNamed(iq.pubsub.children, |
|
530 'items', pubsub.NS_PUBSUB)) |
|
531 self.assertEquals(1, len(children)) |
|
532 child = children[0] |
|
533 self.assertEquals('test', child['node']) |
|
534 |
|
535 set_elts = list(domish.generateElementsQNamed(iq.pubsub.children, |
|
536 'set', NS_RSM)) |
|
537 self.assertEquals(1, len(set_elts)) |
|
538 set_elt = set_elts[0] |
|
539 self.assertEquals(u'0', ''.join(set_elt.max.children)) |
|
540 |
|
541 response = toResponse(iq, 'result') |
|
542 response.addElement((pubsub.NS_PUBSUB, 'pubsub')) |
|
543 RSMResponse(800).render(response.pubsub) |
|
544 self.stub.send(response) |
|
545 |
|
546 return d |
|
547 |
|
548 |
|
549 class PubSubServiceTest(unittest.TestCase, TestableRequestHandlerMixin): |
|
550 |
|
551 def setUp(self): |
|
552 self.stub = XmlStreamStub() |
|
553 self.resource = pubsub.PubSubResource() |
|
554 self.service = PubSubService(self.resource) |
|
555 self.service.send = self.stub.xmlstream.send |
|
556 |
|
557 def test_on_items(self): |
|
558 """ |
|
559 On a items request, return the first item for the given node. |
|
560 """ |
|
561 xml = """ |
|
562 <iq type='get' to='pubsub.example.org' |
|
563 from='user@example.org'> |
|
564 <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
|
565 <items node='test'/> |
|
566 </pubsub> |
|
567 <set xmlns='http://jabber.org/protocol/rsm'> |
|
568 <max>1</max> |
|
569 </set> |
|
570 </iq> |
|
571 """ |
|
572 |
|
573 def items(request): |
|
574 rsm = RSMResponse(800, 0, u'item', u'item').toElement() |
|
575 return defer.succeed([pubsub.Item('current'), rsm]) |
|
576 |
|
577 def cb(element): |
|
578 self.assertEqual(pubsub.NS_PUBSUB, element.uri) |
|
579 self.assertEqual(pubsub.NS_PUBSUB, element.items.uri) |
|
580 self.assertEqual(1, len(element.items.children)) |
|
581 item = element.items.children[-1] |
|
582 self.assertTrue(domish.IElement.providedBy(item)) |
|
583 self.assertEqual('item', item.name) |
|
584 self.assertEqual(pubsub.NS_PUBSUB, item.uri) |
|
585 self.assertEqual('current', item['id']) |
|
586 self.assertEqual(NS_RSM, element.set.uri) |
|
587 self.assertEqual('800', ''.join(element.set.count.children)) |
|
588 self.assertEqual('0', element.set.first['index']) |
|
589 self.assertEqual('item', ''.join(element.set.first.children)) |
|
590 self.assertEqual('item', ''.join(element.set.last.children)) |
|
591 |
|
592 self.resource.items = items |
|
593 verify.verifyObject(iwokkel.IPubSubResource, self.resource) |
|
594 d = self.handleRequest(xml) |
|
595 d.addCallback(cb) |
|
596 return d |
|
597 |
|
598 def test_on_itemsIndex(self): |
|
599 """ |
|
600 On a items request, return some items out of order for the given node. |
|
601 """ |
|
602 xml = """ |
|
603 <iq type='get' to='pubsub.example.org' |
|
604 from='user@example.org'> |
|
605 <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
|
606 <items node='test'/> |
|
607 </pubsub> |
|
608 <set xmlns='http://jabber.org/protocol/rsm'> |
|
609 <max>2</max> |
|
610 <index>3</index> |
|
611 </set> |
|
612 </iq> |
|
613 """ |
|
614 |
|
615 def items(request): |
|
616 rsm = RSMResponse(800, 3, u'i1', u'i2').toElement() |
|
617 return defer.succeed([pubsub.Item('i1'), pubsub.Item('i2'), rsm]) |
|
618 |
|
619 def cb(element): |
|
620 self.assertEqual(pubsub.NS_PUBSUB, element.uri) |
|
621 self.assertEqual(pubsub.NS_PUBSUB, element.items.uri) |
|
622 self.assertEqual(2, len(element.items.children)) |
|
623 item = element.items.children[0] |
|
624 self.assertTrue(domish.IElement.providedBy(item)) |
|
625 self.assertEqual('item', item.name) |
|
626 self.assertEqual(pubsub.NS_PUBSUB, item.uri) |
|
627 self.assertEqual('i1', item['id']) |
|
628 item = element.items.children[1] |
|
629 self.assertTrue(domish.IElement.providedBy(item)) |
|
630 self.assertEqual('item', item.name) |
|
631 self.assertEqual(pubsub.NS_PUBSUB, item.uri) |
|
632 self.assertEqual('i2', item['id']) |
|
633 self.assertEqual(NS_RSM, element.set.uri) |
|
634 self.assertEqual('800', ''.join(element.set.count.children)) |
|
635 self.assertEqual('3', element.set.first['index']) |
|
636 self.assertEqual('i1', ''.join(element.set.first.children)) |
|
637 self.assertEqual('i2', ''.join(element.set.last.children)) |
|
638 |
|
639 self.resource.items = items |
|
640 verify.verifyObject(iwokkel.IPubSubResource, self.resource) |
|
641 d = self.handleRequest(xml) |
|
642 d.addCallback(cb) |
|
643 return d |
|
644 |
|
645 def test_on_itemsCount(self): |
|
646 """ |
|
647 On a items request, return the items count. |
|
648 """ |
|
649 xml = """ |
|
650 <iq type='get' to='pubsub.example.org' |
|
651 from='user@example.org'> |
|
652 <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
|
653 <items node='test'/> |
|
654 </pubsub> |
|
655 <set xmlns='http://jabber.org/protocol/rsm'> |
|
656 <max>0</max> |
|
657 </set> |
|
658 </iq> |
|
659 """ |
|
660 |
|
661 def items(request): |
|
662 rsm = RSMResponse(800).toElement() |
|
663 return defer.succeed([rsm]) |
|
664 |
|
665 def cb(element): |
|
666 self.assertEqual(pubsub.NS_PUBSUB, element.uri) |
|
667 self.assertEqual(pubsub.NS_PUBSUB, element.items.uri) |
|
668 self.assertEqual(0, len(element.items.children)) |
|
669 self.assertEqual(NS_RSM, element.set.uri) |
|
670 self.assertEqual('800', ''.join(element.set.count.children)) |
|
671 self.assertEqual(None, element.set.first) |
|
672 self.assertEqual(None, element.set.last) |
|
673 |
|
674 self.resource.items = items |
|
675 verify.verifyObject(iwokkel.IPubSubResource, self.resource) |
|
676 d = self.handleRequest(xml) |
|
677 d.addCallback(cb) |
|
678 return d |