1 | # Copyright (c) 2001-2007 Twisted Matrix Laboratories. |
---|
2 | # Copyright (c) 2008 Ralph Meijer |
---|
3 | # See LICENSE for details. |
---|
4 | |
---|
5 | """ |
---|
6 | Tests for L{wokkel.compat}. |
---|
7 | """ |
---|
8 | |
---|
9 | from zope.interface.verify import verifyObject |
---|
10 | from twisted.internet import defer, protocol |
---|
11 | from twisted.internet.interfaces import IProtocolFactory |
---|
12 | from twisted.trial import unittest |
---|
13 | from twisted.words.xish import domish, utility |
---|
14 | from twisted.words.protocols.jabber import xmlstream |
---|
15 | from wokkel.compat import toResponse, BootstrapMixin, XmlStreamServerFactory |
---|
16 | |
---|
17 | class DummyProtocol(protocol.Protocol, utility.EventDispatcher): |
---|
18 | """ |
---|
19 | I am a protocol with an event dispatcher without further processing. |
---|
20 | |
---|
21 | This protocol is only used for testing BootstrapMixin to make |
---|
22 | sure the bootstrap observers are added to the protocol instance. |
---|
23 | """ |
---|
24 | |
---|
25 | def __init__(self, *args, **kwargs): |
---|
26 | self.args = args |
---|
27 | self.kwargs = kwargs |
---|
28 | self.observers = [] |
---|
29 | |
---|
30 | utility.EventDispatcher.__init__(self) |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | class BootstrapMixinTest(unittest.TestCase): |
---|
35 | """ |
---|
36 | Tests for L{BootstrapMixin}. |
---|
37 | |
---|
38 | @ivar factory: Instance of the factory or mixin under test. |
---|
39 | """ |
---|
40 | |
---|
41 | def setUp(self): |
---|
42 | self.factory = BootstrapMixin() |
---|
43 | |
---|
44 | |
---|
45 | def test_installBootstraps(self): |
---|
46 | """ |
---|
47 | Dispatching an event should fire registered bootstrap observers. |
---|
48 | """ |
---|
49 | d = defer.Deferred() |
---|
50 | dispatcher = DummyProtocol() |
---|
51 | self.factory.addBootstrap('//event/myevent', d.callback) |
---|
52 | self.factory.installBootstraps(dispatcher) |
---|
53 | dispatcher.dispatch(None, '//event/myevent') |
---|
54 | return d |
---|
55 | |
---|
56 | |
---|
57 | def test_addAndRemoveBootstrap(self): |
---|
58 | """ |
---|
59 | Test addition and removal of a bootstrap event handler. |
---|
60 | """ |
---|
61 | def cb(self): |
---|
62 | pass |
---|
63 | |
---|
64 | self.factory.addBootstrap('//event/myevent', cb) |
---|
65 | self.assertIn(('//event/myevent', cb), self.factory.bootstraps) |
---|
66 | |
---|
67 | self.factory.removeBootstrap('//event/myevent', cb) |
---|
68 | self.assertNotIn(('//event/myevent', cb), self.factory.bootstraps) |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | class ToResponseTest(unittest.TestCase): |
---|
73 | |
---|
74 | def test_toResponse(self): |
---|
75 | """ |
---|
76 | Test that a response stanza is generated with addressing swapped. |
---|
77 | """ |
---|
78 | stanza = domish.Element(('jabber:client', 'iq')) |
---|
79 | stanza['type'] = 'get' |
---|
80 | stanza['to'] = 'user1@example.com' |
---|
81 | stanza['from'] = 'user2@example.com/resource' |
---|
82 | stanza['id'] = 'stanza1' |
---|
83 | response = toResponse(stanza, 'result') |
---|
84 | self.assertNotIdentical(stanza, response) |
---|
85 | self.assertEqual(response['from'], 'user1@example.com') |
---|
86 | self.assertEqual(response['to'], 'user2@example.com/resource') |
---|
87 | self.assertEqual(response['type'], 'result') |
---|
88 | self.assertEqual(response['id'], 'stanza1') |
---|
89 | |
---|
90 | def test_toResponseNoFrom(self): |
---|
91 | """ |
---|
92 | Test that a response is generated from a stanza without a from address. |
---|
93 | """ |
---|
94 | stanza = domish.Element(('jabber:client', 'iq')) |
---|
95 | stanza['type'] = 'get' |
---|
96 | stanza['to'] = 'user1@example.com' |
---|
97 | response = toResponse(stanza) |
---|
98 | self.assertEqual(response['from'], 'user1@example.com') |
---|
99 | self.failIf(response.hasAttribute('to')) |
---|
100 | |
---|
101 | def test_toResponseNoTo(self): |
---|
102 | """ |
---|
103 | Test that a response is generated from a stanza without a to address. |
---|
104 | """ |
---|
105 | stanza = domish.Element(('jabber:client', 'iq')) |
---|
106 | stanza['type'] = 'get' |
---|
107 | stanza['from'] = 'user2@example.com/resource' |
---|
108 | response = toResponse(stanza) |
---|
109 | self.failIf(response.hasAttribute('from')) |
---|
110 | self.assertEqual(response['to'], 'user2@example.com/resource') |
---|
111 | |
---|
112 | def test_toResponseNoAddressing(self): |
---|
113 | """ |
---|
114 | Test that a response is generated from a stanza without any addressing. |
---|
115 | """ |
---|
116 | stanza = domish.Element(('jabber:client', 'message')) |
---|
117 | stanza['type'] = 'chat' |
---|
118 | response = toResponse(stanza) |
---|
119 | self.failIf(response.hasAttribute('to')) |
---|
120 | self.failIf(response.hasAttribute('from')) |
---|
121 | |
---|
122 | def test_noID(self): |
---|
123 | """ |
---|
124 | Test that a proper response is generated without id attribute. |
---|
125 | """ |
---|
126 | stanza = domish.Element(('jabber:client', 'message')) |
---|
127 | response = toResponse(stanza) |
---|
128 | self.failIf(response.hasAttribute('id')) |
---|
129 | |
---|
130 | |
---|
131 | def test_noType(self): |
---|
132 | """ |
---|
133 | Test that a proper response is generated without type attribute. |
---|
134 | """ |
---|
135 | stanza = domish.Element(('jabber:client', 'message')) |
---|
136 | response = toResponse(stanza) |
---|
137 | self.failIf(response.hasAttribute('type')) |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | class XmlStreamServerFactoryTest(BootstrapMixinTest): |
---|
142 | """ |
---|
143 | Tests for L{XmlStreamServerFactory}. |
---|
144 | """ |
---|
145 | |
---|
146 | def setUp(self): |
---|
147 | """ |
---|
148 | Set up a server factory with a authenticator factory function. |
---|
149 | """ |
---|
150 | def authenticatorFactory(): |
---|
151 | return xmlstream.Authenticator() |
---|
152 | |
---|
153 | self.factory = XmlStreamServerFactory(authenticatorFactory) |
---|
154 | |
---|
155 | |
---|
156 | def test_interface(self): |
---|
157 | """ |
---|
158 | L{XmlStreamServerFactory} is a L{Factory}. |
---|
159 | """ |
---|
160 | verifyObject(IProtocolFactory, self.factory) |
---|
161 | |
---|
162 | |
---|
163 | def test_buildProtocol(self): |
---|
164 | """ |
---|
165 | The authenticator factory should be passed to its protocol and it |
---|
166 | should instantiate the authenticator and save it. |
---|
167 | L{xmlstream.XmlStream}s do that, but we also want to ensure it really |
---|
168 | is one. |
---|
169 | """ |
---|
170 | xs = self.factory.buildProtocol(None) |
---|
171 | self.assertIdentical(self.factory, xs.factory) |
---|
172 | self.assertIsInstance(xs, xmlstream.XmlStream) |
---|
173 | self.assertIsInstance(xs.authenticator, xmlstream.Authenticator) |
---|
174 | |
---|
175 | |
---|
176 | def test_buildProtocolTwice(self): |
---|
177 | """ |
---|
178 | Subsequent calls to buildProtocol should result in different instances |
---|
179 | of the protocol, as well as their authenticators. |
---|
180 | """ |
---|
181 | xs1 = self.factory.buildProtocol(None) |
---|
182 | xs2 = self.factory.buildProtocol(None) |
---|
183 | self.assertNotIdentical(xs1, xs2) |
---|
184 | self.assertNotIdentical(xs1.authenticator, xs2.authenticator) |
---|