source: wokkel/test/test_client.py @ 72:d48789c4c484

Last change on this file since 72:d48789c4c484 was 72:d48789c4c484, checked in by Ralph Meijer <ralphm@…>, 12 years ago

Add a jid attribute to XMPPClient that is updated with the current JID.

Author: ralphm.
Fixes #18.

File size: 4.8 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 XMPPClientTest(unittest.TestCase):
25    """
26    Tests for L{client.XMPPClient}.
27    """
28
29    def setUp(self):
30        self.client = client.XMPPClient(JID('user@example.org'), 'secret')
31
32
33    def test_jid(self):
34        """
35        Make sure the JID we pass is stored on the client.
36        """
37        self.assertEquals(JID('user@example.org'), self.client.jid)
38
39
40    def test_jidWhenInitialized(self):
41        """
42        Make sure that upon login, the JID is updated from the authenticator.
43        """
44        xs = self.client.factory.buildProtocol(None)
45        self.client.factory.authenticator.jid = JID('user@example.org/test')
46        xs.dispatch(xs, xmlstream.STREAM_AUTHD_EVENT)
47        self.assertEquals(JID('user@example.org/test'), self.client.jid)
48
49
50
51class DeferredClientFactoryTest(BootstrapMixinTest):
52    """
53    Tests for L{client.DeferredClientFactory}.
54    """
55
56    def setUp(self):
57        self.factory = client.DeferredClientFactory(JID('user@example.org'),
58                                                    'secret')
59
60
61    def test_buildProtocol(self):
62        """
63        The authenticator factory should be passed to its protocol and it
64        should instantiate the authenticator and save it.
65        L{xmlstream.XmlStream}s do that, but we also want to ensure it really
66        is one.
67        """
68        xs = self.factory.buildProtocol(None)
69        self.assertIdentical(self.factory, xs.factory)
70        self.assertIsInstance(xs, xmlstream.XmlStream)
71        self.assertIsInstance(xs.authenticator, XMPPAuthenticator)
72
73
74    def test_deferredOnInitialized(self):
75        """
76        Test the factory's deferred on stream initialization.
77        """
78
79        xs = self.factory.buildProtocol(None)
80        xs.dispatch(xs, STREAM_AUTHD_EVENT)
81        return self.factory.deferred
82
83
84    def test_deferredOnNotInitialized(self):
85        """
86        Test the factory's deferred on failed stream initialization.
87        """
88
89        class TestException(Exception):
90            pass
91
92        xs = self.factory.buildProtocol(None)
93        xs.dispatch(TestException(), INIT_FAILED_EVENT)
94        self.assertFailure(self.factory.deferred, TestException)
95        return self.factory.deferred
96
97
98    def test_deferredOnConnectionFailure(self):
99        """
100        Test the factory's deferred on connection faulure.
101        """
102
103        class TestException(Exception):
104            pass
105
106        xs = self.factory.buildProtocol(None)
107        self.factory.clientConnectionFailed(self, TestException())
108        self.assertFailure(self.factory.deferred, TestException)
109        return self.factory.deferred
110
111
112    def test_addHandler(self):
113        """
114        Test the addition of a protocol handler.
115        """
116        handler = XMPPHandler()
117        handler.setHandlerParent(self.factory.streamManager)
118        self.assertIn(handler, self.factory.streamManager)
119        self.assertIdentical(self.factory.streamManager, handler.parent)
120
121
122    def test_removeHandler(self):
123        """
124        Test removal of a protocol handler.
125        """
126        handler = XMPPHandler()
127        handler.setHandlerParent(self.factory.streamManager)
128        handler.disownHandlerParent(self.factory.streamManager)
129        self.assertNotIn(handler, self.factory.streamManager)
130        self.assertIdentical(None, handler.parent)
131
132
133
134class ClientCreatorTest(unittest.TestCase):
135    """
136    Tests for L{client.clientCreator}.
137    """
138
139    def test_call(self):
140        """
141        The factory is passed to an SRVConnector and a connection initiated.
142        """
143
144        d1 = defer.Deferred()
145        factory = client.DeferredClientFactory(JID('user@example.org'),
146                                               'secret')
147
148        def cb(connector):
149            self.assertEqual('xmpp-client', connector.service)
150            self.assertEqual('example.org', connector.domain)
151            self.assertEqual(factory, connector.factory)
152
153        def connect(connector):
154            d1.callback(connector)
155
156        d1.addCallback(cb)
157        self.patch(client.SRVConnector, 'connect', connect)
158
159        d2 = client.clientCreator(factory)
160        self.assertEqual(factory.deferred, d2)
161
162        return d1
Note: See TracBrowser for help on using the repository browser.