Mercurial > sat_tmp
annotate wokkel/test/test_rsm.py @ 31:e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
- parse has been renamed to fromElement in rev 1771 (54f834e40341)
- use RSMResponse directly instead of ext_data in rev 1773 (777b4e63fc8a)
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 19 Jan 2016 12:18:29 +0100 |
parents | 2cd4f0ab9ad1 |
children | cfee3d472359 |
rev | line source |
---|---|
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 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
53 request = RSMRequest.fromElement(parseXml(xml)) |
10 | 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 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
72 request = RSMRequest.fromElement(parseXml(xml)) |
10 | 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 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
91 request = RSMRequest.fromElement(parseXml(xml)) |
10 | 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 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
110 request = RSMRequest.fromElement(parseXml(xml)) |
10 | 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 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
129 request = RSMRequest.fromElement(parseXml(xml)) |
10 | 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 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
147 request = RSMRequest.fromElement(parseXml(xml)) |
10 | 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 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
255 response = RSMResponse.fromElement(parseXml(xml)) |
10 | 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 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
272 response = RSMResponse.fromElement(parseXml(xml)) |
10 | 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 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
323 def cb(response): |
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
324 items, rsm = response |
10 | 325 self.assertEquals(2, len(items)) |
326 self.assertEquals([item1, item2], items) | |
327 self.assertDictEqual(rsm, {'count': '800', 'index': '0', | |
328 'first': 'item1', 'last': 'item2'}) | |
329 | |
330 d = self.protocol.items(JID('pubsub.example.org'), 'test', | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
331 rsm_request=RSMRequest(2)) |
10 | 332 d.addCallback(cb) |
333 | |
334 iq = self.stub.output[-1] | |
335 self.assertEquals('pubsub.example.org', iq.getAttribute('to')) | |
336 self.assertEquals('get', iq.getAttribute('type')) | |
337 self.assertEquals('pubsub', iq.pubsub.name) | |
338 self.assertEquals(pubsub.NS_PUBSUB, iq.pubsub.uri) | |
339 children = list(domish.generateElementsQNamed(iq.pubsub.children, | |
340 'items', pubsub.NS_PUBSUB)) | |
341 self.assertEquals(1, len(children)) | |
342 child = children[0] | |
343 self.assertEquals('test', child['node']) | |
344 | |
345 set_elts = list(domish.generateElementsQNamed(iq.pubsub.children, | |
346 'set', NS_RSM)) | |
347 self.assertEquals(1, len(set_elts)) | |
348 set_elt = set_elts[0] | |
349 self.assertEquals(u'2', ''.join(set_elt.max.children)) | |
350 | |
351 response = toResponse(iq, 'result') | |
352 items = response.addElement((pubsub.NS_PUBSUB, | |
353 'pubsub')).addElement('items') | |
354 items['node'] = 'test' | |
355 item1 = items.addElement('item') | |
356 item1['id'] = 'item1' | |
357 item2 = items.addElement('item') | |
358 item2['id'] = 'item2' | |
359 RSMResponse(800, 0, u'item1', u'item2').render(response.pubsub) | |
360 self.stub.send(response) | |
361 | |
362 return d | |
363 | |
364 def test_itemsAfter(self): | |
365 """ | |
366 Test sending items request to get the next page. | |
367 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
368 def cb(response): |
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
369 items, rsm = response |
10 | 370 self.assertEquals(2, len(items)) |
371 self.assertEquals([item1, item2], items) | |
372 self.assertDictEqual(rsm, {'count': '800', 'index': '2', | |
373 'first': 'item3', 'last': 'item4'}) | |
374 | |
375 d = self.protocol.items(JID('pubsub.example.org'), 'test', | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
376 rsm_request=RSMRequest(2, after=u'item2')) |
10 | 377 d.addCallback(cb) |
378 | |
379 iq = self.stub.output[-1] | |
380 self.assertEquals('pubsub.example.org', iq.getAttribute('to')) | |
381 self.assertEquals('get', iq.getAttribute('type')) | |
382 self.assertEquals('pubsub', iq.pubsub.name) | |
383 self.assertEquals(pubsub.NS_PUBSUB, iq.pubsub.uri) | |
384 children = list(domish.generateElementsQNamed(iq.pubsub.children, | |
385 'items', pubsub.NS_PUBSUB)) | |
386 self.assertEquals(1, len(children)) | |
387 child = children[0] | |
388 self.assertEquals('test', child['node']) | |
389 | |
390 set_elts = list(domish.generateElementsQNamed(iq.pubsub.children, | |
391 'set', NS_RSM)) | |
392 self.assertEquals(1, len(set_elts)) | |
393 set_elt = set_elts[0] | |
394 self.assertEquals(u'2', ''.join(set_elt.max.children)) | |
395 self.assertEquals(u'item2', ''.join(set_elt.after.children)) | |
396 | |
397 response = toResponse(iq, 'result') | |
398 items = response.addElement((pubsub.NS_PUBSUB, | |
399 'pubsub')).addElement('items') | |
400 items['node'] = 'test' | |
401 item1 = items.addElement('item') | |
402 item1['id'] = 'item3' | |
403 item2 = items.addElement('item') | |
404 item2['id'] = 'item4' | |
405 RSMResponse(800, 2, u'item3', u'item4').render(response.pubsub) | |
406 self.stub.send(response) | |
407 | |
408 return d | |
409 | |
410 def test_itemsBefore(self): | |
411 """ | |
412 Test sending items request to get the previous page. | |
413 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
414 def cb(response): |
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
415 items, rsm = response |
10 | 416 self.assertEquals(2, len(items)) |
417 self.assertEquals([item1, item2], items) | |
418 self.assertDictEqual(rsm, {'count': '800', 'index': '0', | |
419 'first': 'item1', 'last': 'item2'}) | |
420 | |
421 d = self.protocol.items(JID('pubsub.example.org'), 'test', | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
422 rsm_request=RSMRequest(2, before=u'item3')) |
10 | 423 d.addCallback(cb) |
424 | |
425 iq = self.stub.output[-1] | |
426 self.assertEquals('pubsub.example.org', iq.getAttribute('to')) | |
427 self.assertEquals('get', iq.getAttribute('type')) | |
428 self.assertEquals('pubsub', iq.pubsub.name) | |
429 self.assertEquals(pubsub.NS_PUBSUB, iq.pubsub.uri) | |
430 children = list(domish.generateElementsQNamed(iq.pubsub.children, | |
431 'items', pubsub.NS_PUBSUB)) | |
432 self.assertEquals(1, len(children)) | |
433 child = children[0] | |
434 self.assertEquals('test', child['node']) | |
435 | |
436 set_elts = list(domish.generateElementsQNamed(iq.pubsub.children, | |
437 'set', NS_RSM)) | |
438 self.assertEquals(1, len(set_elts)) | |
439 set_elt = set_elts[0] | |
440 self.assertEquals(u'2', ''.join(set_elt.max.children)) | |
441 self.assertEquals(u'item3', ''.join(set_elt.before.children)) | |
442 | |
443 response = toResponse(iq, 'result') | |
444 items = response.addElement((pubsub.NS_PUBSUB, | |
445 'pubsub')).addElement('items') | |
446 items['node'] = 'test' | |
447 item1 = items.addElement('item') | |
448 item1['id'] = 'item1' | |
449 item2 = items.addElement('item') | |
450 item2['id'] = 'item2' | |
451 RSMResponse(800, 0, u'item1', u'item2').render(response.pubsub) | |
452 self.stub.send(response) | |
453 | |
454 return d | |
455 | |
456 def test_itemsIndex(self): | |
457 """ | |
458 Test sending items request to get a page out of order. | |
459 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
460 def cb(response): |
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
461 items, rsm = response |
10 | 462 self.assertEquals(3, len(items)) |
463 self.assertEquals([item1, item2, item3], items) | |
464 self.assertDictEqual(rsm, {'count': '800', 'index': '3', | |
465 'first': 'item4', 'last': 'item6'}) | |
466 | |
467 d = self.protocol.items(JID('pubsub.example.org'), 'test', | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
468 rsm_response=RSMRequest(3, 3)) |
10 | 469 d.addCallback(cb) |
470 | |
471 iq = self.stub.output[-1] | |
472 self.assertEquals('pubsub.example.org', iq.getAttribute('to')) | |
473 self.assertEquals('get', iq.getAttribute('type')) | |
474 self.assertEquals('pubsub', iq.pubsub.name) | |
475 self.assertEquals(pubsub.NS_PUBSUB, iq.pubsub.uri) | |
476 children = list(domish.generateElementsQNamed(iq.pubsub.children, | |
477 'items', pubsub.NS_PUBSUB)) | |
478 self.assertEquals(1, len(children)) | |
479 child = children[0] | |
480 self.assertEquals('test', child['node']) | |
481 | |
482 set_elts = list(domish.generateElementsQNamed(iq.pubsub.children, | |
483 'set', NS_RSM)) | |
484 self.assertEquals(1, len(set_elts)) | |
485 set_elt = set_elts[0] | |
486 self.assertEquals(u'3', ''.join(set_elt.max.children)) | |
487 self.assertEquals(u'3', ''.join(set_elt.index.children)) | |
488 | |
489 response = toResponse(iq, 'result') | |
490 items = response.addElement((pubsub.NS_PUBSUB, | |
491 'pubsub')).addElement('items') | |
492 items['node'] = 'test' | |
493 item1 = items.addElement('item') | |
494 item1['id'] = 'item4' | |
495 item2 = items.addElement('item') | |
496 item2['id'] = 'item5' | |
497 item3 = items.addElement('item') | |
498 item3['id'] = 'item6' | |
499 RSMResponse(800, 3, u'item4', u'item6').render(response.pubsub) | |
500 self.stub.send(response) | |
501 | |
502 return d | |
503 | |
504 def test_itemsCount(self): | |
505 """ | |
506 Test sending items request to count them. | |
507 """ | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
508 def cb(response): |
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
509 items, rsm = response |
10 | 510 self.assertEquals(0, len(items)) |
511 self.assertDictEqual(rsm, {'count': '800'}) | |
512 | |
513 d = self.protocol.items(JID('pubsub.example.org'), 'test', | |
31
e79eedd9c56a
test (RSM): fix according to modification in sat.tmp.wokkel:
souliane <souliane@mailoo.org>
parents:
10
diff
changeset
|
514 rsm_request=RSMRequest(0)) |
10 | 515 d.addCallback(cb) |
516 | |
517 iq = self.stub.output[-1] | |
518 self.assertEquals('pubsub.example.org', iq.getAttribute('to')) | |
519 self.assertEquals('get', iq.getAttribute('type')) | |
520 self.assertEquals('pubsub', iq.pubsub.name) | |
521 self.assertEquals(pubsub.NS_PUBSUB, iq.pubsub.uri) | |
522 children = list(domish.generateElementsQNamed(iq.pubsub.children, | |
523 'items', pubsub.NS_PUBSUB)) | |
524 self.assertEquals(1, len(children)) | |
525 child = children[0] | |
526 self.assertEquals('test', child['node']) | |
527 | |
528 set_elts = list(domish.generateElementsQNamed(iq.pubsub.children, | |
529 'set', NS_RSM)) | |
530 self.assertEquals(1, len(set_elts)) | |
531 set_elt = set_elts[0] | |
532 self.assertEquals(u'0', ''.join(set_elt.max.children)) | |
533 | |
534 response = toResponse(iq, 'result') | |
535 response.addElement((pubsub.NS_PUBSUB, 'pubsub')) | |
536 RSMResponse(800).render(response.pubsub) | |
537 self.stub.send(response) | |
538 | |
539 return d | |
540 | |
541 | |
542 class PubSubServiceTest(unittest.TestCase, TestableRequestHandlerMixin): | |
543 | |
544 def setUp(self): | |
545 self.stub = XmlStreamStub() | |
546 self.resource = pubsub.PubSubResource() | |
547 self.service = PubSubService(self.resource) | |
548 self.service.send = self.stub.xmlstream.send | |
549 | |
550 def test_on_items(self): | |
551 """ | |
552 On a items request, return the first item for the given node. | |
553 """ | |
554 xml = """ | |
555 <iq type='get' to='pubsub.example.org' | |
556 from='user@example.org'> | |
557 <pubsub xmlns='http://jabber.org/protocol/pubsub'> | |
558 <items node='test'/> | |
559 </pubsub> | |
560 <set xmlns='http://jabber.org/protocol/rsm'> | |
561 <max>1</max> | |
562 </set> | |
563 </iq> | |
564 """ | |
565 | |
566 def items(request): | |
567 rsm = RSMResponse(800, 0, u'item', u'item').toElement() | |
568 return defer.succeed([pubsub.Item('current'), rsm]) | |
569 | |
570 def cb(element): | |
571 self.assertEqual(pubsub.NS_PUBSUB, element.uri) | |
572 self.assertEqual(pubsub.NS_PUBSUB, element.items.uri) | |
573 self.assertEqual(1, len(element.items.children)) | |
574 item = element.items.children[-1] | |
575 self.assertTrue(domish.IElement.providedBy(item)) | |
576 self.assertEqual('item', item.name) | |
577 self.assertEqual(pubsub.NS_PUBSUB, item.uri) | |
578 self.assertEqual('current', item['id']) | |
579 self.assertEqual(NS_RSM, element.set.uri) | |
580 self.assertEqual('800', ''.join(element.set.count.children)) | |
581 self.assertEqual('0', element.set.first['index']) | |
582 self.assertEqual('item', ''.join(element.set.first.children)) | |
583 self.assertEqual('item', ''.join(element.set.last.children)) | |
584 | |
585 self.resource.items = items | |
586 verify.verifyObject(iwokkel.IPubSubResource, self.resource) | |
587 d = self.handleRequest(xml) | |
588 d.addCallback(cb) | |
589 return d | |
590 | |
591 def test_on_itemsIndex(self): | |
592 """ | |
593 On a items request, return some items out of order for the given node. | |
594 """ | |
595 xml = """ | |
596 <iq type='get' to='pubsub.example.org' | |
597 from='user@example.org'> | |
598 <pubsub xmlns='http://jabber.org/protocol/pubsub'> | |
599 <items node='test'/> | |
600 </pubsub> | |
601 <set xmlns='http://jabber.org/protocol/rsm'> | |
602 <max>2</max> | |
603 <index>3</index> | |
604 </set> | |
605 </iq> | |
606 """ | |
607 | |
608 def items(request): | |
609 rsm = RSMResponse(800, 3, u'i1', u'i2').toElement() | |
610 return defer.succeed([pubsub.Item('i1'), pubsub.Item('i2'), rsm]) | |
611 | |
612 def cb(element): | |
613 self.assertEqual(pubsub.NS_PUBSUB, element.uri) | |
614 self.assertEqual(pubsub.NS_PUBSUB, element.items.uri) | |
615 self.assertEqual(2, len(element.items.children)) | |
616 item = element.items.children[0] | |
617 self.assertTrue(domish.IElement.providedBy(item)) | |
618 self.assertEqual('item', item.name) | |
619 self.assertEqual(pubsub.NS_PUBSUB, item.uri) | |
620 self.assertEqual('i1', item['id']) | |
621 item = element.items.children[1] | |
622 self.assertTrue(domish.IElement.providedBy(item)) | |
623 self.assertEqual('item', item.name) | |
624 self.assertEqual(pubsub.NS_PUBSUB, item.uri) | |
625 self.assertEqual('i2', item['id']) | |
626 self.assertEqual(NS_RSM, element.set.uri) | |
627 self.assertEqual('800', ''.join(element.set.count.children)) | |
628 self.assertEqual('3', element.set.first['index']) | |
629 self.assertEqual('i1', ''.join(element.set.first.children)) | |
630 self.assertEqual('i2', ''.join(element.set.last.children)) | |
631 | |
632 self.resource.items = items | |
633 verify.verifyObject(iwokkel.IPubSubResource, self.resource) | |
634 d = self.handleRequest(xml) | |
635 d.addCallback(cb) | |
636 return d | |
637 | |
638 def test_on_itemsCount(self): | |
639 """ | |
640 On a items request, return the items count. | |
641 """ | |
642 xml = """ | |
643 <iq type='get' to='pubsub.example.org' | |
644 from='user@example.org'> | |
645 <pubsub xmlns='http://jabber.org/protocol/pubsub'> | |
646 <items node='test'/> | |
647 </pubsub> | |
648 <set xmlns='http://jabber.org/protocol/rsm'> | |
649 <max>0</max> | |
650 </set> | |
651 </iq> | |
652 """ | |
653 | |
654 def items(request): | |
655 rsm = RSMResponse(800).toElement() | |
656 return defer.succeed([rsm]) | |
657 | |
658 def cb(element): | |
659 self.assertEqual(pubsub.NS_PUBSUB, element.uri) | |
660 self.assertEqual(pubsub.NS_PUBSUB, element.items.uri) | |
661 self.assertEqual(0, len(element.items.children)) | |
662 self.assertEqual(NS_RSM, element.set.uri) | |
663 self.assertEqual('800', ''.join(element.set.count.children)) | |
664 self.assertEqual(None, element.set.first) | |
665 self.assertEqual(None, element.set.last) | |
666 | |
667 self.resource.items = items | |
668 verify.verifyObject(iwokkel.IPubSubResource, self.resource) | |
669 d = self.handleRequest(xml) | |
670 d.addCallback(cb) | |
671 return d |