From e16f72d32d895666e0f891b9b43f42c93c9361d2 Mon Sep 17 00:00:00 2001 From: nicoco Date: Sun, 26 Feb 2023 22:32:21 +0100 Subject: [PATCH] fix error namespace for ComponentXMPP in SlixTest, if mode=="component", restore jabber:client namespace afterwards Fixes #3476 Fixes #3474 --- slixmpp/componentxmpp.py | 22 +++++++++++++++++++--- slixmpp/test/slixtest.py | 10 ++++++++++ tests/live_test.py | 3 --- tests/test_events.py | 3 --- tests/test_stanza_iq.py | 4 ---- tests/test_stream.py | 3 --- tests/test_stream_exceptions.py | 3 --- tests/test_stream_filters.py | 3 --- tests/test_stream_handlers.py | 3 --- tests/test_stream_presence.py | 3 --- tests/test_stream_roster.py | 3 --- tests/test_stream_xep_0030.py | 3 --- tests/test_stream_xep_0047.py | 3 --- tests/test_stream_xep_0050.py | 3 --- tests/test_stream_xep_0055.py | 3 --- tests/test_stream_xep_0060.py | 3 --- tests/test_stream_xep_0066.py | 3 --- tests/test_stream_xep_0085.py | 3 --- tests/test_stream_xep_0092.py | 3 --- tests/test_stream_xep_0100.py | 4 ++-- tests/test_stream_xep_0128.py | 3 --- tests/test_stream_xep_0249.py | 3 --- tests/test_stream_xep_0313.py | 3 --- tests/test_stream_xep_0323.py | 3 --- tests/test_stream_xep_0325.py | 3 --- tests/test_stream_xep_0385.py | 3 --- tests/test_stream_xep_0405.py | 3 --- tests/test_stream_xep_0447.py | 2 -- tests/test_stream_xep_0461.py | 3 --- tests/test_tostring.py | 3 --- 30 files changed, 31 insertions(+), 86 deletions(-) diff --git a/slixmpp/componentxmpp.py b/slixmpp/componentxmpp.py index 8927712e..6e0193ac 100644 --- a/slixmpp/componentxmpp.py +++ b/slixmpp/componentxmpp.py @@ -9,13 +9,14 @@ import logging import hashlib +from slixmpp import Message, Iq, Presence from slixmpp.basexmpp import BaseXMPP from slixmpp.stanza import Handshake +from slixmpp.stanza.error import Error from slixmpp.xmlstream import XMLStream -from slixmpp.xmlstream import ET from slixmpp.xmlstream.matcher import MatchXPath from slixmpp.xmlstream.handler import Callback - +from slixmpp.xmlstream.stanzabase import register_stanza_plugin log = logging.getLogger(__name__) @@ -39,9 +40,17 @@ class ComponentXMPP(BaseXMPP): should be used instead of the standard ``'jabber:component:accept'`` namespace. Defaults to ``False``. + :param fix_error_ns: Fix the namespace of error stanzas. + If you use ``use_jc_ns`` namespace, you probably want that, but + it can be a problem if you use both a ClientXMPP and a ComponentXMPP + in the same interpreter. This is ``False`` by default for backwards + compatibility. """ - def __init__(self, jid, secret, host=None, port=None, plugin_config=None, plugin_whitelist=None, use_jc_ns=False): + def __init__(self, jid, secret, + host=None, port=None, plugin_config=None, + plugin_whitelist=None, use_jc_ns=False, + fix_error_ns=False): if not plugin_whitelist: plugin_whitelist = [] @@ -52,6 +61,8 @@ class ComponentXMPP(BaseXMPP): default_ns = 'jabber:client' else: default_ns = 'jabber:component:accept' + if fix_error_ns: + self._fix_error_ns() BaseXMPP.__init__(self, jid, default_ns) self.auto_authorize = None @@ -77,6 +88,11 @@ class ComponentXMPP(BaseXMPP): self.add_event_handler('presence_probe', self._handle_probe) + def _fix_error_ns(self): + Error.namespace = self.default_ns + for st in Message, Iq, Presence: + register_stanza_plugin(st, Error) + def connect(self, host=None, port=None, use_ssl=False): """Connect to the server. diff --git a/slixmpp/test/slixtest.py b/slixmpp/test/slixtest.py index 0d05a4ac..e1b8bbec 100644 --- a/slixmpp/test/slixtest.py +++ b/slixmpp/test/slixtest.py @@ -10,11 +10,13 @@ from xml.parsers.expat import ExpatError from slixmpp.test import TestTransport from slixmpp import ClientXMPP, ComponentXMPP from slixmpp.stanza import Message, Iq, Presence +from slixmpp.stanza.error import Error from slixmpp.xmlstream import ET from slixmpp.xmlstream import ElementBase from slixmpp.xmlstream.tostring import tostring, highlight from slixmpp.xmlstream.matcher import StanzaPath, MatcherId, MatchIDSender from slixmpp.xmlstream.matcher import MatchXMLMask, MatchXPath +from slixmpp.xmlstream.stanzabase import register_stanza_plugin import asyncio @@ -322,6 +324,7 @@ class SlixTest(unittest.TestCase): if not plugin_config: plugin_config = {} + self.mode = mode if mode == 'client': self.xmpp = ClientXMPP(jid, password, sasl_mech=sasl_mech, @@ -740,3 +743,10 @@ class SlixTest(unittest.TestCase): # Everything matches return True + + def tearDown(self): + self.stream_close() + if getattr(self, "mode", None) == "component": + Error.namespace = 'jabber:client' + for st in Message, Iq, Presence: + register_stanza_plugin(st, Error) diff --git a/tests/live_test.py b/tests/live_test.py index 327657a7..b785ad96 100644 --- a/tests/live_test.py +++ b/tests/live_test.py @@ -8,9 +8,6 @@ class TestLiveStream(SlixTest): Test that we can test a live stanza stream. """ - def tearDown(self): - self.stream_close() - def testClientConnection(self): """Test that we can interact with a live ClientXMPP instance.""" self.stream_start(mode='client', diff --git a/tests/test_events.py b/tests/test_events.py index e7a29e71..6dd9f0c1 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -8,9 +8,6 @@ class TestEvents(SlixTest): def setUp(self): self.stream_start() - def tearDown(self): - self.stream_close() - def testEventHappening(self): """Test handler working""" happened = [] diff --git a/tests/test_stanza_iq.py b/tests/test_stanza_iq.py index 2c75a64d..a654dbea 100644 --- a/tests/test_stanza_iq.py +++ b/tests/test_stanza_iq.py @@ -5,10 +5,6 @@ from slixmpp.xmlstream.stanzabase import ET class TestIqStanzas(SlixTest): - def tearDown(self): - """Shutdown the XML stream after testing.""" - self.stream_close() - def testSetup(self): """Test initializing default Iq values.""" iq = self.Iq() diff --git a/tests/test_stream.py b/tests/test_stream.py index 0476039c..d0e84a7c 100644 --- a/tests/test_stream.py +++ b/tests/test_stream.py @@ -8,9 +8,6 @@ class TestStreamTester(SlixTest): Test that we can simulate and test a stanza stream. """ - def tearDown(self): - self.stream_close() - def testClientEcho(self): """Test that we can interact with a ClientXMPP instance.""" self.stream_start(mode='client') diff --git a/tests/test_stream_exceptions.py b/tests/test_stream_exceptions.py index f21f197e..c93b44bf 100644 --- a/tests/test_stream_exceptions.py +++ b/tests/test_stream_exceptions.py @@ -10,9 +10,6 @@ class TestStreamExceptions(SlixTest): Test handling roster updates. """ - def tearDown(self): - self.stream_close() - def testExceptionContinueWorking(self): """Test that Slixmpp continues to respond after an XMPPError is raised.""" diff --git a/tests/test_stream_filters.py b/tests/test_stream_filters.py index c5fde338..101d6534 100644 --- a/tests/test_stream_filters.py +++ b/tests/test_stream_filters.py @@ -14,9 +14,6 @@ class TestFilters(SlixTest): def setUp(self): self.stream_start() - def tearDown(self): - self.stream_close() - def testIncoming(self): data = [] diff --git a/tests/test_stream_handlers.py b/tests/test_stream_handlers.py index a42679f2..52c79135 100644 --- a/tests/test_stream_handlers.py +++ b/tests/test_stream_handlers.py @@ -15,9 +15,6 @@ class TestHandlers(SlixTest): def setUp(self): self.stream_start() - def tearDown(self): - self.stream_close() - def testCallback(self): """Test using stream callback handlers.""" diff --git a/tests/test_stream_presence.py b/tests/test_stream_presence.py index caf3fef9..88019e46 100644 --- a/tests/test_stream_presence.py +++ b/tests/test_stream_presence.py @@ -11,9 +11,6 @@ class TestStreamPresence(SlixTest): def setUp(self): self.stream_start(jid='tester@localhost', plugins=[]) - def tearDown(self): - self.stream_close() - def testInitialUnavailablePresences(self): """ Test receiving unavailable presences from JIDs that diff --git a/tests/test_stream_roster.py b/tests/test_stream_roster.py index 7a94edb8..1a44a93c 100644 --- a/tests/test_stream_roster.py +++ b/tests/test_stream_roster.py @@ -13,9 +13,6 @@ class TestStreamRoster(SlixTest): Test handling roster updates. """ - def tearDown(self): - self.stream_close() - def testGetRoster(self): """Test handling roster requests.""" self.stream_start(mode='client', jid='tester@localhost') diff --git a/tests/test_stream_xep_0030.py b/tests/test_stream_xep_0030.py index 4cabfe38..b6a8d44f 100644 --- a/tests/test_stream_xep_0030.py +++ b/tests/test_stream_xep_0030.py @@ -11,9 +11,6 @@ class TestStreamDisco(SlixTest): Test using the XEP-0030 plugin. """ - def tearDown(self): - self.stream_close() - def testInfoEmptyDefaultNode(self): """ Info query result from an entity MUST have at least one identity diff --git a/tests/test_stream_xep_0047.py b/tests/test_stream_xep_0047.py index a44ffbec..ec18e074 100644 --- a/tests/test_stream_xep_0047.py +++ b/tests/test_stream_xep_0047.py @@ -11,9 +11,6 @@ class TestInBandByteStreams(SlixTest): def setUp(self): self.stream_start(plugins=['xep_0047', 'xep_0030']) - def tearDown(self): - self.stream_close() - def testOpenStream(self): """Test requesting a stream, successfully""" diff --git a/tests/test_stream_xep_0050.py b/tests/test_stream_xep_0050.py index 519c860e..2a5360de 100644 --- a/tests/test_stream_xep_0050.py +++ b/tests/test_stream_xep_0050.py @@ -16,9 +16,6 @@ class TestAdHocCommands(SlixTest): # a dummy value. self.xmpp['xep_0050'].new_session = lambda: '_sessionid_' - def tearDown(self): - self.stream_close() - def testInitialPayloadCommand(self): """Test a command with an initial payload.""" diff --git a/tests/test_stream_xep_0055.py b/tests/test_stream_xep_0055.py index fa028d8b..76e6ddbf 100644 --- a/tests/test_stream_xep_0055.py +++ b/tests/test_stream_xep_0055.py @@ -19,9 +19,6 @@ class TestJabberSearch(SlixTest): self.xmpp["xep_0055"].api.register(get_results, "search_query") self.xmpp["xep_0055"].api.register(get_results, "search_query") - def tearDown(self): - self.stream_close() - def testRequestingSearchFields(self): self.recv( """ diff --git a/tests/test_stream_xep_0060.py b/tests/test_stream_xep_0060.py index a5fd0bdc..3cf496f6 100644 --- a/tests/test_stream_xep_0060.py +++ b/tests/test_stream_xep_0060.py @@ -15,9 +15,6 @@ class TestStreamPubsub(SlixTest): def setUp(self): self.stream_start() - def tearDown(self): - self.stream_close() - def testCreateInstantNode(self): """Test creating an instant node""" self.xmpp['xep_0060'].create_node('pubsub.example.com', None) diff --git a/tests/test_stream_xep_0066.py b/tests/test_stream_xep_0066.py index 159333c7..c6b4c00a 100644 --- a/tests/test_stream_xep_0066.py +++ b/tests/test_stream_xep_0066.py @@ -6,9 +6,6 @@ from slixmpp.test import SlixTest class TestOOB(SlixTest): - def tearDown(self): - self.stream_close() - def testSendOOB(self): """Test sending an OOB transfer request.""" self.stream_start(plugins=['xep_0066', 'xep_0030']) diff --git a/tests/test_stream_xep_0085.py b/tests/test_stream_xep_0085.py index d63fc72b..bee42a98 100644 --- a/tests/test_stream_xep_0085.py +++ b/tests/test_stream_xep_0085.py @@ -6,9 +6,6 @@ from slixmpp.test import SlixTest class TestStreamChatStates(SlixTest): - def tearDown(self): - self.stream_close() - def testChatStates(self): self.stream_start(mode='client', plugins=['xep_0030', 'xep_0085']) diff --git a/tests/test_stream_xep_0092.py b/tests/test_stream_xep_0092.py index 1f1eac00..ac03bbbf 100644 --- a/tests/test_stream_xep_0092.py +++ b/tests/test_stream_xep_0092.py @@ -6,9 +6,6 @@ from slixmpp.test import SlixTest class TestStreamSet(SlixTest): - def tearDown(self): - self.stream_close() - def testHandleSoftwareVersionRequest(self): self.stream_start(mode='client', plugins=['xep_0030', 'xep_0092']) diff --git a/tests/test_stream_xep_0100.py b/tests/test_stream_xep_0100.py index 24e35f9e..dc5e0c1b 100644 --- a/tests/test_stream_xep_0100.py +++ b/tests/test_stream_xep_0100.py @@ -19,6 +19,7 @@ class TestStreamGateway(SlixTest): "xep_0100": {"component_name": "AIM Gateway", "type": "aim"} }, ) + self.xmpp._fix_error_ns() def next_sent(self): self.wait_for_send_queue() @@ -160,7 +161,6 @@ class TestStreamGateway(SlixTest): """ ) - # xmlns="jabber:client" in error substanza, bug in XEP-0077 plugin or OK? self.send( """ RomeoMyRomeo ILoveJuliet - + Not good diff --git a/tests/test_stream_xep_0128.py b/tests/test_stream_xep_0128.py index e987278f..e48f477f 100644 --- a/tests/test_stream_xep_0128.py +++ b/tests/test_stream_xep_0128.py @@ -8,9 +8,6 @@ class TestStreamExtendedDisco(SlixTest): Test using the XEP-0128 plugin. """ - def tearDown(self): - self.stream_close() - def testUsingExtendedInfo(self): self.stream_start(mode='client', jid='tester@localhost', diff --git a/tests/test_stream_xep_0249.py b/tests/test_stream_xep_0249.py index 4f4033d7..634691f8 100644 --- a/tests/test_stream_xep_0249.py +++ b/tests/test_stream_xep_0249.py @@ -10,9 +10,6 @@ class TestStreamDirectInvite(SlixTest): Test using the XEP-0249 plugin. """ - def tearDown(self): - self.stream_close() - def testReceiveInvite(self): self.stream_start(mode='client', plugins=['xep_0030', diff --git a/tests/test_stream_xep_0313.py b/tests/test_stream_xep_0313.py index 25a3a926..9139109e 100644 --- a/tests/test_stream_xep_0313.py +++ b/tests/test_stream_xep_0313.py @@ -9,9 +9,6 @@ class TestMAM(SlixTest): def setUp(self): self.stream_start(plugins=['xep_0313']) - def tearDown(self): - self.stream_close() - def testRetrieveSimple(self): """Test requesting MAM messages without RSM""" diff --git a/tests/test_stream_xep_0323.py b/tests/test_stream_xep_0323.py index baacd7d3..06ba2d8c 100644 --- a/tests/test_stream_xep_0323.py +++ b/tests/test_stream_xep_0323.py @@ -24,9 +24,6 @@ class TestStreamSensorData(SlixTest): def _time_now(self): return datetime.datetime.now().replace(microsecond=0).isoformat() - def tearDown(self): - self.stream_close() - def testRequestAccept(self): self.stream_start(mode='component', plugins=['xep_0030', diff --git a/tests/test_stream_xep_0325.py b/tests/test_stream_xep_0325.py index 600bfa64..6c2ac098 100644 --- a/tests/test_stream_xep_0325.py +++ b/tests/test_stream_xep_0325.py @@ -30,9 +30,6 @@ class TestStreamControl(SlixTest): def _time_now(self): return datetime.datetime.now().replace(microsecond=0).isoformat() - def tearDown(self): - self.stream_close() - def testRequestSetOk(self): self.stream_start(mode='component', plugins=['xep_0030', diff --git a/tests/test_stream_xep_0385.py b/tests/test_stream_xep_0385.py index d6b2d3c6..33e8c8cb 100644 --- a/tests/test_stream_xep_0385.py +++ b/tests/test_stream_xep_0385.py @@ -14,9 +14,6 @@ class TestSIMS(SlixTest): mode="component", jid="whatevs.shakespeare.lit", plugins={"xep_0385"} ) - def tearDown(self): - self.stream_close() - def test_set_file(self): with NamedTemporaryFile("wb+") as f: n = 10 diff --git a/tests/test_stream_xep_0405.py b/tests/test_stream_xep_0405.py index 9bc01601..9f184e76 100644 --- a/tests/test_stream_xep_0405.py +++ b/tests/test_stream_xep_0405.py @@ -8,9 +8,6 @@ class TestMIXPAM(SlixTest): def setUp(self): self.stream_start(plugins=['xep_0405']) - def tearDown(self): - self.stream_close() - def testGetRosterEmpty(self): """Test requesting an empty annotated roster""" diff --git a/tests/test_stream_xep_0447.py b/tests/test_stream_xep_0447.py index bff2b3e9..f1b863bc 100644 --- a/tests/test_stream_xep_0447.py +++ b/tests/test_stream_xep_0447.py @@ -15,8 +15,6 @@ class TestStatelessFileSharing(SlixTest): mode="component", jid="whatevs.shakespeare.lit", plugins={"xep_0447"} ) - def tearDown(self): - self.stream_close() def test_set_file(self): with NamedTemporaryFile("wb+") as f: diff --git a/tests/test_stream_xep_0461.py b/tests/test_stream_xep_0461.py index 9062a056..91388f1d 100644 --- a/tests/test_stream_xep_0461.py +++ b/tests/test_stream_xep_0461.py @@ -7,9 +7,6 @@ class TestReply(SlixTest): def setUp(self): self.stream_start(plugins=["xep_0461"]) - def tearDown(self): - self.stream_close() - def testFallBackBody(self): async def on_reply(msg): start = msg["feature_fallback"]["fallback_body"]["start"] diff --git a/tests/test_tostring.py b/tests/test_tostring.py index 6bc7204f..7d1f7f1e 100644 --- a/tests/test_tostring.py +++ b/tests/test_tostring.py @@ -10,9 +10,6 @@ class TestToString(SlixTest): Test the implementation of slixmpp.xmlstream.tostring """ - def tearDown(self): - self.stream_close() - def tryTostring(self, original='', expected=None, message='', **kwargs): """ Compare the result of calling tostring against an