source: wokkel/test/test_generic.py @ 165:76a61f5aa343

Last change on this file since 165:76a61f5aa343 was 165:76a61f5aa343, checked in by Ralph Meijer <ralphm@…>, 8 years ago

Cleanups leading up to Wokkel 0.7.0.

As we now depend on Twisted 10.0.0 or higher, the following classes and
interfaces were deprecated:

This also resolves all Pyflakes warnings, changes links for www.xmpp.org to
xmpp.org and fixes the copyright notice in LICENSE to include 2012.

  • Property exe set to *
File size: 5.2 KB
Line 
1# Copyright (c) Ralph Meijer.
2# See LICENSE for details.
3
4"""
5Tests for L{wokkel.generic}.
6"""
7
8from twisted.trial import unittest
9from twisted.words.xish import domish
10from twisted.words.protocols.jabber.jid import JID
11
12from wokkel import generic
13from wokkel.test.helpers import XmlStreamStub
14
15NS_VERSION = 'jabber:iq:version'
16
17class VersionHandlerTest(unittest.TestCase):
18    """
19    Tests for L{wokkel.generic.VersionHandler}.
20    """
21
22    def test_onVersion(self):
23        """
24        Test response to incoming version request.
25        """
26        self.stub = XmlStreamStub()
27        self.protocol = generic.VersionHandler('Test', '0.1.0')
28        self.protocol.xmlstream = self.stub.xmlstream
29        self.protocol.send = self.stub.xmlstream.send
30        self.protocol.connectionInitialized()
31
32        iq = domish.Element((None, 'iq'))
33        iq['from'] = 'user@example.org/Home'
34        iq['to'] = 'example.org'
35        iq['type'] = 'get'
36        iq.addElement((NS_VERSION, 'query'))
37        self.stub.send(iq)
38
39        response = self.stub.output[-1]
40        self.assertEquals('user@example.org/Home', response['to'])
41        self.assertEquals('example.org', response['from'])
42        self.assertEquals('result', response['type'])
43        self.assertEquals(NS_VERSION, response.query.uri)
44        elements = list(domish.generateElementsQNamed(response.query.children,
45                                                      'name', NS_VERSION))
46        self.assertEquals(1, len(elements))
47        self.assertEquals('Test', unicode(elements[0]))
48        elements = list(domish.generateElementsQNamed(response.query.children,
49                                                      'version', NS_VERSION))
50        self.assertEquals(1, len(elements))
51        self.assertEquals('0.1.0', unicode(elements[0]))
52
53
54
55class XmlPipeTest(unittest.TestCase):
56    """
57    Tests for L{wokkel.generic.XmlPipe}.
58    """
59
60    def setUp(self):
61        self.pipe = generic.XmlPipe()
62
63
64    def test_sendFromSource(self):
65        """
66        Send an element from the source and observe it from the sink.
67        """
68        def cb(obj):
69            called.append(obj)
70
71        called = []
72        self.pipe.sink.addObserver('/test[@xmlns="testns"]', cb)
73        element = domish.Element(('testns', 'test'))
74        self.pipe.source.send(element)
75        self.assertEquals([element], called)
76
77
78    def test_sendFromSink(self):
79        """
80        Send an element from the sink and observe it from the source.
81        """
82        def cb(obj):
83            called.append(obj)
84
85        called = []
86        self.pipe.source.addObserver('/test[@xmlns="testns"]', cb)
87        element = domish.Element(('testns', 'test'))
88        self.pipe.sink.send(element)
89        self.assertEquals([element], called)
90
91
92
93class RequestTest(unittest.TestCase):
94    """
95    Tests for L{generic.Request}.
96    """
97
98    def setUp(self):
99        self.request = generic.Request()
100
101
102    def test_toElementStanzaKind(self):
103        """
104        A request is an iq stanza.
105        """
106        element = self.request.toElement()
107        self.assertIdentical(None, element.uri)
108        self.assertEquals('iq', element.name)
109
110
111    def test_toElementStanzaType(self):
112        """
113        The request has type 'get'.
114        """
115        self.assertEquals('get', self.request.stanzaType)
116        element = self.request.toElement()
117        self.assertEquals('get', element.getAttribute('type'))
118
119
120    def test_toElementStanzaTypeSet(self):
121        """
122        The request has type 'set'.
123        """
124        self.request.stanzaType = 'set'
125        element = self.request.toElement()
126        self.assertEquals('set', element.getAttribute('type'))
127
128
129    def test_toElementStanzaID(self):
130        """
131        A request, when rendered, has an identifier.
132        """
133        element = self.request.toElement()
134        self.assertNotIdentical(None, self.request.stanzaID)
135        self.assertEquals(self.request.stanzaID, element.getAttribute('id'))
136
137
138    def test_toElementRecipient(self):
139        """
140        A request without recipient, has no 'to' attribute.
141        """
142        self.request = generic.Request(recipient=JID('other@example.org'))
143        self.assertEquals(JID('other@example.org'), self.request.recipient)
144        element = self.request.toElement()
145        self.assertEquals(u'other@example.org', element.getAttribute('to'))
146
147
148    def test_toElementRecipientNone(self):
149        """
150        A request without recipient, has no 'to' attribute.
151        """
152        element = self.request.toElement()
153        self.assertFalse(element.hasAttribute('to'))
154
155
156    def test_toElementSender(self):
157        """
158        A request with sender, has a 'from' attribute.
159        """
160        self.request = generic.Request(sender=JID('user@example.org'))
161        self.assertEquals(JID('user@example.org'), self.request.sender)
162        element = self.request.toElement()
163        self.assertEquals(u'user@example.org', element.getAttribute('from'))
164
165
166    def test_toElementSenderNone(self):
167        """
168        A request without sender, has no 'from' attribute.
169        """
170        element = self.request.toElement()
171        self.assertFalse(element.hasAttribute('from'))
172
173
174    def test_timeoutDefault(self):
175        """
176        The default is no timeout.
177        """
178        self.assertIdentical(None, self.request.timeout)
Note: See TracBrowser for help on using the repository browser.