1 | # Copyright (c) 2003-2007 Ralph Meijer |
---|
2 | # See LICENSE for details. |
---|
3 | |
---|
4 | """ |
---|
5 | Tests for L{wokkel.client}. |
---|
6 | """ |
---|
7 | |
---|
8 | from twisted.trial import unittest |
---|
9 | from twisted.words.protocols.jabber import xmlstream |
---|
10 | from twisted.words.protocols.jabber.client import XMPPAuthenticator |
---|
11 | from twisted.words.protocols.jabber.jid import JID |
---|
12 | from twisted.words.protocols.jabber.xmlstream import STREAM_AUTHD_EVENT |
---|
13 | from twisted.words.protocols.jabber.xmlstream import INIT_FAILED_EVENT |
---|
14 | |
---|
15 | from wokkel.client import DeferredClientFactory |
---|
16 | from wokkel.test.test_compat import BootstrapMixinTest |
---|
17 | |
---|
18 | class DeferredClientFactoryTest(BootstrapMixinTest): |
---|
19 | |
---|
20 | |
---|
21 | def setUp(self): |
---|
22 | self.factory = DeferredClientFactory(JID('user@example.org'), 'secret') |
---|
23 | |
---|
24 | |
---|
25 | def test_buildProtocol(self): |
---|
26 | """ |
---|
27 | The authenticator factory should be passed to its protocol and it |
---|
28 | should instantiate the authenticator and save it. |
---|
29 | L{xmlstream.XmlStream}s do that, but we also want to ensure it really |
---|
30 | is one. |
---|
31 | """ |
---|
32 | xs = self.factory.buildProtocol(None) |
---|
33 | self.assertIdentical(self.factory, xs.factory) |
---|
34 | self.assertIsInstance(xs, xmlstream.XmlStream) |
---|
35 | self.assertIsInstance(xs.authenticator, XMPPAuthenticator) |
---|
36 | |
---|
37 | |
---|
38 | def test_deferredOnInitialized(self): |
---|
39 | """ |
---|
40 | Test the factory's deferred on stream initialization. |
---|
41 | """ |
---|
42 | |
---|
43 | xs = self.factory.buildProtocol(None) |
---|
44 | xs.dispatch(xs, STREAM_AUTHD_EVENT) |
---|
45 | return self.factory.deferred |
---|
46 | |
---|
47 | |
---|
48 | def test_deferredOnNotInitialized(self): |
---|
49 | """ |
---|
50 | Test the factory's deferred on failed stream initialization. |
---|
51 | """ |
---|
52 | |
---|
53 | class TestException(Exception): |
---|
54 | pass |
---|
55 | |
---|
56 | xs = self.factory.buildProtocol(None) |
---|
57 | xs.dispatch(TestException(), INIT_FAILED_EVENT) |
---|
58 | self.assertFailure(self.factory.deferred, TestException) |
---|
59 | return self.factory.deferred |
---|
60 | |
---|
61 | |
---|
62 | def test_deferredOnConnectionFailure(self): |
---|
63 | """ |
---|
64 | Test the factory's deferred on connection faulure. |
---|
65 | """ |
---|
66 | |
---|
67 | class TestException(Exception): |
---|
68 | pass |
---|
69 | |
---|
70 | xs = self.factory.buildProtocol(None) |
---|
71 | self.factory.clientConnectionFailed(self, TestException()) |
---|
72 | self.assertFailure(self.factory.deferred, TestException) |
---|
73 | return self.factory.deferred |
---|