source: wokkel/test/test_client.py @ 165:76a61f5aa343

Last change on this file since 165:76a61f5aa343 was 165:76a61f5aa343, checked in by Ralph Meijer <ralphm@…>, 8 years ago

Cleanups leading up to Wokkel 0.7.0.

As we now depend on Twisted 10.0.0 or higher, the following classes and
interfaces were deprecated:

This also resolves all Pyflakes warnings, changes links for www.xmpp.org to
xmpp.org and fixes the copyright notice in LICENSE to include 2012.

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