/usr/share/pyshared/wokkel/test/test_generic.py is in python-wokkel 0.7.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | # Copyright (c) Ralph Meijer.
# See LICENSE for details.
"""
Tests for L{wokkel.generic}.
"""
from twisted.trial import unittest
from twisted.words.xish import domish
from twisted.words.protocols.jabber.jid import JID
from wokkel import generic
from wokkel.test.helpers import XmlStreamStub
NS_VERSION = 'jabber:iq:version'
class VersionHandlerTest(unittest.TestCase):
"""
Tests for L{wokkel.generic.VersionHandler}.
"""
def test_onVersion(self):
"""
Test response to incoming version request.
"""
self.stub = XmlStreamStub()
self.protocol = generic.VersionHandler('Test', '0.1.0')
self.protocol.xmlstream = self.stub.xmlstream
self.protocol.send = self.stub.xmlstream.send
self.protocol.connectionInitialized()
iq = domish.Element((None, 'iq'))
iq['from'] = 'user@example.org/Home'
iq['to'] = 'example.org'
iq['type'] = 'get'
iq.addElement((NS_VERSION, 'query'))
self.stub.send(iq)
response = self.stub.output[-1]
self.assertEquals('user@example.org/Home', response['to'])
self.assertEquals('example.org', response['from'])
self.assertEquals('result', response['type'])
self.assertEquals(NS_VERSION, response.query.uri)
elements = list(domish.generateElementsQNamed(response.query.children,
'name', NS_VERSION))
self.assertEquals(1, len(elements))
self.assertEquals('Test', unicode(elements[0]))
elements = list(domish.generateElementsQNamed(response.query.children,
'version', NS_VERSION))
self.assertEquals(1, len(elements))
self.assertEquals('0.1.0', unicode(elements[0]))
class XmlPipeTest(unittest.TestCase):
"""
Tests for L{wokkel.generic.XmlPipe}.
"""
def setUp(self):
self.pipe = generic.XmlPipe()
def test_sendFromSource(self):
"""
Send an element from the source and observe it from the sink.
"""
def cb(obj):
called.append(obj)
called = []
self.pipe.sink.addObserver('/test[@xmlns="testns"]', cb)
element = domish.Element(('testns', 'test'))
self.pipe.source.send(element)
self.assertEquals([element], called)
def test_sendFromSink(self):
"""
Send an element from the sink and observe it from the source.
"""
def cb(obj):
called.append(obj)
called = []
self.pipe.source.addObserver('/test[@xmlns="testns"]', cb)
element = domish.Element(('testns', 'test'))
self.pipe.sink.send(element)
self.assertEquals([element], called)
class StanzaTest(unittest.TestCase):
"""
Tests for L{generic.Stanza}.
"""
def test_fromElement(self):
xml = """
<message type='chat' from='other@example.org' to='user@example.org'/>
"""
stanza = generic.Stanza.fromElement(generic.parseXml(xml))
self.assertEqual('chat', stanza.stanzaType)
self.assertEqual(JID('other@example.org'), stanza.sender)
self.assertEqual(JID('user@example.org'), stanza.recipient)
def test_fromElementChildParser(self):
"""
Child elements for which no parser is defined are ignored.
"""
xml = """
<message from='other@example.org' to='user@example.org'>
<x xmlns='http://example.org/'/>
</message>
"""
class Message(generic.Stanza):
childParsers = {('http://example.org/', 'x'): '_childParser_x'}
elements = []
def _childParser_x(self, element):
self.elements.append(element)
message = Message.fromElement(generic.parseXml(xml))
self.assertEqual(1, len(message.elements))
def test_fromElementChildParserAll(self):
"""
Child elements for which no parser is defined are ignored.
"""
xml = """
<message from='other@example.org' to='user@example.org'>
<x xmlns='http://example.org/'/>
</message>
"""
class Message(generic.Stanza):
childParsers = {None: '_childParser'}
elements = []
def _childParser(self, element):
self.elements.append(element)
message = Message.fromElement(generic.parseXml(xml))
self.assertEqual(1, len(message.elements))
def test_fromElementChildParserUnknown(self):
"""
Child elements for which no parser is defined are ignored.
"""
xml = """
<message from='other@example.org' to='user@example.org'>
<x xmlns='http://example.org/'/>
</message>
"""
generic.Stanza.fromElement(generic.parseXml(xml))
class RequestTest(unittest.TestCase):
"""
Tests for L{generic.Request}.
"""
def setUp(self):
self.request = generic.Request()
def test_requestParser(self):
"""
The request's child element is passed to requestParser.
"""
xml = """
<iq type='get'>
<query xmlns='jabber:iq:version'/>
</iq>
"""
class VersionRequest(generic.Request):
elements = []
def parseRequest(self, element):
self.elements.append((element.uri, element.name))
request = VersionRequest.fromElement(generic.parseXml(xml))
self.assertEqual([(NS_VERSION, 'query')], request.elements)
def test_toElementStanzaKind(self):
"""
A request is an iq stanza.
"""
element = self.request.toElement()
self.assertIdentical(None, element.uri)
self.assertEquals('iq', element.name)
def test_toElementStanzaType(self):
"""
The request has type 'get'.
"""
self.assertEquals('get', self.request.stanzaType)
element = self.request.toElement()
self.assertEquals('get', element.getAttribute('type'))
def test_toElementStanzaTypeSet(self):
"""
The request has type 'set'.
"""
self.request.stanzaType = 'set'
element = self.request.toElement()
self.assertEquals('set', element.getAttribute('type'))
def test_toElementStanzaID(self):
"""
A request, when rendered, has an identifier.
"""
element = self.request.toElement()
self.assertNotIdentical(None, self.request.stanzaID)
self.assertEquals(self.request.stanzaID, element.getAttribute('id'))
def test_toElementRecipient(self):
"""
A request without recipient, has no 'to' attribute.
"""
self.request = generic.Request(recipient=JID('other@example.org'))
self.assertEquals(JID('other@example.org'), self.request.recipient)
element = self.request.toElement()
self.assertEquals(u'other@example.org', element.getAttribute('to'))
def test_toElementRecipientNone(self):
"""
A request without recipient, has no 'to' attribute.
"""
element = self.request.toElement()
self.assertFalse(element.hasAttribute('to'))
def test_toElementSender(self):
"""
A request with sender, has a 'from' attribute.
"""
self.request = generic.Request(sender=JID('user@example.org'))
self.assertEquals(JID('user@example.org'), self.request.sender)
element = self.request.toElement()
self.assertEquals(u'user@example.org', element.getAttribute('from'))
def test_toElementSenderNone(self):
"""
A request without sender, has no 'from' attribute.
"""
element = self.request.toElement()
self.assertFalse(element.hasAttribute('from'))
def test_timeoutDefault(self):
"""
The default is no timeout.
"""
self.assertIdentical(None, self.request.timeout)
class PrepareIDNNameTests(unittest.TestCase):
"""
Tests for L{wokkel.generic.prepareIDNName}.
"""
def test_bytestring(self):
"""
An ASCII-encoded byte string is left as-is.
"""
name = b"example.com"
result = generic.prepareIDNName(name)
self.assertEqual(b"example.com", result)
def test_unicode(self):
"""
A unicode all-ASCII name is converted to an ASCII byte string.
"""
name = u"example.com"
result = generic.prepareIDNName(name)
self.assertEqual(b"example.com", result)
def test_unicodeNonASCII(self):
"""
A unicode with non-ASCII is converted to its ACE equivalent.
"""
name = u"\u00e9chec.example.com"
result = generic.prepareIDNName(name)
self.assertEqual(b"xn--chec-9oa.example.com", result)
def test_unicodeHalfwidthIdeographicFullStop(self):
"""
Exotic dots in unicode names are converted to Full Stop.
"""
name = u"\u00e9chec.example\uff61com"
result = generic.prepareIDNName(name)
self.assertEqual(b"xn--chec-9oa.example.com", result)
def test_unicodeTrailingDot(self):
"""
Unicode names with trailing dots retain the trailing dot.
L{encodings.idna.ToASCII} doesn't allow the empty string as the input,
hence the implementation needs to strip a trailing dot, and re-add it
after encoding the labels.
"""
name = u"example.com."
result = generic.prepareIDNName(name)
self.assertEqual(b"example.com.", result)
|