def testSetItem(self): """Test assigning to stanza interfaces.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz', 'qux'} sub_interfaces = {'baz'} def set_qux(self, value): pass class TestStanzaPlugin(ElementBase): name = "foobar" namespace = "foo" plugin_attrib = "foobar" interfaces = {'foobar'} register_stanza_plugin(TestStanza, TestStanzaPlugin) stanza = TestStanza() stanza['bar'] = 'attribute!' stanza['baz'] = 'element!' stanza['qux'] = 'overridden' stanza['foobar'] = 'plugin' self.check( stanza, """ <foo xmlns="foo" bar="attribute!"> <baz>element!</baz> <foobar foobar="plugin" /> </foo> """)
def testSetItem(self): """Test assigning to stanza interfaces.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz', 'qux'} sub_interfaces = {'baz'} def set_qux(self, value): pass class TestStanzaPlugin(ElementBase): name = "foobar" namespace = "foo" plugin_attrib = "foobar" interfaces = {'foobar'} register_stanza_plugin(TestStanza, TestStanzaPlugin) stanza = TestStanza() stanza['bar'] = 'attribute!' stanza['baz'] = 'element!' stanza['qux'] = 'overridden' stanza['foobar'] = 'plugin' self.check(stanza, """ <foo xmlns="foo" bar="attribute!"> <baz>element!</baz> <foobar foobar="plugin" /> </foo> """)
def testSubStanzas(self): """Test manipulating substanzas of a stanza object.""" class TestSubStanza(ElementBase): name = "foobar" namespace = "foo" interfaces = {'qux'} class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} register_stanza_plugin(TestStanza, TestSubStanza, iterable=True) stanza = TestStanza() substanza1 = TestSubStanza() substanza2 = TestSubStanza() substanza1['qux'] = 'a' substanza2['qux'] = 'b' # Test appending substanzas self.failUnless(len(stanza) == 0, "Incorrect empty stanza size.") stanza.append(substanza1) self.check(stanza, """ <foo xmlns="foo"> <foobar qux="a" /> </foo> """, use_values=False) self.failUnless(len(stanza) == 1, "Incorrect stanza size with 1 substanza.") stanza.append(substanza2) self.check(stanza, """ <foo xmlns="foo"> <foobar qux="a" /> <foobar qux="b" /> </foo> """, use_values=False) self.failUnless(len(stanza) == 2, "Incorrect stanza size with 2 substanzas.") # Test popping substanzas stanza.pop(0) self.check(stanza, """ <foo xmlns="foo"> <foobar qux="b" /> </foo> """, use_values=False) # Test iterating over substanzas stanza.append(substanza1) results = [] for substanza in stanza: results.append(substanza['qux']) self.failUnless(results == ['b', 'a'], "Iteration over substanzas failed: %s." % str(results))
def testSubStanzas(self): """Test manipulating substanzas of a stanza object.""" class TestSubStanza(ElementBase): name = "foobar" namespace = "foo" interfaces = {'qux'} class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} register_stanza_plugin(TestStanza, TestSubStanza, iterable=True) stanza = TestStanza() substanza1 = TestSubStanza() substanza2 = TestSubStanza() substanza1['qux'] = 'a' substanza2['qux'] = 'b' # Test appending substanzas self.assertTrue(len(stanza) == 0, "Incorrect empty stanza size.") stanza.append(substanza1) self.check(stanza, """ <foo xmlns="foo"> <foobar qux="a" /> </foo> """, use_values=False) self.assertTrue(len(stanza) == 1, "Incorrect stanza size with 1 substanza.") stanza.append(substanza2) self.check(stanza, """ <foo xmlns="foo"> <foobar qux="a" /> <foobar qux="b" /> </foo> """, use_values=False) self.assertTrue(len(stanza) == 2, "Incorrect stanza size with 2 substanzas.") # Test popping substanzas stanza.pop(0) self.check(stanza, """ <foo xmlns="foo"> <foobar qux="b" /> </foo> """, use_values=False) # Test iterating over substanzas stanza.append(substanza1) results = [] for substanza in stanza: results.append(substanza['qux']) self.assertTrue(results == ['b', 'a'], "Iteration over substanzas failed: %s." % str(results))
def testGetMultiAttrib(self): """Test retrieving multi_attrib substanzas.""" class TestStanza(ElementBase): name = 'foo' namespace = 'foo' interfaces = set() class TestMultiStanza1(ElementBase): name = 'bar' namespace = 'bar' plugin_attrib = name plugin_multi_attrib = 'bars' class TestMultiStanza2(ElementBase): name = 'baz' namespace = 'baz' plugin_attrib = name plugin_multi_attrib = 'bazs' register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True) register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True) stanza = TestStanza() stanza.append(TestMultiStanza1()) stanza.append(TestMultiStanza2()) stanza.append(TestMultiStanza1()) stanza.append(TestMultiStanza2()) self.check(stanza, """ <foo xmlns="foo"> <bar xmlns="bar" /> <baz xmlns="baz" /> <bar xmlns="bar" /> <baz xmlns="baz" /> </foo> """, use_values=False) bars = stanza['bars'] bazs = stanza['bazs'] for bar in bars: self.check(bar, """ <bar xmlns="bar" /> """) for baz in bazs: self.check(baz, """ <baz xmlns="baz" /> """) self.assertEqual(len(bars), 2, "Wrong number of <bar /> stanzas: %s" % len(bars)) self.assertEqual(len(bazs), 2, "Wrong number of <baz /> stanzas: %s" % len(bazs))
def testSetMultiAttrib(self): """Test setting multi_attrib substanzas.""" class TestStanza(ElementBase): name = 'foo' namespace = 'foo' interfaces = set() class TestMultiStanza1(ElementBase): name = 'bar' namespace = 'bar' plugin_attrib = name plugin_multi_attrib = 'bars' class TestMultiStanza2(ElementBase): name = 'baz' namespace = 'baz' plugin_attrib = name plugin_multi_attrib = 'bazs' register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True) register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True) stanza = TestStanza() stanza['bars'] = [TestMultiStanza1(), TestMultiStanza1()] stanza['bazs'] = [TestMultiStanza2(), TestMultiStanza2()] self.check(stanza, """ <foo xmlns="foo"> <bar xmlns="bar" /> <bar xmlns="bar" /> <baz xmlns="baz" /> <baz xmlns="baz" /> </foo> """, use_values=False) self.assertEqual( len(stanza['substanzas']), 4, "Wrong number of substanzas: %s" % len(stanza['substanzas'])) stanza['bars'] = [TestMultiStanza1()] self.check(stanza, """ <foo xmlns="foo"> <baz xmlns="baz" /> <baz xmlns="baz" /> <bar xmlns="bar" /> </foo> """, use_values=False) self.assertEqual( len(stanza['substanzas']), 3, "Wrong number of substanzas: %s" % len(stanza['substanzas']))
def testGetStanzaValues(self): """Test get_stanza_values using plugins and substanzas.""" class TestStanzaPlugin(ElementBase): name = "foo2" namespace = "foo" interfaces = {'bar', 'baz'} plugin_attrib = "foo2" class TestSubStanza(ElementBase): name = "subfoo" namespace = "foo" interfaces = {'bar', 'baz'} class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} register_stanza_plugin(TestStanza, TestStanzaPlugin, iterable=True) stanza = TestStanza() stanza['bar'] = 'a' stanza['foo2']['baz'] = 'b' substanza = TestSubStanza() substanza['bar'] = 'c' stanza.append(substanza) values = stanza.get_stanza_values() expected = { 'lang': '', 'bar': 'a', 'baz': '', 'foo2': { 'lang': '', 'bar': '', 'baz': 'b' }, 'substanzas': [{ '__childtag__': '{foo}foo2', 'lang': '', 'bar': '', 'baz': 'b' }, { '__childtag__': '{foo}subfoo', 'lang': '', 'bar': 'c', 'baz': '' }] } self.assertTrue( values == expected, "Unexpected stanza values:\n%s\n%s" % (str(expected), str(values)))
def testGetMultiAttrib(self): """Test retrieving multi_attrib substanzas.""" class TestStanza(ElementBase): name = 'foo' namespace = 'foo' interfaces = set() class TestMultiStanza1(ElementBase): name = 'bar' namespace = 'bar' plugin_attrib = name plugin_multi_attrib = 'bars' class TestMultiStanza2(ElementBase): name = 'baz' namespace = 'baz' plugin_attrib = name plugin_multi_attrib = 'bazs' register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True) register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True) stanza = TestStanza() stanza.append(TestMultiStanza1()) stanza.append(TestMultiStanza2()) stanza.append(TestMultiStanza1()) stanza.append(TestMultiStanza2()) self.check(stanza, """ <foo xmlns="foo"> <bar xmlns="bar" /> <baz xmlns="baz" /> <bar xmlns="bar" /> <baz xmlns="baz" /> </foo> """, use_values=False) bars = stanza['bars'] bazs = stanza['bazs'] for bar in bars: self.check(bar, """ <bar xmlns="bar" /> """) for baz in bazs: self.check(baz, """ <baz xmlns="baz" /> """) self.assertEqual(len(bars), 2, "Wrong number of <bar /> stanzas: %s" % len(bars)) self.assertEqual(len(bazs), 2, "Wrong number of <baz /> stanzas: %s" % len(bazs))
def testGetItem(self): """Test accessing stanza interfaces.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz', 'qux'} sub_interfaces = {'baz'} def get_qux(self): return 'qux' class TestStanzaPlugin(ElementBase): name = "foobar" namespace = "foo" plugin_attrib = "foobar" interfaces = {'fizz'} register_stanza_plugin(TestStanza, TestStanza, iterable=True) register_stanza_plugin(TestStanza, TestStanzaPlugin) stanza = TestStanza() substanza = TestStanza() stanza.append(substanza) stanza.set_stanza_values({ 'bar': 'a', 'baz': 'b', 'qux': 42, 'foobar': { 'fizz': 'c' } }) # Test non-plugin interfaces expected = { 'substanzas': [substanza], 'bar': 'a', 'baz': 'b', 'qux': 'qux', 'meh': '' } for interface, value in expected.items(): result = stanza[interface] self.assertTrue( result == value, "Incorrect stanza interface access result: %s" % result) # Test plugin interfaces self.assertTrue(isinstance(stanza['foobar'], TestStanzaPlugin), "Incorrect plugin object result.") self.assertTrue(stanza['foobar']['fizz'] == 'c', "Incorrect plugin subvalue result.")
def testSetMultiAttrib(self): """Test setting multi_attrib substanzas.""" class TestStanza(ElementBase): name = 'foo' namespace = 'foo' interfaces = set() class TestMultiStanza1(ElementBase): name = 'bar' namespace = 'bar' plugin_attrib = name plugin_multi_attrib = 'bars' class TestMultiStanza2(ElementBase): name = 'baz' namespace = 'baz' plugin_attrib = name plugin_multi_attrib = 'bazs' register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True) register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True) stanza = TestStanza() stanza['bars'] = [TestMultiStanza1(), TestMultiStanza1()] stanza['bazs'] = [TestMultiStanza2(), TestMultiStanza2()] self.check(stanza, """ <foo xmlns="foo"> <bar xmlns="bar" /> <bar xmlns="bar" /> <baz xmlns="baz" /> <baz xmlns="baz" /> </foo> """, use_values=False) self.assertEqual(len(stanza['substanzas']), 4, "Wrong number of substanzas: %s" % len(stanza['substanzas'])) stanza['bars'] = [TestMultiStanza1()] self.check(stanza, """ <foo xmlns="foo"> <baz xmlns="baz" /> <baz xmlns="baz" /> <bar xmlns="bar" /> </foo> """, use_values=False) self.assertEqual(len(stanza['substanzas']), 3, "Wrong number of substanzas: %s" % len(stanza['substanzas']))
def plugin_init(self): register_stanza_plugin(RPCQuery, stanza.MethodTimeout) self.xmpp.register_handler( Callback( "RPC Call", MatchXPath("{%s}iq/{%s}query/{%s}methodTimeout" % (self.xmpp.default_ns, RPCQuery.namespace, RPCQuery.namespace)), self._handle_method_timeout, )) self.xmpp.add_event_handler("jabber_rpc_method_timeout", self._on_jabber_rpc_method_timeout)
def testGetStanzaValues(self): """Test get_stanza_values using plugins and substanzas.""" class TestStanzaPlugin(ElementBase): name = "foo2" namespace = "foo" interfaces = {'bar', 'baz'} plugin_attrib = "foo2" class TestSubStanza(ElementBase): name = "subfoo" namespace = "foo" interfaces = {'bar', 'baz'} class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} register_stanza_plugin(TestStanza, TestStanzaPlugin, iterable=True) stanza = TestStanza() stanza['bar'] = 'a' stanza['foo2']['baz'] = 'b' substanza = TestSubStanza() substanza['bar'] = 'c' stanza.append(substanza) values = stanza.get_stanza_values() expected = {'lang': '', 'bar': 'a', 'baz': '', 'foo2': {'lang': '', 'bar': '', 'baz': 'b'}, 'substanzas': [{'__childtag__': '{foo}foo2', 'lang': '', 'bar': '', 'baz': 'b'}, {'__childtag__': '{foo}subfoo', 'lang': '', 'bar': 'c', 'baz': ''}]} self.assertTrue(values == expected, "Unexpected stanza values:\n%s\n%s" % (str(expected), str(values)))
def testGetItem(self): """Test accessing stanza interfaces.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz', 'qux'} sub_interfaces = {'baz'} def get_qux(self): return 'qux' class TestStanzaPlugin(ElementBase): name = "foobar" namespace = "foo" plugin_attrib = "foobar" interfaces = {'fizz'} register_stanza_plugin(TestStanza, TestStanza, iterable=True) register_stanza_plugin(TestStanza, TestStanzaPlugin) stanza = TestStanza() substanza = TestStanza() stanza.append(substanza) stanza.set_stanza_values({'bar': 'a', 'baz': 'b', 'qux': 42, 'foobar': {'fizz': 'c'}}) # Test non-plugin interfaces expected = {'substanzas': [substanza], 'bar': 'a', 'baz': 'b', 'qux': 'qux', 'meh': ''} for interface, value in expected.items(): result = stanza[interface] self.assertTrue(result == value, "Incorrect stanza interface access result: %s" % result) # Test plugin interfaces self.assertTrue(isinstance(stanza['foobar'], TestStanzaPlugin), "Incorrect plugin object result.") self.assertTrue(stanza['foobar']['fizz'] == 'c', "Incorrect plugin subvalue result.")
def testExtension(self): """Testing using is_extension.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} class TestExtension(ElementBase): name = 'extended' namespace = 'foo' plugin_attrib = name interfaces = {name} is_extension = True def set_extended(self, value): self.xml.text = value def get_extended(self): return self.xml.text def del_extended(self): self.parent().xml.remove(self.xml) register_stanza_plugin(TestStanza, TestExtension) stanza = TestStanza() stanza['extended'] = 'testing' self.check( stanza, """ <foo xmlns="foo"> <extended>testing</extended> </foo> """) self.assertTrue(stanza['extended'] == 'testing', "Could not retrieve stanza extension value.") del stanza['extended'] self.check(stanza, """ <foo xmlns="foo" /> """)
def testExtension(self): """Testing using is_extension.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} class TestExtension(ElementBase): name = 'extended' namespace = 'foo' plugin_attrib = name interfaces = {name} is_extension = True def set_extended(self, value): self.xml.text = value def get_extended(self): return self.xml.text def del_extended(self): self.parent().xml.remove(self.xml) register_stanza_plugin(TestStanza, TestExtension) stanza = TestStanza() stanza['extended'] = 'testing' self.check(stanza, """ <foo xmlns="foo"> <extended>testing</extended> </foo> """) self.assertTrue(stanza['extended'] == 'testing', "Could not retrieve stanza extension value.") del stanza['extended'] self.check(stanza, """ <foo xmlns="foo" /> """)
def testKeys(self): """Test extracting interface names from a stanza object.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} plugin_attrib = 'qux' register_stanza_plugin(TestStanza, TestStanza) stanza = TestStanza() self.failUnless(set(stanza.keys()) == {'lang', 'bar', 'baz'}, "Returned set of interface keys does not match expected.") stanza.enable('qux') self.failUnless(set(stanza.keys()) == {'lang', 'bar', 'baz', 'qux'}, "Incorrect set of interface and plugin keys.")
def testKeys(self): """Test extracting interface names from a stanza object.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} plugin_attrib = 'qux' register_stanza_plugin(TestStanza, TestStanza) stanza = TestStanza() self.assertTrue(set(stanza.keys()) == {'lang', 'bar', 'baz'}, "Returned set of interface keys does not match expected.") stanza.enable('qux') self.assertTrue(set(stanza.keys()) == {'lang', 'bar', 'baz', 'qux'}, "Incorrect set of interface and plugin keys.")
def testDelItem(self): """Test deleting stanza interface values.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz', 'qux'} sub_interfaces = {'bar'} def del_qux(self): pass class TestStanzaPlugin(ElementBase): name = "foobar" namespace = "foo" plugin_attrib = "foobar" interfaces = {'foobar'} register_stanza_plugin(TestStanza, TestStanzaPlugin) stanza = TestStanza() stanza['bar'] = 'a' stanza['baz'] = 'b' stanza['qux'] = 'c' stanza['foobar']['foobar'] = 'd' self.check( stanza, """ <foo xmlns="foo" baz="b" qux="c"> <bar>a</bar> <foobar foobar="d" /> </foo> """) del stanza['bar'] del stanza['baz'] del stanza['qux'] del stanza['foobar'] self.check(stanza, """ <foo xmlns="foo" qux="c" /> """)
def testDelItem(self): """Test deleting stanza interface values.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz', 'qux'} sub_interfaces = {'bar'} def del_qux(self): pass class TestStanzaPlugin(ElementBase): name = "foobar" namespace = "foo" plugin_attrib = "foobar" interfaces = {'foobar'} register_stanza_plugin(TestStanza, TestStanzaPlugin) stanza = TestStanza() stanza['bar'] = 'a' stanza['baz'] = 'b' stanza['qux'] = 'c' stanza['foobar']['foobar'] = 'd' self.check(stanza, """ <foo xmlns="foo" baz="b" qux="c"> <bar>a</bar> <foobar foobar="d" /> </foo> """) del stanza['bar'] del stanza['baz'] del stanza['qux'] del stanza['foobar'] self.check(stanza, """ <foo xmlns="foo" qux="c" /> """)
def __init__(self, protocol): super(TCPStream, self).__init__() self.update_logger({'transport': 'TCP'}) self.protocol = protocol self.protocol_logger = protocol.logger self.transport = protocol.transport self.tls_options = protocol.factory.options self.socket = None if self.tls_options: register_stanza_plugin(StreamFeatures, tls_stanza.STARTTLS) self.register_stanza(StartTLS) self.register_handler( Callback('STARTTLS', StanzaPath('starttls'), self._handle_starttls)) self.add_event_handler('auth_success', self._auth_success) self.add_event_handler('session_bind', self._session_bind) self.init_parser()
def testOverrides(self): """Test using interface overrides.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} class TestOverride(ElementBase): name = 'overrider' namespace = 'foo' plugin_attrib = name interfaces = {'bar'} overrides = ['set_bar'] def setup(self, xml): # Don't create XML for the plugin self.xml = ET.Element('') def set_bar(self, value): if not value.startswith('override-'): self.parent()._set_attr('bar', 'override-%s' % value) else: self.parent()._set_attr('bar', value) stanza = TestStanza() stanza['bar'] = 'foo' self.check(stanza, """ <foo xmlns="foo" bar="foo" /> """) register_stanza_plugin(TestStanza, TestOverride, overrides=True) stanza = TestStanza() stanza['bar'] = 'foo' self.check(stanza, """ <foo xmlns="foo" bar="override-foo" /> """)
def testOverrides(self): """Test using interface overrides.""" class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} class TestOverride(ElementBase): name = 'overrider' namespace = 'foo' plugin_attrib = name interfaces = {'bar'} overrides = ['set_bar'] def setup(self, xml): # Don't create XML for the plugin self.xml = ET.Element('') def set_bar(self, value): if not value.startswith('override-'): self.parent()._set_attr('bar', 'override-%s' % value) else: self.parent()._set_attr('bar', value) stanza = TestStanza() stanza['bar'] = 'foo' self.check(stanza, """ <foo xmlns="foo" bar="foo" /> """) register_stanza_plugin(TestStanza, TestOverride, overrides=True) stanza = TestStanza() stanza['bar'] = 'foo' self.check( stanza, """ <foo xmlns="foo" bar="override-foo" /> """)
def testSetStanzaValues(self): """Test using set_stanza_values with substanzas and plugins.""" class TestStanzaPlugin(ElementBase): name = "pluginfoo" namespace = "foo" interfaces = {'bar', 'baz'} plugin_attrib = "plugin_foo" class TestStanzaPlugin2(ElementBase): name = "pluginfoo2" namespace = "foo" interfaces = {'bar', 'baz'} plugin_attrib = "plugin_foo2" class TestSubStanza(ElementBase): name = "subfoo" namespace = "foo" interfaces = {'bar', 'baz'} class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} register_stanza_plugin(TestStanza, TestSubStanza, iterable=True) register_stanza_plugin(TestStanza, TestStanzaPlugin) register_stanza_plugin(TestStanza, TestStanzaPlugin2) stanza = TestStanza() values = { 'bar': 'a', 'baz': '', 'plugin_foo': { 'bar': '', 'baz': 'b' }, 'plugin_foo2': { 'bar': 'd', 'baz': 'e' }, 'substanzas': [{ '__childtag__': '{foo}subfoo', 'bar': 'c', 'baz': '' }] } stanza.values = values self.check( stanza, """ <foo xmlns="foo" bar="a"> <pluginfoo baz="b" /> <pluginfoo2 bar="d" baz="e" /> <subfoo bar="c" /> </foo> """)
def testSetStanzaValues(self): """Test using set_stanza_values with substanzas and plugins.""" class TestStanzaPlugin(ElementBase): name = "pluginfoo" namespace = "foo" interfaces = {'bar', 'baz'} plugin_attrib = "plugin_foo" class TestStanzaPlugin2(ElementBase): name = "pluginfoo2" namespace = "foo" interfaces = {'bar', 'baz'} plugin_attrib = "plugin_foo2" class TestSubStanza(ElementBase): name = "subfoo" namespace = "foo" interfaces = {'bar', 'baz'} class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz'} register_stanza_plugin(TestStanza, TestSubStanza, iterable=True) register_stanza_plugin(TestStanza, TestStanzaPlugin) register_stanza_plugin(TestStanza, TestStanzaPlugin2) stanza = TestStanza() values = {'bar': 'a', 'baz': '', 'plugin_foo': {'bar': '', 'baz': 'b'}, 'plugin_foo2': {'bar': 'd', 'baz': 'e'}, 'substanzas': [{'__childtag__': '{foo}subfoo', 'bar': 'c', 'baz': ''}]} stanza.values = values self.check(stanza, """ <foo xmlns="foo" bar="a"> <pluginfoo baz="b" /> <pluginfoo2 bar="d" baz="e" /> <subfoo bar="c" /> </foo> """)
def setUp(self): register_stanza_plugin(Iq, RPCQuery) register_stanza_plugin(RPCQuery, MethodCall) register_stanza_plugin(RPCQuery, MethodResponse)
def testMatch(self): """Test matching a stanza against an XPath expression.""" class TestSubStanza(ElementBase): name = "sub" namespace = "baz" interfaces = {'attrib'} class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar', 'baz', 'qux'} sub_interfaces = {'qux'} def set_qux(self, value): self._set_sub_text('qux', text=value) def get_qux(self): return self._get_sub_text('qux') class TestStanzaPlugin(ElementBase): name = "plugin" namespace = "http://test/slash/bar" interfaces = {'attrib'} register_stanza_plugin(TestStanza, TestSubStanza, iterable=True) register_stanza_plugin(TestStanza, TestStanzaPlugin) stanza = TestStanza() self.assertTrue(stanza.match("foo"), "Stanza did not match its own tag name.") self.assertTrue(stanza.match("{foo}foo"), "Stanza did not match its own namespaced name.") stanza['bar'] = 'a' self.assertTrue( stanza.match("foo@bar=a"), "Stanza did not match its own name with attribute value check.") stanza['baz'] = 'b' self.assertTrue( stanza.match("foo@bar=a@baz=b"), "Stanza did not match its own name with multiple attributes.") stanza['qux'] = 'c' self.assertTrue(stanza.match("foo/qux"), "Stanza did not match with subelements.") stanza['qux'] = '' self.assertTrue( stanza.match("foo/qux") == False, "Stanza matched missing subinterface element.") self.assertTrue( stanza.match("foo/bar") == False, "Stanza matched nonexistent element.") stanza['plugin']['attrib'] = 'c' self.assertTrue(stanza.match("foo/plugin@attrib=c"), "Stanza did not match with plugin and attribute.") self.assertTrue(stanza.match("foo/{http://test/slash/bar}plugin"), "Stanza did not match with namespaced plugin.") substanza = TestSubStanza() substanza['attrib'] = 'd' stanza.append(substanza) self.assertTrue(stanza.match("foo/sub@attrib=d"), "Stanza did not match with substanzas and attribute.") self.assertTrue(stanza.match("foo/{baz}sub"), "Stanza did not match with namespaced substanza.")
def testMatch(self): """Test matching a stanza against an XPath expression.""" class TestSubStanza(ElementBase): name = "sub" namespace = "baz" interfaces = {'attrib'} class TestStanza(ElementBase): name = "foo" namespace = "foo" interfaces = {'bar','baz', 'qux'} sub_interfaces = {'qux'} def set_qux(self, value): self._set_sub_text('qux', text=value) def get_qux(self): return self._get_sub_text('qux') class TestStanzaPlugin(ElementBase): name = "plugin" namespace = "http://test/slash/bar" interfaces = {'attrib'} register_stanza_plugin(TestStanza, TestSubStanza, iterable=True) register_stanza_plugin(TestStanza, TestStanzaPlugin) stanza = TestStanza() self.assertTrue(stanza.match("foo"), "Stanza did not match its own tag name.") self.assertTrue(stanza.match("{foo}foo"), "Stanza did not match its own namespaced name.") stanza['bar'] = 'a' self.assertTrue(stanza.match("foo@bar=a"), "Stanza did not match its own name with attribute value check.") stanza['baz'] = 'b' self.assertTrue(stanza.match("foo@bar=a@baz=b"), "Stanza did not match its own name with multiple attributes.") stanza['qux'] = 'c' self.assertTrue(stanza.match("foo/qux"), "Stanza did not match with subelements.") stanza['qux'] = '' self.assertTrue(stanza.match("foo/qux") == False, "Stanza matched missing subinterface element.") self.assertTrue(stanza.match("foo/bar") == False, "Stanza matched nonexistent element.") stanza['plugin']['attrib'] = 'c' self.assertTrue(stanza.match("foo/plugin@attrib=c"), "Stanza did not match with plugin and attribute.") self.assertTrue(stanza.match("foo/{http://test/slash/bar}plugin"), "Stanza did not match with namespaced plugin.") substanza = TestSubStanza() substanza['attrib'] = 'd' stanza.append(substanza) self.assertTrue(stanza.match("foo/sub@attrib=d"), "Stanza did not match with substanzas and attribute.") self.assertTrue(stanza.match("foo/{baz}sub"), "Stanza did not match with namespaced substanza.")