1 | # Copyright (c) 2001-2007 Twisted Matrix Laboratories. |
---|
2 | # Copyright (c) 2008 Ralph Meijer |
---|
3 | # See LICENSE for details. |
---|
4 | |
---|
5 | """ |
---|
6 | Tests for L{wokkel.compat}. |
---|
7 | """ |
---|
8 | |
---|
9 | from twisted.internet import defer, protocol |
---|
10 | from twisted.trial import unittest |
---|
11 | from twisted.words.xish import domish, utility |
---|
12 | from wokkel.compat import toResponse, BootstrapMixin |
---|
13 | |
---|
14 | class DummyProtocol(protocol.Protocol, utility.EventDispatcher): |
---|
15 | """ |
---|
16 | I am a protocol with an event dispatcher without further processing. |
---|
17 | |
---|
18 | This protocol is only used for testing BootstrapMixin to make |
---|
19 | sure the bootstrap observers are added to the protocol instance. |
---|
20 | """ |
---|
21 | |
---|
22 | def __init__(self, *args, **kwargs): |
---|
23 | self.args = args |
---|
24 | self.kwargs = kwargs |
---|
25 | self.observers = [] |
---|
26 | |
---|
27 | utility.EventDispatcher.__init__(self) |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | class BootstrapMixinTest(unittest.TestCase): |
---|
32 | """ |
---|
33 | Tests for L{BootstrapMixin}. |
---|
34 | |
---|
35 | @ivar factory: Instance of the factory or mixin under test. |
---|
36 | """ |
---|
37 | |
---|
38 | def setUp(self): |
---|
39 | self.factory = BootstrapMixin() |
---|
40 | |
---|
41 | |
---|
42 | def test_installBootstraps(self): |
---|
43 | """ |
---|
44 | Dispatching an event should fire registered bootstrap observers. |
---|
45 | """ |
---|
46 | d = defer.Deferred() |
---|
47 | dispatcher = DummyProtocol() |
---|
48 | self.factory.addBootstrap('//event/myevent', d.callback) |
---|
49 | self.factory.installBootstraps(dispatcher) |
---|
50 | dispatcher.dispatch(None, '//event/myevent') |
---|
51 | return d |
---|
52 | |
---|
53 | |
---|
54 | def test_addAndRemoveBootstrap(self): |
---|
55 | """ |
---|
56 | Test addition and removal of a bootstrap event handler. |
---|
57 | """ |
---|
58 | def cb(self): |
---|
59 | pass |
---|
60 | |
---|
61 | self.factory.addBootstrap('//event/myevent', cb) |
---|
62 | self.assertIn(('//event/myevent', cb), self.factory.bootstraps) |
---|
63 | |
---|
64 | self.factory.removeBootstrap('//event/myevent', cb) |
---|
65 | self.assertNotIn(('//event/myevent', cb), self.factory.bootstraps) |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | class ToResponseTest(unittest.TestCase): |
---|
70 | |
---|
71 | def test_toResponse(self): |
---|
72 | """ |
---|
73 | Test that a response stanza is generated with addressing swapped. |
---|
74 | """ |
---|
75 | stanza = domish.Element(('jabber:client', 'iq')) |
---|
76 | stanza['type'] = 'get' |
---|
77 | stanza['to'] = 'user1@example.com' |
---|
78 | stanza['from'] = 'user2@example.com/resource' |
---|
79 | stanza['id'] = 'stanza1' |
---|
80 | response = toResponse(stanza, 'result') |
---|
81 | self.assertNotIdentical(stanza, response) |
---|
82 | self.assertEqual(response['from'], 'user1@example.com') |
---|
83 | self.assertEqual(response['to'], 'user2@example.com/resource') |
---|
84 | self.assertEqual(response['type'], 'result') |
---|
85 | self.assertEqual(response['id'], 'stanza1') |
---|
86 | |
---|
87 | def test_toResponseNoFrom(self): |
---|
88 | """ |
---|
89 | Test that a response is generated from a stanza without a from address. |
---|
90 | """ |
---|
91 | stanza = domish.Element(('jabber:client', 'iq')) |
---|
92 | stanza['type'] = 'get' |
---|
93 | stanza['to'] = 'user1@example.com' |
---|
94 | response = toResponse(stanza) |
---|
95 | self.assertEqual(response['from'], 'user1@example.com') |
---|
96 | self.failIf(response.hasAttribute('to')) |
---|
97 | |
---|
98 | def test_toResponseNoTo(self): |
---|
99 | """ |
---|
100 | Test that a response is generated from a stanza without a to address. |
---|
101 | """ |
---|
102 | stanza = domish.Element(('jabber:client', 'iq')) |
---|
103 | stanza['type'] = 'get' |
---|
104 | stanza['from'] = 'user2@example.com/resource' |
---|
105 | response = toResponse(stanza) |
---|
106 | self.failIf(response.hasAttribute('from')) |
---|
107 | self.assertEqual(response['to'], 'user2@example.com/resource') |
---|
108 | |
---|
109 | def test_toResponseNoAddressing(self): |
---|
110 | """ |
---|
111 | Test that a response is generated from a stanza without any addressing. |
---|
112 | """ |
---|
113 | stanza = domish.Element(('jabber:client', 'message')) |
---|
114 | stanza['type'] = 'chat' |
---|
115 | response = toResponse(stanza) |
---|
116 | self.failIf(response.hasAttribute('to')) |
---|
117 | self.failIf(response.hasAttribute('from')) |
---|
118 | |
---|
119 | def test_noID(self): |
---|
120 | """ |
---|
121 | Test that a proper response is generated without id attribute. |
---|
122 | """ |
---|
123 | stanza = domish.Element(('jabber:client', 'message')) |
---|
124 | response = toResponse(stanza) |
---|
125 | self.failIf(response.hasAttribute('id')) |
---|
126 | |
---|
127 | |
---|
128 | def test_noType(self): |
---|
129 | """ |
---|
130 | Test that a proper response is generated without type attribute. |
---|
131 | """ |
---|
132 | stanza = domish.Element(('jabber:client', 'message')) |
---|
133 | response = toResponse(stanza) |
---|
134 | self.failIf(response.hasAttribute('type')) |
---|