comparison sat_pubsub/test/test_gateway.py @ 232:923281d4c5bc

renamed idavoll directory to sat_pubsub
author Goffi <goffi@goffi.org>
date Thu, 17 May 2012 12:48:14 +0200
parents idavoll/test/test_gateway.py@bfc198af5d27
children 564ae55219e1
comparison
equal deleted inserted replaced
231:d99047cd90f9 232:923281d4c5bc
1 # Copyright (c) 2003-2009 Ralph Meijer
2 # See LICENSE for details.
3
4 """
5 Tests for L{idavoll.gateway}.
6
7 Note that some tests are functional tests that require a running idavoll
8 service.
9 """
10
11 from twisted.internet import defer
12 from twisted.trial import unittest
13 from twisted.web import error
14 from twisted.words.xish import domish
15
16 from idavoll import gateway
17
18 AGENT = "Idavoll Test Script"
19 NS_ATOM = "http://www.w3.org/2005/Atom"
20
21 TEST_ENTRY = domish.Element((NS_ATOM, 'entry'))
22 TEST_ENTRY.addElement("id",
23 content="urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a")
24 TEST_ENTRY.addElement("title", content="Atom-Powered Robots Run Amok")
25 TEST_ENTRY.addElement("author").addElement("name", content="John Doe")
26 TEST_ENTRY.addElement("content", content="Some text.")
27
28 baseURI = "http://localhost:8086/"
29 componentJID = "pubsub"
30
31 class GatewayTest(unittest.TestCase):
32 timeout = 2
33
34 def setUp(self):
35 self.client = gateway.GatewayClient(baseURI)
36 self.client.startService()
37 self.addCleanup(self.client.stopService)
38
39 def trapConnectionRefused(failure):
40 from twisted.internet.error import ConnectionRefusedError
41 failure.trap(ConnectionRefusedError)
42 raise unittest.SkipTest("Gateway to test against is not available")
43
44 def trapNotFound(failure):
45 from twisted.web.error import Error
46 failure.trap(Error)
47
48 d = self.client.ping()
49 d.addErrback(trapConnectionRefused)
50 d.addErrback(trapNotFound)
51 return d
52
53
54 def tearDown(self):
55 return self.client.stopService()
56
57
58 def test_create(self):
59
60 def cb(response):
61 self.assertIn('uri', response)
62
63 d = self.client.create()
64 d.addCallback(cb)
65 return d
66
67 def test_publish(self):
68
69 def cb(response):
70 self.assertIn('uri', response)
71
72 d = self.client.publish(TEST_ENTRY)
73 d.addCallback(cb)
74 return d
75
76 def test_publishExistingNode(self):
77
78 def cb2(response, xmppURI):
79 self.assertEquals(xmppURI, response['uri'])
80
81 def cb1(response):
82 xmppURI = response['uri']
83 d = self.client.publish(TEST_ENTRY, xmppURI)
84 d.addCallback(cb2, xmppURI)
85 return d
86
87 d = self.client.create()
88 d.addCallback(cb1)
89 return d
90
91 def test_publishNonExisting(self):
92 def cb(err):
93 self.assertEqual('404', err.status)
94
95 d = self.client.publish(TEST_ENTRY, 'xmpp:%s?node=test' % componentJID)
96 self.assertFailure(d, error.Error)
97 d.addCallback(cb)
98 return d
99
100 def test_delete(self):
101 def cb(response):
102 xmppURI = response['uri']
103 d = self.client.delete(xmppURI)
104 return d
105
106 d = self.client.create()
107 d.addCallback(cb)
108 return d
109
110 def test_deleteWithRedirect(self):
111 def cb(response):
112 xmppURI = response['uri']
113 redirectURI = 'xmpp:%s?node=test' % componentJID
114 d = self.client.delete(xmppURI, redirectURI)
115 return d
116
117 d = self.client.create()
118 d.addCallback(cb)
119 return d
120
121 def test_deleteNotification(self):
122 def onNotification(data, headers):
123 try:
124 self.assertTrue(headers.hasHeader('Event'))
125 self.assertEquals(['DELETED'], headers.getRawHeaders('Event'))
126 self.assertFalse(headers.hasHeader('Link'))
127 except:
128 self.client.deferred.errback()
129 else:
130 self.client.deferred.callback(None)
131
132 def cb(response):
133 xmppURI = response['uri']
134 d = self.client.subscribe(xmppURI)
135 d.addCallback(lambda _: xmppURI)
136 return d
137
138 def cb2(xmppURI):
139 d = self.client.delete(xmppURI)
140 return d
141
142 self.client.callback = onNotification
143 self.client.deferred = defer.Deferred()
144 d = self.client.create()
145 d.addCallback(cb)
146 d.addCallback(cb2)
147 return defer.gatherResults([d, self.client.deferred])
148
149 def test_deleteNotificationWithRedirect(self):
150 redirectURI = 'xmpp:%s?node=test' % componentJID
151
152 def onNotification(data, headers):
153 try:
154 self.assertTrue(headers.hasHeader('Event'))
155 self.assertEquals(['DELETED'], headers.getRawHeaders('Event'))
156 self.assertEquals(['<%s>; rel=alternate' % redirectURI],
157 headers.getRawHeaders('Link'))
158 except:
159 self.client.deferred.errback()
160 else:
161 self.client.deferred.callback(None)
162
163 def cb(response):
164 xmppURI = response['uri']
165 d = self.client.subscribe(xmppURI)
166 d.addCallback(lambda _: xmppURI)
167 return d
168
169 def cb2(xmppURI):
170 d = self.client.delete(xmppURI, redirectURI)
171 return d
172
173 self.client.callback = onNotification
174 self.client.deferred = defer.Deferred()
175 d = self.client.create()
176 d.addCallback(cb)
177 d.addCallback(cb2)
178 return defer.gatherResults([d, self.client.deferred])
179
180 def test_list(self):
181 d = self.client.listNodes()
182 return d
183
184 def test_subscribe(self):
185 def cb(response):
186 xmppURI = response['uri']
187 d = self.client.subscribe(xmppURI)
188 return d
189
190 d = self.client.create()
191 d.addCallback(cb)
192 return d
193
194 def test_subscribeGetNotification(self):
195
196 def onNotification(data, headers):
197 self.client.deferred.callback(None)
198
199 def cb(response):
200 xmppURI = response['uri']
201 d = self.client.subscribe(xmppURI)
202 d.addCallback(lambda _: xmppURI)
203 return d
204
205 def cb2(xmppURI):
206 d = self.client.publish(TEST_ENTRY, xmppURI)
207 return d
208
209
210 self.client.callback = onNotification
211 self.client.deferred = defer.Deferred()
212 d = self.client.create()
213 d.addCallback(cb)
214 d.addCallback(cb2)
215 return defer.gatherResults([d, self.client.deferred])
216
217
218 def test_subscribeTwiceGetNotification(self):
219
220 def onNotification1(data, headers):
221 d = client1.stopService()
222 d.chainDeferred(client1.deferred)
223
224 def onNotification2(data, headers):
225 d = client2.stopService()
226 d.chainDeferred(client2.deferred)
227
228 def cb(response):
229 xmppURI = response['uri']
230 d = client1.subscribe(xmppURI)
231 d.addCallback(lambda _: xmppURI)
232 return d
233
234 def cb2(xmppURI):
235 d = client2.subscribe(xmppURI)
236 d.addCallback(lambda _: xmppURI)
237 return d
238
239 def cb3(xmppURI):
240 d = self.client.publish(TEST_ENTRY, xmppURI)
241 return d
242
243
244 client1 = gateway.GatewayClient(baseURI, callbackPort=8088)
245 client1.startService()
246 client1.callback = onNotification1
247 client1.deferred = defer.Deferred()
248 client2 = gateway.GatewayClient(baseURI, callbackPort=8089)
249 client2.startService()
250 client2.callback = onNotification2
251 client2.deferred = defer.Deferred()
252
253 d = self.client.create()
254 d.addCallback(cb)
255 d.addCallback(cb2)
256 d.addCallback(cb3)
257 dl = defer.gatherResults([d, client1.deferred, client2.deferred])
258 return dl
259
260
261 def test_subscribeGetDelayedNotification(self):
262
263 def onNotification(data, headers):
264 self.client.deferred.callback(None)
265
266 def cb(response):
267 xmppURI = response['uri']
268 self.assertNot(self.client.deferred.called)
269 d = self.client.publish(TEST_ENTRY, xmppURI)
270 d.addCallback(lambda _: xmppURI)
271 return d
272
273 def cb2(xmppURI):
274 d = self.client.subscribe(xmppURI)
275 return d
276
277
278 self.client.callback = onNotification
279 self.client.deferred = defer.Deferred()
280 d = self.client.create()
281 d.addCallback(cb)
282 d.addCallback(cb2)
283 return defer.gatherResults([d, self.client.deferred])
284
285 def test_subscribeGetDelayedNotification2(self):
286 """
287 Test that subscribing as second results in a notification being sent.
288 """
289
290 def onNotification1(data, headers):
291 client1.deferred.callback(None)
292 client1.stopService()
293
294 def onNotification2(data, headers):
295 client2.deferred.callback(None)
296 client2.stopService()
297
298 def cb(response):
299 xmppURI = response['uri']
300 self.assertNot(client1.deferred.called)
301 self.assertNot(client2.deferred.called)
302 d = self.client.publish(TEST_ENTRY, xmppURI)
303 d.addCallback(lambda _: xmppURI)
304 return d
305
306 def cb2(xmppURI):
307 d = client1.subscribe(xmppURI)
308 d.addCallback(lambda _: xmppURI)
309 return d
310
311 def cb3(xmppURI):
312 d = client2.subscribe(xmppURI)
313 return d
314
315 client1 = gateway.GatewayClient(baseURI, callbackPort=8088)
316 client1.startService()
317 client1.callback = onNotification1
318 client1.deferred = defer.Deferred()
319 client2 = gateway.GatewayClient(baseURI, callbackPort=8089)
320 client2.startService()
321 client2.callback = onNotification2
322 client2.deferred = defer.Deferred()
323
324
325 d = self.client.create()
326 d.addCallback(cb)
327 d.addCallback(cb2)
328 d.addCallback(cb3)
329 dl = defer.gatherResults([d, client1.deferred, client2.deferred])
330 return dl
331
332
333 def test_subscribeNonExisting(self):
334 def cb(err):
335 self.assertEqual('403', err.status)
336
337 d = self.client.subscribe('xmpp:%s?node=test' % componentJID)
338 self.assertFailure(d, error.Error)
339 d.addCallback(cb)
340 return d
341
342
343 def test_subscribeRootGetNotification(self):
344
345 def onNotification(data, headers):
346 self.client.deferred.callback(None)
347
348 def cb(response):
349 xmppURI = response['uri']
350 jid, nodeIdentifier = gateway.getServiceAndNode(xmppURI)
351 rootNode = gateway.getXMPPURI(jid, '')
352
353 d = self.client.subscribe(rootNode)
354 d.addCallback(lambda _: xmppURI)
355 return d
356
357 def cb2(xmppURI):
358 return self.client.publish(TEST_ENTRY, xmppURI)
359
360
361 self.client.callback = onNotification
362 self.client.deferred = defer.Deferred()
363 d = self.client.create()
364 d.addCallback(cb)
365 d.addCallback(cb2)
366 return defer.gatherResults([d, self.client.deferred])
367
368
369 def test_unsubscribeNonExisting(self):
370 def cb(err):
371 self.assertEqual('403', err.status)
372
373 d = self.client.unsubscribe('xmpp:%s?node=test' % componentJID)
374 self.assertFailure(d, error.Error)
375 d.addCallback(cb)
376 return d
377
378
379 def test_items(self):
380 def cb(response):
381 xmppURI = response['uri']
382 d = self.client.items(xmppURI)
383 return d
384
385 d = self.client.publish(TEST_ENTRY)
386 d.addCallback(cb)
387 return d
388
389
390 def test_itemsMaxItems(self):
391 def cb(response):
392 xmppURI = response['uri']
393 d = self.client.items(xmppURI, 2)
394 return d
395
396 d = self.client.publish(TEST_ENTRY)
397 d.addCallback(cb)
398 return d