diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_stanza_xep_0060.py | 2 | ||||
-rw-r--r-- | tests/test_stream_exceptions.py | 126 | ||||
-rw-r--r-- | tests/test_stream_presence.py | 51 | ||||
-rw-r--r-- | tests/test_stream_xep_0030.py | 14 | ||||
-rw-r--r-- | tests/test_stream_xep_0066.py | 44 | ||||
-rw-r--r-- | tests/test_stream_xep_0128.py | 1 | ||||
-rw-r--r-- | tests/test_stream_xep_0249.py | 1 | ||||
-rw-r--r-- | tests/test_tostring.py | 4 |
8 files changed, 229 insertions, 14 deletions
diff --git a/tests/test_stanza_xep_0060.py b/tests/test_stanza_xep_0060.py index 8e6e820d..d42c11bd 100644 --- a/tests/test_stanza_xep_0060.py +++ b/tests/test_stanza_xep_0060.py @@ -1,6 +1,6 @@ from sleekxmpp.test import * import sleekxmpp.plugins.xep_0004 as xep_0004 -import sleekxmpp.plugins.stanza_pubsub as pubsub +import sleekxmpp.plugins.xep_0060.stanza as pubsub class TestPubsubStanzas(SleekTest): diff --git a/tests/test_stream_exceptions.py b/tests/test_stream_exceptions.py index bc01c2a7..c41edbb2 100644 --- a/tests/test_stream_exceptions.py +++ b/tests/test_stream_exceptions.py @@ -12,9 +12,78 @@ class TestStreamExceptions(SleekTest): """ def tearDown(self): - sys.excepthook = sys.__excepthook__ self.stream_close() + def testExceptionReply(self): + """Test that raising an exception replies with the original stanza.""" + + def message(msg): + msg.reply() + msg['body'] = 'Body changed' + raise XMPPError(clear=False) + + self.stream_start() + self.xmpp.add_event_handler('message', message) + + self.recv(""" + <message> + <body>This is going to cause an error.</body> + </message> + """) + + self.send(""" + <message type="error"> + <body>This is going to cause an error.</body> + <error type="cancel" code="500"> + <undefined-condition + xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /> + </error> + </message> + """) + + def testExceptionContinueWorking(self): + """Test that Sleek continues to respond after an XMPPError is raised.""" + + def message(msg): + msg.reply() + msg['body'] = 'Body changed' + raise XMPPError(clear=False) + + self.stream_start() + self.xmpp.add_event_handler('message', message) + + self.recv(""" + <message> + <body>This is going to cause an error.</body> + </message> + """) + + self.send(""" + <message type="error"> + <body>This is going to cause an error.</body> + <error type="cancel" code="500"> + <undefined-condition + xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /> + </error> + </message> + """) + + self.recv(""" + <message> + <body>This is going to cause an error.</body> + </message> + """) + + self.send(""" + <message type="error"> + <body>This is going to cause an error.</body> + <error type="cancel" code="500"> + <undefined-condition + xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /> + </error> + </message> + """) + def testXMPPErrorException(self): """Test raising an XMPPError exception.""" @@ -124,9 +193,8 @@ class TestStreamExceptions(SleekTest): def catch_error(*args, **kwargs): raised_errors.append(True) - sys.excepthook = catch_error - self.stream_start() + self.xmpp.exception = catch_error self.xmpp.add_event_handler('message', message) self.recv(""" @@ -149,6 +217,58 @@ class TestStreamExceptions(SleekTest): self.assertEqual(raised_errors, [True], "Exception was not raised: %s" % raised_errors) + def testUnknownException(self): + """Test Sleek continues to respond after an unknown exception.""" + + raised_errors = [] + + def message(msg): + raise ValueError("Did something wrong") + + def catch_error(*args, **kwargs): + raised_errors.append(True) + + self.stream_start() + self.xmpp.exception = catch_error + self.xmpp.add_event_handler('message', message) + + self.recv(""" + <message> + <body>This is going to cause an error.</body> + </message> + """) + + self.send(""" + <message type="error"> + <error type="cancel" code="500"> + <undefined-condition + xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /> + <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"> + SleekXMPP got into trouble. + </text> + </error> + </message> + """) + + self.recv(""" + <message> + <body>This is going to cause an error.</body> + </message> + """) + + self.send(""" + <message type="error"> + <error type="cancel" code="500"> + <undefined-condition + xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /> + <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"> + SleekXMPP got into trouble. + </text> + </error> + </message> + """) + + self.assertEqual(raised_errors, [True, True], "Exceptions were not raised: %s" % raised_errors) suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamExceptions) diff --git a/tests/test_stream_presence.py b/tests/test_stream_presence.py index 1d5caa98..0b086266 100644 --- a/tests/test_stream_presence.py +++ b/tests/test_stream_presence.py @@ -184,5 +184,56 @@ class TestStreamPresence(SleekTest): self.assertEqual(events, expected, "Incorrect events triggered: %s" % events) + def test_presence_events(self): + """Test that presence events are raised.""" + + events = [] + + self.stream_start() + + ptypes = ['available', 'away', 'dnd', 'xa', 'chat', + 'unavailable', 'subscribe', 'subscribed', + 'unsubscribe', 'unsubscribed'] + + for ptype in ptypes: + handler = lambda p: events.append(p['type']) + self.xmpp.add_event_handler('presence_%s' % ptype, handler) + + self.recv(""" + <presence /> + """) + self.recv(""" + <presence><show>away</show></presence> + """) + self.recv(""" + <presence><show>dnd</show></presence> + """) + self.recv(""" + <presence><show>xa</show></presence> + """) + self.recv(""" + <presence><show>chat</show></presence> + """) + self.recv(""" + <presence type="unavailable" /> + """) + self.recv(""" + <presence type="subscribe" /> + """) + self.recv(""" + <presence type="subscribed" /> + """) + self.recv(""" + <presence type="unsubscribe" /> + """) + self.recv(""" + <presence type="unsubscribed" /> + """) + + time.sleep(.5) + + self.assertEqual(events, ptypes, + "Not all events raised: %s" % events) + suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamPresence) diff --git a/tests/test_stream_xep_0030.py b/tests/test_stream_xep_0030.py index c960fc7a..1666d3a1 100644 --- a/tests/test_stream_xep_0030.py +++ b/tests/test_stream_xep_0030.py @@ -12,7 +12,6 @@ class TestStreamDisco(SleekTest): """ def tearDown(self): - sys.excepthook = sys.__excepthook__ self.stream_close() def testInfoEmptyDefaultNode(self): @@ -531,11 +530,6 @@ class TestStreamDisco(SleekTest): raised_exceptions = [] - def catch_exception(*args, **kwargs): - raised_exceptions.append(True) - - sys.excepthook = catch_exception - self.stream_start(mode='client', plugins=['xep_0030', 'xep_0059']) @@ -544,8 +538,14 @@ class TestStreamDisco(SleekTest): iterator=True) results.amount = 10 + def run_test(): + try: + results.next() + except StopIteration: + raised_exceptions.append(True) + t = threading.Thread(name="get_items_iterator", - target=results.next) + target=run_test) t.start() self.send(""" diff --git a/tests/test_stream_xep_0066.py b/tests/test_stream_xep_0066.py new file mode 100644 index 00000000..e3f2ddfa --- /dev/null +++ b/tests/test_stream_xep_0066.py @@ -0,0 +1,44 @@ +import time +import threading + +from sleekxmpp.test import * + + +class TestOOB(SleekTest): + + def tearDown(self): + self.stream_close() + + def testSendOOB(self): + """Test sending an OOB transfer request.""" + self.stream_start(plugins=['xep_0066', 'xep_0030']) + + url = 'http://github.com/fritzy/SleekXMPP/blob/master/README' + + t = threading.Thread( + name='send_oob', + target=self.xmpp['xep_0066'].send_oob, + args=('user@example.com', url), + kwargs={'desc': 'SleekXMPP README'}) + + t.start() + + self.send(""" + <iq to="user@example.com" type="set" id="1"> + <query xmlns="jabber:iq:oob"> + <url>http://github.com/fritzy/SleekXMPP/blob/master/README</url> + <desc>SleekXMPP README</desc> + </query> + </iq> + """) + + self.recv(""" + <iq id="1" type="result" + to="tester@localhost" + from="user@example.com" /> + """) + + t.join() + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestOOB) diff --git a/tests/test_stream_xep_0128.py b/tests/test_stream_xep_0128.py index 6fee6556..42fc9143 100644 --- a/tests/test_stream_xep_0128.py +++ b/tests/test_stream_xep_0128.py @@ -13,7 +13,6 @@ class TestStreamExtendedDisco(SleekTest): """ def tearDown(self): - sys.excepthook = sys.__excepthook__ self.stream_close() def testUsingExtendedInfo(self): diff --git a/tests/test_stream_xep_0249.py b/tests/test_stream_xep_0249.py index f49d1f7e..9a25253f 100644 --- a/tests/test_stream_xep_0249.py +++ b/tests/test_stream_xep_0249.py @@ -13,7 +13,6 @@ class TestStreamDirectInvite(SleekTest): """ def tearDown(self): - sys.excepthook = sys.__excepthook__ self.stream_close() def testReceiveInvite(self): diff --git a/tests/test_tostring.py b/tests/test_tostring.py index 638e613a..e456d28e 100644 --- a/tests/test_tostring.py +++ b/tests/test_tostring.py @@ -102,11 +102,13 @@ class TestToString(SleekTest): """ Test that stanza objects are serialized properly. """ + self.stream_start() + utf8_message = '\xe0\xb2\xa0_\xe0\xb2\xa0' if not hasattr(utf8_message, 'decode'): # Python 3 utf8_message = bytes(utf8_message, encoding='utf-8') - msg = Message() + msg = self.Message() msg['body'] = utf8_message.decode('utf-8') expected = '<message><body>\xe0\xb2\xa0_\xe0\xb2\xa0</body></message>' result = msg.__str__() |