comparison idavoll/test/test_gateway.py @ 206:274a45d2a5ab

Implement root collection that includes all leaf nodes.
author Ralph Meijer <ralphm@ik.nu>
date Mon, 04 Aug 2008 13:47:10 +0000
parents b4bf0a5ce50d
children 7f3ffb7a1a9e
comparison
equal deleted inserted replaced
205:e6b710bf2b24 206:274a45d2a5ab
16 from idavoll import gateway 16 from idavoll import gateway
17 17
18 AGENT = "Idavoll Test Script" 18 AGENT = "Idavoll Test Script"
19 NS_ATOM = "http://www.w3.org/2005/Atom" 19 NS_ATOM = "http://www.w3.org/2005/Atom"
20 20
21 entry = domish.Element((NS_ATOM, 'entry')) 21 TEST_ENTRY = domish.Element((NS_ATOM, 'entry'))
22 entry.addElement("id", content="urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a") 22 TEST_ENTRY.addElement("id",
23 entry.addElement("title", content="Atom-Powered Robots Run Amok") 23 content="urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a")
24 entry.addElement("author").addElement("name", content="John Doe") 24 TEST_ENTRY.addElement("title", content="Atom-Powered Robots Run Amok")
25 entry.addElement("content", content="Some text.") 25 TEST_ENTRY.addElement("author").addElement("name", content="John Doe")
26 TEST_ENTRY.addElement("content", content="Some text.")
26 27
27 baseURI = "http://localhost:8086/" 28 baseURI = "http://localhost:8086/"
28 componentJID = "pubsub" 29 componentJID = "pubsub"
29 30
30 class GatewayTest(unittest.TestCase): 31 class GatewayTest(unittest.TestCase):
31 timeout = 2 32 timeout = 2
32 33
33 def setUp(self): 34 def setUp(self):
34 self.client = gateway.GatewayClient(baseURI) 35 self.client = gateway.GatewayClient(baseURI)
35 self.client.startService() 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
36 53
37 def tearDown(self): 54 def tearDown(self):
38 return self.client.stopService() 55 return self.client.stopService()
39 56
57
40 def test_create(self): 58 def test_create(self):
41 59
42 def cb(response): 60 def cb(response):
43 self.assertIn('uri', response) 61 self.assertIn('uri', response)
44 62
49 def test_publish(self): 67 def test_publish(self):
50 68
51 def cb(response): 69 def cb(response):
52 self.assertIn('uri', response) 70 self.assertIn('uri', response)
53 71
54 d = self.client.publish(entry) 72 d = self.client.publish(TEST_ENTRY)
55 d.addCallback(cb) 73 d.addCallback(cb)
56 return d 74 return d
57 75
58 def test_publishExistingNode(self): 76 def test_publishExistingNode(self):
59 77
60 def cb2(response, xmppURI): 78 def cb2(response, xmppURI):
61 self.assertEquals(xmppURI, response['uri']) 79 self.assertEquals(xmppURI, response['uri'])
62 80
63 def cb1(response): 81 def cb1(response):
64 xmppURI = response['uri'] 82 xmppURI = response['uri']
65 d = self.client.publish(entry, xmppURI) 83 d = self.client.publish(TEST_ENTRY, xmppURI)
66 d.addCallback(cb2, xmppURI) 84 d.addCallback(cb2, xmppURI)
67 return d 85 return d
68 86
69 d = self.client.create() 87 d = self.client.create()
70 d.addCallback(cb1) 88 d.addCallback(cb1)
72 90
73 def test_publishNonExisting(self): 91 def test_publishNonExisting(self):
74 def cb(err): 92 def cb(err):
75 self.assertEqual('404', err.status) 93 self.assertEqual('404', err.status)
76 94
77 d = self.client.publish(entry, 'xmpp:%s?node=test' % componentJID) 95 d = self.client.publish(TEST_ENTRY, 'xmpp:%s?node=test' % componentJID)
78 self.assertFailure(d, error.Error) 96 self.assertFailure(d, error.Error)
79 d.addCallback(cb) 97 d.addCallback(cb)
80 return d 98 return d
81 99
82 def test_list(self): 100 def test_list(self):
91 109
92 d = self.client.create() 110 d = self.client.create()
93 d.addCallback(cb) 111 d.addCallback(cb)
94 return d 112 return d
95 113
96
97 def test_subscribeGetNotification(self): 114 def test_subscribeGetNotification(self):
98 115
99 def onNotification(data, headers): 116 def onNotification(data, headers):
100 self.client.deferred.callback(None) 117 self.client.deferred.callback(None)
101 118
104 d = self.client.subscribe(xmppURI) 121 d = self.client.subscribe(xmppURI)
105 d.addCallback(lambda _: xmppURI) 122 d.addCallback(lambda _: xmppURI)
106 return d 123 return d
107 124
108 def cb2(xmppURI): 125 def cb2(xmppURI):
109 d = self.client.publish(entry, xmppURI) 126 d = self.client.publish(TEST_ENTRY, xmppURI)
110 return d 127 return d
111 128
112 129
113 self.client.callback = onNotification 130 self.client.callback = onNotification
114 self.client.deferred = defer.Deferred() 131 self.client.deferred = defer.Deferred()
138 d = client2.subscribe(xmppURI) 155 d = client2.subscribe(xmppURI)
139 d.addCallback(lambda _: xmppURI) 156 d.addCallback(lambda _: xmppURI)
140 return d 157 return d
141 158
142 def cb3(xmppURI): 159 def cb3(xmppURI):
143 d = self.client.publish(entry, xmppURI) 160 d = self.client.publish(TEST_ENTRY, xmppURI)
144 return d 161 return d
145 162
146 163
147 client1 = gateway.GatewayClient(baseURI, callbackPort=8088) 164 client1 = gateway.GatewayClient(baseURI, callbackPort=8088)
148 client1.startService() 165 client1.startService()
167 self.client.deferred.callback(None) 184 self.client.deferred.callback(None)
168 185
169 def cb(response): 186 def cb(response):
170 xmppURI = response['uri'] 187 xmppURI = response['uri']
171 self.assertNot(self.client.deferred.called) 188 self.assertNot(self.client.deferred.called)
172 d = self.client.publish(entry, xmppURI) 189 d = self.client.publish(TEST_ENTRY, xmppURI)
173 d.addCallback(lambda _: xmppURI) 190 d.addCallback(lambda _: xmppURI)
174 return d 191 return d
175 192
176 def cb2(xmppURI): 193 def cb2(xmppURI):
177 d = self.client.subscribe(xmppURI) 194 d = self.client.subscribe(xmppURI)
200 217
201 def cb(response): 218 def cb(response):
202 xmppURI = response['uri'] 219 xmppURI = response['uri']
203 self.assertNot(client1.deferred.called) 220 self.assertNot(client1.deferred.called)
204 self.assertNot(client2.deferred.called) 221 self.assertNot(client2.deferred.called)
205 d = self.client.publish(entry, xmppURI) 222 d = self.client.publish(TEST_ENTRY, xmppURI)
206 d.addCallback(lambda _: xmppURI) 223 d.addCallback(lambda _: xmppURI)
207 return d 224 return d
208 225
209 def cb2(xmppURI): 226 def cb2(xmppURI):
210 d = client1.subscribe(xmppURI) 227 d = client1.subscribe(xmppURI)
222 client2 = gateway.GatewayClient(baseURI, callbackPort=8089) 239 client2 = gateway.GatewayClient(baseURI, callbackPort=8089)
223 client2.startService() 240 client2.startService()
224 client2.callback = onNotification2 241 client2.callback = onNotification2
225 client2.deferred = defer.Deferred() 242 client2.deferred = defer.Deferred()
226 243
244
227 d = self.client.create() 245 d = self.client.create()
228 d.addCallback(cb) 246 d.addCallback(cb)
229 d.addCallback(cb2) 247 d.addCallback(cb2)
230 d.addCallback(cb3) 248 d.addCallback(cb3)
231 dl = defer.gatherResults([d, client1.deferred, client2.deferred]) 249 dl = defer.gatherResults([d, client1.deferred, client2.deferred])
240 self.assertFailure(d, error.Error) 258 self.assertFailure(d, error.Error)
241 d.addCallback(cb) 259 d.addCallback(cb)
242 return d 260 return d
243 261
244 262
263 def test_subscribeRootGetNotification(self):
264
265 def onNotification(data, headers):
266 self.client.deferred.callback(None)
267
268 def cb(response):
269 xmppURI = response['uri']
270 jid, nodeIdentifier = gateway.getServiceAndNode(xmppURI)
271 rootNode = gateway.getXMPPURI(jid, '')
272
273 d = self.client.subscribe(rootNode)
274 d.addCallback(lambda _: xmppURI)
275 return d
276
277 def cb2(xmppURI):
278 return self.client.publish(TEST_ENTRY, xmppURI)
279
280
281 self.client.callback = onNotification
282 self.client.deferred = defer.Deferred()
283 d = self.client.create()
284 d.addCallback(cb)
285 d.addCallback(cb2)
286 return defer.gatherResults([d, self.client.deferred])
287
288
245 def test_unsubscribeNonExisting(self): 289 def test_unsubscribeNonExisting(self):
246 def cb(err): 290 def cb(err):
247 self.assertEqual('403', err.status) 291 self.assertEqual('403', err.status)
248 292
249 d = self.client.unsubscribe('xmpp:%s?node=test' % componentJID) 293 d = self.client.unsubscribe('xmpp:%s?node=test' % componentJID)
256 def cb(response): 300 def cb(response):
257 xmppURI = response['uri'] 301 xmppURI = response['uri']
258 d = self.client.items(xmppURI) 302 d = self.client.items(xmppURI)
259 return d 303 return d
260 304
261 d = self.client.publish(entry) 305 d = self.client.publish(TEST_ENTRY)
262 d.addCallback(cb) 306 d.addCallback(cb)
263 return d 307 return d
264 308
265 309
266 def test_itemsMaxItems(self): 310 def test_itemsMaxItems(self):
267 def cb(response): 311 def cb(response):
268 xmppURI = response['uri'] 312 xmppURI = response['uri']
269 d = self.client.items(xmppURI, 2) 313 d = self.client.items(xmppURI, 2)
270 return d 314 return d
271 315
272 d = self.client.publish(entry) 316 d = self.client.publish(TEST_ENTRY)
273 d.addCallback(cb) 317 d.addCallback(cb)
274 return d 318 return d