source: wokkel/test/test_client.py @ 68:2c6d1f895583

Last change on this file since 68:2c6d1f895583 was 61:5a5840e066fb, checked in by Ralph Meijer <ralphm@…>, 13 years ago

Fix regression that made DeferredClientFactory? have no stream manager.

Author: ralphm.
Fixes #51.

File size: 4.0 KB
Line 
1# Copyright (c) 2003-2007 Ralph Meijer
2# See LICENSE for details.
3
4"""
5Tests for L{wokkel.client}.
6"""
7
8from twisted.internet import defer
9from twisted.trial import unittest
10from twisted.words.protocols.jabber import xmlstream
11from twisted.words.protocols.jabber.client import XMPPAuthenticator
12from twisted.words.protocols.jabber.jid import JID
13from twisted.words.protocols.jabber.xmlstream import STREAM_AUTHD_EVENT
14from twisted.words.protocols.jabber.xmlstream import INIT_FAILED_EVENT
15
16try:
17    from twisted.words.protocols.jabber.xmlstream import XMPPHandler
18except ImportError:
19    from wokkel.subprotocols import XMPPHandler
20
21from wokkel import client
22from wokkel.test.test_compat import BootstrapMixinTest
23
24class DeferredClientFactoryTest(BootstrapMixinTest):
25    """
26    Tests for L{client.DeferredClientFactory}.
27    """
28
29    def setUp(self):
30        self.factory = client.DeferredClientFactory(JID('user@example.org'),
31                                                    'secret')
32
33
34    def test_buildProtocol(self):
35        """
36        The authenticator factory should be passed to its protocol and it
37        should instantiate the authenticator and save it.
38        L{xmlstream.XmlStream}s do that, but we also want to ensure it really
39        is one.
40        """
41        xs = self.factory.buildProtocol(None)
42        self.assertIdentical(self.factory, xs.factory)
43        self.assertIsInstance(xs, xmlstream.XmlStream)
44        self.assertIsInstance(xs.authenticator, XMPPAuthenticator)
45
46
47    def test_deferredOnInitialized(self):
48        """
49        Test the factory's deferred on stream initialization.
50        """
51
52        xs = self.factory.buildProtocol(None)
53        xs.dispatch(xs, STREAM_AUTHD_EVENT)
54        return self.factory.deferred
55
56
57    def test_deferredOnNotInitialized(self):
58        """
59        Test the factory's deferred on failed stream initialization.
60        """
61
62        class TestException(Exception):
63            pass
64
65        xs = self.factory.buildProtocol(None)
66        xs.dispatch(TestException(), INIT_FAILED_EVENT)
67        self.assertFailure(self.factory.deferred, TestException)
68        return self.factory.deferred
69
70
71    def test_deferredOnConnectionFailure(self):
72        """
73        Test the factory's deferred on connection faulure.
74        """
75
76        class TestException(Exception):
77            pass
78
79        xs = self.factory.buildProtocol(None)
80        self.factory.clientConnectionFailed(self, TestException())
81        self.assertFailure(self.factory.deferred, TestException)
82        return self.factory.deferred
83
84
85    def test_addHandler(self):
86        """
87        Test the addition of a protocol handler.
88        """
89        handler = XMPPHandler()
90        handler.setHandlerParent(self.factory.streamManager)
91        self.assertIn(handler, self.factory.streamManager)
92        self.assertIdentical(self.factory.streamManager, handler.parent)
93
94
95    def test_removeHandler(self):
96        """
97        Test removal of a protocol handler.
98        """
99        handler = XMPPHandler()
100        handler.setHandlerParent(self.factory.streamManager)
101        handler.disownHandlerParent(self.factory.streamManager)
102        self.assertNotIn(handler, self.factory.streamManager)
103        self.assertIdentical(None, handler.parent)
104
105
106
107class ClientCreatorTest(unittest.TestCase):
108    """
109    Tests for L{client.clientCreator}.
110    """
111
112    def test_call(self):
113        """
114        The factory is passed to an SRVConnector and a connection initiated.
115        """
116
117        d1 = defer.Deferred()
118        factory = client.DeferredClientFactory(JID('user@example.org'),
119                                               'secret')
120
121        def cb(connector):
122            self.assertEqual('xmpp-client', connector.service)
123            self.assertEqual('example.org', connector.domain)
124            self.assertEqual(factory, connector.factory)
125
126        def connect(connector):
127            d1.callback(connector)
128
129        d1.addCallback(cb)
130        self.patch(client.SRVConnector, 'connect', connect)
131
132        d2 = client.clientCreator(factory)
133        self.assertEqual(factory.deferred, d2)
134
135        return d1
Note: See TracBrowser for help on using the repository browser.