source: wokkel/test/test_compat.py @ 53:4aec395fda5b

Last change on this file since 53:4aec395fda5b was 53:4aec395fda5b, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Release Wokkel 0.5.0.

File size: 4.7 KB
Line 
1# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
2# Copyright (c) 2008-2009 Ralph Meijer
3# See LICENSE for details.
4
5"""
6Tests for L{wokkel.compat}.
7"""
8
9from zope.interface.verify import verifyObject
10from twisted.internet import defer, protocol
11from twisted.internet.interfaces import IProtocolFactory
12from twisted.trial import unittest
13from twisted.words.xish import domish, utility
14from twisted.words.protocols.jabber import xmlstream
15from wokkel.compat import BootstrapMixin, XmlStreamServerFactory
16
17class 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
34class 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        called = []
50
51        def cb(data):
52            called.append(data)
53
54        dispatcher = DummyProtocol()
55        self.factory.addBootstrap('//event/myevent', cb)
56        self.factory.installBootstraps(dispatcher)
57
58        dispatcher.dispatch(None, '//event/myevent')
59        self.assertEquals(1, len(called))
60
61
62    def test_addAndRemoveBootstrap(self):
63        """
64        Test addition and removal of a bootstrap event handler.
65        """
66
67        called = []
68
69        def cb(data):
70            called.append(data)
71
72        self.factory.addBootstrap('//event/myevent', cb)
73        self.factory.removeBootstrap('//event/myevent', cb)
74
75        dispatcher = DummyProtocol()
76        self.factory.installBootstraps(dispatcher)
77
78        dispatcher.dispatch(None, '//event/myevent')
79        self.assertFalse(called)
80
81
82
83class XmlStreamServerFactoryTest(BootstrapMixinTest):
84    """
85    Tests for L{XmlStreamServerFactory}.
86    """
87
88    def setUp(self):
89        """
90        Set up a server factory with a authenticator factory function.
91        """
92        class TestAuthenticator(object):
93            def __init__(self):
94                self.xmlstreams = []
95
96            def associateWithStream(self, xs):
97                self.xmlstreams.append(xs)
98
99        def authenticatorFactory():
100            return TestAuthenticator()
101
102        self.factory = XmlStreamServerFactory(authenticatorFactory)
103
104
105    def test_interface(self):
106        """
107        L{XmlStreamServerFactory} is a L{Factory}.
108        """
109        verifyObject(IProtocolFactory, self.factory)
110
111
112    def test_buildProtocolAuthenticatorInstantiation(self):
113        """
114        The authenticator factory should be used to instantiate the
115        authenticator and pass it to the protocol.
116
117        The default protocol, L{XmlStream} stores the authenticator it is
118        passed, and calls its C{associateWithStream} method. so we use that to
119        check whether our authenticator factory is used and the protocol
120        instance gets an authenticator.
121        """
122        xs = self.factory.buildProtocol(None)
123        self.assertEquals([xs], xs.authenticator.xmlstreams)
124
125
126    def test_buildProtocolXmlStream(self):
127        """
128        The protocol factory creates Jabber XML Stream protocols by default.
129        """
130        xs = self.factory.buildProtocol(None)
131        self.assertIsInstance(xs, xmlstream.XmlStream)
132
133
134    def test_buildProtocolTwice(self):
135        """
136        Subsequent calls to buildProtocol should result in different instances
137        of the protocol, as well as their authenticators.
138        """
139        xs1 = self.factory.buildProtocol(None)
140        xs2 = self.factory.buildProtocol(None)
141        self.assertNotIdentical(xs1, xs2)
142        self.assertNotIdentical(xs1.authenticator, xs2.authenticator)
143
144
145    def test_buildProtocolInstallsBootstraps(self):
146        """
147        The protocol factory installs bootstrap event handlers on the protocol.
148        """
149        called = []
150
151        def cb(data):
152            called.append(data)
153
154        self.factory.addBootstrap('//event/myevent', cb)
155
156        xs = self.factory.buildProtocol(None)
157        xs.dispatch(None, '//event/myevent')
158
159        self.assertEquals(1, len(called))
160
161
162    def test_buildProtocolStoresFactory(self):
163        """
164        The protocol factory is saved in the protocol.
165        """
166        xs = self.factory.buildProtocol(None)
167        self.assertIdentical(self.factory, xs.factory)
Note: See TracBrowser for help on using the repository browser.