2013-07-26 11:02:26 +00:00
|
|
|
import unittest
|
2014-07-17 12:19:04 +00:00
|
|
|
from slixmpp.test import SlixTest
|
|
|
|
from slixmpp.xmlstream.stanzabase import ET, StanzaBase
|
2010-08-27 16:22:35 +00:00
|
|
|
|
2010-10-07 14:58:13 +00:00
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
class TestStanzaBase(SlixTest):
|
2010-08-27 16:22:35 +00:00
|
|
|
|
|
|
|
def testTo(self):
|
|
|
|
"""Test the 'to' interface of StanzaBase."""
|
|
|
|
stanza = StanzaBase()
|
|
|
|
stanza['to'] = 'user@example.com'
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(str(stanza['to']) == 'user@example.com',
|
2010-08-27 16:22:35 +00:00
|
|
|
"Setting and retrieving stanza 'to' attribute did not work.")
|
|
|
|
|
|
|
|
def testFrom(self):
|
|
|
|
"""Test the 'from' interface of StanzaBase."""
|
|
|
|
stanza = StanzaBase()
|
|
|
|
stanza['from'] = 'user@example.com'
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(str(stanza['from']) == 'user@example.com',
|
2010-08-27 16:22:35 +00:00
|
|
|
"Setting and retrieving stanza 'from' attribute did not work.")
|
|
|
|
|
|
|
|
def testPayload(self):
|
|
|
|
"""Test the 'payload' interface of StanzaBase."""
|
|
|
|
stanza = StanzaBase()
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza['payload'] == [],
|
2010-08-27 16:22:35 +00:00
|
|
|
"Empty stanza does not have an empty payload.")
|
|
|
|
|
|
|
|
stanza['payload'] = ET.Element("{foo}foo")
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(len(stanza['payload']) == 1,
|
2010-08-27 16:22:35 +00:00
|
|
|
"Stanza contents and payload do not match.")
|
|
|
|
|
|
|
|
stanza['payload'] = ET.Element('{bar}bar')
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(len(stanza['payload']) == 2,
|
2010-08-27 16:22:35 +00:00
|
|
|
"Stanza payload was not appended.")
|
|
|
|
|
|
|
|
del stanza['payload']
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza['payload'] == [],
|
2010-08-27 16:22:35 +00:00
|
|
|
"Stanza payload not cleared after deletion.")
|
|
|
|
|
|
|
|
stanza['payload'] = [ET.Element('{foo}foo'),
|
|
|
|
ET.Element('{bar}bar')]
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(len(stanza['payload']) == 2,
|
2010-08-27 16:22:35 +00:00
|
|
|
"Adding multiple elements to stanza's payload did not work.")
|
|
|
|
|
|
|
|
def testClear(self):
|
|
|
|
"""Test clearing a stanza."""
|
|
|
|
stanza = StanzaBase()
|
|
|
|
stanza['to'] = 'user@example.com'
|
|
|
|
stanza['payload'] = ET.Element("{foo}foo")
|
|
|
|
stanza.clear()
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza['payload'] == [],
|
2010-08-27 16:22:35 +00:00
|
|
|
"Stanza payload was not cleared after calling .clear()")
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(str(stanza['to']) == "user@example.com",
|
2010-08-27 16:22:35 +00:00
|
|
|
"Stanza attributes were not preserved after calling .clear()")
|
|
|
|
|
|
|
|
def testReply(self):
|
|
|
|
"""Test creating a reply stanza."""
|
|
|
|
stanza = StanzaBase()
|
|
|
|
stanza['to'] = "recipient@example.com"
|
|
|
|
stanza['from'] = "sender@example.com"
|
|
|
|
stanza['payload'] = ET.Element("{foo}foo")
|
|
|
|
|
2015-02-12 11:23:47 +00:00
|
|
|
stanza = stanza.reply()
|
2010-08-27 16:22:35 +00:00
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(str(stanza['to'] == "sender@example.com"),
|
2010-08-27 16:22:35 +00:00
|
|
|
"Stanza reply did not change 'to' attribute.")
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(stanza['payload'] == [],
|
2010-08-27 16:22:35 +00:00
|
|
|
"Stanza reply did not empty stanza payload.")
|
|
|
|
|
|
|
|
|
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestStanzaBase)
|