~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/words/test/test_jabbererror.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from twisted.trial import unittest
 
2
 
 
3
from twisted.words.protocols.jabber import error
 
4
from twisted.words.xish import domish
 
5
 
 
6
NS_XML = 'http://www.w3.org/XML/1998/namespace'
 
7
NS_STREAMS = 'http://etherx.jabber.org/streams'
 
8
NS_XMPP_STANZAS = 'urn:ietf:params:xml:ns:xmpp-stanzas'
 
9
 
 
10
class ErrorTest(unittest.TestCase):
 
11
 
 
12
    def testGetElementPlain(self):
 
13
        e = error.Error('feature-not-implemented')
 
14
        element = e.getElement()
 
15
        self.assertIdentical(element.uri, None)
 
16
        self.assertEquals(len(element.children), 1)
 
17
        self.assertEquals(element.children[0].name, 'feature-not-implemented')
 
18
        self.assertEquals(element.children[0].uri, NS_XMPP_STANZAS)
 
19
 
 
20
    def testGetElementText(self):
 
21
        e = error.Error('feature-not-implemented', 'text')
 
22
        element = e.getElement()
 
23
        self.assertEquals(len(element.children), 2)
 
24
        self.assertEquals(element.text.uri, NS_XMPP_STANZAS)
 
25
        self.assertEquals(unicode(element.text), 'text')
 
26
        self.assertEquals(element.text.getAttribute((NS_XML, 'lang')), None)
 
27
 
 
28
    def testGetElementTextLang(self):
 
29
        e = error.Error('feature-not-implemented', 'text', 'en_US')
 
30
        element = e.getElement()
 
31
        self.assertEquals(len(element.children), 2)
 
32
        self.assertEquals(element.text.uri, NS_XMPP_STANZAS)
 
33
        self.assertEquals(unicode(element.text), 'text')
 
34
        self.assertEquals(element.text[(NS_XML, 'lang')], 'en_US')
 
35
 
 
36
    def testGetElementAppCondition(self):
 
37
        ac = domish.Element(('testns', 'myerror'))
 
38
        e = error.Error('feature-not-implemented', appCondition=ac)
 
39
        element = e.getElement()
 
40
        self.assertEquals(len(element.children), 2)
 
41
        self.assertEquals(element.myerror, ac)
 
42
 
 
43
class StreamErrorTest(unittest.TestCase):
 
44
 
 
45
    def testGetElementPlain(self):
 
46
        e = error.StreamError('feature-not-implemented')
 
47
        element = e.getElement()
 
48
        self.assertEquals(element.uri, NS_STREAMS)
 
49
 
 
50
class StanzaErrorTest(unittest.TestCase):
 
51
 
 
52
    def testGetElementPlain(self):
 
53
        e = error.StanzaError('feature-not-implemented')
 
54
        element = e.getElement()
 
55
        self.assertEquals(element.uri, None)
 
56
        self.assertEquals(element['type'], 'cancel')
 
57
        self.assertEquals(element['code'], '501')
 
58
 
 
59
    def testGetElementType(self):
 
60
        e = error.StanzaError('feature-not-implemented', 'auth')
 
61
        element = e.getElement()
 
62
        self.assertEquals(element.uri, None)
 
63
        self.assertEquals(element['type'], 'auth')
 
64
        self.assertEquals(element['code'], '501')
 
65
 
 
66
    def testToResponse(self):
 
67
        stanza = domish.Element(('jabber:client', 'message'))
 
68
        stanza['type'] = 'get'
 
69
        stanza['to'] = 'user1@example.com'
 
70
        stanza['from'] = 'user2@example.com/resource'
 
71
        e = error.StanzaError('service-unavailable')
 
72
        response = e.toResponse(stanza)
 
73
        self.assertEqual(response['from'], 'user1@example.com')
 
74
        self.assertEqual(response['to'], 'user2@example.com/resource')
 
75
        self.assertEqual(response['type'], 'error')
 
76
        self.assertEqual(response.error.children[0].name,
 
77
                         'service-unavailable')
 
78
        self.assertEqual(response.error['type'], 'cancel')
 
79
 
 
80
class ParseErrorTest(unittest.TestCase):
 
81
 
 
82
    def setUp(self):
 
83
        self.error = domish.Element((None, 'error'))
 
84
 
 
85
    def testEmpty(self):
 
86
        result = error._parseError(self.error)
 
87
        self.assertEqual({'condition': None,
 
88
                          'text': None,
 
89
                          'textLang': None,
 
90
                          'appCondition': None}, result)
 
91
 
 
92
    def testCondition(self):
 
93
        self.error.addElement((NS_XMPP_STANZAS, 'bad-request'))
 
94
        result = error._parseError(self.error)
 
95
        self.assertEqual('bad-request', result['condition'])
 
96
 
 
97
    def testText(self):
 
98
        text = self.error.addElement((NS_XMPP_STANZAS, 'text'))
 
99
        text.addContent('test')
 
100
        result = error._parseError(self.error)
 
101
        self.assertEqual('test', result['text'])
 
102
        self.assertEqual(None, result['textLang'])
 
103
 
 
104
    def testTextLang(self):
 
105
        text = self.error.addElement((NS_XMPP_STANZAS, 'text'))
 
106
        text[NS_XML, 'lang'] = 'en_US'
 
107
        text.addContent('test')
 
108
        result = error._parseError(self.error)
 
109
        self.assertEqual('en_US', result['textLang'])
 
110
 
 
111
    def testTextLangInherited(self):
 
112
        text = self.error.addElement((NS_XMPP_STANZAS, 'text'))
 
113
        self.error[NS_XML, 'lang'] = 'en_US'
 
114
        text.addContent('test')
 
115
        result = error._parseError(self.error)
 
116
        self.assertEqual('en_US', result['textLang'])
 
117
    testTextLangInherited.todo = "xml:lang inheritance not implemented"
 
118
 
 
119
    def testAppCondition(self):
 
120
        condition = self.error.addElement(('testns', 'condition'))
 
121
        result = error._parseError(self.error)
 
122
        self.assertEqual(condition, result['appCondition'])
 
123
 
 
124
    def testMultipleAppConditions(self):
 
125
        condition = self.error.addElement(('testns', 'condition'))
 
126
        condition2 = self.error.addElement(('testns', 'condition2'))
 
127
        result = error._parseError(self.error)
 
128
        self.assertEqual(condition2, result['appCondition'])
 
129
 
 
130
class ExceptionFromStanzaTest(unittest.TestCase):
 
131
 
 
132
    def testBasic(self):
 
133
        """
 
134
        Test basic operations of exceptionFromStanza.
 
135
 
 
136
        Given a realistic stanza, check if a sane exception is returned.
 
137
 
 
138
        Using this stanza::
 
139
 
 
140
          <iq type='error'
 
141
              from='pubsub.shakespeare.lit'
 
142
              to='francisco@denmark.lit/barracks'
 
143
              id='subscriptions1'>
 
144
            <pubsub xmlns='http://jabber.org/protocol/pubsub'>
 
145
              <subscriptions/>
 
146
            </pubsub>
 
147
            <error type='cancel'>
 
148
              <feature-not-implemented
 
149
                xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
 
150
              <unsupported xmlns='http://jabber.org/protocol/pubsub#errors'
 
151
                           feature='retrieve-subscriptions'/>
 
152
            </error>
 
153
          </iq>
 
154
        """
 
155
 
 
156
        stanza = domish.Element((None, 'stanza'))
 
157
        p = stanza.addElement(('http://jabber.org/protocol/pubsub', 'pubsub'))
 
158
        p.addElement('subscriptions')
 
159
        e = stanza.addElement('error')
 
160
        e['type'] = 'cancel'
 
161
        e.addElement((NS_XMPP_STANZAS, 'feature-not-implemented'))
 
162
        uc = e.addElement(('http://jabber.org/protocol/pubsub#errors',
 
163
                           'unsupported'))
 
164
        uc['feature'] = 'retrieve-subscriptions'
 
165
 
 
166
        result = error.exceptionFromStanza(stanza)
 
167
        self.assert_(isinstance(result, error.StanzaError))
 
168
        self.assertEquals('feature-not-implemented', result.condition)
 
169
        self.assertEquals('cancel', result.type)
 
170
        self.assertEquals(uc, result.appCondition)
 
171
        self.assertEquals([p], result.children)
 
172
 
 
173
    def testLegacy(self):
 
174
        """
 
175
        Test legacy operations of exceptionFromStanza.
 
176
 
 
177
        Given a realistic stanza with only legacy (pre-XMPP) error information,
 
178
        check if a sane exception is returned.
 
179
 
 
180
        Using this stanza::
 
181
 
 
182
          <message type='error'
 
183
                   to='piers@pipetree.com/Home'
 
184
                   from='qmacro@jaber.org'>
 
185
            <body>Are you there?</body>
 
186
            <error code='502'>Unable to resolve hostname.</error>
 
187
          </message>
 
188
        """
 
189
        stanza = domish.Element((None, 'stanza'))
 
190
        p = stanza.addElement('body', content='Are you there?')
 
191
        e = stanza.addElement('error', content='Unable to resolve hostname.')
 
192
        e['code'] = '502'
 
193
 
 
194
        result = error.exceptionFromStanza(stanza)
 
195
        self.assert_(isinstance(result, error.StanzaError))
 
196
        self.assertEquals('service-unavailable', result.condition)
 
197
        self.assertEquals('wait', result.type)
 
198
        self.assertEquals('Unable to resolve hostname.', result.text)
 
199
        self.assertEquals([p], result.children)