[73] | 1 | # Copyright (c) 2003-2009 Ralph Meijer |
---|
[8] | 2 | # See LICENSE for details. |
---|
| 3 | |
---|
| 4 | """ |
---|
| 5 | Tests for L{wokkel.client}. |
---|
| 6 | """ |
---|
| 7 | |
---|
[42] | 8 | from twisted.internet import defer |
---|
| 9 | from twisted.trial import unittest |
---|
[34] | 10 | from twisted.words.protocols.jabber import xmlstream |
---|
| 11 | from twisted.words.protocols.jabber.client import XMPPAuthenticator |
---|
[8] | 12 | from twisted.words.protocols.jabber.jid import JID |
---|
| 13 | from twisted.words.protocols.jabber.xmlstream import STREAM_AUTHD_EVENT |
---|
| 14 | from twisted.words.protocols.jabber.xmlstream import INIT_FAILED_EVENT |
---|
| 15 | |
---|
[61] | 16 | try: |
---|
| 17 | from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
---|
| 18 | except ImportError: |
---|
| 19 | from wokkel.subprotocols import XMPPHandler |
---|
| 20 | |
---|
[42] | 21 | from wokkel import client |
---|
[34] | 22 | from wokkel.test.test_compat import BootstrapMixinTest |
---|
[8] | 23 | |
---|
[72] | 24 | class 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 | |
---|
[34] | 51 | class DeferredClientFactoryTest(BootstrapMixinTest): |
---|
[42] | 52 | """ |
---|
| 53 | Tests for L{client.DeferredClientFactory}. |
---|
| 54 | """ |
---|
[34] | 55 | |
---|
| 56 | def setUp(self): |
---|
[42] | 57 | self.factory = client.DeferredClientFactory(JID('user@example.org'), |
---|
| 58 | 'secret') |
---|
[34] | 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 | |
---|
[8] | 73 | |
---|
| 74 | def test_deferredOnInitialized(self): |
---|
| 75 | """ |
---|
| 76 | Test the factory's deferred on stream initialization. |
---|
| 77 | """ |
---|
| 78 | |
---|
[34] | 79 | xs = self.factory.buildProtocol(None) |
---|
| 80 | xs.dispatch(xs, STREAM_AUTHD_EVENT) |
---|
| 81 | return self.factory.deferred |
---|
| 82 | |
---|
[8] | 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 | |
---|
[34] | 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 | |
---|
[8] | 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 | |
---|
[34] | 106 | xs = self.factory.buildProtocol(None) |
---|
| 107 | self.factory.clientConnectionFailed(self, TestException()) |
---|
| 108 | self.assertFailure(self.factory.deferred, TestException) |
---|
| 109 | return self.factory.deferred |
---|
[42] | 110 | |
---|
| 111 | |
---|
[61] | 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 | |
---|
[42] | 133 | |
---|
| 134 | class 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 |
---|