1
from twisted.trial import unittest
3
from twisted.words.protocols.jabber import error
4
from twisted.words.xish import domish
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'
10
class ErrorTest(unittest.TestCase):
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)
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)
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')
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)
43
class StreamErrorTest(unittest.TestCase):
45
def testGetElementPlain(self):
46
e = error.StreamError('feature-not-implemented')
47
element = e.getElement()
48
self.assertEquals(element.uri, NS_STREAMS)
50
class StanzaErrorTest(unittest.TestCase):
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')
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')
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')
80
class ParseErrorTest(unittest.TestCase):
83
self.error = domish.Element((None, 'error'))
86
result = error._parseError(self.error)
87
self.assertEqual({'condition': None,
90
'appCondition': None}, result)
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'])
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'])
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'])
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"
119
def testAppCondition(self):
120
condition = self.error.addElement(('testns', 'condition'))
121
result = error._parseError(self.error)
122
self.assertEqual(condition, result['appCondition'])
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'])
130
class ExceptionFromStanzaTest(unittest.TestCase):
134
Test basic operations of exceptionFromStanza.
136
Given a realistic stanza, check if a sane exception is returned.
141
from='pubsub.shakespeare.lit'
142
to='francisco@denmark.lit/barracks'
144
<pubsub xmlns='http://jabber.org/protocol/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'/>
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')
161
e.addElement((NS_XMPP_STANZAS, 'feature-not-implemented'))
162
uc = e.addElement(('http://jabber.org/protocol/pubsub#errors',
164
uc['feature'] = 'retrieve-subscriptions'
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)
173
def testLegacy(self):
175
Test legacy operations of exceptionFromStanza.
177
Given a realistic stanza with only legacy (pre-XMPP) error information,
178
check if a sane exception is returned.
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>
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.')
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)