import unittest from slixmpp import ComponentXMPP, Iq, Message from slixmpp.roster import RosterItem from slixmpp.test import SlixTest class TestPermissions(SlixTest): def setUp(self): self.stream_start( mode="component", plugins=["xep_0356"], jid="pubsub.capulet.lit", server="capulet.net", ) def testPluginEnd(self): exc = False try: self.xmpp.plugin.disable("xep_0356") except Exception as e: exc = True self.assertFalse(exc) def testGrantedPrivileges(self): # https://xmpp.org/extensions/xep-0356.html#example-4 results = {"event": False} self.xmpp.add_event_handler( "privileges_advertised", lambda msg: results.__setitem__("event", True) ) self.recv( """ """ ) self.assertEqual(self.xmpp["xep_0356"].granted_privileges["roster"], "both") self.assertEqual( self.xmpp["xep_0356"].granted_privileges["message"], "outgoing" ) self.assertEqual(self.xmpp["xep_0356"].granted_privileges["presence"], "none") self.assertTrue(results["event"]) def testGetRosterIq(self): iq = self.xmpp["xep_0356"]._make_get_roster("juliet@example.com") xmlstring = """ """ self.check(iq, xmlstring, use_values=False) def testSetRosterIq(self): jid = "juliet@example.com" items = { "friend1@example.com": { "name": "Friend 1", "subscription": "both", "groups": ["group1", "group2"], }, "friend2@example.com": { "name": "Friend 2", "subscription": "from", "groups": ["group3"], }, } iq = self.xmpp["xep_0356"]._make_set_roster(jid, items) xmlstring = f""" group1 group2 group3 """ self.check(iq, xmlstring, use_values=False) def testMakeOutgoingMessage(self): xmlstring = """ I do not hate you """ msg = Message() msg["from"] = "juliet@capulet.lit" msg["to"] = "romeo@montague.lit" msg["body"] = "I do not hate you" priv_msg = self.xmpp["xep_0356"]._make_privileged_message(msg) self.check(priv_msg, xmlstring, use_values=False) suite = unittest.TestLoader().loadTestsFromTestCase(TestPermissions)