source: wokkel/test/test_client.py @ 129:29e517800b96

Last change on this file since 129:29e517800b96 was 42:93675ba90f36, checked in by Ralph Meijer <ralphm@…>, 14 years ago

Change clientCreator to use the JID passed to the factory correctly.

Author: ralphm
Fixes #37.

File size: 3.2 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
16from wokkel import client
17from wokkel.test.test_compat import BootstrapMixinTest
18
19class DeferredClientFactoryTest(BootstrapMixinTest):
20    """
21    Tests for L{client.DeferredClientFactory}.
22    """
23
24    def setUp(self):
25        self.factory = client.DeferredClientFactory(JID('user@example.org'),
26                                                    'secret')
27
28
29    def test_buildProtocol(self):
30        """
31        The authenticator factory should be passed to its protocol and it
32        should instantiate the authenticator and save it.
33        L{xmlstream.XmlStream}s do that, but we also want to ensure it really
34        is one.
35        """
36        xs = self.factory.buildProtocol(None)
37        self.assertIdentical(self.factory, xs.factory)
38        self.assertIsInstance(xs, xmlstream.XmlStream)
39        self.assertIsInstance(xs.authenticator, XMPPAuthenticator)
40
41
42    def test_deferredOnInitialized(self):
43        """
44        Test the factory's deferred on stream initialization.
45        """
46
47        xs = self.factory.buildProtocol(None)
48        xs.dispatch(xs, STREAM_AUTHD_EVENT)
49        return self.factory.deferred
50
51
52    def test_deferredOnNotInitialized(self):
53        """
54        Test the factory's deferred on failed stream initialization.
55        """
56
57        class TestException(Exception):
58            pass
59
60        xs = self.factory.buildProtocol(None)
61        xs.dispatch(TestException(), INIT_FAILED_EVENT)
62        self.assertFailure(self.factory.deferred, TestException)
63        return self.factory.deferred
64
65
66    def test_deferredOnConnectionFailure(self):
67        """
68        Test the factory's deferred on connection faulure.
69        """
70
71        class TestException(Exception):
72            pass
73
74        xs = self.factory.buildProtocol(None)
75        self.factory.clientConnectionFailed(self, TestException())
76        self.assertFailure(self.factory.deferred, TestException)
77        return self.factory.deferred
78
79
80
81class ClientCreatorTest(unittest.TestCase):
82    """
83    Tests for L{client.clientCreator}.
84    """
85
86    def test_call(self):
87        """
88        The factory is passed to an SRVConnector and a connection initiated.
89        """
90
91        d1 = defer.Deferred()
92        factory = client.DeferredClientFactory(JID('user@example.org'),
93                                               'secret')
94
95        def cb(connector):
96            self.assertEqual('xmpp-client', connector.service)
97            self.assertEqual('example.org', connector.domain)
98            self.assertEqual(factory, connector.factory)
99
100        def connect(connector):
101            d1.callback(connector)
102
103        d1.addCallback(cb)
104        self.patch(client.SRVConnector, 'connect', connect)
105
106        d2 = client.clientCreator(factory)
107        self.assertEqual(factory.deferred, d2)
108
109        return d1
Note: See TracBrowser for help on using the repository browser.