Debian Codegraph @

tx_xmpp/test/test_ping.py

raw
1 # Copyright (c) Ralph Meijer.
2 # See LICENSE for details.
3
4 """
5 Tests for L{tx_xmpp.ping}.
6 """
7
8 from zope.interface import verify
9
10 from twisted.internet import defer
11 from twisted.trial import unittest
12 from twisted.words.protocols.jabber.error import StanzaError
13 from twisted.words.protocols.jabber.jid import JID
14 from twisted.words.protocols.jabber.xmlstream import toResponse
15
16 from .. import disco, itx_xmpp, ping
17 from ..generic import parseXml
18 from .helpers import XmlStreamStub
19
20
21 class PingClientProtocolTest(unittest.TestCase):
22 """
23 Tests for L{ping.PingClientProtocol}.
24 """
25
26 def setUp(self):
27 self.stub = XmlStreamStub()
28 self.protocol = ping.PingClientProtocol()
29 self.protocol.xmlstream = self.stub.xmlstream
30 self.protocol.connectionInitialized()
31
32 def test_ping(self):
33 """
34 Pinging a service should fire a deferred with None
35 """
36
37 def cb(result):
38 self.assertIdentical(None, result)
39
40 d = self.protocol.ping(JID("example.com"))
41 d.addCallback(cb)
42
43 iq = self.stub.output[-1]
44 self.assertEqual("example.com", iq.getAttribute("to"))
45 self.assertEqual("get", iq.getAttribute("type"))
46 self.assertEqual("urn:xmpp:ping", iq.ping.uri)
47
48 response = toResponse(iq, "result")
49 self.stub.send(response)
50
51 return d
52
53 def test_pingWithSender(self):
54 """
55 Pinging a service with a sender address should include that address.
56 """
57 d = self.protocol.ping(JID("example.com"), sender=JID("[email protected]"))
58
59 iq = self.stub.output[-1]
60 self.assertEqual("[email protected]", iq.getAttribute("from"))
61
62 response = toResponse(iq, "result")
63 self.stub.send(response)
64
65 return d
66
67 def test_pingNotSupported(self):
68 """
69 Pinging a service should fire a deferred with None if not supported.
70 """
71
72 def cb(result):
73 self.assertIdentical(None, result)
74
75 d = self.protocol.ping(JID("example.com"))
76 d.addCallback(cb)
77
78 iq = self.stub.output[-1]
79
80 exc = StanzaError("service-unavailable")
81 response = exc.toResponse(iq)
82 self.stub.send(response)
83
84 return d
85
86 def test_pingStanzaError(self):
87 """
88 Pinging a service should errback a deferred on other (stanza) errors.
89 """
90
91 def cb(exc):
92 self.assertEquals("item-not-found", exc.condition)
93
94 d = self.protocol.ping(JID("example.com"))
95 self.assertFailure(d, StanzaError)
96 d.addCallback(cb)
97
98 iq = self.stub.output[-1]
99
100 exc = StanzaError("item-not-found")
101 response = exc.toResponse(iq)
102 self.stub.send(response)
103
104 return d
105
106
107 class PingHandlerTest(unittest.TestCase):
108 """
109 Tests for L{ping.PingHandler}.
110 """
111
112 def setUp(self):
113 self.stub = XmlStreamStub()
114 self.protocol = ping.PingHandler()
115 self.protocol.xmlstream = self.stub.xmlstream
116 self.protocol.connectionInitialized()
117
118 def test_onPing(self):
119 """
120 A ping should have a simple result response.
121 """
122 xml = """<iq from='[email protected]' to='example.com' type='get'>
123 <ping xmlns='urn:xmpp:ping'/>
124 </iq>"""
125 self.stub.send(parseXml(xml))
126
127 response = self.stub.output[-1]
128 self.assertEquals("example.com", response.getAttribute("from"))
129 self.assertEquals("[email protected]", response.getAttribute("to"))
130 self.assertEquals("result", response.getAttribute("type"))
131
132 def test_onPingHandled(self):
133 """
134 The ping handler should mark the stanza as handled.
135 """
136 xml = """<iq from='[email protected]' to='example.com' type='get'>
137 <ping xmlns='urn:xmpp:ping'/>
138 </iq>"""
139 iq = parseXml(xml)
140 self.stub.send(iq)
141
142 self.assertTrue(iq.handled)
143
144 def test_interfaceIDisco(self):
145 """
146 The ping handler should provice Service Discovery information.
147 """
148 verify.verifyObject(itx_xmpp.IDisco, self.protocol)
149
150 def test_getDiscoInfo(self):
151 """
152 The ping namespace should be returned as a supported feature.
153 """
154
155 def cb(info):
156 discoInfo = disco.DiscoInfo()
157 for item in info:
158 discoInfo.append(item)
159 self.assertIn("urn:xmpp:ping", discoInfo.features)
160
161 d = defer.maybeDeferred(
162 self.protocol.getDiscoInfo,
163 JID("[email protected]/home"),
164 JID("pubsub.example.org"),
165 "",
166 )
167 d.addCallback(cb)
168 return d
169
170 def test_getDiscoInfoNode(self):
171 """
172 The ping namespace should not be returned for a node.
173 """
174
175 def cb(info):
176 discoInfo = disco.DiscoInfo()
177 for item in info:
178 discoInfo.append(item)
179 self.assertNotIn("urn:xmpp:ping", discoInfo.features)
180
181 d = defer.maybeDeferred(
182 self.protocol.getDiscoInfo,
183 JID("[email protected]/home"),
184 JID("pubsub.example.org"),
185 "test",
186 )
187 d.addCallback(cb)
188 return d
189
190 def test_getDiscoItems(self):
191 """
192 Items are not supported by this handler, so an empty list is expected.
193 """
194
195 def cb(items):
196 self.assertEquals(0, len(items))
197
198 d = defer.maybeDeferred(
199 self.protocol.getDiscoItems,
200 JID("[email protected]/home"),
201 JID("pubsub.example.org"),
202 "",
203 )
204 d.addCallback(cb)
205 return d