From c2ece57dee13ca8971bff66ff6e4899e9d2cfe71 Mon Sep 17 00:00:00 2001 From: nicoco Date: Sun, 11 Sep 2022 23:16:34 +0200 Subject: Add XEP-0055 (Jabber Search) --- tests/test_stanza_xep_0055.py | 59 +++++++++++++++ tests/test_stream_xep_0055.py | 170 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 229 insertions(+) create mode 100644 tests/test_stanza_xep_0055.py create mode 100644 tests/test_stream_xep_0055.py (limited to 'tests') diff --git a/tests/test_stanza_xep_0055.py b/tests/test_stanza_xep_0055.py new file mode 100644 index 00000000..9ff45efa --- /dev/null +++ b/tests/test_stanza_xep_0055.py @@ -0,0 +1,59 @@ +import unittest + +from slixmpp import register_stanza_plugin, Iq +from slixmpp.test import SlixTest + +from slixmpp.plugins.xep_0055 import stanza + + +class TestJabberSearch(SlixTest): + def setUp(self): + register_stanza_plugin(Iq, stanza.Search) + self.stream_start(plugins={"xep_0055"}) + + def testRequestSearchFields(self): + iq = self.Iq() + iq.set_from("juliet@capulet.com/balcony") + iq.set_to("characters.shakespeare.lit") + iq.set_type("get") + iq.enable("search") + iq["id"] = "0" + self.check( + iq, + """ + + + + """, + ) + + def testSendSearch(self): + iq = self.xmpp["xep_0055"].make_search_iq( + ifrom="juliet@capulet.com/balcony", ito="characters.shakespeare.lit" + ) + iq["search"]["form"].add_field(var="x-gender", value="male") + self.check( + iq, + """ + + + + + jabber:iq:search + + + male + + + + + """, + use_values=False, + ) + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestJabberSearch) diff --git a/tests/test_stream_xep_0055.py b/tests/test_stream_xep_0055.py new file mode 100644 index 00000000..fa028d8b --- /dev/null +++ b/tests/test_stream_xep_0055.py @@ -0,0 +1,170 @@ +import unittest +from slixmpp.test import SlixTest + + +class TestJabberSearch(SlixTest): + def setUp(self): + self.stream_start( + mode="component", + plugin_config={ + "xep_0055": { + "form_fields": {"first", "last"}, + "form_instructions": "INSTRUCTIONS", + "form_title": "User Directory Search", + } + }, + jid="characters.shakespeare.lit", + plugins={"xep_0055"} + ) + 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( + """ + + + + """ + ) + self.send( + """ + + + + User Directory Search + INSTRUCTIONS + + jabber:iq:search + + + + + + + """, + use_values=False, + ) + + def testSearchResult(self): + self.recv( + """ + + + + + jabber:iq:search + + + Montague + + + + + """ + ) + self.send( + """ + + + + + jabber:iq:search + + + + + + + Benvolio + Montague + + + + + """, + use_values=False, # TypeError: element indices must be integers without that + ) + + def testSearchNoResult(self): + self.xmpp["xep_0055"].api.register(get_results, "search_query") + self.recv( + """ + + + + + jabber:iq:search + + + Capulet + + + + + """ + ) + self.send( + """ + + + + + jabber:iq:search + + + + + + + + + """, + use_values=False, # TypeError: element indices must be integers without that + ) + +async def get_results(jid, node, ifrom, iq): + reply = iq.reply() + form = reply["search"]["form"] + form["type"] = "result" + + form.add_reported("first", label="Given Name") + form.add_reported("last", label="Family Name") + + d = iq["search"]["form"].get_values() + + if d["last"] == "Montague": + form.add_item({"first": "Benvolio", "last": "Montague"}) + + return reply + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestJabberSearch) -- cgit v1.2.3