def toResponse(self, stanza): """ Construct error response stanza. The C{stanza} is transformed into an error response stanza by swapping the C{to} and C{from} addresses and inserting an error element. @note: This creates a shallow copy of the list of child elements of the stanza. The child elements themselves are not copied themselves, and references to their parent element will still point to the original stanza element. The serialization of an element does not use the reference to its parent, so the typical use case of immediately sending out the constructed error response is not affected. @param stanza: the stanza to respond to @type stanza: L{domish.Element} """ from reqs.twisted.words.protocols.jabber.xmlstream import toResponse response = toResponse(stanza, stanzaType='error') response.children = copy.copy(stanza.children) response.addChild(self.getElement()) return response
def test_noType(self): """ Test that a proper response is generated without type attribute. """ stanza = domish.Element(('jabber:client', 'message')) response = xmlstream.toResponse(stanza) self.assertFalse(response.hasAttribute('type'))
def test_toResponseNoAddressing(self): """ Test that a response is generated from a stanza without any addressing. """ stanza = domish.Element(('jabber:client', 'message')) stanza['type'] = 'chat' response = xmlstream.toResponse(stanza) self.assertFalse(response.hasAttribute('to')) self.assertFalse(response.hasAttribute('from'))
def test_toResponseNoTo(self): """ Test that a response is generated from a stanza without a to address. """ stanza = domish.Element(('jabber:client', 'iq')) stanza['type'] = 'get' stanza['from'] = '[email protected]/resource' response = xmlstream.toResponse(stanza) self.assertFalse(response.hasAttribute('from')) self.assertEqual(response['to'], '[email protected]/resource')
def onAuthSet(iq): """ Called when the initializer sent the authentication request. The server checks the credentials and responds with an empty result signalling success. """ self.assertEquals('user', unicode(iq.query.username)) self.assertEquals('secret', unicode(iq.query.password)) self.assertEquals('resource', unicode(iq.query.resource)) # Send server response response = xmlstream.toResponse(iq, 'result') self.pipe.source.send(response)
def test_toResponse(self): """ Test that a response stanza is generated with addressing swapped. """ stanza = domish.Element(('jabber:client', 'iq')) stanza['type'] = 'get' stanza['to'] = '*****@*****.**' stanza['from'] = '[email protected]/resource' stanza['id'] = 'stanza1' response = xmlstream.toResponse(stanza, 'result') self.assertNotIdentical(stanza, response) self.assertEqual(response['from'], '*****@*****.**') self.assertEqual(response['to'], '[email protected]/resource') self.assertEqual(response['type'], 'result') self.assertEqual(response['id'], 'stanza1')
def onAuthGet(iq): """ Called when the initializer sent a query for authentication methods. The response informs the client that plain-text authentication is supported. """ # Send server response response = xmlstream.toResponse(iq, 'result') response.addElement(('jabber:iq:auth', 'query')) response.query.addElement('username') response.query.addElement('password') response.query.addElement('resource') # Set up an observer for the next request we expect. d = self.waitFor(IQ_AUTH_SET, onAuthSet) # Send server response self.pipe.source.send(response) return d
def onSession(iq): response = xmlstream.toResponse(iq, 'result') self.pipe.source.send(response)
def onBind(iq): response = xmlstream.toResponse(iq, 'result') response.addElement((NS_BIND, 'bind')) response.bind.addElement('jid', content='[email protected]/other resource') self.pipe.source.send(response)