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