source: wokkel/test/test_client.py @ 208:0538df970eaf

Last change on this file since 208:0538df970eaf was 208:0538df970eaf, checked in by Ralph Meijer <ralphm@…>, 4 years ago

imported patch py3-client.patch

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